2

報酬データベースにクエリを実行して、スタッフのメンバーが今週と合計で何ポイントを割り当てたかを調べようとしています。

教師が割り当てた合計ポイントを計算するために使用しているクエリは次のとおりです。

SELECT  `Giver_ID` , SUM(  `Points` ) AS TotalPoints
FROM  `transactions` 
GROUP BY  `Giver_ID` 
ORDER BY  `Giver_ID` ASC 

毎週の割り当てを計算するために使用しているクエリは非常に似ています。

SELECT  `Giver_ID` , SUM(  `Points` ) AS WeeklyPoints
FROM  `transactions`
WHERE ( `Datetime` >= '2012-09-24' AND `Datetime` <= '2012-09-30' )
GROUP BY  `Giver_ID` 
ORDER BY  `Giver_ID` ASC 

私の質問はこれです:クエリを組み合わせて、単一のクエリからを生成することは可能Giver_IDですかTotalPointsWeeklyPoints

前もって感謝します、

4

3 に答える 3

2

これを試して:

SELECT  a.`Giver_ID` , 
            MAX(b.`TotalPoints`) as `TotalPoints`, 
            MAX(c.`WeeklyPoints`) as `WeeklyPoints`
    FROM  `transactions` as a 
        LEFT JOIN (SELECT `Giver_ID`, SUM(`Points`) AS TotalPoints FROM `transactions` GROUP BY `Giver_ID`) as b ON a.`Giver_ID`=b.`Giver_ID`
        LEFT JOIN (SELECT `Giver_ID`, SUM(`Points`) AS WeeklyPoints FROM `transactions` WHERE ( `Datetime` >= '2012-09-24 00:00:00' AND `Datetime` <= '2012-09-30' ) GROUP BY `Giver_ID`) as c ON a.`Giver_ID`=c.`Giver_ID`
    GROUP BY  a.`Giver_ID` 
    ORDER BY  a.`Giver_ID` ASC 
于 2012-09-26T11:14:45.317 に答える
2

はい、可能です-

SELECT
  Giver_ID,
  SUM(Points) AS TotalPoints,
  SUM(IF(Datetime >= '2012-09-24' AND Datetime <= '2012-09-30', Points, NULL)) AS WeeklyPoints
FROM transactions
GROUP BY Giver_ID
于 2012-09-26T11:19:50.557 に答える
0
SELECT  `Giver_ID` , SUM( `Points`) AS WeeklyPoints,(SELECT SUM(`Points`)
FROM  `transactions` where `Giver_ID`=t.`Giver_ID` GROUP BY  `Giver_ID`)  AS TotalPoints
FROM  `transactions` t
WHERE ( `Datetime` >= '2012-09-24' AND `Datetime` <= '2012-09-30' )
GROUP BY  `Giver_ID` 
ORDER BY  `Giver_ID` ASC 
于 2012-09-26T10:59:59.383 に答える