code_junkie

T-SQL: How do I get the rows from one table whose values completely match up with values in another table?

Nov 11th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. declare @a table
  2. (
  3. pkid int,
  4. value int
  5. )
  6.  
  7. declare @b table
  8. (
  9. otherID int,
  10. value int
  11. )
  12.  
  13.  
  14. insert into @a values (1, 1000)
  15. insert into @a values (1, 1001)
  16. insert into @a values (2, 1000)
  17. insert into @a values (2, 1001)
  18. insert into @a values (2, 1002)
  19.  
  20. insert into @b values (-1, 1000)
  21. insert into @b values (-1, 1001)
  22. insert into @b values (-1, 1002)
  23.  
  24. insert into @b values (-2, 1000)
  25.  
  26. SELECT a.pkId,b.otherId FROM
  27. (SELECT a.pkId,CHECKSUM_AGG(DISTINCT a.value) as 'ValueHash' FROM @a a GROUP BY a.pkId) a
  28. INNER JOIN (SELECT b.otherId,CHECKSUM_AGG(DISTINCT b.value) as 'ValueHash' FROM @b b GROUP BY b.otherId) b
  29. ON a.ValueHash = b.ValueHash
  30.  
  31. SELECT *
  32. FROM (
  33. SELECT ab.pkid, ab.otherID,
  34. (
  35. SELECT TOP 1 COALESCE(ai.value, bi.value)
  36. FROM (
  37. SELECT *
  38. FROM @a aii
  39. WHERE aii.pkid = ab.pkid
  40. ) ai
  41. FULL OUTER JOIN
  42. (
  43. SELECT *
  44. FROM @b bii
  45. WHERE bii.otherID = ab.otherID
  46. ) bi
  47. ON ai.value = bi.value
  48. WHERE ai.pkid IS NULL OR bi.otherID IS NULL
  49. ) unmatch
  50. FROM
  51. (
  52. SELECT DISTINCT pkid, otherid
  53. FROM @a a , @b b
  54. ) ab
  55. ) q
  56. WHERE unmatch IS NOT NULL
  57.  
  58. select A.pkid, B.otherId
  59. from @a A, @b B
  60. where A.value = B.value
  61. group by A.pkid, B.otherId
  62. having count(B.value) = (
  63. select count(*) from @b BB where B.otherId = BB.otherId)
  64.  
  65. SELECT
  66. SQ1.pkid
  67. FROM
  68. (
  69. SELECT
  70. a.pkid, COUNT(*) AS cnt
  71. FROM
  72. @a AS a
  73. GROUP BY
  74. a.pkid
  75. ) SQ1
  76. INNER JOIN
  77. (
  78. SELECT
  79. a1.pkid, b1.otherID, COUNT(*) AS cnt
  80. FROM
  81. @a AS a1
  82. INNER JOIN @b AS b1 ON b1.value = a1.value
  83. GROUP BY
  84. a1.pkid, b1.otherID
  85. ) SQ2 ON
  86. SQ2.pkid = SQ1.pkid AND
  87. SQ2.cnt = SQ1.cnt
  88. INNER JOIN
  89. (
  90. SELECT
  91. b2.otherID, COUNT(*) AS cnt
  92. FROM
  93. @b AS b2
  94. GROUP BY
  95. b2.otherID
  96. ) SQ3 ON
  97. SQ3.otherID = SQ2.otherID AND
  98. SQ3.cnt = SQ1.cnt
  99.  
  100. declare @a table
  101. (
  102. pkid int,
  103. value int
  104. )
  105.  
  106. declare @b table
  107. (
  108. otherID int,
  109. value int
  110. )
  111.  
  112.  
  113. insert into @a values (1, 1000)
  114. insert into @a values (1, 1001)
  115.  
  116. insert into @a values (2, 1000)
  117. insert into @a values (2, 1001)
  118. insert into @a values (2, 1002)
  119.  
  120. insert into @a values (3, 1000)
  121. insert into @a values (3, 1001)
  122. insert into @a values (3, 1001)
  123.  
  124. insert into @a values (4, 1000)
  125. insert into @a values (4, 1000)
  126. insert into @a values (4, 1001)
  127.  
  128.  
  129. insert into @b values (-1, 1000)
  130. insert into @b values (-1, 1001)
  131. insert into @b values (-1, 1002)
  132.  
  133. insert into @b values (-2, 1001)
  134. insert into @b values (-2, 1002)
  135.  
  136. insert into @b values (-3, 1000)
  137. insert into @b values (-3, 1001)
  138. insert into @b values (-3, 1001)
  139.  
  140.  
  141.  
  142. SELECT Matches.pkid, Matches.otherId
  143. FROM
  144. (
  145. SELECT a.pkid, b.otherId, n = COUNT(*)
  146. FROM @a a
  147. INNER JOIN @b b
  148. ON a.Value = b.Value
  149. GROUP BY a.pkid, b.otherId
  150. ) AS Matches
  151.  
  152. INNER JOIN
  153. (
  154. SELECT
  155. pkid,
  156. n = COUNT(DISTINCT value)
  157. FROM @a
  158. GROUP BY pkid
  159. ) AS ACount
  160. ON Matches.pkid = ACount.pkid
  161.  
  162. INNER JOIN
  163. (
  164. SELECT
  165. otherId,
  166. n = COUNT(DISTINCT value)
  167. FROM @b
  168. GROUP BY otherId
  169. ) AS BCount
  170. ON Matches.otherId = BCount.otherId
  171.  
  172. WHERE Matches.n = ACount.n AND Matches.n = BCount.n
  173.  
  174. declare @a table
  175. (
  176. pkid int,
  177. value int
  178. )
  179.  
  180. declare @b table
  181. (
  182. otherID int,
  183. value int
  184. )
  185.  
  186.  
  187. insert into @a values (1, 1000)
  188. insert into @a values (1, 1001)
  189.  
  190. insert into @a values (2, 1000)
  191. insert into @a values (2, 1001)
  192. insert into @a values (2, 1002)
  193.  
  194. insert into @a values (3, 1000)
  195. insert into @a values (3, 1001)
  196. insert into @a values (3, 1001)
  197.  
  198. insert into @a values (4, 1000)
  199. insert into @a values (4, 1000)
  200. insert into @a values (4, 1001)
  201.  
  202.  
  203. insert into @b values (-1, 1000)
  204. insert into @b values (-1, 1001)
  205. insert into @b values (-1, 1002)
  206.  
  207. insert into @b values (-2, 1001)
  208. insert into @b values (-2, 1002)
  209.  
  210. insert into @b values (-3, 1000)
  211. insert into @b values (-3, 1001)
  212. insert into @b values (-3, 1001)
  213.  
  214.  
  215.  
  216. SELECT Matches.pkid, Matches.otherId
  217. FROM
  218. (
  219. SELECT a.pkid, b.otherId, n = COUNT(*)
  220. FROM @a a
  221. INNER JOIN @b b
  222. ON a.Value = b.Value
  223. GROUP BY a.pkid, b.otherId
  224. ) AS Matches
  225.  
  226. INNER JOIN
  227. (
  228. SELECT
  229. pkid,
  230. n = COUNT(DISTINCT value)
  231. FROM @a
  232. GROUP BY pkid
  233. ) AS ACount
  234. ON Matches.pkid = ACount.pkid
  235.  
  236. INNER JOIN
  237. (
  238. SELECT
  239. otherId,
  240. n = COUNT(DISTINCT value)
  241. FROM @b
  242. GROUP BY otherId
  243. ) AS BCount
  244. ON Matches.otherId = BCount.otherId
  245.  
  246. WHERE Matches.n = ACount.n AND Matches.n = BCount.n
  247.  
  248. select
  249. matches.pkid
  250. ,matches.otherID
  251. from
  252. (
  253. select
  254. a.pkid
  255. ,b.otherID
  256. ,count(1) as cnt
  257. from @a a
  258. inner join @b b
  259. on b.value = a.value
  260. group by
  261. a.pkid
  262. ,b.otherID
  263. ) as matches
  264. inner join
  265. (
  266. select
  267. otherID
  268. ,count(1) as cnt
  269. from @b
  270. group by otherID
  271. ) as b_counts
  272. on b_counts.otherID = matches.otherID
  273. where matches.cnt = b_counts.cnt
  274.  
  275. select a.*
  276. from @a a
  277. inner join @b b on a.value = b.value
  278.  
  279. select
  280. a.pkid,
  281. a.value
  282. from
  283. @a a
  284. where
  285. a.pkid in
  286. (
  287. select
  288. pkid
  289. from
  290. (
  291. select
  292. c.pkid,
  293. c.otherid,
  294. count(*) matching_count
  295. from
  296. (
  297. select
  298. a.pkid,
  299. a.value,
  300. b.otherid
  301. from
  302. @a a inner join @b b
  303. on a.value = b.value
  304. ) c
  305. group by
  306. c.pkid,
  307. c.otherid
  308. ) d
  309. inner join
  310. (
  311. select
  312. b.otherid,
  313. count(*) b_record_count
  314. from
  315. @b b
  316. group by
  317. b.otherid
  318. ) e
  319. on d.otherid = e.otherid
  320. and d.matching_count = e.b_record_count
  321. inner join
  322. (
  323. select
  324. a.pkid match_pkid,
  325. count(*) a_record_count
  326. from
  327. @a a
  328. group by
  329. a.pkid
  330. ) f
  331. on d.pkid = f.match_pkid
  332. and d.matching_count = f.a_record_count
  333. )
  334.  
  335. from
  336. (select cnt.cnt, cnt.cntid1, cnt.cntid2 from
  337. (select acnt.cnt as cnt, acnt.cntid as cntid1, bcnt.cntid as cntid2 from
  338. (select count(pkid) as cnt, pkid as cntid from @a group by pkid)
  339. as acnt
  340. full join
  341. (select count(otherID) as cnt, otherID as cntid from @b group by otherID)
  342. as bcnt
  343. on acnt.cnt = bcnt.cnt)
  344. as cnt
  345. where cntid1 is not null and cntid2 is not null)
  346. as cntok
  347. inner join
  348. (select count(1) as cnt, cnta.cntid1 as cntid1, cnta.cntid2 as cntid2
  349. from
  350. (select cnt, cntid1, cntid2, a.value as value1
  351. from
  352. (select cnt.cnt, cnt.cntid1, cnt.cntid2 from
  353. (select acnt.cnt as cnt, acnt.cntid as cntid1, bcnt.cntid as cntid2 from
  354. (select count(pkid) as cnt, pkid as cntid from @a group by pkid)
  355. as acnt
  356. full join
  357. (select count(otherID) as cnt, otherID as cntid from @b group by otherID)
  358. as bcnt
  359. on acnt.cnt = bcnt.cnt)
  360. as cnt
  361. where cntid1 is not null and cntid2 is not null)
  362. as cntok
  363. inner join @a as a on a.pkid = cntok.cntid1)
  364. as cnta
  365. inner join
  366.  
  367. (select cnt, cntid1, cntid2, b.value as value2
  368. from
  369. (select cnt.cnt, cnt.cntid1, cnt.cntid2 from
  370. (select acnt.cnt as cnt, acnt.cntid as cntid1, bcnt.cntid as cntid2 from
  371. (select count(pkid) as cnt, pkid as cntid from @a group by pkid)
  372. as acnt
  373. full join
  374. (select count(otherID) as cnt, otherID as cntid from @b group by otherID)
  375. as bcnt
  376. on acnt.cnt = bcnt.cnt)
  377. as cnt
  378. where cntid1 is not null and cntid2 is not null)
  379. as cntok
  380. inner join @b as b on b.otherid = cntok.cntid2)
  381. as cntb
  382. on cnta.cntid1 = cntb.cntid1 and cnta.cntid2 = cntb.cntid2 and cnta.value1 = cntb.value2
  383. group by cnta.cntid1, cnta.cntid2)
  384. as cntequals
  385. on cntok.cnt = cntequals.cnt and cntok.cntid1 = cntequals.cntid1 and cntok.cntid2 = cntequals.cntid2
  386.  
  387. Select * -- all columns but only from #a
  388. from #a
  389. inner join #b
  390. on #a.value = #b.value -- only return matching rows
  391. where #a.pkid = 2
Add Comment
Please, Sign In to add comment