| Author |
Topic |
|
rajendrarama
Starting Member
14 Posts |
Posted - 2011-10-16 : 10:57:37
|
| I have a table likecustomerid address addressclass101 India 2101 UK 1102 GER 2103 USA 2Default address class is 2. If a customerid has two addressclasses then I should select the address belong to addressclass '1'. The output should becustomerid address101 UK102 GER103 USAPlease note that I have to select some other columns from other tables too. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-10-16 : 11:39:38
|
what is the version of SQL Server you are using ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajendrarama
Starting Member
14 Posts |
Posted - 2011-10-16 : 12:25:05
|
| 2005 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-16 : 13:56:05
|
| [code]select customerid ,address from(select row_number() over (partition by customerid order by addressclass asc) as Rn,*from table)twhere Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jassi.singh
Posting Yak Master
122 Posts |
Posted - 2011-10-17 : 01:36:18
|
| select * from(select row_number() over (partition by customerid order by addressclass) as Sequence,*from customer)customertempwhere customertemp.Sequence=1Please mark answer as accepted if it helped you.Thanks,Jassi Singh |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-17 : 01:54:07
|
quote: Originally posted by jassi.singh select * from(select row_number() over (partition by customerid order by addressclass) as Sequence,*from customer)customertempwhere customertemp.Sequence=1Please mark answer as accepted if it helped you.Thanks,Jassi Singh
How do you think this is different from what i posted a day ago?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-10-17 : 03:38:04
|
quote: Originally posted by visakh16
quote: Originally posted by jassi.singh select * from(select row_number() over (partition by customerid order by addressclass) as Sequence,*from customer)customertempwhere customertemp.Sequence=1Please mark answer as accepted if it helped you.Thanks,Jassi Singh
How do you think this is different from what i posted a day ago?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Well he has used a different table alias and the column name for the sequence generator..PBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-17 : 04:06:12
|
quote: Originally posted by Sachin.Nand
quote: Originally posted by visakh16
quote: Originally posted by jassi.singh select * from(select row_number() over (partition by customerid order by addressclass) as Sequence,*from customer)customertempwhere customertemp.Sequence=1Please mark answer as accepted if it helped you.Thanks,Jassi Singh
How do you think this is different from what i posted a day ago?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Well he has used a different table alias and the column name for the sequence generator..PBUH
yeah thts a difference ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rajendrarama
Starting Member
14 Posts |
Posted - 2011-10-17 : 05:14:04
|
| Could anyone please write the code for sql 2000 too. I would also need to use in 2000 version too.Many many thanks |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-10-17 : 05:29:58
|
[code]select t.customerid, t.addressfrom yourtable t inner join ( select customerid, addressclass = min(addressclass) from yourtable group by customerid ) m on t.customerid = m.customerid and t.addressclass = m.addressclass[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajendrarama
Starting Member
14 Posts |
Posted - 2011-10-17 : 08:57:22
|
| I have written the query,select t.*from customeraddress t, (select customerid, addressclass = min(addressclass) from customeraddress group by customerid) m where t.customerid = m.customeridand t.addressclass = m.addressclassThe inner query is giving distinct rows and is fine. But when is joined with the outer query, it is also including some of the duplicates of addressclass=1Can anyone help with this.Many thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-17 : 09:01:41
|
| do you mean you can have multiple records with same customerid and addressclass?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rajendrarama
Starting Member
14 Posts |
Posted - 2011-10-17 : 09:11:06
|
| I do not want to have multiple records with same customerid and addressclass. |
 |
|
|
rajendrarama
Starting Member
14 Posts |
Posted - 2011-10-17 : 09:17:16
|
| Is there any way to prevent writing select distinct(columnname1), columnname2,,,,,,,,,,,,, .since there are so many columns in this table and I have to join to some other tables. so I have written select t.* |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-17 : 09:23:58
|
quote: Originally posted by rajendrarama Is there any way to prevent writing select distinct(columnname1), columnname2,,,,,,,,,,,,, .since there are so many columns in this table and I have to join to some other tables. so I have written select t.*
sorry you explanation doesnt make much senseplease post some sample data to clearly illustrate your problem and explain what you want.For the data and output posted, Tan has already given you solution.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rajendrarama
Starting Member
14 Posts |
Posted - 2011-10-17 : 09:50:51
|
| customerid address addressclass time101 India 2 10:02101 UK 1 10:03101 FRA 1 10:04102 GER 2 10:05103 USA 2 10:06For the above table,when I run the queryselect t.*from customeraddress t, (select customerid, addressclass = min(addressclass)from customeraddressgroup by customerid) m where t.customerid = m.customeridand t.addressclass = m.addressclassThe output is like this:101 UK 1 10:03101 FRA 1 10:04102 GER 2 10:05103 USA 2 10:06It has got one duplicate row.The following code would prevent that byselect distinct t.customerid,t.address, t.addressclassfrom customeraddress t, (select customerid, addressclass = min(addressclass)from customeraddressgroup by customerid) m where t.customerid = m.customeridand t.addressclass = m.addressclass My question is:There are so many columns to write after the distinct keyword. So rather than writing all the columns, is there any way we can make changes to select t.*from customeraddress t, (select customerid, addressclass = min(addressclass)from customeraddressgroup by customerid) m where t.customerid = m.customeridand t.addressclass = m.addressclassto get the output101 FRA 1 10:04102 GER 2 10:05103 USA 2 10:06 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-17 : 10:30:42
|
| [code]select t.customerid, t.addressfrom yourtable t inner join ( select customerid, time = max(cast(time as datetime)),maxaddress1time = max(case when addressclass = 1 then cast(time as datetime) else null end) from yourtable group by customerid ) m on t.customerid = m.customerid and t.time= coalesce(m.maxaddress1time ,m.time) [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|