当前数据库:
tbl_Order:
ID.........UserName........date
1..........john............march 30 2017
2..........mike............march 31 2017
tbl_Products_In_Order
Order_ID.........Qty_Purchased........UnitPrice........TotalPrice
1...............2.....................1.00.............2.00
1...............3.....................2.00.............6.00
2...............1.....................8.00.............8.00
我需要输出:
Order_ID...UserName...date............Number_Of_Products.........Total_Qty_All
1..........John.......march 30 2017...2..........................5
2..........mike.......march 31 2017...1..........................1
有人可以帮我创建SQL查询吗?
答案 0 :(得分:0)
这是一个PostgreSQL解决方案(但只使用基本的东西,所以在大多数SQL方言中应该相同或非常相似):
SELECT
Order_ID,
UserName,
date,
count(Order_ID) AS Number_Of_Products,
sum(Qty_Purchased) AS Total_Qty_All
FROM
tbl_Products_In_Order AS pio
INNER JOIN tbl_Order AS o ON pio.Order_ID = o.ID
GROUP BY Order_ID, UserName, date
ORDER BY Order_ID