Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用union以不同的分组方式连接不同的查询_Sql_Sql Server - Fatal编程技术网

Sql 使用union以不同的分组方式连接不同的查询

Sql 使用union以不同的分组方式连接不同的查询,sql,sql-server,Sql,Sql Server,我有不同的查询(3),它们有不同的条件来获取不同的计数器 其中两个组的分组相同,但其中一个组没有 数线拣选 select carusrrsppck as 'users', COUNT(carusrrsppck) AS 'lines picking', cardepid as 'deposit' from order_products where DATEADD(dd, 0, DATEDIFF(dd, 0, carfchcar)) between '20190220' and '201

我有不同的查询(3),它们有不同的条件来获取不同的计数器

其中两个组的分组相同,但其中一个组没有

数线拣选

select 
carusrrsppck as 'users', 
COUNT(carusrrsppck) AS 'lines picking', 
cardepid as 'deposit'  
from order_products
where DATEADD(dd, 0, DATEDIFF(dd, 0, carfchcar)) 
between '20190220' and '20190220' 
and cardepid in ('D32', 'DA', 'DA1', 'DB', 'DC', 'DD', 'DE', 'DR')
and carusrrsppck<>''
and ordprdcnt<>0
group by carusrrsppck, cardepid
order by cardepid, COUNT(*) DESC
即使3个查询分组不同,也可以加入它们,或者我只能加入具有相同“分组依据”的查询


谢谢。

分拣的
计数行
拒绝计数行
查询使用相同的分组列,仅在条件上有所不同,因此可以简化为:

SELECT 
  carusrrsppck as 'users', 
  SUM(CASE WHEN carusrrsppck <> '' and ordprdcnt <> 0 THEN 1 ELSE 0 END) AS 'lines picking', 
  SUM(CASE WHEN carusrrsppck <> '' THEN 1 ELSE 0 END) AS 'lines denied',
  cardepid AS 'deposit'  
FROM order_products
WHERE 
  DATEADD(dd, 0, DATEDIFF(dd, 0, carfchcar)) BETWEEN '20190220' and '20190220' 
  AND 
  cardepid IN ('D32', 'DA', 'DA1', 'DB', 'DC', 'DD', 'DE', 'DR')
GROUP BY carusrrsppck, cardepid
ORDER BY cardepid, COUNT(*) DESC
选择
carusrrsppck作为“用户”,
求和(CARUSRRSPCK“”和ordprdcnt 0,然后1,否则0结束时的情况)为“行拾取”,
总和(CARUSRRSPCK为“”时为1,否则为0结束)为“拒绝的行”,
信用卡作为“存款”
从订购产品
哪里
“20190220”和“20190220”之间的日期添加(dd,0,DATEDIFF(dd,0,carfchcar))
及
cardepid IN('D32','DA','DA1','DB','DC','DD','DE','DR'))
carusrrsppck集团,cardepid
按卡片订购,计数(*)说明

已确认的
计数行
查询是另一种情况,无法与上述情况关联。

我认为您只需要条件聚合:

select carusrrsppck as users, 
       cardepid as deposit  
       sum(case when carusrrsppck <> '' and ordprdcnt <> 0 then 1 else 0 end) AS lines_picking, 
       sum(case when carusrrsppck <> '' and ordprdcnt = 0 then 1 else 0 end) AS lines_denied, 
       count(*) as lines_confirmed
from order_products
where carfchar >= '20190220' and 
      carfchar < '20190221' and
      cardepid in ('D32', 'DA', 'DA1', 'DB', 'DC', 'DD', 'DE', 'DR')
group by carusrrsppck, cardepid
order by cardepid, COUNT(*) DESC;
选择carusrrsppck作为用户,
卡德皮德存款
求和(当carusrrsppck“”和ordprdcnt 0时,则为1,否则为0结束)作为行,
求和(当carusrrsppck“”和ordprdcnt=0时,则为1,否则为0结束)作为行被拒绝,
将(*)计算为已确认的行
从订购产品
其中carfchar>='20190220'和
carfchar<'20190221'和
cardepid in('D32','DA','DA1','DB','DC','DD','DE','DR'))
carusrrsppck集团,cardepid
按卡片订购,计数(*)说明;
注:不进行转换时,日期算法更好。以上内容应与您拥有的内容相同


另外,不要对列别名使用单引号。相反,选择不需要转义的列名。

只要它们具有相同的列数和类型,就可以合并它们。请注意,默认情况下,UNION语句只返回不同的结果(如果这很重要的话)。语法应该是“UNION UNION”。对于同一个carusrrspck,“lines picking”匹配“lines denied”的行@BoCoKeith-point将获得重复数据消除,这可能会偏离您的预期结果。最好添加一个指示结果集是什么的附加列(例如,
SELECT rsType='Lines Picking',carusrrsppck为'usres',COUNT(*)…
)@Forty3是的,我需要为每个计数添加一个附加列。在这种情况下,应该在子查询中进行分隔吗?
SELECT 
  carusrrsppck as 'users', 
  SUM(CASE WHEN carusrrsppck <> '' and ordprdcnt <> 0 THEN 1 ELSE 0 END) AS 'lines picking', 
  SUM(CASE WHEN carusrrsppck <> '' THEN 1 ELSE 0 END) AS 'lines denied',
  cardepid AS 'deposit'  
FROM order_products
WHERE 
  DATEADD(dd, 0, DATEDIFF(dd, 0, carfchcar)) BETWEEN '20190220' and '20190220' 
  AND 
  cardepid IN ('D32', 'DA', 'DA1', 'DB', 'DC', 'DD', 'DE', 'DR')
GROUP BY carusrrsppck, cardepid
ORDER BY cardepid, COUNT(*) DESC
select carusrrsppck as users, 
       cardepid as deposit  
       sum(case when carusrrsppck <> '' and ordprdcnt <> 0 then 1 else 0 end) AS lines_picking, 
       sum(case when carusrrsppck <> '' and ordprdcnt = 0 then 1 else 0 end) AS lines_denied, 
       count(*) as lines_confirmed
from order_products
where carfchar >= '20190220' and 
      carfchar < '20190221' and
      cardepid in ('D32', 'DA', 'DA1', 'DB', 'DC', 'DD', 'DE', 'DR')
group by carusrrsppck, cardepid
order by cardepid, COUNT(*) DESC;