SQL语句表达,我这个月比如有100份订单,每份订单有多项产品,每个产品会有预交期与实际完工日期。例:
so_no(订单号),prd_no(产品型号),mo_dd(预交期),fid_dd(实际完工日期)
A1001,HTE101,2013-06-11,2013-06-15
A1001,HTE102,2013-06-14,2013-06-10
A1002,HTR111,2013-06-11,2013-06-12
A1002,HTR112,2013-06-12,2013-06-13
A1003,TTE101,2013-06-11,2013-06-09
我要的结果是(‘笔’这个字可以不出现)
本月总订单数,超交期数(实际日期>预交期),按时完工数(实际日期<=预交期)
3(笔),2(笔),1(笔)
总的笔数我知道怎么出来,后来二个数据还不懂怎么出来,请高手们帮忙。
COUNT(DISTINCT so_no) 总订单数
sql 如何统计订单按时完工的次数,依据订单,只要有一项未按时完工,整个订单算没完工。
答案:4 悬赏:0 手机版
解决时间 2022-01-01 01:54
- 提问者网友:我没有何以琛的痴心不悔
- 2021-12-31 02:07
最佳答案
- 五星知识达人网友:话散在刀尖上
- 2021-12-31 02:44
SELECt COUNT(DISTINCT so_no) as 总订单数
,sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)<1 THEN 1 ELSE 0 END ) as 超交期数,
sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)>1 THEN 1 ELSE 0 END ) as 按时完工数
FROM test
CREATE TABLE test (so_no VARCHAr(10),
prd_no VARCHAr(10),
mo_dd DATETIME,
fid_dd DATETIME)
INSERT INTO dbo.test
( so_no ,
prd_no ,
mo_dd ,
fid_dd
)
SELECt
'A1001','HTE101','2013-06-11','2013-06-15'
UNIOn ALL
SELECT
'A1001','HTE102','2013-06-14','2013-06-10'
UNIOn ALL
SELECT
'A1002','HTR111','2013-06-11','2013-06-12'
UNIOn ALL
SELECT
'A1002','HTR112','2013-06-12','2013-06-13'
UNIOn ALL
SELECT
'A1003','TTE101','2013-06-11','2013-06-09'
SELECT COUNT(DISTINCT so_no) as 总订单数
,sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)<1 THEN 1 ELSE 0 END ) as 超交期数,
sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)>1 THEN 1 ELSE 0 END ) as 按时完工数
FROM test
-----------------------
3 2 1 sqlserver版
,sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)<1 THEN 1 ELSE 0 END ) as 超交期数,
sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)>1 THEN 1 ELSE 0 END ) as 按时完工数
FROM test
CREATE TABLE test (so_no VARCHAr(10),
prd_no VARCHAr(10),
mo_dd DATETIME,
fid_dd DATETIME)
INSERT INTO dbo.test
( so_no ,
prd_no ,
mo_dd ,
fid_dd
)
SELECt
'A1001','HTE101','2013-06-11','2013-06-15'
UNIOn ALL
SELECT
'A1001','HTE102','2013-06-14','2013-06-10'
UNIOn ALL
SELECT
'A1002','HTR111','2013-06-11','2013-06-12'
UNIOn ALL
SELECT
'A1002','HTR112','2013-06-12','2013-06-13'
UNIOn ALL
SELECT
'A1003','TTE101','2013-06-11','2013-06-09'
SELECT COUNT(DISTINCT so_no) as 总订单数
,sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)<1 THEN 1 ELSE 0 END ) as 超交期数,
sum(CASE WHEN DATEDIFF(DAY,mo_dd,fid_dd)>1 THEN 1 ELSE 0 END ) as 按时完工数
FROM test
-----------------------
3 2 1 sqlserver版
全部回答
- 1楼网友:从此江山别
- 2021-12-31 04:41
SELECt COUNT(DISTINCT so_no) AS '订单总数' FROM C
SELECt count(so_no) AS '超交期数' FROM (SELECt COUNT(so_no) AS '超交期数',so_no FROM C
WHERe fid_dd>mo_dd
GROUP BY so_no) AS A
SELECt COUNT(so_no) AS '按时完工数' FROM C
WHERe fid_dd<=mo_dd AND so_no NOT IN (SELECt so_no FROM C WHERe fid_dd > mo_dd)
将三条语句执行的结果插入到临时表,最后查询临时表,可以在临时表中新增个月份字段,这样就可以按月份统计
- 2楼网友:逃夭
- 2021-12-31 03:14
select COUNT(DISTINCT so_no) 总订单数,sum(case when fid_dd>mo_dd then 1 else 0) 超交期数,
sum(case when fid_dd<=mo_dd then 1 else 0) 按时完工数
from table1;
- 3楼网友:長槍戰八方
- 2021-12-31 02:57
你好!
select COUNT(DISTINCT so_no) 总订单数,sum(case when fid_dd>mo_dd then 1 else 0) 超交期数,
sum(case when fid_dd<=mo_dd then 1 else 0) 按时完工数
from table1;
打字不易,采纳哦!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯