উপাত্ত
DECLARE @Example AS table
(
UserName varchar(30) NULL,
Town varchar(30) NULL
);
INSERT @Example
(UserName, Town)
VALUES
('Aaron', 'Not Townsville'),
('Bob', 'Not Townsville'),
('Charles', 'Townsville'),
('Charles', 'Townsville'),
('Charles', 'Townsville'),
('Charles', 'Townsville'),
('Dan', 'Townsville'),
('Eric', 'Not Townsville');
বিকল্প সমাধান
SELECT E.UserName, E.Town
FROM @Example AS E
GROUP BY E.Town, E.UserName
HAVING E.Town = 'Townsville'
-- OR
SELECT E.UserName, 'Townsville' AS Town
FROM @Example AS E
GROUP BY E.UserName
HAVING 1 = MAX(CASE WHEN E.Town = 'Townsville' THEN 1 ELSE 0 END);
-- OR
SELECT E.UserName, E.Town
FROM @Example AS E
INTERSECT
SELECT E.UserName, 'Townsville' AS Town
FROM @Example AS E
সদৃশ পুনরুদ্ধার করা
-- :)
SELECT E.UserName, E.Town
FROM @Example AS E
CROSS APPLY (VALUES(NEWID())) AS CA (n)
GROUP BY E.Town, E.UserName, CA.n
HAVING E.Town = 'Townsville'
-- Simulating INTERSECT ALL
SELECT
R.UserName,
R.Town
FROM
(
SELECT
E.UserName,
E.Town,
rn =
ROW_NUMBER() OVER (
PARTITION BY E.UserName, E.Town
ORDER BY E.UserName, E.Town)
FROM @Example AS E
INTERSECT
SELECT
E.UserName,
'Townsville',
rn =
ROW_NUMBER() OVER (
PARTITION BY E.UserName
ORDER BY E.UserName)
FROM @Example AS E
) AS R;
আউটপুট:
╔══════════╦════════════╗
║ UserName ║ Town ║
╠══════════╬════════════╣
║ Charles ║ Townsville ║
║ Dan ║ Townsville ║
╚══════════╩════════════╝
চূড়ান্ত উদাহরণের জন্য:
╔══════════╦════════════╗
║ UserName ║ Town ║
╠══════════╬════════════╣
║ Charles ║ Townsville ║
║ Charles ║ Townsville ║
║ Charles ║ Townsville ║
║ Charles ║ Townsville ║
║ Dan ║ Townsville ║
╚══════════╩════════════╝
এটি এখানে চেষ্টা করুন: Stack Exchange Data Explorer