我有3个查询,最终可以获取每日财务信息。我想要做的是将我需要的列组合为1个输入。我尝试做派生表,但它非常草率,不起作用。有没有人有任何建议我如何将这3个组合在一起给我输出?
以下是我的疑问:
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')
答案 0 :(得分: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个不同的查询。
答案 1 :(得分: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'))
答案 2 :(得分:1)
好吧,如果你想要丑陋,凌乱的解决方案,你可以在每个语句周围加上括号,然后将每个语句视为巨型JOIN
中的表(类似于Marc所说的那样)。
如果你想要一个好的解决方案,你将不得不描述你实际上想要做的事情。在不知道您的数据和实际任务的情况下we can't optimize your code。