简体   繁体   English

:SQL如何计算共有多个if列的不同行的数量(从另一个表连接)

[英]:SQL how to count the amount of distinct rows the have a multiple if column in common( that is joined from from another table)

Ok, so here is my question. 好的,这是我的问题。 Maybe my brain is just fried, but im struggling to wrap my head around this 也许我的大脑只是被炸了,但是我很难把我的头缠住

i have one table(lets say TableA with a set of columns, lets say there names are: 我有一个表(比如说TableA带有一组列,可以说名字是:

pk1, Pk2, a, b, c,APPLE(has 3 set values from another table), etc (the PK1 and PK2 are primary keys) pk1,Pk2,a,b,c,APPLE(具有来自另一个表的3个设置值)等(PK1和PK2是主键)

and then I have another table(lets say tableB) 然后我有另一个表(让我们说tableB)

PK1(is linked with PK1 on TableA), ag, DOG, sf, STAR(also has a set nr of values),etc PK1(与TableA上的PK1链接),ag,DOG,sf,STAR(也具有一组nr值)等

Ok, so now i want to join these 2 tables, thats the easy part. 好的,所以现在我想加入这两个表,这就是简单的部分。 the values in PK1 can repeat, as long as the combination of PK1 and PK2 are unique,and this is the case. 只要PK1和PK2的组合唯一,就可以重复PK1中的值。

ok, here comes the tricky part. 好的,这是棘手的部分。 i want get a list of of the distinct combinations of PK1+PK2+a 我想获得PK1 + PK2 + a的不同组合的列表

but i also want to know how many of them are of each type of STAR. 但我也想知道每种STAR类型有多少个。

ok, this part is optional(for you bright sparks out there): 好的,这部分是可选的(为您提供明亮的火花):

and just to really make it tricky, each type of star must then be sub divided into types of APPLES 为了使它真正棘手,必须将每种类型的恒星细分为APPLES类型

1) how do i do this? 1)我该怎么做? 2) is it possible to do it with just 1 query, i will i have to use something like a cursor? 2)有可能只用一个查询就可以做到吗,我将不得不使用类似游标的东西吗?

What have you tried? 你尝试了什么? Maybe this can help? 也许这可以帮助?

select distinct pk1,pk2,a from tableA

,

select tableA.pk1,pk2,a,star,count(*)
from tableA join tableB on tableA.pk1 = tableB.pk1
group by tableA.pk1,pk2,a,star

,

select tableA.pk1,pk2,a,star,apples,count(*)
from tableA join tableB on tableA.pk1 = tableB.pk1
group by tableA.pk1,pk2,a,star,apples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM