SQL 分组依据的分组依据?

发布于 2024-10-17 00:12:16 字数 1312 浏览 0 评论 0原文

假设我有 3 张表:顾客、武器和订单。我想要的是创建一个表格,对于每个客户,我需要显示他/她按类别(枪支、手枪等)购买的商品数量和武器总数?只用SQL查询就可以实现吗?因为我打算使用 SQL CE,所以我可以在没有子查询的情况下实现这个吗?

表具有以下结构: 客户(ID、姓名) 武器(id,武器类型) 订单(cust_id,weap_id,数量)

我想得到这个:

Name       | Order(w_type) |    Quantity  |   Total
-----------+---------------+--------------+-----------
John Smith |   Guns        |     3        |   7
John Smith |   Pistols     |     4        |   7
-----------+---------------+--------------+-----------
Jane Doe   |   Guns        |     5        |   14
Jane Doe   |   Pistols     |     7        |   14
Jane Doe   |   Cannons     |     2        |   14

或者(更好)

Name       |   Order       |    Quantity  |   Total
-----------+---------------+--------------+-----------
John Smith |   Guns        |     3        |   
John Smith |   Pistols     |     4        |   
-----------+---------------+--------------+-----------                                         
                                              7   
-----------+---------------+--------------+-----------
Jane Doe   |   Guns        |     5        |   
Jane Doe   |   Pistols     |     7        |   
Jane Doe   |   Cannons     |     2        |   
-----------+---------------+--------------+-----------
                                              14 

谢谢。

Let's say I have 3 tables: customers, weapons and orders. What I want is to create a table where for each customer I need to display quantity of goods he/she bought per category(gun, pistol, etc.) and total quantity of weapons? Is it implementable using just SQL query? Can I implement this without subquery because I intend to use SQL CE?

Tables has following structure:
customers(id, name)
weapons(id, weapon_type)
orders(cust_id, weap_id, quantity)

And I want to get this:

Name       | Order(w_type) |    Quantity  |   Total
-----------+---------------+--------------+-----------
John Smith |   Guns        |     3        |   7
John Smith |   Pistols     |     4        |   7
-----------+---------------+--------------+-----------
Jane Doe   |   Guns        |     5        |   14
Jane Doe   |   Pistols     |     7        |   14
Jane Doe   |   Cannons     |     2        |   14

or (even better)

Name       |   Order       |    Quantity  |   Total
-----------+---------------+--------------+-----------
John Smith |   Guns        |     3        |   
John Smith |   Pistols     |     4        |   
-----------+---------------+--------------+-----------                                         
                                              7   
-----------+---------------+--------------+-----------
Jane Doe   |   Guns        |     5        |   
Jane Doe   |   Pistols     |     7        |   
Jane Doe   |   Cannons     |     2        |   
-----------+---------------+--------------+-----------
                                              14 

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2024-10-24 00:12:16

CE 中没有可以给出第一个结果的分析函数(例如 SUM() OVER )。

如果您想要的(“甚至更好”)结果是第二个,甚至不是表格,您需要触发两个查询;一份用于表格,一份用于总和。或者,或者在您浏览表格结果时收集总和(按人排序)。

There is no analytical function (e.g. SUM() OVER ) in CE that can give you the first result.

If your intended ("even better") results is the 2nd, which isn't even tabular, you need to fire two queries; one for the table and one for the sum. Either that, or collect the sum as you are going through the table result (ordered by person).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文