鉴于这两个表/集包含不同的项目组,如何找到怎么样?我发现set1
中set2
中多个组中的哪些组跨越set1
?set2
中的哪些群组无法由A (1,2,5)
中的单个群组覆盖?
e.g。对于下表,s1
是唯一跨越s2
( 1 , 2 ,3)和B
(2)的群组,3,4-,的 5 )。 C
和s2
不是答案,因为两者都包含在一个组SQL
中。
我更愿意使用Sql Server 2008 R2
(set1 set2
+---------+----------+ +---------+----------+
| group | item | | group | item |
`````````````````````+ `````````````````````+
| A | 1 | | s1 | 1 |
| A | 2 | | s1 | 2 |
| A | 5 | | s1 | 3 |
| B | 4 | | s2 | 2 |
| B | 5 | | s2 | 3 |
| C | 3 | | s2 | 4 |
| C | 5 | | s2 | 5 |
+---------+----------+ +---------+----------+
可用)。
感谢。
create table #set1 (grp varchar(5),item int)
create table #set2 (grp varchar(5),item int)
insert into #set1 select 'a',1 union select 'a',2 union select 'a',5 union select 'b',4 union select 'b',5 union select 'c',3 union select 'c',5
insert into #set2 select 's1',1 union select 's1',2 union select 's1',3 union select 's2',2 union select 's2',3 union select 's2',4 union select 's2',5
select * from #set1
select * from #set2
--drop table #set1
--drop table #set2
使用此sqlfiddle尝试:http://sqlfiddle.com/#!6/fac8a/3
或者使用下面的脚本生成临时表以试用答案:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Flurry.startSession("XXXXXXXX")
if let userEmail = email{
// user logged with our login
println("User logged via ff.")
Flurry.setUserID(userEmail as String)
}else if let userID = id{
// user logged with Facebook
println("User logged with facebook.")
Flurry.setUserID(userID as String)
}else{
// user not logged
}
}
答案 0 :(得分:3)
从set1
中选择set2
中没有任何群组的群组,set1
中set2
中的所有内容都存在于select s1.grp from set1 s1
where not exists(
select * from set2 s2 where not exists(
select item from set1 s11
where s11.grp = s1.grp
except
select item from set2 s22
where s22.grp = s2.grp))
group by s1.grp
中
self.HeaderView
答案 1 :(得分:2)
确定。这很难看,但应该有效。我小提琴尝试了。我认为可以通过开窗来完成,但我必须考虑一下。
现在这是丑陋的。
WITH d1 AS (
SELECT set1.grp
, COUNT(*) cnt
FROM set1
GROUP BY set1.grp
), d2 AS (
SELECT set1.grp grp1
, set2.grp grp2
, COUNT(set1.item) cnt
FROM set1
INNER JOIN set2
ON set1.item = set2.item
GROUP BY set1.grp
, set2.grp
)
SELECT grp
FROM d1
EXCEPT
SELECT d1.grp
FROM d1
INNER JOIN d2
ON d2.grp1 = d1.grp
AND d2.cnt = d1.cnt
答案 2 :(得分:0)
您可以通过以下查询找到解决方案:
SELECT A.GROUP AS G1, A.ITEM AS T1, B.GROUP, B.ITEM
FROM SET1 A RIGHT JOIN SET2 B ON A.ITEM=B.ITEM
WHERE A.GROUP IS NULL
答案 3 :(得分:0)
你能看看这个
吗?SELECT DISTINCT a.Group1, a.Item, b.CNT
FROM SET1 a
INNER JOIN
(SELECT GroupA, COUNT(*) CNT
FROM
(
SELECT DISTINCT a.Group1 GroupA, b.Group1 GroupB
FROM SET1 a
INNER JOIN SET2 b ON a.Item = b.Item
) a GROUP BY GroupA
) b ON a.Group1 = b.GroupA
WHERE b.CNT > 1
答案 4 :(得分:0)
感谢您的评论。我相信以下编辑的查询将起作用:
Select distinct grp1, initialRows, max(MatchedRows) from
(
select a.grp as grp1, b.grp as grp2
, count(distinct case when b.item is not null then a.item end) as MatchedRows
, d.InitialRows
from set1 a
left join set2 b
on a.item = b.item
left join
(select grp, count(distinct Item) as InitialRows from set1
group by grp) d
on a.grp = d.grp
group by a.grp, b.grp, InitialRows
) c
group by grp1, InitialRows
having max(MatchedRows) < InitialRows
答案 5 :(得分:0)
我认为这样做会有所帮助。子查询通过计算匹配并将匹配计数与set1组计数进行比较,为每个set1组返回set2组,这些组与set1中的所有项匹配。
select s.grp from #set1 s
group by s.grp
having not exists (
select s2.grp from #set2 s2 inner join #set1 s1 on s2.item = s1.item
where s1.grp = s.grp
group by s2.grp
having count(s.item) = count(s2.item)
)
答案 6 :(得分:0)
基本上和Robert Co一样 我没有从他的答案中得到这个 - 独立提出这个
select set1.group
from set1
except
select set1count.group
from ( select set1.group , count(*) as [count]
from set1
) as set1count
join ( select set1.group as [group1], count(*) as [count]
from set1
join set2
on set2.item = set1.item
group by set1.group, set2.group -- this is the magic
) as set1count
on set1count.group = set2count.[group1] -- note no set2.group match
and set1count.count = set12count.count -- the items in set1 are in at least on set2 group