循环访问1个SQL查询,而不是使用2个查询

时间:2017-06-15 01:45:33

标签: sql sql-server tsql

下面我有一个带有两个基本SELECT语句的变量。如果用户只能使用一个变量@StartDate来输入日期参数​​,那么如何循环查询只使用一个SELECT语句而不是两个,如下所示。因此,假设您没有两个查询,但只有一个查询,它只能运行一次但满足下面WHERE子句中的2个条件,即“FullDate = @StartDate”和“Fulldate BETWEEN DATEADD(mm,DATEDIFF( mm,0,@ StartDate),0)AND @StartDate“。所以,基本上我试图将两个SELECT语句压缩成一个,并从两个查询中提供相关数据。 UNION ALL不能在这里使用。

DECLARE @StartDate DATE = '20170610'

SELECT
    FirstName,
    LastName,
    SUM(MoneySpent) AS 'Spent'  
FROM
    TableOne  
WHERE
    FullDate = @StartDate;  
GROUP BY FirstName, LastName

SELECT 
    FirstName,
    LastName,
    SUM(MoneySpent) AS 'Spent'    
FROM
    TableOne    
WHERE
    Fulldate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, @StartDate), 0) AND @StartDate;  
GROUP BY FirstName, LastName

4 个答案:

答案 0 :(得分:0)

如果你有两个日期@ Date1和@ Date2,那么你可以使用它:

SELECT
    FirstName,
    LastName,
    SUM(CASE WHEN FullDate = @Date1 THEN MoneySpent ELSE 0 END) AS 'SpentDate1',
    SUM(CASE WHEN FullDate = @Date2 THEN MoneySpent ELSE 0 END) AS 'SpentDate2'  
FROM
    TableOne  
WHERE
    FullDate = @Date1 OR FullDate = @Date2
GROUP BY FirstName, LastName

BTW,UNION ALL也可以使用,你只需将其包装为子查询并使用额外的GROUP BY

答案 1 :(得分:0)

关于BETWEEN,您需要了解的是它包含端点,因此您只需要第二个查询来获取您要查找的内容。

SELECT 
    FirstName,
    LastName,
    SUM(MoneySpent) AS 'Spent'    
FROM
    TableOne    
WHERE
    Fulldate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, @StartDate), 0) AND @StartDate;  
GROUP BY FirstName, LastName

答案 2 :(得分:0)

我认为第一个查询已经被第二个查询覆盖了。

            SELECT  FirstName ,
                    LastName ,
                    SUM(MoneySpent) AS 'Spent'
            FROM    TableOne
            WHERE   Fulldate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, @StartDate), 0)
                             AND     @StartDate 
            GROUP BY FirstName, LastName

但是为了论证,许多选项中的一个是您可以将每个查询的相关数据放入临时表中。

E.g。

        DECLARE @StartDate DATE = '2017.06.10';


        IF ( OBJECT_ID('tempdb..#tempTbl') IS NOT NULL )
            BEGIN
                DROP TABLE #tempTbl
            END

        CREATE TABLE #tempTbl
            (
              ID INT IDENTITY(1, 1)
                     PRIMARY KEY ,
              FirstName VARCHAR(100) ,
              LastName VARCHAR(100) ,
              Spent MONEY
            )

        INSERT  INTO #tempTbl
                ( FirstName ,
                  LastName ,
                  Spent
                )
                SELECT  FirstName ,
                        LastName ,
                        SUM(MoneySpent) AS 'Spent'
                FROM    TableOne
                WHERE   FullDate = @StartDate

        INSERT  INTO #tempTbl
                ( FirstName ,
                  LastName ,
                  Spent
                )
                SELECT  FirstName ,
                        LastName ,
                        SUM(MoneySpent) AS 'Spent'
                FROM    TableOne
                WHERE   Fulldate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, @StartDate), 0)
                                 AND     @StartDate 

        SELECT  FirstName,LastName,Spent
        FROM    #tempTbl
        GROUP BY FirstName, LastName

答案 3 :(得分:0)

我认为,在这里你可以看出One Resultset是另一个结果集的子集。 换句话说,Range Between条件满足条件。所以结果不重复。

示例 DECLARE @StartDate DATE = '20020224' DECLARE @StartDate1 DATE = '20020303' select a.BusinessEntityID ,a.RateChangeDate,sum(a.rate) Rate from HumanResources.EmployeePayHistory a where a.RateChangeDate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, @StartDate), 0) AND @StartDate or a.RateChangeDate=@StartDate1 GROUP by a.BusinessEntityID,a.RateChangeDate

6 rows

此处两个日期的结果均可见( DECLARE @StartDate DATE = '20020224' DECLARE @StartDate1 DATE = StartDate

如果我们这样做

between condtion

此处RateChangeDate=@StartDate1满足5 rows 所以输出只有 select ... FullDate between

所以你的想法是无法实现的。

如果我遇到同样的问题,那么我只会使用pull data for fulldate=@StartDate

在前端我会;With CTE as ( SELECT FirstName, LastName, Fulldate, SUM(MoneySpent) AS 'Spent' FROM TableOne WHERE Fulldate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, @StartDate), 0) AND @StartDate; GROUP BY FirstName, LastName,Fulldate ) select *,0 flg from cte union ALL select * ,1 flg from cte where Fulldate=@StartDate

如果你真的使用2选择就像你现在这样做,

d-flex justify-content-center align-items-center