Hi, using sql2005. I want to select unique records from a table.
Unfortunitely the field that is used to sort the output cannot be in the
select distinct statement. Any ideas?
Many thanks,
Jonathan
|
|
0
|
|
|
|
Reply
|
Utf
|
12/5/2009 11:51:01 AM |
|
> Hi, using sql2005. I want to select unique records from a table.
> Unfortunitely the field that is used to sort the output cannot be in the
> select distinct statement. Any ideas?
If I understand correctly, you have a scenario like:
CREATE TABLE dbo.FooBar(
Foo int NOT NULL,
Bar int NOT NULL
);
INSERT INTO dbo.FooBar VALUES
(1,1),
(1,3),
(2,2);
SELECT DISTINCT Foo
FROM dbo.FooBar
ORDER BY Bar;
Of course, the above SELECT will not work because the ORDER BY expression
must be in the SELECT list when you use DISTINCT. The desired sequence
would otherwise be ambiguous.
Can you elaborate on your expected results? Maybe you could use a GROUP BY
instead of DISTINCT so that a well-defined aggregate value can be specified
in the ORDER BY:
SELECT Foo
FROM dbo.FooBar
GROUP BY Foo
ORDER BY MIN(Bar);
--
Hope this helps.
Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/
"Jonathan" <Jonathan@discussions.microsoft.com> wrote in message
news:0E3E7D65-9770-46E2-93BE-7C006A8566A9@microsoft.com...
> Hi, using sql2005. I want to select unique records from a table.
> Unfortunitely the field that is used to sort the output cannot be in the
> select distinct statement. Any ideas?
>
> Many thanks,
> Jonathan
>
>
|
|
0
|
|
|
|
Reply
|
Dan
|
12/5/2009 3:06:15 PM
|
|
> INSERT INTO dbo.FooBar VALUES
> (1,1),
> (1,3),
> (2,2);
By the way, the INSERT script I posted uses SQL 2008 syntax but my
suggestion applies to SQL 2005 as well.
--
Hope this helps.
Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/
|
|
0
|
|
|
|
Reply
|
Dan
|
12/5/2009 3:13:10 PM
|
|
Jonathan (Jonathan@discussions.microsoft.com) writes:
> Hi, using sql2005. I want to select unique records from a table.
> Unfortunitely the field that is used to sort the output cannot be in the
> select distinct statement. Any ideas?
In a general perspective your request does not make sense. Consider this
data:
id City
1 London
2 Brussels
3 C�rdoba
4 Birmingham
5 London
6 Copenhagen
7 C�rdoba
8 Copenhagen
9 London
So you want:
SELECT DISTINCT City FROM tbl ORDER BY id
But what would this mean? Should London sort before or after Brussels?
What could make sense is to sort the cities by the smallest id (or
largest, sum of ids etc), which can be expressed as:
SELECT City
FROM (SELECT City, minid = MIN(id)
FROM cities
GROUP BY City) AS x
ORDER BY minid
Now, it may be that in your case there is a constraint of some sort
which makes your request sensible, but I since don't know your
query, I can't tell. There is also the possibility that you I have
really thought the whole way what you are asking for.
I would suggest that you post more information about your query.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
|
|
0
|
|
|
|
Reply
|
Erland
|
12/5/2009 3:20:01 PM
|
|
You can use the ranking functions with a partition on the distinct columns and then select rank 1:
CREATE TABLE Foo (
keycol INT NOT NULL PRIMARY KEY,
col1 INT,
col2 INT,
col3 INT);
INSERT INTO Foo VALUES(1, 1, 1, 1);
INSERT INTO Foo VALUES(2, 1, 1, 2);
INSERT INTO Foo VALUES(3, 1, 1, 3);
INSERT INTO Foo VALUES(4, 1, 2, 4);
SELECT col1, col2
FROM (
SELECT col1, col2, col3,
ROW_NUMBER() OVER(PARTITION BY col1, col2 ORDER BY col3) AS rn
FROM Foo) AS T
WHERE rn = 1
ORDER BY col3;
/*
col1 col2
----------- -----------
1 1
1 2
*/
--
Plamen Ratchev
http://www.SQLStudio.com
|
|
0
|
|
|
|
Reply
|
Plamen
|
12/5/2009 3:31:16 PM
|
|
Hi Erland, thanks for this suggestion I'll give it a go.
Many thanks,
Jonathan
"Erland Sommarskog" wrote:
> Jonathan (Jonathan@discussions.microsoft.com) writes:
> > Hi, using sql2005. I want to select unique records from a table.
> > Unfortunitely the field that is used to sort the output cannot be in the
> > select distinct statement. Any ideas?
>
> In a general perspective your request does not make sense. Consider this
> data:
>
> id City
> 1 London
> 2 Brussels
> 3 Córdoba
> 4 Birmingham
> 5 London
> 6 Copenhagen
> 7 Córdoba
> 8 Copenhagen
> 9 London
>
> So you want:
>
> SELECT DISTINCT City FROM tbl ORDER BY id
>
> But what would this mean? Should London sort before or after Brussels?
>
> What could make sense is to sort the cities by the smallest id (or
> largest, sum of ids etc), which can be expressed as:
>
> SELECT City
> FROM (SELECT City, minid = MIN(id)
> FROM cities
> GROUP BY City) AS x
> ORDER BY minid
>
> Now, it may be that in your case there is a constraint of some sort
> which makes your request sensible, but I since don't know your
> query, I can't tell. There is also the possibility that you I have
> really thought the whole way what you are asking for.
>
> I would suggest that you post more information about your query.
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
12/6/2009 7:57:01 AM
|
|
Hi Plamen, thanks for this suggestion I'll give it a go.
Many thanks,
Jonathan
"Plamen Ratchev" wrote:
> You can use the ranking functions with a partition on the distinct columns and then select rank 1:
>
> CREATE TABLE Foo (
> keycol INT NOT NULL PRIMARY KEY,
> col1 INT,
> col2 INT,
> col3 INT);
>
> INSERT INTO Foo VALUES(1, 1, 1, 1);
> INSERT INTO Foo VALUES(2, 1, 1, 2);
> INSERT INTO Foo VALUES(3, 1, 1, 3);
> INSERT INTO Foo VALUES(4, 1, 2, 4);
>
> SELECT col1, col2
> FROM (
> SELECT col1, col2, col3,
> ROW_NUMBER() OVER(PARTITION BY col1, col2 ORDER BY col3) AS rn
> FROM Foo) AS T
> WHERE rn = 1
> ORDER BY col3;
>
> /*
>
> col1 col2
> ----------- -----------
> 1 1
> 1 2
>
> */
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
12/6/2009 7:58:01 AM
|
|
Worked like a charm.
Thanks,
Jonathan
"Erland Sommarskog" wrote:
> Jonathan (Jonathan@discussions.microsoft.com) writes:
> > Hi, using sql2005. I want to select unique records from a table.
> > Unfortunitely the field that is used to sort the output cannot be in the
> > select distinct statement. Any ideas?
>
> In a general perspective your request does not make sense. Consider this
> data:
>
> id City
> 1 London
> 2 Brussels
> 3 Córdoba
> 4 Birmingham
> 5 London
> 6 Copenhagen
> 7 Córdoba
> 8 Copenhagen
> 9 London
>
> So you want:
>
> SELECT DISTINCT City FROM tbl ORDER BY id
>
> But what would this mean? Should London sort before or after Brussels?
>
> What could make sense is to sort the cities by the smallest id (or
> largest, sum of ids etc), which can be expressed as:
>
> SELECT City
> FROM (SELECT City, minid = MIN(id)
> FROM cities
> GROUP BY City) AS x
> ORDER BY minid
>
> Now, it may be that in your case there is a constraint of some sort
> which makes your request sensible, but I since don't know your
> query, I can't tell. There is also the possibility that you I have
> really thought the whole way what you are asking for.
>
> I would suggest that you post more information about your query.
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
12/6/2009 8:04:01 AM
|
|
Hi Plamen, unfortunitely I could not make this method get the desired
outcome. However, I did appreciated exploring this approach.
Many thanks,
Jonathan
"Plamen Ratchev" wrote:
> You can use the ranking functions with a partition on the distinct columns and then select rank 1:
>
> CREATE TABLE Foo (
> keycol INT NOT NULL PRIMARY KEY,
> col1 INT,
> col2 INT,
> col3 INT);
>
> INSERT INTO Foo VALUES(1, 1, 1, 1);
> INSERT INTO Foo VALUES(2, 1, 1, 2);
> INSERT INTO Foo VALUES(3, 1, 1, 3);
> INSERT INTO Foo VALUES(4, 1, 2, 4);
>
> SELECT col1, col2
> FROM (
> SELECT col1, col2, col3,
> ROW_NUMBER() OVER(PARTITION BY col1, col2 ORDER BY col3) AS rn
> FROM Foo) AS T
> WHERE rn = 1
> ORDER BY col3;
>
> /*
>
> col1 col2
> ----------- -----------
> 1 1
> 1 2
>
> */
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
12/6/2009 8:16:01 AM
|
|
Hi Plamen, yep I got it to work. Thanks.
Jonathan
"Plamen Ratchev" wrote:
> You can use the ranking functions with a partition on the distinct columns and then select rank 1:
>
> CREATE TABLE Foo (
> keycol INT NOT NULL PRIMARY KEY,
> col1 INT,
> col2 INT,
> col3 INT);
>
> INSERT INTO Foo VALUES(1, 1, 1, 1);
> INSERT INTO Foo VALUES(2, 1, 1, 2);
> INSERT INTO Foo VALUES(3, 1, 1, 3);
> INSERT INTO Foo VALUES(4, 1, 2, 4);
>
> SELECT col1, col2
> FROM (
> SELECT col1, col2, col3,
> ROW_NUMBER() OVER(PARTITION BY col1, col2 ORDER BY col3) AS rn
> FROM Foo) AS T
> WHERE rn = 1
> ORDER BY col3;
>
> /*
>
> col1 col2
> ----------- -----------
> 1 1
> 1 2
>
> */
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
12/6/2009 8:34:01 AM
|
|
One more
http://dimantdatabasesolutions.blogspot.com/2007/02/dealing-with-duplicates.html
"Jonathan" <Jonathan@discussions.microsoft.com> wrote in message
news:0E3E7D65-9770-46E2-93BE-7C006A8566A9@microsoft.com...
> Hi, using sql2005. I want to select unique records from a table.
> Unfortunitely the field that is used to sort the output cannot be in the
> select distinct statement. Any ideas?
>
> Many thanks,
> Jonathan
>
>
|
|
0
|
|
|
|
Reply
|
Uri
|
12/6/2009 1:26:46 PM
|
|
nice
Many thanks,
Jonathan
"Uri Dimant" wrote:
> One more
> http://dimantdatabasesolutions.blogspot.com/2007/02/dealing-with-duplicates.html
>
>
>
> "Jonathan" <Jonathan@discussions.microsoft.com> wrote in message
> news:0E3E7D65-9770-46E2-93BE-7C006A8566A9@microsoft.com...
> > Hi, using sql2005. I want to select unique records from a table.
> > Unfortunitely the field that is used to sort the output cannot be in the
> > select distinct statement. Any ideas?
> >
> > Many thanks,
> > Jonathan
> >
> >
>
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
12/13/2009 7:34:01 PM
|
|
|
11 Replies
115 Views
(page loaded in 1.215 seconds)
Similiar Articles: Removing duplicate records based off of date - microsoft.public ...Is there a way to look at a group of records where some have been duplicated and for those that are duplicated , keep the most current date (keeping... How to duplicate records in a subForm? 1)Copy&Paste?? 2 ...We have a Form with a subForm We want to create a subForm Record and then duplicate it "x" times. 1 - How can we just copy and paste the record whil... Duplicate records in report - microsoft.public.access.reports ...Look at your report's RecordSource query in datasheet view; I'm sure that you'll see the same duplicate records there, too. This duplication is caused by one of the ... Hiding Duplicate Records in a List Box - microsoft.public.access ...Is there a way to hide (or not list) duplicate records that might be in a List Box? Thanks in advance for any help. Fred ... MULTIPLE DUPLICATE RECORDS IN REPORT - microsoft.public.access ...I have two tables. Table one is a customer table with general information. Table 2 have activity relating to the customer in table 1. For examples... Create a duplicate record - microsoft.public.access.forms ...I use a form to input the data into the table. From time to time, I need to create a duplicate record from previous records or just completed new r... Duplicate Record command - microsoft.public.access.forms ...I am using the duplicate record command and it works except for the 2 linked fields in the form. It fills in all other values and then won't even l... Find Duplicate Records in two tables - microsoft.public.access ...First of all by accident I also posted this in the General Questions section of the discussion board. So if it shows in both places my aplogies, I t... Primary Key for Sharepoint list to prevent duplicates - microsoft ...I need to make sure that no duplicate records can be created in a SP list. If this were an Access table, I would simply make the field I want to pre... if duplicate goto record - microsoft.public.accessaccess 2003 Main form is called f018ContrPerfEmissGua has the fields UnitNo(number) and ckGEN(yes/No ckbox) subform is called f018ContrPerfEmissGu... Prevent duplicate groups of records in a table - microsoft.public ...Access 2007 I have a formulas table with the following fields: Auto FormulaID Ingredient Percent 267 127 FlourA ... eliminate message from append query to eliminate duplicate record ...Create a new query in design view. Add the two tables for which you wish to compare. When you see the two tables in the design view of the query click... Duplicating records from one table to itself - microsoft.public ...So adding a record to one table does ... Delete all records from a table (but not the table itself) ... The original and duplicate records might be in a single table or ... How do I create a report that shows duplicate records? - microsoft ...I am building a database based on people's calls to my organization. I need to be able to see how many times we have received a call from an address... Delete duplicates from a table - microsoft.public.access ...I want to delete duplicate records by comparing 25 fields( basically check all the columns) . I always use distinct to check a single column but not... Check For Duplicate Name Before Creating Record - microsoft.public ...Hi, I currently have a database that keeps track of patients. Before a new patient is added as a record, I want to check and make sure there is no... M2M: Avoiding Duplicate Key Pairs? - microsoft.public.access ...I have a many-to-many relationship in which I want to trap attempts to relate a child record to the current parent record more than once. I think I ca... Append Records without Duplicates - microsoft.public.access ...I need to append time log to time_log table. ... Is there any way of changing VBA code for the add duplicate record (via the wizard), to append ... report creating a fake duplicate - microsoft.public.access.reports ...The query from which the report is created has no duplicates. But the last record in each group is duplicating itself. I need any direction to remov... duplicate address detection add-in for MS Access - microsoft ...Under Available fields, double-click CompanyName, Address, and City. Do not add any other fields because that might cause Access to treat some duplicate records as unique ... Duplicate RecordsOut fall, now available to preorder: VIRUS - The Black Flux VINYL . CLICK IMAGE TO ENTER . Huge summer sale! GO TO SALE Deleting Duplicate Records - SQLTeam.com - SQL Server Information ...Seema writes "There is a Table with no key constraints. It has duplicate records. The duplicate records have to be deleted (eg there are 3 similar records, only 2 ... User:Ipeirotis/Duplicate Record Detection - Wikipedia, the free ...Often, in the real world, entities have two or more representations in databases. Duplicate records do not share a common key and/or they contain errors that make ... Find, eliminate, or hide duplicate records in Access - Access ...Show All Hide All Applies to Microsoft Office Access 2003 Microsoft Access 2002 The main principle behind designing and using a relational database, such as the ones ... Removing Duplicate Records — DatabaseJournal.com'How do I remove duplicate rows from a table' is a common question in the newsgroups, and here we will look at a few different techniques for achieving this. How to remove duplicate records or create list of unique records ...When Microsoft Excel imports or combines many records, it may create duplicates. This article describes how to create a compiled list of unique records. Microsoft Access tips: Duplicate the record in form and subformHow to duplicate the current record on a form in a Microsoft Access database, along with the related records in the subform. How to Remove Duplicate Records in Excel | eHow.comDeleting a row in Microsoft Excel 2007 takes two clicks. In previous versions, unless you modified the available toolbars, it took more steps than that. If you simply ... How to Find Duplicate Records Using SQL | eHow.comStructured Query Language, or SQL, creates complex databases in online and offline application environments. For businesses, SQL presents a way of organizing records ... Duplicate Medical Records: A Survey of Twin Cities Healthcare ...Duplicate medical records occur when a single patient is associated with more than one medical record number. This causes a dangerous and expensive issue ... 7/22/2012 6:00:35 PM
|