![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I need to get a unique column but I also need to return multiple rows. Using the following doesn't work to get the unique last name since it gets all unique columns. select unique userid, title, address from myTable where etc......... order by userid The userid will be returned multiple times because the other columns cause the row to be unique. How do I get a unique userid while still returning the other rows. Thanks! Dave |
#3
| |||
| |||
|
|
Hi, I need to get a unique column but I also need to return multiple rows. Using the following doesn't work to get the unique last name since it gets all unique columns. select unique userid, title, address from myTable where etc......... order by userid The userid will be returned multiple times because the other columns cause the row to be unique. How do I get a unique userid while still returning the other rows. |
#4
| |||
| |||
|
|
Hi, I need to get a unique column but I also need to return multiple rows. Using the following doesn't work to get the unique last name since it gets all unique columns. select unique userid, title, address from myTable where etc......... order by userid The userid will be returned multiple times because the other columns cause the row to be unique. How do I get a unique userid while still returning the other rows. Thanks! Dave |
#5
| |||
| |||
|
|
On Mon, 19 Sep 2011 10:38:54 -0500, t...@m.com wrote: Hi, I need to get a unique column but I also need to return multiple rows. Using the following doesn't work to get the unique last name since it gets all unique columns. * select unique userid, title, address from myTable where etc......... order by userid The userid will be returned multiple times because the other columns cause the row to be unique. How do I get a unique userid while still returning the other rows. Thanks! Dave Thanks for the replies. Lets say a table has the following data. userid *title * zipcode ----------------------------- 1 * * * mgr * * 12345 2 * * * dev * * 56789 1 * * * mgr * * 56789 And I wanted to return one row for each unique userid such as... 1 * * * mgr * * 12345 2 * * * dev * * 56789 If I use - select unique userid, title, zipcode it will return all 3 rows because each row is unique userid *title * zipcode ----------------------------- 1 * * * mgr * * 12345 2 * * * dev * * 56789 1 * * * mgr * * 56789 I only want unique userids, so it should return. *I realize that there are two different rows for userid = 1 but returning only one would be OK. *The last one would be good. userid *title * zipcode ----------------------------- 1 * * * mgr * * 56789 2 * * * dev * * 56789 Thanks for any help. Dave. |
#6
| |||
| |||
|
|
If I use - select unique userid, title, zipcode it will return all 3 rows because each row is unique userid title zipcode ----------------------------- 1 mgr 12345 2 dev 56789 1 mgr 56789 I only want unique userids, so it should return. I realize that there are two different rows for userid = 1 but returning only one would be OK. The last one would be good. userid title zipcode ----------------------------- 1 mgr 56789 2 dev 56789 |
#7
| |||
| |||
|
|
On 2011-09-20 22:28, test@m.com wrote: [...] If I use - select unique userid, title, zipcode it will return all 3 rows because each row is unique userid title zipcode ----------------------------- 1 mgr 12345 2 dev 56789 1 mgr 56789 I only want unique userids, so it should return. I realize that there are two different rows for userid = 1 but returning only one would be OK. The last one would be good. userid title zipcode ----------------------------- 1 mgr 56789 2 dev 56789 select userid, title, zipcode from ( select userid, title, zipcode, row_number() over (partition by userid) as rn from myTable ) as T where rn = 1 if you have a preference for which row that should be returned, you can add an order by clause: row_number() over (partition by userid order by zipcode) as rn |
#8
| |||
| |||
|
|
In that case I'd prefer this as it is so much simpler: select userid, title, min(zipcode) as zip_min from myTable group by userid, title |
#9
| |||
| |||
|
|
On 2011-09-21 19:28, Robert Klemme wrote: [...] In that case I'd prefer this as it is so much simpler: select userid, title, min(zipcode) as zip_min from myTable group by userid, title If a user can have several titles it won't work, but that can of course be solved by adding an aggregate functions on title. |
![]() |
| Thread Tools | |
| Display Modes | |
| |