简体   繁体   English

SQL从不同的表中选择计数和总和

[英]SQL select count and sum from different tables

I need help to resolve this.我需要帮助来解决这个问题。 I have 4 tables :我有 4 张桌子:

Transactions交易

| id | cid | gt |   rt |

|  1 |  6  | 2  |  5   |
|  2 |  6  | 9  |  7.5 |
|  3 |  6  | 3  |  9.7 |
|  4 |  3  | 3  |  7.0 |
|  5 |  3  | 7  |  6.8 |
|  6 |  9  | 4  |  2.5 |
|  7 |  9  | 2  |  5.4 |

Clients客户

| id | firstname | lastname | date |

|  1 |  jean     | moulin   | 1987 | 
|  2 |  salah    | fera     | 1968
|  3 |  marouan  | youra    | 2001 |
|  4 |  amin     | esa      | 1963 |
|  5 |  kamal    | tara     | 1789 |
|  6 |  moad     | mara     | 2005 |
|  9 |  safaa    | dara     | 2004 |

Produit A产品A

| id | cid | 

|  1 |  6  | 
|  2 |  6  | 
|  3 |  3  | 
|  4 |  3  | 
|  5 |  3  |
|  6 |  4  | 
|  7 |  1  | 

Produit B产品B

| id | cid |

|  1 |  6  | 
|  2 |  3  | 
|  3 |  9  | 
|  4 |  3  | 
|  5 |  3  | 
|  6 |  4  | 
|  7 |  6  | 

The result that i need is :我需要的结果是:

cid |  name |  date  |  pa | pb | gt | rt | 
3   |       |        |     |    |    |    |
6   |       |        |     |    |    |    |
9   |       |        |     |    |    |    |

I need to select from transaction all distinct client id (pid) and select the firstname and last name (name = firstname lastname) and date from clients table and sum all values (gt) and (rt) and search in table produitA the number of products for this client by his id and the same thing for the table produitB.我需要从交易中选择所有不同的客户 ID (pid) 并从客户表中选择名字和姓氏 (name = firstname lastname) 和日期,并总结所有值 (gt) 和 (rt) 并在表中搜索 produitA 的数量这个客户的产品通过他的 id 和表 produitB 相同。

What i do for this but it don't work is (suggested by Gimeniux):我为此所做的但不起作用的是(由 Gimeniux 建议):

SELECT 
clients.id, 
CONCAT(firstname, ' ', lastname) as name,
date, 
count(distinct produitA.id) as pa, 
count(distinct produitB.id) as pb, 
sum(gt) AS gt, 
sum(rt) AS rt 

FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id  = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid

where pid is not null
group by clients.id

The probleme here is that gt and rt values are true for only the first client.这里的问题是 gt 和 rt 值仅适用于第一个客户端。 For the second client and third and ... there is different values that are not true.对于第二个客户端和第三个和......有不同的值是不正确的。

Although is hard for me to see the logic between your tables, you can use this query to get the result you desire.虽然我很难看到你的表之间的逻辑,但你可以使用这个查询来获得你想要的结果。 But i think it won't work if there are two same 'gt' or two same 'rt' values for one client.但是我认为如果一个客户端有两个相同的“gt”或两个相同的“rt”值,它就行不通了。

SELECT 
clients.id, 
CONCAT(firstname, ' ', lastname) as name,
date, 
count(distinct produitA.id) as pa, 
count(distinct produitB.id) as pb, 
sum(distinct gt) AS gt, 
sum(distinct rt) AS rt 

FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id  = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid

where pid is not null

group by clients.id

Row for pid=9 doesn't show because in the data you gave there is no client with id=9 pid=9 的行未显示,因为在您提供的数据中没有 id=9 的客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM