2011-11-04 130 views
2

結合兩組有什麼辦法這兩個查詢合併成一個單一的查詢需要通過查詢

SELECT siftBal.SystemName,convert(varchar(10),siftBal.Date,110) as WorkDate,SUM(siftBal.CashIn) as CashIn,SUM(siftBal.CashOut) as CashOut, 
(SUM(siftBal.CashIn) - SUM(siftBal.CashOut)) as NetCash 
FROM tblShiftBalanceDetail siftBal 
WHERE siftBal.Date between '10-28-2011' and '11-04-2011' group by convert(varchar(10),siftBal.Date,110),SystemName 

SELECT convert(varchar(10),custPlay.DatePlayed,110) as WorkDate,SUM(custPlay.TotalPoints) as EntriesIn,SUM(custPlay.TotalWin) as Payout, 
(SUM(custPlay.TotalPoints)*0.01) - SUM(custPlay.TotalWin) as NetProfit, 
(SUM(custPlay.TotalWin)*100/SUM(custPlay.TotalPoints))*100 as Payout 
FROM [tblCustomerPlay] custPlay 
WHERE custPlay.DatePlayed between '10-28-2011' and '11-04-2011' group by convert(varchar(10),custPlay.DatePlayed,110) 
+0

列是不同的,所以UNION不會工作。這些數據集與我看起來完全不同;你能詳細說明你的「結合」他們的意思嗎?顯示兩個表格中的數據是什麼樣子以及期望的結果是什麼樣子會有所幫助,那麼有人可以幫助你到達那裏。 –

回答

0

如果你想在兩個結果集在一起:

[query 1] 
UNION ALL 
[query 2] 
0

假設他們有同一列的數目和類型,你可以使用UNION:

select foo, bar from table1 
union 
select foo, bar from tabe2 
0
SELECT 
    entries.EntriesIn 
    , entries.NetProfit 
    , entries.Payout 
    , entries.Perchentage 
    , siftBal.SystemName 
    , siftBal.WorkDate 
    , siftBal.TotalCashIn 
    , siftBal.TotalCashout 
    , NetCash 
FROM 
    (SELECT 
     shift.SystemName 
     , CONVERT(VARCHAR(10), shift.Date, 110) AS WorkDate 
     , SUM(shift.CashIn) AS TotalCashIn 
     , SUM(shift.CashOut) AS TotalCashout 
     , (SUM(shift.CashIn) - SUM(shift.CashOut)) AS NetCash 
     FROM 
     tblShiftBalanceDetail AS shift 
     WHERE 
     shift.Date BETWEEN '10-28-2011' AND '11-03-2011' 
     GROUP BY 
     CONVERT(VARCHAR(10), shift.Date, 110) 
     , shift.SystemName) AS siftBal 
    JOIN (SELECT 
      CONVERT(VARCHAR(10), custPlay.DatePlayed, 110) AS WorkDate 
      , SUM(custPlay.TotalPoints) AS EntriesIn 
      , SUM(custPlay.TotalWin) AS Payout 
      , (SUM(custPlay.TotalPoints) * 0.01) - SUM(custPlay.TotalWin) AS NetProfit 
      , (SUM(custPlay.TotalWin) * 100/SUM(custPlay.TotalPoints)) * 100 AS Perchentage 
      FROM 
      [tblCustomerPlay] custPlay 
      WHERE 
      custPlay.DatePlayed BETWEEN '10-28-2011' 
           AND  '11-04-2011' 
      GROUP BY 
      CONVERT(VARCHAR(10), custPlay.DatePlayed, 110)) entries ON entries.WorkDate = siftBal.WorkDate 
0

在第一個組查詢中,我添加了空白列'Payout',在第二個查詢中我添加了一個空白列'SystemName'。

SELECT siftBal.SystemName , 
      CONVERT(VARCHAR(10), siftBal.Date, 110) AS WorkDate , 
      SUM(siftBal.CashIn) AS CashIn , 
      SUM(siftBal.CashOut) AS CashOut , 
      (SUM(siftBal.CashIn) - SUM(siftBal.CashOut)) AS NetCash, 
      '' AS 'Payout' 
    FROM tblShiftBalanceDetail siftBal 
    WHERE siftBal.Date BETWEEN '10-28-2011' 
         AND  '11-04-2011' 
    GROUP BY CONVERT(VARCHAR(10), siftBal.Date, 110) , 
      SystemName 
    UNION ALL 
    SELECT '' AS 'SystemName', 
      CONVERT(VARCHAR(10), custPlay.DatePlayed, 110) AS WorkDate , 
      SUM(custPlay.TotalPoints) AS EntriesIn , 
      SUM(custPlay.TotalWin) AS Payout , 
      (SUM(custPlay.TotalPoints) * 0.01) - SUM(custPlay.TotalWin) AS NetProfit , 
      (SUM(custPlay.TotalWin) * 100/SUM(custPlay.TotalPoints)) * 100 AS Payout 
    FROM [tblCustomerPlay] custPlay 
    WHERE custPlay.DatePlayed BETWEEN '10-28-2011' 
           AND  '11-04-2011' 
    GROUP BY CONVERT(VARCHAR(10), custPlay.DatePlayed, 110)