• 分开统计的sql写法


    DECLARE @StartDate DATETIME= '2017-10-13 00:00:00';
    DECLARE @EndDate DATETIME= '2017-11-13 23:00:00';
    DECLARE @Country NVARCHAR(20)= '中国香港';
    DECLARE @到港单量 INT;
    DECLARE @派送单量 INT;
    DECLARE @妥投单量 INT;
    DECLARE @累积在分拨仓单量 INT;
    DECLARE @累积派送在途单量 INT;
    DECLARE @累积未派送完成单量 INT;
    DECLARE @自提已上架量 INT;
    DECLARE @派送已签收量 INT;
    DECLARE @派送异常单量 INT;
    DECLARE @派送异常单量占比 VARCHAR(10);

    SELECT @到港单量 = COUNT(DISTINCT a.TransportOrderID)
    FROM dbo.TWX_TransportOrder a WITH ( NOLOCK )
    LEFT JOIN dbo.Log_TransportOrderTrack b WITH ( NOLOCK ) ON a.TransportOrderID = b.TransportOrderID
    WHERE a.country = @Country
    AND a.InterDeliveryID = CAST(SUBSTRING('1[4PX]', 0, CHARINDEX('[', '1[4PX]')) AS INT)
    AND b.TrackActionCode IN ( 'CAI_CUSTOMS_CLR_SUC', 'OTMS_SENT_CITY' )
    AND b.TrackTime >= @StartDate
    AND b.TrackTime <= @EndDate;
    SELECT @派送单量 = COUNT(DISTINCT a.TransportOrderID)
    FROM dbo.TWX_TransportOrder a WITH ( NOLOCK )
    LEFT JOIN dbo.Log_TransportOrderTrack b WITH ( NOLOCK ) ON a.TransportOrderID = b.TransportOrderID
    WHERE a.country = @Country
    AND a.InterDeliveryID = CAST(SUBSTRING('1[4PX]', 0,CHARINDEX('[', '1[4PX]')) AS INT)
    AND b.TrackActionCode IN ( 'GTMS_DELIVERING', 'SENT_SCAN',
    'OTMS_DLV_ATTEMPT', 'GTMS_RE_DELIVERING',
    'GTMS_DELIVERING' )
    AND b.TrackTime >= @StartDate
    AND b.TrackTime <= @EndDate;
    SET @累积在分拨仓单量 = @到港单量 - @派送单量;
    IF @累积在分拨仓单量 < 0
    BEGIN
    SET @累积在分拨仓单量 = 0;
    END;
    SELECT @派送异常单量 = COUNT(DISTINCT a.TransportOrderID)
    FROM dbo.TWX_TransportOrder a WITH ( NOLOCK )
    LEFT JOIN dbo.Log_TransportOrderTrack b WITH ( NOLOCK ) ON a.TransportOrderID = b.TransportOrderID
    WHERE a.country = @Country
    AND a.InterDeliveryID = CAST(SUBSTRING('1[4PX]', 0, CHARINDEX('[', '1[4PX]')) AS INT)
    AND b.TrackActionCode IN ( 'CAI_SIGN_IN_FAIL', 'FAILED',
    'GTMS_DEL_FAILURE' )
    AND b.TrackTime >= @StartDate
    AND b.TrackTime <= @EndDate;
    SET @累积派送在途单量 = @派送单量 + @派送异常单量;
    SET @累积未派送完成单量 = @累积在分拨仓单量 + @累积派送在途单量;
    SELECT @派送已签收量 = COUNT(DISTINCT a.TransportOrderID)
    FROM dbo.TWX_TransportOrder a WITH ( NOLOCK )
    LEFT JOIN dbo.Log_TransportOrderTrack b WITH ( NOLOCK ) ON a.TransportOrderID = b.TransportOrderID
    WHERE a.country = @Country
    AND a.InterDeliveryID = CAST(SUBSTRING('1[4PX]', 0,CHARINDEX('[', '1[4PX]')) AS INT)
    AND b.TrackCode IN ( 'GTMS_SIGNED', 'SIGNED', 'GSTA_SIGN', 'STATION_SIGNED' )

    AND b.TrackTime >= @StartDate
    AND b.TrackTime <= @EndDate;
    SET @妥投单量 = @派送已签收量 + @累积派送在途单量;
    IF ( @派送单量 = 0
    AND @派送异常单量 = 0
    )
    BEGIN
    SET @派送异常单量占比 = '0%';
    END;
    ELSE
    BEGIN
    SET @派送异常单量占比 = CAST(ROUND(( CONVERT(FLOAT, @派送异常单量)
    / CONVERT(FLOAT, @妥投单量) ) * 100, 2) AS VARCHAR(10))
    + '%';
    END;
    SELECT @StartDate '开始时间' ,
    @EndDate '结束时间' ,
    @Country '区域' ,
    @到港单量 '到港单量' ,
    @派送单量 '派送单量' ,
    @累积在分拨仓单量 '累积在分拨仓单量' ,
    @累积派送在途单量 '累积派送在途单量' ,
    @累积未派送完成单量 '累积未派送完成单量' ,
    @妥投单量 '妥投单量' ,
    @派送异常单量 '派送异常单量' ,
    @派送异常单量占比 '派送异常单量占比';

  • 相关阅读:
    php 请求另一个服务器接口返回数据
    yii2 安装
    无限分类的
    程序员学习能力提升三要素转载
    为什么有的人工作多年还是老样子
    随笔杂谈
    Ecshop商品描述上传中文名图片无法显示解决方法
    「PSR 规范」PSR-2 编码风格规范
    leetcode 之Set Matrix Zeroes(10)
    leetcode 之Plus One(9)
  • 原文地址:https://www.cnblogs.com/chengjun/p/7842986.html
Copyright © 2020-2023  润新知