SQL 分组依据的分组依据?
假设我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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).