0

我有一个包含 6 个基本表的数据库,我在正确显示总和以进行简单总结时遇到问题。这是我的精简表设置:

table: customer_info
---------------------------------------------------------
custID   |   store      |   custName
1        |   bayport    |   renee
2        |   bayport    |   april
3        |   plainfield |   john
4        |   bayport    |   tree


table: deals
---------------------------------------------------------
dealID |  custID   |  dealDate   |  empName
1      |  1        | 2013-04-01  |  shak 
2      |  2        | 2013-04-01  |  shak 
3      |  3        | 2013-04-04  |  jen
4      |  4        | 2013-04-05  |  shak

table: phones
---------------------------------------------------------
phoneID  | dealID   | instore  | online
1        | 1        | 1        |  0       
2        | 1        | 0        |  1
3        | 2        | 1        |  0
4        | 3        | 0        |  1
5        | 3        | 1        |  0
6        | 3        | 1        |  0
7        | 4        | 0        |  1    

table: accessory
---------------------------------------------------------
accID   |  dealID   |  acName   | price
1       |  1        |  lanyard  | 10
2       |  2        |  pen      | 5
3       |  4        |  acc      | 2
3       |  4        |  blip     | 15

table: others
-----------------------------------------------------------
otherID  |  dealID  | otName   | otPrice
1        |  2       | other    | 250
2        |  2       | other2   | 100

table:payments
-----------------------------------------------------------
paymentID |  dealID  | cash   | credit   
1         |  1       | 10     | 0
2         |  2       | 0      | 355
3         |  3       | 0      | 0
4         |  4       | 17     | 0    

这是我需要的摘要:

Date       | empName | instore | online | credit | cash | total | accTotal | otherTotal | countOthers 
2013-04-01 | shak    | 2       | 1      | 355    | 10   | 360   | 15       | 350        |  2
2013-04-04 | jen     | 2       | 1      | 0      | 0    | 0     | 0        | 0          |  0
2013-04-05 | shak    | 0       | 0      | 0      | 17   | 17    | 17       | 0          |  0      

WHERE instore、online、credit、cash、total、accTotal、otherTotal 是它们各自列的总金额,而 countOthers 是完成了多少“其他”的计数

这是我到目前为止尝试过的,但数量就像在整个地方成倍增加

select dealDate,empName, 
Sum(phone.instore) AS 'In-store',
Sum(phone.online) As 'Online',
sum(credit) AS 'Credit', 
sum(cash) AS 'Cash', 
sum(cash+credit) AS 'Total',

sum(price) AS 'accTotal'
sum(otprice) AS 'otherTotal'
COUNT(otName) AS 'OthersCount'

FROM customer_info
JOIN deals
LEFT join phones
ON deals.dealID = phones.dealID
LEFT JOIN payments
ON deals.dealID = payments.dealID
LEFT JOIN accessory
ON deals.dealID = accessory.dealID
LEFT JOIN others
ON deals.dealID = others.dealID

WHERE customer_info.custID = deals.custID

GROUP BY deals.dealDate
ORDER BY dealDate DESC

任何帮助,将不胜感激。结果可以按 dealDate 或 dealID 分组,以较容易者为准。非常感谢你。

4

1 回答 1

0

我得到了它!我只是对那些使我的总数倍增的部分进行了子查询。

于 2013-04-08T00:58:07.683 回答