确定一个表中的日期是否介于另一个表中的日期之间

时间:2018-09-21 15:16:57

标签: reporting-services

我有两列日期,第一列是收到要检查的采购订单的日期,第二列是接受或拒绝采购订单的日期。我想要一个在X轴上带有日期的图表,然后在Y轴上带有当天队列中的采购订单数量。

某些采购订单在当天完成,因此仍会被计算在内,但几天或几周内可能无法得到处理,因此将在所有这些天中对它们进行计数,直到得到处理为止。

以下示例数据:

Row ID     Date In      Date Out
1          9/1/18       9/1/18
2          9/1/18       9/1/18
3          9/1/18       9/2/18
4          9/1/18       9/3/18
1          9/2/18       9/2/18
2          9/2/18       9/4/18

因此,9/1/18为4,9 / 2/18为4,9 / 3/18为2,9 / 4/18为1。

我对Excel提出了类似的问题,并取得了成功。但是,我们希望从ERP系统中生成数据(而不是将数据手动复制到Excel),我认为使用SQL和SSRS可能实现这一点。

Excel的公式为=COUNTIFS($A$1:$A$1000,"<="&C1,Sheet1!$B$1:$B$1000,">="&C1),因此A和B分别是Date In和Date Out列,然后C是一年中所有天的列。

我不确定如何进行这样的查询,因此我首先使其类似于Excel解决方案。我生成了一个日期列表,我试图将该列表中的日期与我的采购订单日期数据集进行比较,但是我不确定如何执行此操作,因为它会将一个(行)进行比较。 / p>

2 个答案:

答案 0 :(得分:0)

尝试使用“ ReportItems”进行比较!属性。尝试编写类似以下内容的表达式:

iif(ReportItems!DateColumnX = somevalue, "Then Do something", "Else Do Something else")

编辑:

尝试这样的事情:

Fields!ValueFromCurrentDataset(CompareLogic like(<,>,<> etc)) LookUp( Fields!ValueFromDataset2, "DataSet2")

答案 1 :(得分:0)

您需要创建一个日期表,然后根据日期在之间的日期加入到您的订单中,以便将它们计算在多天内。

DECLARE @START_DATE AS DATE = '2018-09-01'
DECLARE @END_DATE AS DATE = '2018-09-30'

IF OBJECT_ID('tempdb..#DATES')  IS NOT NULL DROP TABLE #DATES;
IF OBJECT_ID('tempdb..#ORDERS') IS NOT NULL DROP TABLE #ORDERS;

SELECT 1 AS ROW_ID, CAST('2018-09-01' AS DATE) AS DATE_IN, CAST('2018-09-01' AS DATE) AS DATE_OUT
INTO #ORDERS
UNION
SELECT 2 AS ROW_ID, CAST('2018-09-01' AS DATE) AS DATE_IN, CAST('2018-09-01' AS DATE) AS DATE_OUT
UNION
SELECT 3 AS ROW_ID, CAST('2018-09-01' AS DATE) AS DATE_IN, CAST('2018-09-02' AS DATE) AS DATE_OUT
UNION
SELECT 4 AS ROW_ID, CAST('2018-09-01' AS DATE) AS DATE_IN, CAST('2018-09-03' AS DATE) AS DATE_OUT
UNION
SELECT 1 AS ROW_ID, CAST('2018-09-02' AS DATE) AS DATE_IN, CAST('2018-09-02' AS DATE) AS DATE_OUT
UNION
SELECT 2 AS ROW_ID, CAST('2018-09-02' AS DATE) AS DATE_IN, CAST('2018-09-04' AS DATE) AS DATE_OUT


;WITH GETDATES AS (SELECT @START_DATE AS DATE1
                  UNION ALL
                  SELECT DATEADD(DAY, 1, DATE1)
                  FROM GETDATES
                  WHERE DATE1 < @END_DATE
                 )
SELECT *
INTO #DATES
FROM GETDATES;

SELECT  DATE1, COUNT(O.ROW_ID) AS ORDERS
FROM #DATES D 
LEFT JOIN #ORDERS O ON D.DATE1 BETWEEN O.DATE_IN AND O.DATE_OUT
GROUP BY D.DATE1