3

我有 3 个查询最终获得了我的日常财务信息。我想做的是将我需要的列组合为 1 个输入。我试着做派生表,但它非常草率而且没有用。有没有人对我如何将这 3 个结合起来在 1 行上给我输出有任何建议?

这是我的查询:

SELECT        ReportCategoryID, ReportCategoryName, PrintOrder, QTY, NetAmt, customers, Average
FROM            dbo.fRevenueSummary(@startdate, @enddate) AS fRevenueSummary_1

第二个查询:

SELECT        COUNT(*) AS count, SUM(TicketsDetails.dblTotal) AS Merch_Total
FROM            TicketsDetails INNER JOIN
                         Surcharges ON TicketsDetails.Reference_ID = Surcharges.Surcharge_ID INNER JOIN
                         SurchargesTypes ON Surcharges.lSurchargeTypeID = SurchargesTypes.lSurchargeTypeID
WHERE        (TicketsDetails.dtCreated BETWEEN @startdate AND @enddate) AND (SurchargesTypes.lSurchargeTypeID = '2') AND (TicketsDetails.bPaid = 1) AND 
                         (TicketsDetails.Detail_Type_ID = 3) AND (NOT (TicketsDetails.sDescription = N'TIP'))

第三个查询:

SELECT        SUM(dblRegLabor) AS Regular_Amount, SUM(dblOTLabor) AS OT_Amount
FROM            TimeClock
WHERE        (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '1') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '2') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '3') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '4') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '5') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '6') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '7')
4

3 回答 3

2

最简单的方法是这样

SELECT *
FROM
(
    SELECT        ReportCategoryID, ReportCategoryName, PrintOrder, QTY, NetAmt, customers, Average
    FROM            dbo.fRevenueSummary(@startdate, @enddate) AS fRevenueSummary_1
)
,
(
    SELECT        COUNT(*) AS count, SUM(TicketsDetails.dblTotal) AS Merch_Total
    FROM            TicketsDetails INNER JOIN
                         Surcharges ON TicketsDetails.Reference_ID = Surcharges.Surcharge_ID INNER JOIN
                         SurchargesTypes ON Surcharges.lSurchargeTypeID = SurchargesTypes.lSurchargeTypeID
    WHERE        (TicketsDetails.dtCreated BETWEEN @startdate AND @enddate) AND (SurchargesTypes.lSurchargeTypeID = '2') AND (TicketsDetails.bPaid = 1) AND 
                         (TicketsDetails.Detail_Type_ID = 3) AND (NOT (TicketsDetails.sDescription = N'TIP'))
)
,
(
    SELECT        SUM(dblRegLabor) AS Regular_Amount, SUM(dblOTLabor) AS OT_Amount
    FROM            TimeClock
    WHERE        (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '1') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '2') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '3') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '4') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '5') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '6') OR
                         (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = '7')
)

但如果它们之间没有关系,您将返回笛卡尔积。

我不认为没有笛卡尔积就可以组合 3 个不同的查询。

于 2012-12-17T15:03:15.173 回答
1

这不是您问题的答案,但它可以简化您的想法,知道您的第三个查询可以大大简化:

SELECT  SUM(dblRegLabor) AS Regular_Amount, SUM(dblOTLabor) AS OT_Amount
FROM    TimeClock
WHERE   (dtTimeIn > @startdate) AND (dtTimeOut < @enddate) AND (sDept = IN('1', '2', '3', '4', '5', '6', '7'))
于 2012-12-17T15:03:36.100 回答
1

好吧,如果您想要丑陋、凌乱的解决方案,您可以在每个语句周围加上括号,然后将每个语句视为一个巨大的表格JOIN(类似于 Marc 说明的内容)。

如果您想要一个好的解决方案,您将不得不描述您实际尝试做的事情。在不了解您的数据和实际任务的情况下,我们无法优化您的代码

于 2012-12-17T15:09:17.750 回答