DATEDIFF 的用法
DECLARE @date DATETIME = '2017-12-26 00:00:00';
DECLARE @date2 DATETIME = DATEADD(DAY, 1, @date);
DECLARE @1 TABLE ( subdate INT );
DECLARE @result TABLE
(
Stype VARCHAR(50) ,
count1 INT ,
count2 INT ,
count3 INT ,
count4 INT ,
count5 INT ,
percent1 DECIMAL(10, 2) ,
percent2 DECIMAL(10, 2) ,
percent3 DECIMAL(10, 2) ,
percent4 DECIMAL(10, 2)
);
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
JOIN dbo.TWX_TransportOrder b ON a.TransportOrderCode = b.TransportOrderCode
WHERE b.Country = '中国台湾'
AND b.CarrierType = 'ZHIYUN'
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT '台湾直送' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '新加坡'
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT '新加坡' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '马来西亚'
AND a.Province NOT IN ( '纳闵', '沙捞越', '沙巴' )
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT '马来西亚(西)' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '马来西亚'
AND a.Province IN ( '纳闵', '沙捞越', '沙巴' )
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT '马来西亚(东)' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '澳大利亚'
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT '澳大利亚' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '新加坡'
AND a.ShipType = 0
AND a.OrderFrom = 15
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT 'Lazada新加坡空运' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '马来西亚'
AND a.ShipType = 0
AND a.OrderFrom = 15
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT 'Lazada马来西亚空运' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '新加坡'
AND a.TooWeight <> 1
AND a.ShipType = 1
AND a.OrderFrom = 15
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT 'Lazada新加坡海运(小货)' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '新加坡'
AND a.TooWeight = 1
AND a.ShipType = 1
AND a.OrderFrom = 15
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT 'Lazada新加坡海运(大货)' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '马来西亚'
AND a.TooWeight <> 1
AND a.ShipType = 1
AND a.OrderFrom = 15
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT 'Lazada马来西亚海运(小货)' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '马来西亚'
AND a.TooWeight = 1
AND a.ShipType = 1
AND a.OrderFrom = 15
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT 'Lazada马来西亚海运(大货)' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '新西兰'
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT '新西兰' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
INSERT INTO @1
SELECT DATEDIFF(HOUR, a.ReceivedTime, DomesticWarehouseInTime) AS subdate
FROM TWX_TransportFromMst a WITH ( NOLOCK )
WHERE a.Country = '日本'
AND a.ReceivedTime BETWEEN @date AND @date2;
INSERT @result
SELECT '日本' ,
a ,
a1 ,
a2 ,
a3 ,
a4 ,
a1 * 100.00 / ( a + a5 ) ,
( a1 + a2 ) * 100.00 / ( a + a5 ) ,
( a1 + a2 + a3 ) * 100.00 / ( a + a5 ) ,
a4 * 100.00 / ( a + a5 )
FROM ( SELECT ( SELECT COUNT(1)
FROM @1
) a ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > -1000
AND subdate <= 8
) a1 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 8
AND subdate <= 24
) a2 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 24
AND subdate <= 48
) a3 ,
( SELECT COUNT(1)
FROM @1
WHERE subdate > 48
) a4 ,
( CASE WHEN ( SELECT COUNT(1)
FROM @1
) > 0 THEN 0
ELSE 1
END ) a5
) b;
DELETE FROM @1;
SELECT CONVERT(VARCHAR(100), @date, 23) 日期 ,
Stype 渠道 ,
count1 签收单量 ,
count2 '0-8h入库单量' ,
count3 '8-24h入库单量' ,
count4 '24-48h入库单量' ,
count5 '48小时以后入库单量' ,
CONVERT (VARCHAR(10), percent1) + '%' '8小时内达成率' ,
CONVERT (VARCHAR(10), percent2) + '%' '24小时内达成率' ,
CONVERT (VARCHAR(10), percent3) + '%' '48小时内达成率' ,
CONVERT (VARCHAR(10), percent4) + '%' '48小时外占比'
FROM @result
UNION ALL
SELECT '' ,
'合计' ,
SUM(count1) ,
SUM(count2) ,
SUM(count3) ,
SUM(count4) ,
SUM(count5) ,
'' ,
'' ,
'' ,
''
FROM @result;
posted on 2017-12-26 09:51 chengjunde 阅读(278) 评论(0) 编辑 收藏 举报