Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-07 : 17:01:46
|
| helloI have a table with below columns and i noticed in my data the records are being repeated if client has more than one address like belowid name pin dob gender streetadd1 joe 234 12/1/2000 m 123 test st1 joe 234 12/1/2000 m 456 lost dr2 karen 345 1/1/1988 f 34 ditch ct3 smith 565 3/1/1999 m 987 first st3 smith 565 3/1/1999 m 564 second sti have below queryselect id,name,pin,dob,gender,streetadd from client group by id,name,pin,dob,gender,streetaddand i get all above records but i need one record per idthe desired results should look like belowid name pin dob gender streetadd1 joe 234 12/1/2000 m 456 lost dr2 karen 345 1/1/1988 f 34 ditch ct3 smith 565 3/1/1999 m 564 second sthow should i write the sql the give above results? |
|
|
nhess80
Yak Posting Veteran
83 Posts |
Posted - 2009-05-07 : 17:46:13
|
| If you write it like this and remove the streetadd from the query it will display fine. Or do you need it to display at least one of the addressesselect id,name,pin,dob,genderfrom client group by id,name,pin,dob,gender |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-07 : 17:58:28
|
You'll have to figure out how you want to detiermine which row comes first, but this might help you:SELECT id, name, pin, dob, gender, streetaddFROM ( SELECT id, name, pin, dob, gender, streetadd, ROW_NUMBER() OVER (PARTITION BY id ORDER BY streetadd) AS RowNum -- Not sure about the ORRDER BY as it wasn't specified. FROM Client ) AS TWHERE RowNum = 1 |
 |
|
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-08 : 10:01:34
|
| i would like to get at least one address back, the last row's would be perfect. |
 |
|
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-08 : 10:40:32
|
| Lamprey - Your query returning me error with message Incorrect Syntax near "(" |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-05-08 : 10:43:34
|
The query looks fine.Maybe you do not use SQL Server 2005? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-08 : 10:44:35
|
| lamprey - i got it working. Thanks alot. |
 |
|
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-08 : 10:48:14
|
| lampreyhow can i get record with max row number from above query. In where you have RowNum=1 how can i say where Row Num is Max?Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-08 : 10:54:55
|
quote: Originally posted by Harris00 i would like to get at least one address back, the last row's would be perfect.
last based on which column value? there's no concept of first and last in sql table unless you specify order based on a unique valued column. |
 |
|
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-08 : 11:54:09
|
| The query below gives me what i need but it gives the data from first row but i would like to get the data from max row for that id.SELECT id, name, pin, dob, gender, streetaddFROM ( SELECT id, name, pin, dob, gender, streetadd, ROW_NUMBER() OVER (PARTITION BY id ORDER BY streetadd) AS RowNum -- Not sure about the ORRDER BY as it wasn't specified. FROM Client ) AS TWHERE RowNum = 1 |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-08 : 11:57:52
|
Not sure what you mean by MAX row? MAX of what Date or...? Here is a possible solution..quote: Originally posted by Harris00 The query below gives me what i need but it gives the data from first row but i would like to get the data from max row for that id.SELECT id, name, pin, dob, gender, streetaddFROM ( SELECT id, name, pin, dob, gender, streetadd, ROW_NUMBER() OVER (PARTITION BY id ORDER BY streetadd DESC) AS RowNum -- Not sure about the ORRDER BY as it wasn't specified. FROM Client ) AS TWHERE RowNum = 1
|
 |
|
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-08 : 12:26:11
|
| Right now if i run below query i get a RowNum column which gives 1,2,3 for each row.SELECTid,name,pin,dob,gender,streetadd,ROW_NUMBER() OVER (PARTITION BY id ORDER BY streetadd DESC) AS RowNum-- Not sure about the ORRDER BY as it wasn't specified.FROMClientIn your outer query the clause WHERE RowNum=1 returns record where RowNum=1, but i would need to have it return RowNum=Max for that Id.i.eid name pin dob gender streetadd rownum1 joe 234 12/1/2000 m 123 test st 11 joe 234 12/1/2000 m 456 lost dr 22 karen 345 1/1/1988 f 34 ditch ct 13 smith 565 3/1/1999 m 987 first st 13 smith 565 3/1/1999 m 564 second st 23 smith 565 3/1/1999 m 343 third st 3I would like to get below records backid name pin dob gender streetadd rownum1 joe 234 12/1/2000 m 456 lost dr 22 karen 345 1/1/1988 f 34 ditch ct 13 smith 565 3/1/1999 m 343 third st 3 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-08 : 12:47:41
|
| shouldnt your original posted query give you what you want in that case? i.e using ORDER BY streetadd along with PARTITION |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-08 : 13:23:49
|
Ok, I gave you one query and you said you wanted the max. I then gave you the solution for that (with the DESC on the streetadd). You still claimed that is not what you want, but clearly gives the desired outpout that you posted on 05/08/2009 : 12:26:11. I've reproduced each query back to back so we see what each one produces:-- Setup DataDECLARE @Client TABLE ( id int, name VARCHAR(50), pin INT, dob DATETIME, gender CHAR(1), streetadd VARCHAR(50))INSERT @ClientSELECT 1, 'joe', 234, '12/1/2000', 'm', '123 test st'UNION ALL SELECT 1, 'joe', 234, '12/1/2000', 'm', '456 lost dr'UNION ALL SELECT 2, 'karen', 345, '1/1/1988', 'f', '34 ditch ct'UNION ALL SELECT 3, 'smith', 565, '3/1/1999', 'm', '987 first st'UNION ALL SELECT 3, 'smith', 565, '3/1/1999', 'm', '564 second st'UNION ALL SELECT 3, 'smith', 565, '3/1/1999', 'm', '343 third st'-- Query 1SELECT id, name, pin, dob, gender, streetadd, ROW_NUMBER() OVER (PARTITION BY id ORDER BY streetadd ASC) AS RowNumFROM @Client-- Query 2SELECT id, name, pin, dob, gender, streetadd, ROW_NUMBER() OVER (PARTITION BY id ORDER BY streetadd DESC) AS RowNumFROM @Client If you select from either query where RowNum = 1 do you get the desired results from one of them? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-08 : 13:29:32
|
| Perhaps I spoke too fast:here is the data you say is returned:id name pin dob gender streetadd rownum1 joe 234 12/1/2000 m 123 test st 11 joe 234 12/1/2000 m 456 lost dr 22 karen 345 1/1/1988 f 34 ditch ct 13 smith 565 3/1/1999 m 987 first st 13 smith 565 3/1/1999 m 564 second st 23 smith 565 3/1/1999 m 343 third st 3 But there is an issue with the sorting.. Either ID 1 is wrong or ID 3 is wrong if you are sorting by STREETADD.ID 1 is storted ASCENDING and ID 3 is sorted DESCENDING. Which is correct or is there more criteria you have not mentioned? |
 |
|
|
Harris00
Starting Member
19 Posts |
Posted - 2009-05-11 : 09:31:12
|
| I did both sort ASC and DESC but getting the same results. If a client has two or more different address i want to show the address on the last row or may be the last row of that client's recordIn below exampleid name pin dob gender streetadd rownum1 joe 234 12/1/2000 m 123 test st 11 joe 234 12/1/2000 m 456 lost dr 22 karen 345 1/1/1988 f 34 ditch ct 13 smith 565 3/1/1999 m 987 first st 13 smith 565 3/1/1999 m 564 second st 23 smith 565 3/1/1999 m 343 third st 3the desired result should be1 joe 234 12/1/2000 m 456 lost dr 22 karen 345 1/1/1988 f 34 ditch ct 13 smith 565 3/1/1999 m 343 third st 3 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-11 : 09:49:32
|
Harris,You need to understand that in the database / table context, there isn't such things as FIRST ROW or LAST ROW. Records are not stored in the table in any particular sequence. You define how the data are being retrieve with your SELECT statement and the sequence of record being return with the ORDER BY clause with ASC or DESC.quote: If a client has two or more different address i want to show the address on the last row or may be the last row of that client's record
So basically you have to use ORDER BY <column(s)> and sort it to get your desired sequence as what Lamprey as demonstrated in his query KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|