Sql 带内部联接的计数
我在计算我的程序时遇到了问题 我的SQL查询是:Sql 带内部联接的计数,sql,Sql,我在计算我的程序时遇到了问题 我的SQL查询是: select table_3.SERIAL, TABLE_3.ID, Table_3.OPERATOR, TABLE_1.PROGRAM, TABLE_1.COLOR, tABLE_1.PART_customer from table_3 inner join table_1 on Table_1.[SERIAL]=table_3.[SERIAL] GROUP BY TABLE_3.SERIAL,TABLE_3.ID,TABLE_3.
select table_3.SERIAL, TABLE_3.ID, Table_3.OPERATOR, TABLE_1.PROGRAM, TABLE_1.COLOR, tABLE_1.PART_customer
from table_3
inner join table_1
on Table_1.[SERIAL]=table_3.[SERIAL]
GROUP BY TABLE_3.SERIAL,TABLE_3.ID,TABLE_3.OPERATOR,TABLE_1.PROGRAM,Table_1.COLOR,Table_1.PART_customer
我的输出是
Serial id operator program color part_CUSTOMER
104451 1 a1 GMT-172 Switch 23250063
104451 1 a1 GMT-177 Summit 23214845
104552 9 b1 GMT-172 Switch 23250063
104552 9 b1 GMT-177 Summit 23214845
104855 3 c1 GMT-172 Switch 23250063
104855 3 c1 GMT-177 Summit 23214845
我需要以一种可以得到我的方式来计算它们
Serial id operator program color part_CUSTOMER TOTAL
104451 1 a1 GMT-172 Switch 23250063 2
104552 9 b1 GMT-172 Switch 23250063 2
104855 3 c1 GMT-172 Switch 23250063 2
似乎您并不真正希望输出中包含
程序
或颜色
。据我所知,您正在为它们选择任意值
因此,只需按需要的值进行聚合并添加计数:
select t3.SERIAL, t3.ID, t1.operator, t1.PART_customer, COUNT(*)
from table_3 t3 inner join
table_1 t1
on t1.[SERIAL] = t3.[SERIAL]
group by t3.SERIAL, t3.ID, t1.operator, t1.PART_customer;
如果需要程序
或颜色
值,则可以根据需要使用MIN()
或MAX()
编辑:
从技术上讲,你可以通过以下方式获得你想要的:
select t.*
from (select t3.SERIAL, t3.ID, t1.operator, t1.PROGRAM, t1.COLOR, t1.PART_customer,
COUNT(*) OVER (PARTITOIN BY t3.SERIAL, t3.ID, t1.operator, t1.PART_customer) as cnt
from table_3 t3 inner join
table_1 t1
on t1.[SERIAL] = t3.[SERIAL]
) t
where program = 'GMT-172';
您如何确定输出中的
程序
和颜色
是什么?是“表1”和“表3”临时表吗?这些数据的摘要看起来非常随意,我不相信。假设它们是临时表,我会进一步检查它们是否正确创建。我想你的意思是select t3.SERIAL,t3.ID,**t1.operator**,COUNT(*)
因为part\u customer
看起来也是随机的