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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 distinct columns

Author  Topic 

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-03-17 : 02:21:48
i have a table with following columns,
col1 col2
-----------
a 1
a1 1
a2 2
a3 2
a4 2

i need a query which will read the first record take the value of col2 and then return all the records of type col2='1'.
then it will give me records of type coll2='2'
how do i write the t-SQL ?

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-17 : 02:24:32
can u explain clearly and post ur required output?
Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-03-17 : 02:28:01
col1 -------------col2
a------------------1
a1-----------------1
a2-----------------2
a3-----------------2
a4-----------------2
a5-----------------1
i need a query which will give me all the records of col2='1' (the value of the first record)
how will i write the t-sql. note that col2 value could be anything.......
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-17 : 02:29:29
select * from table where col2 = 1
Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-03-17 : 02:30:33
thats too smart nageswar! i said i will not know the value of the first record!
Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-03-17 : 02:35:28
no answers? or the question was not understood?
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-17 : 02:36:24
ok,

try this once
select * from @temp where col2 = ( select top 1 col2 from @temp)
Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-03-17 : 02:47:33
well i found myself thanks.
Select top 4 * from PTAX where product_id in (select top 1 product_id from PTAX)
thanks nageswar too, that was the same as i did..
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-17 : 02:58:57
quote:
Originally posted by misterraj

well i found myself thanks.
Select top 4 * from PTAX where product_id in (select top 1 product_id from PTAX)
thanks nageswar too, that was the same as i did..



Ok Welcome
Go to Top of Page
   

- Advertisement -