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;

DATEDIFF 的用法的更多相关文章

  1. SQL Server DATEDIFF() 函数用法

    定义和用法 DATEDIFF() 函数返回两个日期之间的时间,例如计算年龄大小. DATEDIFF(datepart,startdate,enddate)startdate 和 enddate 参数是 ...

  2. SQL中CONVERT()函数用法详解

    SQL中CONVERT函数格式: CONVERT(data_type,expression[,style]) 参数说明: expression 是任何有效的 Microsoft® SQL Server ...

  3. sql server 的datediff函数

    这两天要把一个sqlserver数据库的程序改成oracle的,发现两个数据库之间的函数很多不一样.sqlserver的数据库中的DateDiff 函数用法解释如下: 描述 返回两个日期之间的时间间隔 ...

  4. 【T-SQL基础】01.单表查询-几道sql查询题

    概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...

  5. VBScript常用函数

    VBScript常用函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1.Abs函数 Option Explicit '@author :yinzhengjie 'blog:ht ...

  6. sql-datediff

    SQL中DateDiff的用法 DATEDIFF返回跨两个指定日期的日期和时间边界数. 语法DATEDIFF ( datepart , startdate , enddate ) 参数datepart ...

  7. Sql server中DateDiff用法【转】

    记录下来.每次使用都忘记.... DATEDIFF 函数 [日期和时间] 功能 返回两个日期之间的间隔. 语法 DATEDIFF ( date-part, date-expression-1, dat ...

  8. 详细的SQL中datediff用法

    DATEDIFF 函数 [日期和时间] 功能返回两个日期之间的间隔. 语法DATEDIFF ( date-part, date-expression-1, date-expression-2 ) da ...

  9. DATEDIFF interval=ms的用法

    datediff(ms,@CurrDateTime,@Date)>0 当上面的日期超过24天,用上面的sql会有问题 要修改成如下: (CONVERT(VARCHAR,@CurrDateTime ...

随机推荐

  1. 【BZOJ1923】[Sdoi2010]外星千足虫 高斯消元

    [BZOJ1923][Sdoi2010]外星千足虫 Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用“点足机”的统计结果.每行 ...

  2. Chart控件文档

    假设c1Chart1为Chart控件的一个实例. 一.基本框架图 二.主要外层属性(即this.c1Chart1的主要属性) 1.Header和Footer,上标题和下标题.位于this.c1Char ...

  3. JS添加标签

    <script>            function show(){                $('.add').unbind();                $('.low ...

  4. SqlProfiler的替代品-ExpressProfiler

    可以用来跟踪执行的sql语句.安装SqlServer之后SqlServerManagementStudio自带一个SqlProfiler,但是如果安装的SqlExpress,那就没有了. 项目的主页在 ...

  5. python函数回顾:getattr()

    描述 getattr() 函数用于返回一个对象属性值. 语法 getattr 语法: getattr(object, name[, default]) 参数 object -- 对象. name -- ...

  6. 介绍一下except的用法和作用?

    Python的except用来捕获所有异常,因为Python里面的每次错误都会抛出一个异常,所以每个程序的错误都被当作一个运行时错误.

  7. centos7 安装python3.6

    •到python官网找到下载路径, 用wget下载 wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz •解压tgz包 tar ...

  8. Android-自己定义meta-data扩展数据

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/wwj_748/article/details/25079991 Android-自己定义meta-d ...

  9. 剑指offer 面试14题

    面试14题: 题目:剪绳子 题:给你一根长度为n的绳子,请把绳子剪成m段(m,n都是整数,且n>1,m>1),每段绳子的长度记为k[0],k[1],k[2],...,k[m].请问k[0] ...

  10. key points & (QA) about RPKI

    @1: Q: What does ROA look like?Since ROA means which ASes are allowed for originating routes to some ...