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. java中通过配置文件的方式(Jedis驱动)使用Redis

    在java中使用Redis,实际上是将Redis的一些命令封装到Jedis的实体类中,然后进行调用.

  2. 3673: 可持久化并查集 by zky

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 2170  Solved: 978[Submit][Status ...

  3. C#窗体传值

    整理一下: 1.静态变量传值,非常简单适合简单的非实例的 public calss form1:Form{ public static int A; } public class form2:Form ...

  4. nosql_action

    ps -aux  查当前端口占用 connecting to: test > show dbs local .078125GB testphp .203125GB > use testph ...

  5. ORACLE中的MERGE语法使用记录

    项目中使用到了Oracle的MERGE INTO语句,在这里简单记录下使用方法 使用场景如下: 存在对一张数据量很大的表,你需要对里面的大量数据进行更新,如果数据不存在,就进行插入的操作. 常规想到的 ...

  6. Qt里的原子操作QAtomicInteger

    所谓原子操作,即一系列复杂的操作能一气呵成,中间不被其他的操作打断.这在多线程程序中尤其常见,但要实现这种功能,既要考虑程序的良好设计,又要关心特定平台的体系结构和相关编译器对原子特性的支持程度.所以 ...

  7. Python操作Redis(一)

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  8. 目标是:互联网方向的Java开发工程师

    我们需要这样完美的你: --师出名门:应届统招本科及以上学历,计算机相关专业: --技艺超群:熟悉JAVA面向对象编程, J2EE体系结构,熟悉Struts2.Spring.Ibtais.Memcac ...

  9. spring中配置缓存—ehcache

    常用的缓存工具有ehcache.memcache和redis,这里介绍spring中ehcache的配置. 1.在pom添加依赖: <!-- ehcache 相关依赖 --> <de ...

  10. Webpack,Browserify和Gulp三者之间到底是怎样的关系

    转:https://zhidao.baidu.com/question/1799220342210982227.html怎么解释呢?因为 Gulp 和 browserify / webpack 不是一 ...