带数据透视表的存储过程不返回结果结构,而是零值

时间:2019-04-20 15:34:42

标签: mysql sql stored-procedures

MyActualresult我正在尝试获取在不同日期具有特定状态的数据计数。诸如跟踪装运并获取今天的报告之类的东西。

我已将Pivot与存储过程一起使用。我得到的结果是列和行,但是执行Sp时返回值null

有人可以建议我如何解决该问题吗?

我试图将查询放入某个变量中,然后执行,但未正确完成。

    Create PROCEDURE [dbo].[GetCountOfShipmentWithStatus]

    @DateToStart Date,
    @DateToEnd Date,
    @LabName nvarchar(30)

    AS
    BEGIN

        SET NOCOUNT ON;

        WITH TrackShipment AS
    (
      SELECT exData.ID, CAST(ExpectedDeliveryDt AS DATE) AS Deliverydate,A.AccountCode,L.Name, DATENAME(dw, ExpectedDeliveryDt) AS DayOfWeek,exData.MileStoneTypeId AS TrackingStatus,exData.AccountNo 
      FROM [Tracking].[TrackingExternalData] exData
      Left Join [systemManagement].[SystemMetaData] sysmetadat on exdata.MileStoneTypeId=sysmetadat.systemMetaDataId
      Left Join  [Data_Replication_EZSHIP].[dbo].[AccountNumber] AS A on exData.AccountNo=A.AccountCode
      Left JOIN [Data_Replication_EZSHIP].[dbo].[Location] AS L ON A.LocationId = L.Id 
      WHERE L.Name=@LabName
      AND sysmetadat.systemMetaDataId IN ('E770CE7C-E0E6-40C6-AC51-5D2129F2DEB7','D6A011C8-C39C-45B5-9127-52D20C68E1C3','C7657AE1-9354-E911-BB4A-005056B00B08','559ECEC2-969A-4F8C-9A95-21C613D82F3A')
      AND exData.ExpectedDeliveryDt <= @DateToStart AND exData.ExpectedDeliveryDt >= @DateToEnd
      )

    SELECT  Deliverydate, DayOfWeek, 
            -- List of Pivoted Columns
            [E770CE7C-E0E6-40C6-AC51-5D2129F2DEB7],[D6A011C8-C39C-45B5-9127-52D20C68E1C3], [C7657AE1-9354-E911-BB4A-005056B00B08], [559ECEC2-969A-4F8C-9A95-21C613D82F3A]
    FROM TrackShipment

    PIVOT 
    (
       COUNT(Id)
       -- List of Pivoted columns
       FOR TrackingStatus IN([E770CE7C-E0E6-40C6-AC51-5D2129F2DEB7],[D6A011C8-C39C-45B5-9127-52D20C68E1C3], [C7657AE1-9354-E911-BB4A-005056B00B08], [559ECEC2-969A-4F8C-9A95-21C613D82F3A])
    ) as pvt
    ORDER BY Deliverydate DESC


    END
    GO

我希望返回值与数据透视表的结果相同。

1 个答案:

答案 0 :(得分:0)

我猜这种情况是不正确的:

exData.ExpectedDeliveryDt <= @DateToStart AND 
exData.ExpectedDeliveryDt >= @DateToEnd

我不知道您打算做什么,因为您没有提供示例数据,所需结果或所需逻辑的解释。

但是我猜测比较是倒退的:

exData.ExpectedDeliveryDt >= @DateToStart AND 
exData.ExpectedDeliveryDt <= @DateToEnd