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
 General SQL Server Forums
 New to SQL Server Programming
 Eliminatng Records in a Query

Author  Topic 

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2014-09-29 : 12:04:00
Hi Everyone
Need help to eliminate certain records from my query.The below is a simple query to illustrate my problem

My Query
Select RequestNo,Event_type from Event_log where Event_type in (10,20)


Data
RequestNo Event_type
123456 10
123457 10
123457 20
123458 10
123459 10
123459 20

This above query returns all requests that meets atleast one criteria. How do i edit my query such that i get requests that meet both criteria and the result set looks like below

Data

RequestNo Event_type
123457 10
123457 20
123459 10
123459 20


Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-09-29 : 12:56:02
[code]
; WITH cte (RequestNo, EventTypeCount)
AS
(
SELECT RequestNo, COUNT(*) AS EventTypeCount
FROM Event_log
GROUP BY RequestNo
HAVING COUNT(*) > 1
)
SELECT e.RequestNo, e.Event_type
FROM Event_log e
JOIN cte
ON e.RequestNo = cte.RequestNo
[/code]

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2014-10-01 : 18:20:15
Thanks
Go to Top of Page
   

- Advertisement -