2014-07-04 53 views
0

是否有查詢可檢索每個StoreName的TotalMoney,TotalPaidMoney,TotalReturnMoney。SQL PHP檢索特定商店的總金額

像:

StoreName| TotalPaid  |  ToatalReturn  | TotalMoney  
Pizza | sum of PaidMoney | sum of ReturnMoney | sum of TotalMoney 
     |     

從這個表

Date | StoreName|PaidMoney| ReturnMoney| TotalMoney 
********* 
2014 | Pizaa | 33$  | 12$  | 120$ 
2014 Main  12$  23$   123$ 
2014 | Pizaa | 33$  | 12$  | 120$ 
2014 | Main  | 33$  | 12$  | 120$ 

表,我有: 存儲表

+1

是美元角色的數據庫字段的內容實際上一部分?如果是這樣,你想要什麼$ 55 +¢24? –

+0

你好,其實我不在乎$字符 – user2600543

+0

好吧,所以這些字段是包含字符的文本字段和$需要被忽略,因爲不打破任何計算...? –

回答

0

您可以輕鬆地使用GROUP BY語句,就像這樣:

SELECT StoreName, SUM(PaidMoney) as TotalPaid, SUM(ReturnMoney) as TotalReturn, SUM(TotalMoney) as TotalMoney FROM store_table GROUP BY StoreName 
+0

非常感謝你的工作:D,1其他事情我可以排序這個Monthly..like我有一個完整的日期( 2014-7-4)我可以按月訂購,所以結果將會是2014-7-4會有Pizaa和總金額等。 – user2600543

+0

是的,您可以:「SELECT DATE('Date'),StoreName ,SUM(PaidMoney)作爲TotalPaid,SUM(ReturnMoney)作爲TotalReturn,SUM(TotalMoney)作爲TotalMoney FROM store_table GROUP BY DATE('Date'),StoreName「 –

+0

謝謝Thomas.I Ment根據商店名稱和當前月份對數據進行排序 – user2600543

0

可以使用下面的命令:

SELECT SUM(PaidMoney) AS TotalPaid, SUM(ReturnMoney) AS TotalReturn, SUM(TotalMoney) AS TotalMoney FROM Table WHERE Store='Pizaa'; 

或者,如果你想所有的商店和專賣店重組:

SELECT SUM(PaidMoney) AS TotalPaid, SUM(ReturnMoney) AS TotalReturn, SUM(TotalMoney) AS TotalMoney FROM Table GROUP BY Store; 
+0

非常感謝你的工作:D,1件其他事情我可以排序這個Monthly..like我有一個完整的日期(2014年7月4日)我可以按月訂購,所以結果將會是2014-7-4會與總錢等Pizaa ... – user2600543

0

如果你真的在你的表該貨幣的跡象,這可能是多一點複雜。我現在假設,你總是有$符號作爲最後登錄您的專欄,並且沒有十進制值:

SELECT 
StoreName, 
SUM(PAID_MONEY_VALUE) AS SUM_PAID_MONEY_VALUE, 
SUM(RETURN_MONEY_VALUE) AS SUM_RETURN_MONEY_VALUE, 
SUM(TOTAL_MONEY_VALUE) AS SUM_TOTAL_MONEY_VALUE 
FROM 
(
    SELECT 
    StoreName, 
    CONVERT(int, SUBSTRING(PaidMoney, 1, LEN(PaidMoney - 1))) AS PAID_MONEY_VALUE, 
    CONVERT(int, SUBSTRING(ReturnMoney, 1, LEN(ReturnMoney - 1))) AS RETURN_MONEY_VALUE, 
    CONVERT(int, SUBSTRING(TotalMoney, 1, LEN(TotalMoney - 1))) AS TOTAL_MONEY_VALUE 
    FROM <YOUR_TABLE> 
) AS SUBQUERY 
GROUP BY StoreName 
+0

非常感謝你的工作:D,1其他事情我可以排序這個Monthly..like我哈哈我有一個完整的日期(2014年7月4日),我可以按月訂購,所以結果將會是2014-7-4將有Pizaa與總金額等.... – user2600543

+0

是的,你可以替換* SELECT StoreName * * SELECT StoreName,DATEPART(m,)*並將* GROUP BY StoreName *替換爲* GROUP BY StoreName,DATEPART(m,)* 最後,您可以添加* ORDER BY * DATEPART(m, )* – schlonzo