大佬教程收集整理的这篇文章主要介绍了GROUP sum BY 通过加入两个表,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试编写一个 sql 查询,该查询将根据产品类别正确地将销售项目 sell_qyt 和小额总价组合在一起,以便我可以在可打印的发票上显示该产品来自 jelly Sheet = 4 的比率此类别产品的 62 小计为 248(4 * 62 = 248)。但是当我尝试运行下面提到的查询时,它显示输出为 12,但我希望根据类别将小计和 sell_qyt 分开。
我尝试运行不同的查询,只有一个查询给出了下面提到的输出,这只是所有已售出的总和。数据库示例也如下所示
DB 示例:(为了更好地理解) 表#1:
category
ID | code | name
1 | 1 | jelly sheet
2 | 2 | 9D Glass
3 | 3 | Polished Glass
@H_419_8@
表#2:
Product:
ID | code | name | cost | category_ID | price
1 | 1 | IP11Js | 50 | 1 | 62
2 | 2 | IP12Js | 50 | 1 | 62
3 | 3 | IP119D | 40 | 2 | 55
4 | 4 | IP129D | 40 | 2 | 55
5 | 5 | IP11PG | 18 | 3 | 25
6 | 6 | IP12PG | 18 | 3 | 25
@H_419_8@
表#3:
SALE_items:
ID | SALE_ID | product_ID | product_code | product_name | unit_price | sold_qyt | sub@R_307_10586@l |
1 | 1 | 1 | 1 | IP11Js | 62 | 2 | 124 |
2 | 1 | 2 | 2 | IP12Js | 62 | 2 | 124 |
3 | 1 | 3 | 3 | IP119D | 55 | 2 | 110 |
4 | 1 | 4 | 4 | IP129D | 55 | 2 | 110 |
5 | 1 | 5 | 5 | IP11PG | 25 | 2 | 50 |
6 | 1 | 6 | 6 | IP12PG | 25 | 2 | 50 |
7 | 2 | 7 | 1 | IP11Js | 62 | 2 | 124 |
8 | 2 | 8 | 2 | IP12Js | 62 | 2 | 124 |
9 | 2 | 9 | 3 | IP119D | 55 | 2 | 110 |
10 | 2 | 10 | 4 | IP129D | 55 | 2 | 110 |
11 | 2 | 11 | 5 | IP11PG | 25 | 2 | 50 |
12 | 2 | 12 | 6 | IP12PG | 25 | 2 | 50 |
@H_419_8@
我运行的SQL查询:
SELECT SALE_ID,SUM(sold_qyt) AS sold_qyt
FROM SALE_items
GROUP BY SALE_ID
@H_419_8@
请帮助我解决这个困难,谢谢
更新:1-21-2021 我执行新查询
SELECT (sma_SALE_items.SALE_ID,sma_categorIEs.code AS sma_products.category_ID,sma_products.code AS sma_SALE_items.product_code,)
SUM(sold_qyt) AS sold_qyt
SUM(sub@R_307_10586@l) AS sub@R_307_10586@l
FROM sma_SALE_items
left JOIN sma_products ON sma_products.ID=sma_SALE_items.product_ID
left JOIN sma_categorIEs ON sma_categorIEs.code=sma_products.category_ID
GROUP BY sma_SALE_items.SALE_ID
ORDER BY sma_categorIEs
@H_419_8@
但没有运气:(
我想要这样的输出:
预期输出:
ID | SALE_ID | category_name | sold_qyt | sub@R_307_10586@l |
1 | 1 | jelly Sheet | 4 | 248 |
2 | 1 | 9D Glass | 4 | 220 |
3 | 1 | Polished Glass | 4 | 100 |
4 | 2 | jelly Sheet | 4 | 248 |
5 | 2 | 9D Glass | 4 | 220 |
6 | 2 | Polished Glass | 4 | 100 |
@H_419_8@
预期结果集中的 ID 列非常具有误导性 - 它似乎只是输出结果集中的新 ID 值,而不是源表中的任何 ID 值。
如果它对您很重要,那么您可以使用此查询:
SELECT ROW_numbER() OVER (ORDER BY SALE_id,category_id),SALE_id,category_name,sold_qty,sub@R_307_10586@l
FROM (
SELECT c.ID as category_id,si.SALE_id,c.[name] as category_name,SUM(si.sold_qty) as sold_qty,SUM(si.sub@R_307_10586@l) as sub@R_307_10586@l
FROM SALE_items si
JOIN product p ON p.ID = si.product_code
JOIN category c ON c.ID = p.category_id
GROUP BY c.ID,c.[name]
) r
如果它不相关并且您只想要 SALE_id
、category_name
和总数,则将其简化为:
SELECT si.SALE_id,SUM(si.sub@R_307_10586@l) as sub@R_307_10586@l
FROM SALE_items si
JOIN product p ON p.ID = si.product_code
JOIN category c ON c.ID = p.category_id
GROUP BY si.SALE_id,c.[name]
ORDER BY SALE_id,category_name
以上是大佬教程为你收集整理的GROUP sum BY 通过加入两个表全部内容,希望文章能够帮你解决GROUP sum BY 通过加入两个表所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。