Larry Brister
asked on
SQL Server Count where two id types exist in column
I have a simple table
IndividualID DirectionID
123 1
123 2
129 1
130 1
I a result with IndividualID and if it has at LEAST one instance of a 1 and a 2 with a Bit result
Data able
Would return this
IndividualID TwoWatCOnversation
123 1
129 0
130 0
IndividualID DirectionID
123 1
123 2
129 1
130 1
I a result with IndividualID and if it has at LEAST one instance of a 1 and a 2 with a Bit result
Data able
Would return this
IndividualID TwoWatCOnversation
123 1
129 0
130 0
or this -
OUTPUT
Hope it helps!
SELECT IndividualID , CASE WHEN cnt > 1 THEN 1 ELSE 0 END TwoWatCOnversation
FROM
(
SELECT IndividualID , COUNT(*) cnt FROM Bits
GROUP BY IndividualID
)k
OUTPUT
/*------------------------
SELECT IndividualID , CASE WHEN cnt > 1 THEN 1 ELSE 0 END TwoWatCOnversation
FROM
(
SELECT IndividualID , COUNT(*) cnt FROM Bits
GROUP BY IndividualID
)k
------------------------*/
IndividualID TwoWatCOnversation
------------ ------------------
123 1
129 0
130 0
(3 row(s) affected)
Hope it helps!
ASKER
Pawar...
That LOOKS right but doesn't include the logic that it MUST have a DirectionID = 1 AND a DirectionID = 2
That LOOKS right but doesn't include the logic that it MUST have a DirectionID = 1 AND a DirectionID = 2
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Edited my last comment
ASKER
Hey guys...
You put me on the right track
This is my final solution
SELECT a.IndividualID ,
CASE WHEN b.cnt = 2 THEN 1
ELSE 0
END TwoWayC0nversation,
c.indCount
FROM ewMessages a
LEFT JOIN ( SELECT IndividualID ,
COUNT(DirectionID) cnt
FROM ewMessages
WHERE DirectionID IN ( 1, 2 )
GROUP BY IndividualID
) b ON a.IndividualID = b.IndividualID
LEFT JOIN ( SELECT IndividualID ,
COUNT(IndividualID) indCount
FROM ewMessages
WHERE DirectionID IN ( 1, 2 )
GROUP BY IndividualID
) c ON a.IndividualID = c.IndividualID
WHERE SalesSiteID IN ( 102816, 103052, 103053, 103054 )
GROUP BY a.IndividualID ,
c.indCount,
CASE WHEN b.cnt = 2 THEN 1
ELSE 0
END
ORDER BY a.IndividualID;
You put me on the right track
This is my final solution
SELECT a.IndividualID ,
CASE WHEN b.cnt = 2 THEN 1
ELSE 0
END TwoWayC0nversation,
c.indCount
FROM ewMessages a
LEFT JOIN ( SELECT IndividualID ,
COUNT(DirectionID) cnt
FROM ewMessages
WHERE DirectionID IN ( 1, 2 )
GROUP BY IndividualID
) b ON a.IndividualID = b.IndividualID
LEFT JOIN ( SELECT IndividualID ,
COUNT(IndividualID) indCount
FROM ewMessages
WHERE DirectionID IN ( 1, 2 )
GROUP BY IndividualID
) c ON a.IndividualID = c.IndividualID
WHERE SalesSiteID IN ( 102816, 103052, 103053, 103054 )
GROUP BY a.IndividualID ,
c.indCount,
CASE WHEN b.cnt = 2 THEN 1
ELSE 0
END
ORDER BY a.IndividualID;
Please try this - Full and tested solution for you-
Open in new window
Output
Open in new window
Hope it helps!