0

我有以下两个 Select 语句-

SELECT * 
FROM tblAllocations
WHERE AllocID IN
(
    SELECT MAX(AllocID)    
    FROM tblAllocations
    WHERE FeeEarner = 'KLW' AND [Date] <= '2013-12-31'
    GROUP BY FeeEarner, CaseNo

SELECT UserID, CaseNo, SUM(Fees) AS [Fees]
FROM tblTimesheetEntries
WHERE UserID = 'KLW' AND [Date] <= '2013-12-31'
GROUP BY UserID, CaseNo

返回以下结果 -

单独查询的结果

我想要的是将它们组合在一个 Select 语句中,该语句从第一个查询中提取一些字段,从第二个查询中提取其他字段。根据上面的结果,应该只返回 5 行,其中 3 行将包含来自两个 Query 的字段,2 个将只有来自一个 Query 的字段(因此它会有一些 NULL 值)

我尝试了以下 -

SELECT q1.CaseNo, q1.FeeEarner, 
       q2.Fees AS [Fees], 
       q1.Fees AS [Billed], 
      (q2.Fees - q1.Fees) AS WIP
FROM
(
    SELECT * 
    FROM tblAllocations
    WHERE AllocID IN
    (
        SELECT MAX(AllocID)    
        FROM tblAllocations
        WHERE FeeEarner = 'KLW'
        AND [Date] <= '2013-12-31'
        GROUP BY FeeEarner, CaseNo
    )
) AS q1,
(
    SELECT UserID, CaseNo, SUM(Fees) AS [Fees]
    FROM tblTimesheetEntries
    WHERE UserID = 'KLW'
    AND [Date] <= '2013-12-31'
    GROUP BY UserID, CaseNo
) AS q2

然而,这就像一个交叉连接,并给了我 15 行如下 -

错误的结果

有人可以建议我如何正确组合这两个查询以便只返回 5 行。

4

3 回答 3

1

以下是更正后的查询:

 SELECT q1.CaseNo, q1.FeeEarner, 
           q2.Fees AS [Fees], 
           q1.Fees AS [Billed], 
          (q2.Fees - q1.Fees) AS WIP
    FROM
    (
    SELECT * 
    FROM tblAllocations
    WHERE AllocID IN
    (
    SELECT MAX(AllocID)    
    FROM tblAllocations
    WHERE FeeEarner = 'KLW'
    AND [Date] <= '2013-12-31'
    GROUP BY FeeEarner, CaseNo
    )
    ) AS q1,
    (
    SELECT UserID, CaseNo, SUM(Fees) AS [Fees]
    FROM tblTimesheetEntries
    WHERE UserID = 'KLW'
    AND [Date] <= '2013-12-31'
    GROUP BY UserID, CaseNo
    ) AS q2
    where q1.CaseNo = q2.CaseNo
于 2013-09-29T10:43:20.313 回答
0

它正在进行交叉连接,因为您没有说如何q1以及q2“相关”(已连接)。

此外,由于您需要五行(即,tblTimesheetEntries无论 in 匹配的所有行tblAllocations),您应该使用右连接(或左但反转q1and q2):

SELECT
  q2.CaseNo,
  q1.FeeEarner,
  q2.Fees AS [Fees],
  q1.Fees AS [Billed],
  (q2.Fees - q1.Fees) AS WIP
FROM (
  SELECT * FROM tblAllocations
  WHERE AllocID IN (
    SELECT MAX(AllocID)
    FROM tblAllocations
    WHERE FeeEarner = 'KLW' AND [Date] <= '2013-12-31'
    GROUP BY FeeEarner, CaseNo
  )) AS q1
RIGHT JOIN (
  SELECT UserID, CaseNo, SUM(Fees) AS[Fees]
  FROM tblTimesheetEntries
  WHERE UserID = 'KLW'
  AND [Date] <= '2013-12-31'
  GROUP BY UserID, CaseNo
) AS q2
  ON q1.CaseNo = q2.CaseNo

SqlFiddle在这里

于 2013-09-29T11:01:01.010 回答
0

我将从您的第二个查询开始,然后向您的第一个查询 LEFT-JOIN。第二个查询具有相应的案例编号 15、25 和 33,它们将显示所有列,还有案例 5 和 12,这将导致其他列的 NULL。

select 
      AllThese.UserID,
      AllThese.Fees,
      MaxAlloc.*
   from
      ( SELECT 
              UserID,
              CaseNo, 
              SUM(Fees) AS [Fees]
           FROM 
              tblTimesheetEntries
           WHERE 
                  UserID = 'KLW'
              AND [Date] <= '2013-12-31'
           GROUP BY 
              UserID, 
              CaseNo ) AllThese
      LEFT JOIN
      ( SELECT * 
           FROM tblAllocations
           WHERE AllocID IN
             ( SELECT MAX(AllocID)
                  FROM tblAllocations
                  WHERE FeeEarner = 'KLW'
                    AND [Date] <= '2013-12-31'
                  GROUP BY FeeEarner, CaseNo )) MaxAlloc
         on AllThese.CaseNo = MaxAlloc.CaseNo
于 2013-09-29T11:01:53.403 回答