September 26, 2014 at 3:44 am
Hi,
Let’s have the next set of data
Incident # 162002982 has among the results 2 different id’s, these are id’s 9 &11
Incident # 162002562 has among the results the same id’s, in this case Id’s 13
I am only interested in get those incident # and results where among the results we find 2 or more different id’s; that means that from the sample set of data below, I am only interested in Incident # 162002982 and its results
What type of SQL sentence can use for that purpose?
Beforehand I appreciate your advices, thanks
1620029829
16200298211
16200298211
16200298211
16200298211
16200298211
16200298211
16200298211
16200298211
16200298211
1620029829
16200256213
16200256213
16200256213
Claudio
September 26, 2014 at 4:35 am
;WITH Sampledata AS (
SELECT Incident = 162002982, ID = 9 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 11 UNION ALL
SELECT 162002982, 9 UNION ALL
SELECT 162002562, 13 UNION ALL
SELECT 162002562, 13 UNION ALL
SELECT 162002562, 13
)
SELECT Incident, ID
FROM (
SELECT
*,
MIN_ID = MIN(ID) OVER(PARTITION BY Incident),
MAX_ID = MAX(ID) OVER(PARTITION BY Incident)
FROM Sampledata
) d
WHERE MIN_ID <> MAX_ID
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 15, 2014 at 2:02 am
Thank you ChrisM 🙂
October 15, 2014 at 2:28 am
SELECT Incident, cnt = COUNT(distinct ID)
FROM Sampledata
GROUP BY Incident
HAVING COUNT(distinct ID) > 1
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply