简体   繁体   English

需要结合两个SQL查询

[英]Need to combine two SQL queries

I have two code samples that I would like to combine. 我想合并两个代码示例。 The first provides the number of people in Jail for a point in time. 第一个提供了某个时间点在监狱中的人数。

SELECT
    COUNT (Distinct b.BookingNumber)
FROM 
    Booking b
INNER JOIN 
    HousingNodePersonHistory hnph ON b.BookingKey = hnph.BookingKey
INNER JOIN 
    View_Housing vh ON hnph.HousingNodeKey = vh.HousingNodeKey
WHERE 
    b.BookingDateTime <= '2018-08-13 06:23:59.999'
    AND hnph.BeginDateTime <= '2018-08-13 06:23:59.999' 
    AND (hnph.EndDateTime >= '2018-08-13 06:23:59.999' OR hnph.EndDateTime IS NULL OR hnph.EndDateTime = '')
    AND (vh.HousingDescription LIKE '%ACJ%')

The second returns a list of datetimes that I would like the first to use to give me the same population return for every day in 2018 so I can provide an updated average daily population for 2018. 第二个返回日期时间列表,我希望第一个时间使用该时间为我提供2018年每天的相同人口收益,因此我可以提供更新的2018年平均每日人口数。

DECLARE @StartDate DATETIME = '2018-01-01 00:00:00.000',
        @EndDate   DATETIME = GETDATE();

WITH theDates AS
(
    SELECT 
        @StartDate AS theDate
    UNION ALL
    SELECT 
        DATEADD(DAY, 1, theDate)
    FROM 
        theDates
    WHERE 
        DATEADD(DAY, 1, theDate) <= @EndDate
)   
SELECT 
    theDate
    --,1 as theValue
FROM 
    theDates
OPTION (MAXRECURSION 0)

Is there any way to use the dates provided by the second query to populate and run in the where clause in the first query? 有什么方法可以使用第二个查询提供的日期来填充并在第一个查询的where子句中运行?

You can use a join : 您可以使用join

WITH theDates AS (
      SELECT @StartDate as theDate
      UNION ALL
      SELECT DATEADD(day, 1, theDate)
      FROM theDates
      WHERE DATEADD(day, 1, theDate) <= @EndDate
     ) 
SELECT d.theDate, COUNT(Distinct b.BookingNumber)
FROM (SELECT d.theDate, DATEADD(minute, d.theDate, 6*60 + 24) as ddhhmm
      FROM thedates d
     ) d CROSS JOIN
     Booking b JOIN
     HousingNodePersonHistory hnph
     ON b.BookingKey = hnph.BookingKey JOIN
     View_Housing vh
     ON hnph.HousingNodeKey = vh.HousingNodeKey
WHERE b.BookingDateTime <= d.ddhhmm AND
      hnph.BeginDateTime <= d.ddhhmm AND
      (hnph.EndDateTime >= d.ddhhmm OR hnph.EndDateTime IS NULL OR hnph.EndDateTime = ''
      ) AND
      vh.HousingDescription LIKE '%ACJ%' 
GROUP BY d.theDate
OPTION (MAXRECURSION 0);

I would do it by adding a day range to your calendar table and filtering by day. 我可以通过在日历表中添加日期范围并按日期进行过滤来实现。 You then simply add the StartDate in the select list and aggregate it in the GROUP BY . 然后,您只需在选择列表中添加StartDate并将其聚合到GROUP BY

DECLARE @StartDate datetime = '2018-01-01 00:00:00.000'
       ,@EndDate   datetime = GETDATE()

     ;WITH theDates AS
     (SELECT @StartDate as startDate, DATEADD(minute,-1,DATEADD(day,1,@StartDate)) as endDate
      UNION ALL
      SELECT DATEADD(day, 1, startDate) as startDate, DATEADD(minute,-1,DATEADD(day,2,startDate))  as endDate
        FROM theDates
       WHERE DATEADD(day, 1, startDate) <= @EndDate
     )

    Select 
        StartDate = theDates.startDate,  
        BookingCount = COUNT (Distinct b.BookingNumber)
    FROM 
        Booking b
        INNER JOIN HousingNodePersonHistory hnph ON b.BookingKey = hnph.BookingKey
        INNER JOIN View_Housing vh ON hnph.HousingNodeKey = vh.HousingNodeKey
        INNER JOIN theDates D ON b.BookingDateTime BETWEEN D.startDate AND d.endDate 
    WHERE 
        vh.HousingDescription LIKE '%ACJ%'
    GROUP BY
       theDates.StartDate
OPTION (MAXRECURSION 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM