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 2008 Forums
 SQL Server Administration (2008)
 SQL Query help !

Author  Topic 

sqldba20
Posting Yak Master

183 Posts

Posted - 2011-10-25 : 11:55:04
Folks,

I need help on a SQL Query. I have a data in this format. I want to select only records where HID <= 2

TID Name HID Hname
37 JAN 103 TOM
38 NOV 103 TOM
48 DEC 103 TOM
58 MAR 103 TOM
23 JAN 104 XYZ
24 FEB 105 RUD
26 MAY 109 PQR
29 JUN 109 KHG
12 JUL 109 KJG
12 AUG 101 ABC
7 SEP 116 UHK


The output should be:

TID Name HID Hname
23 JAN 104 XYZ
24 FEB 105 RUD
12 AUG 101 ABC
7 SEP 116 UHK


Thanks !

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-25 : 12:02:21
[code]
select *
from a_data d
inner join
(
select HID
from a_data
group by HID
having count(*) <= 2
) h on d.HID = h.HID
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-10-25 : 12:03:05
that doesn't make any sense.

WHERE HID less than or equal to 2
wouldn't remove any of those rows.

HID are all Greater than 100


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

sqldba20
Posting Yak Master

183 Posts

Posted - 2011-10-25 : 12:12:17
I should have said --- HID count......

quote:
Originally posted by Transact Charlie

that doesn't make any sense.

WHERE HID less than or equal to 2
wouldn't remove any of those rows.

HID are all Greater than 100


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION


Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-10-25 : 12:16:06
ah -- what khtan said then.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -