SQL 条件求和sum()

山鬼谣007 2015-09-01 03:16:04

SELECT
sinv_repmst.locsum as locsum,--------
sum(sinv_repmst.locsum) as sum,
ordermst.compno
FROM sinv_repmst,ordermst
WHERE sinv_repmst.compno='K1038-10' and ordermst.compno='K1038-10'
---数据太多,暂时我就看新增的这一条,可以忽略这条件,

GROUP BY sinv_repmst.locsum, sinv_repmst.compno,ordermst.compno


想要的结果是根据compno ,相同则求和 想显示出locsum为 41707.7
...全文
732 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
看到大神回复我随后修改执行
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
引用 10 楼 czfxwpy 的回复:
看着有点乱,基础语法没错的话,有需要就加上isnull(列名,0),再去做sum 可以防止~~~~~~~
============= 没看懂,我菜鸟, 我加了 where 1=1 做的测试
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
引用 7 楼 Tiger_Zhao 的回复:
请贴出原始数据(文本)、期待结果
-----------------------------------sinv_repmst 显示名定义: invoiceno other_no correl_no orderno invstyle invstatus locsum taxsum deptno empno carddt cardpsn checkpsn iscancell compno ischeck isprint isvcr ispost postsum cursum payway payno havelist vcrno remarks concflag def_int1 def_int2 def_int3 def_int4 def_date1 def_date2 def_num1 def_num2 def_num3 def_num4 def_str1 def_str2 def_str3 def_str4 fc_locsum fc_taxsum fc_postsum fc_cursum curstyle exgrate vcrtype vcrdt postno senddt backdt carriage othersum fc_carriage fc_othersum shiptype trans_comp isstaybill taxno bank accountno tax tranflg postsum2 fc_postsum2 billto postmode fintype tr_proj balflg srctype comptemp_id cfintype cvcrno cvcrtype cvcrdt saletype csaletype ocode sinvno bookno contractno fillpsn toads repflg qinsideno 以下为示例数据(Top20): RE1508310001 00000000000000000000 1 14321.6 2080.92 1000.09 928 2015-08-31 00:00:00 9999 0 K1038-10 1 0 0 0 0 0 0 14321.6 2080.92 0 CNY 1 0 0 0 K1038 F48392 0 03 03 1000 9999 RE1508310002 00000000000000000000 1 27386.1 3979.17 1000.09 928 2015-08-31 00:00:00 9999 0 K1038-10 1 0 0 0 0 0 明天,接电后发灰 0 27386.1 3979.17 0 CNY 1 0 0 0 K1038 F47392 0 03 03 1000 9999 ----------------------------------------------ordermst 显示名定义: sysno pinsideno precardno contractno carddt checkdt compno warehouse curstyle locsum empno deptno cardpsn checkpsn isclose ischeck isoos iscancell isprint isrec istransout remarks concflag def_num1 def_num2 def_num3 def_num4 def_str1 def_str2 def_str3 def_str4 def_date1 def_date2 def_int1 def_int2 def_int3 def_int4 fc_locsum types clienttype senddt amount carriage effectdt goodsplace paytype modu disc disctype shiptype fc_amount fc_carriage ordertype exgrate compnetno paytype1 postsum fc_postsum ispost orderstyle isexec goodsto billto address isedit prepaysum recsum discsum sinvsum sendsum fc_prepaysum fc_recsum fc_discsum fc_sinvsum fc_sendsum isprepay ispay issend issinv tr_proj srctype pricerank varydt varyman source comptemp_id plan_dt countday isexport ordmode saletype csaletype ocode dacode quotepur traceman con_code con_name stylecode conprop notes_shipping notes_packaging attention tel email faxno address1 bill_attention bill_tel bill_email bill_faxno bill_address1 goodsto_attention goodsto_tel goodsto_email goodsto_faxno goodsto_address1 speed csr taxrate sendpsn isprojectcontract postcode bill_postcode goodsto_postcode 以下为示例数据(Top20): P1508300011 2015-08-30 10:34:38 2015-08-30 11:31:12 K1038-10 03 CNY 260 906 1000.09 201 201 0 1 系统生成 260 2015-08-30 11:31:12 260 0 1 1 0 260 0 1 K1038-10 K1038 F47612 0 2015-08-30 00:00:00 0 ONSELL 03 03 P1508310029 2015-08-31 17:40:05 2015-09-01 06:37:29 K1038-10 03 CNY 7280 904 1000.09 202 202 0 1 压车,早 系统生成 7280 2015-09-01 06:37:29 7280 0 1 1 0 7280 0 1 K1038-10 K1038 F47732 0 2015-08-31 00:00:00 0 ONSELL 03 03 P1508170068 2015-07-01 00:00:00 2015-08-17 17:24:08 K1038-10 03 CNY 292264.2 801 1000.08 801 801 0 1 系统生成 292264.2 2015-08-17 17:24:08 292264.2 0 1 1 0 292264.2 0 1 K1038-10 K1038 0 2015-07-01 00:00:00 0 ONSELL 03 03 P1508270006 2015-08-27 07:24:51 2015-08-27 07:33:10 K1038-10 03 CNY 7280 928 1000.09 201 201 0 1 系统生成 7280 2015-08-27 07:33:10 7280 0 1 1 0 7280 0 1 K1038-10 K1038 F47463 0 2015-08-27 00:00:00 0 ONSELL 03 03 P1508150049 2015-08-15 17:31:32 2015-08-15 17:33:03 K1038-10 03 CNY 6860 928 1000.09 202 202 0 1 压车,早 系统生成 6860 2015-08-15 17:33:03 6860 0 1 1 0 6860 0 1 K1038-10 K1038 F47463 0 2015-08-15 00:00:00 0 ONSELL 03 03 P1508090019 2015-08-09 16:31:51 2015-08-09 16:42:05 K1038-10 03 CNY 7000 912 1000.09 202 202 0 1 压车,早 系统生成 7000 2015-08-09 16:42:05 7000 0 1 1 0 7000 0 1 K1038-10 K1038 F47231 0 2015-08-09 00:00:00 0 ONSELL 03 03 P1508050040 2015-08-05 18:47:17 2015-08-06 10:36:52 K1038-10 03 CNY 7280 912 1000.09 201 201 0 1 9点左右.,不下雨发 系统生成 7280 2015-08-06 10:36:52 7280 0 1 1 0 7280 0 1 K1038-10 K1038 F47231 0 2015-08-05 00:00:00 0 ONSELL 03 03 P1508070020 2015-08-07 16:32:21 2015-08-08 09:49:10 K1038-10 03 CNY 6860 914 1000.09 201 201 0 1 明天,接电后发灰 系统生成 6860 2015-08-08 09:49:10 6860 0 1 1 0 6860 0 1 K1038-10 K1038 F47392 0 2015-08-07 00:00:00 0 ONSELL 03 03 P1507310010 2015-07-31 11:15:32 2015-07-31 11:16:23 K1038-10 03 CNY 6860 920 1000.09 201 201 0 1 系统生成 6860 2015-07-31 11:16:23 6860 0 1 1 0 6860 0 1 K1038-10 K1038 F48307 0 2015-07-31 00:00:00 0 ONSELL 03 03 P1507270017 2015-07-27 17:57:49 2015-07-27 18:00:48 K1038-10 03 CNY 6860 909 1000.09 201 201 0 1 压车 208# 13792556406 系统生成 6860 2015-07-27 18:00:48 6860 0 1 1 0 6860 0 1 K1038-10 K1038 F47463 0 2015-07-27 00:00:00 0 ONSELL 03 03 P1507280021 2015-07-28 16:33:39 2015-07-28 16:34:37 K1038-10 03 CNY 5880 906 1000.09 202 202 0 1 系统生成 5880 2015-07-28 16:34:37 5880 0 1 1 0 5880 0 1 K1038-10 K1038 F47612 0 2015-07-28 00:00:00 0 ONSELL 03 03 P1507290013 2015-07-29 09:03:42 2015-07-29 13:33:53 K1038-10 03 CNY 6860 903 1000.09 201 201 0 1 系统生成 6860 2015-07-29 13:33:53 6860 0 1 1 0 6860 0 1 K1038-10 K1038 F48392 0 2015-07-29 00:00:00 0 ONSELL 03 03 P1507240032 2015-07-24 12:32:00 2015-07-25 12:15:21 K1038-10 CNY 50750000 1000.09 806 9999 0 1 0 0 0 50750000 3 1 2015-07-24 12:32:00 50750000 0 中国新兴建设开发总公司 1 1 1 50750000 0 1 0 0 0 K1038-10 K1038 0 0 0 0 0 0 0 1 0 2015-12-31 00:00:00 0 03 03 P1508020014 2015-08-02 17:43:24 2015-08-02 19:50:27 K1038-10 03 CNY 6860 925 1000.09 201 201 0 1 系统生成 6860 2015-08-02 19:50:27 6860 0 1 1 0 6860 0 1 K1038-10 K1038 F48317 0 2015-08-02 00:00:00 0 ONSELL 03 03 P1507260004 2015-07-26 08:05:39 2015-07-26 08:34:25 K1038-10 03 CNY 7840 914 1000.09 202 202 0 1 系统生成 7840 2015-07-26 08:34:25 7840 0 1 1 0 7840 0 1 K1038-10 K1038 F47392 0 2015-07-26 00:00:00 0 ONSELL 03 03 P1508190031 2015-08-19 10:14:48 2015-08-19 10:55:05 K1038-10 03 CNY 8320 911 1000.09 201 201 0 1 系统生成 8320 2015-08-19 10:55:05 8320 0 1 1 0 8320 0 1 K1038-10 K1038 F47456 0 2015-08-19 00:00:00 0 ONSELL 03 03 -------------------------------------------------------SQL语句--------------------------------------- SELECT compno,SUM(DZJE)AS 对账金额 FROM (SELECT sinv_repmst.locsum as DZJE,-------- ordermst.compno FROM sinv_repmst,ordermst )AQA where compno='K1038-10' GROUP BY compno ------------------------------------结果-- 显示名定义: compno 对账金额 以下为示例数据(Top20): K1038-10 105973759.04
czfxwpy 2015-09-02
  • 打赏
  • 举报
回复
看着有点乱,基础语法没错的话,有需要就加上isnull(列名,0),再去做sum 可以防止~~~~~~~
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
引用 6 楼 Landa_Alice 的回复:
[quote=引用 4 楼 xd739853965 的回复:] [quote=引用 1 楼 Landa_Alice 的回复:] SELECT compno,SUM(locsum)AS locsum SUM(suma) AS suma FROM ( SELECT sinv_repmst.locsum as locsum,-------- sum(sinv_repmst.locsum) as suma, ordermst.compno FROM sinv_repmst,ordermst WHERE sinv_repmst.compno=ordermst.compno GROUP BY sinv_repmst.locsum,ordermst.compno )a GROUP BY compno
================================= 看着很期待的样子,可能我数据库版本低? ============================== 'SUM' 附近有语法错误。 'a' 附近有语法错误。[/quote] 好像是我少写一个逗号,是不是,我是直接回复的,没有在数据库中写,所以可能写漏了[/quote] —————————————————————— 是,少了个逗号, 少了个where
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
引用 7 楼 Tiger_Zhao 的回复:
请贴出原始数据(文本)、期待结果
———————— 等等吧,我改完一查询,程序卡死了
Tiger_Zhao 2015-09-02
  • 打赏
  • 举报
回复
请贴出原始数据(文本)、期待结果
Pact_Alice 2015-09-02
  • 打赏
  • 举报
回复
引用 4 楼 xd739853965 的回复:
[quote=引用 1 楼 Landa_Alice 的回复:] SELECT compno,SUM(locsum)AS locsum SUM(suma) AS suma FROM ( SELECT sinv_repmst.locsum as locsum,-------- sum(sinv_repmst.locsum) as suma, ordermst.compno FROM sinv_repmst,ordermst WHERE sinv_repmst.compno=ordermst.compno GROUP BY sinv_repmst.locsum,ordermst.compno )a GROUP BY compno
================================= 看着很期待的样子,可能我数据库版本低? ============================== 'SUM' 附近有语法错误。 'a' 附近有语法错误。[/quote] 好像是我少写一个逗号,是不是,我是直接回复的,没有在数据库中写,所以可能写漏了
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
引用 4 楼 xd739853965 的回复:
[quote=引用 1 楼 Landa_Alice 的回复:] SELECT compno,SUM(locsum)AS locsum SUM(suma) AS suma FROM ( SELECT sinv_repmst.locsum as locsum,-------- sum(sinv_repmst.locsum) as suma, ordermst.compno FROM sinv_repmst,ordermst WHERE sinv_repmst.compno=ordermst.compno GROUP BY sinv_repmst.locsum,ordermst.compno )a GROUP BY compno
================================= 看着很期待的样子,可能我数据库版本低? ============================== 'SUM' 附近有语法错误。 'a' 附近有语法错误。[/quote]报错找到了
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
引用 1 楼 Landa_Alice 的回复:
SELECT compno,SUM(locsum)AS locsum SUM(suma) AS suma FROM ( SELECT sinv_repmst.locsum as locsum,-------- sum(sinv_repmst.locsum) as suma, ordermst.compno FROM sinv_repmst,ordermst WHERE sinv_repmst.compno=ordermst.compno GROUP BY sinv_repmst.locsum,ordermst.compno )a GROUP BY compno
================================= 看着很期待的样子,可能我数据库版本低? ============================== 'SUM' 附近有语法错误。 'a' 附近有语法错误。
山鬼谣007 2015-09-02
  • 打赏
  • 举报
回复
引用 2 楼 Tiger_Zhao 的回复:
请贴出原始数据(文本)、期待结果。


----------图中左上角红色表名-----------


Tiger_Zhao 2015-09-01
  • 打赏
  • 举报
回复
请贴出原始数据(文本)、期待结果。
Pact_Alice 2015-09-01
  • 打赏
  • 举报
回复
SELECT compno,SUM(locsum)AS locsum SUM(suma) AS suma FROM ( SELECT sinv_repmst.locsum as locsum,-------- sum(sinv_repmst.locsum) as suma, ordermst.compno FROM sinv_repmst,ordermst WHERE sinv_repmst.compno=ordermst.compno GROUP BY sinv_repmst.locsum,ordermst.compno )a GROUP BY compno

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧