周一至周五 : 08:30 - 17:30 客服专员电话/微信:18049989370 QQ:857188287
微信咨询

扫码微信咨询

关注公众号

关注微信公众号

电话: 021 5161 9370
返回顶部
分组滚动累加求和
显示全部楼层 倒序浏览 发表日期 2022-11-14 16:05:41 2447次阅读 1次回复
hessen
2022-11-14 16:05:41
本帖最后由 hessen 于 2022-11-18 17:17 编辑


分组滚动累加求和

按分组滚动累计求和。下买呢的例子复制后在SQL查询分析器中就可以得到结果。在快表中应用时,主要是填充那,按快表的填充规则填充就可以。如果没搞定可以联系快表客服!

  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE TABLE [dbo].[KBOrder](
  6.         [COMPANY] [varchar](50) NULL,
  7.         [PERSON] [varchar](50) NULL,
  8.         [JIE] [numeric](18, 2) NULL,
  9.         [DAI] [numeric](18, 2) NULL,
  10.         [YU] [numeric](18, 2) NULL
  11. ) ON [PRIMARY]
  12. GO
  13. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'A', N'张三', CAST(1.00 AS Numeric(18, 2)), CAST(10.00 AS Numeric(18, 2)), NULL)
  14. GO
  15. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'A', N'张三', CAST(2.00 AS Numeric(18, 2)), CAST(9.00 AS Numeric(18, 2)), NULL)
  16. GO
  17. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'A', N'张三', CAST(3.00 AS Numeric(18, 2)), CAST(8.00 AS Numeric(18, 2)), NULL)
  18. GO
  19. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'B', N'李四', CAST(4.00 AS Numeric(18, 2)), CAST(7.00 AS Numeric(18, 2)), NULL)
  20. GO
  21. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'B', N'李四', CAST(5.00 AS Numeric(18, 2)), CAST(6.00 AS Numeric(18, 2)), NULL)
  22. GO
  23. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'A', N'王五', CAST(6.00 AS Numeric(18, 2)), CAST(5.00 AS Numeric(18, 2)), NULL)
  24. GO
  25. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'A', N'王五', CAST(7.00 AS Numeric(18, 2)), CAST(4.00 AS Numeric(18, 2)), NULL)
  26. GO
  27. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'A', N'王五', CAST(8.00 AS Numeric(18, 2)), CAST(3.00 AS Numeric(18, 2)), NULL)
  28. GO
  29. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'B', N'赵六', CAST(9.00 AS Numeric(18, 2)), CAST(2.00 AS Numeric(18, 2)), NULL)
  30. GO
  31. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [JIE], [DAI], [YU]) VALUES (N'B', N'赵六', CAST(10.00 AS Numeric(18, 2)), CAST(1.00 AS Numeric(18, 2)), NULL)
  32. GO


  33. SELECT [COMPANY]
  34.       ,[PERSON]
  35.       ,[JIE]
  36.       ,[DAI]
  37.       ,[YU]
  38.           ,SUM([JIE] - [DAI]) OVER(PARTITION BY COMPANY,PERSON
  39.                                                                 ORDER BY COMPANY,PERSON
  40.                                                                 ROWS BETWEEN UNBOUNDED PRECEDING
  41.                                                                          AND CURRENT ROW) AS RunSum
  42.   FROM [dbo].[KBOrder]
复制代码




20221114160519.png














电话/微信:18049989370 QQ:857188287
hessen

2022-11-18 17:14:46

  1. DROP TABLE [dbo].[KBOrder]
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. CREATE TABLE [dbo].[KBOrder](
  7.         [COMPANY] [varchar](50) NULL,
  8.         [PERSON] [varchar](50) NULL,
  9.         [JIE] [numeric](18, 2) NULL,
  10.         [DAI] [numeric](18, 2) NULL,
  11.         [YU] [numeric](18, 2) NULL
  12. ) ON [PRIMARY]
  13. GO
  14. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'张三', CAST(1.00 AS Numeric(18, 2)), CAST(10.00 AS Numeric(18, 2)), NULL)
  15. GO
  16. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'张三', CAST(2.00 AS Numeric(18, 2)), CAST(9.00 AS Numeric(18, 2)), NULL)
  17. GO
  18. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'张三', CAST(3.00 AS Numeric(18, 2)), CAST(8.00 AS Numeric(18, 2)), NULL)
  19. GO
  20. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'李四', CAST(4.00 AS Numeric(18, 2)), CAST(7.00 AS Numeric(18, 2)), NULL)
  21. GO
  22. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'李四', CAST(5.00 AS Numeric(18, 2)), CAST(6.00 AS Numeric(18, 2)), NULL)
  23. GO
  24. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'王五', CAST(6.00 AS Numeric(18, 2)), CAST(5.00 AS Numeric(18, 2)), NULL)
  25. GO
  26. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'王五', CAST(7.00 AS Numeric(18, 2)), CAST(4.00 AS Numeric(18, 2)), NULL)
  27. GO
  28. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'王五', CAST(8.00 AS Numeric(18, 2)), CAST(3.00 AS Numeric(18, 2)), NULL)
  29. GO
  30. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'赵六', CAST(9.00 AS Numeric(18, 2)), CAST(2.00 AS Numeric(18, 2)), NULL)
  31. GO
  32. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'赵六', CAST(10.00 AS Numeric(18, 2)), CAST(1.00 AS Numeric(18, 2)), NULL)
  33. GO

  34. WITH C AS(
  35. SELECT [COMPANY]
  36.       ,[PERSON]
  37.       ,[JIE]
  38.       ,[DAI]
  39.       ,[YU]
  40.           ,SUM([JIE]) OVER(PARTITION BY COMPANY,PERSON
  41.                                                                 ORDER BY COMPANY,PERSON
  42.                                                                 ROWS BETWEEN UNBOUNDED PRECEDING
  43.                                                                 AND CURRENT ROW) RunSumJIE
  44.           ,SUM(DAI) OVER(PARTITION BY COMPANY,PERSON ORDER BY COMPANY,PERSON) SumDai

  45.           ,SUM([JIE]) OVER(PARTITION BY COMPANY,PERSON
  46.                                                                 ORDER BY COMPANY,PERSON
  47.                                                                 ROWS BETWEEN UNBOUNDED PRECEDING
  48.                                                                 AND CURRENT ROW)
  49.           - SUM(DAI) OVER(PARTITION BY COMPANY,PERSON ORDER BY COMPANY,PERSON) CHA

  50.   FROM [dbo].[KBOrder]
  51.   )
  52.   SELECT C.COMPANY,C.PERSON,C.JIE,C.DAI,C.YU,C.RunSumJIE,C.SumDai,C.CHA
  53.   ,CASE WHEN C.CHA <= 0 THEN 0 WHEN C.CHA > 0 AND C.CHA < C.JIE THEN C.CHA WHEN C.CHA > 0 AND C.CHA > C.JIE THEN C.JIE END
  54.   FROM C
复制代码


电话/微信:18049989370 QQ:857188287

精彩评论1

hessen

2022-11-18 17:14:46

  1. DROP TABLE [dbo].[KBOrder]
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. CREATE TABLE [dbo].[KBOrder](
  7.         [COMPANY] [varchar](50) NULL,
  8.         [PERSON] [varchar](50) NULL,
  9.         [JIE] [numeric](18, 2) NULL,
  10.         [DAI] [numeric](18, 2) NULL,
  11.         [YU] [numeric](18, 2) NULL
  12. ) ON [PRIMARY]
  13. GO
  14. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'张三', CAST(1.00 AS Numeric(18, 2)), CAST(10.00 AS Numeric(18, 2)), NULL)
  15. GO
  16. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'张三', CAST(2.00 AS Numeric(18, 2)), CAST(9.00 AS Numeric(18, 2)), NULL)
  17. GO
  18. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'张三', CAST(3.00 AS Numeric(18, 2)), CAST(8.00 AS Numeric(18, 2)), NULL)
  19. GO
  20. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'李四', CAST(4.00 AS Numeric(18, 2)), CAST(7.00 AS Numeric(18, 2)), NULL)
  21. GO
  22. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'李四', CAST(5.00 AS Numeric(18, 2)), CAST(6.00 AS Numeric(18, 2)), NULL)
  23. GO
  24. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'王五', CAST(6.00 AS Numeric(18, 2)), CAST(5.00 AS Numeric(18, 2)), NULL)
  25. GO
  26. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'王五', CAST(7.00 AS Numeric(18, 2)), CAST(4.00 AS Numeric(18, 2)), NULL)
  27. GO
  28. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'A', N'王五', CAST(8.00 AS Numeric(18, 2)), CAST(3.00 AS Numeric(18, 2)), NULL)
  29. GO
  30. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'赵六', CAST(9.00 AS Numeric(18, 2)), CAST(2.00 AS Numeric(18, 2)), NULL)
  31. GO
  32. INSERT [dbo].[KBOrder] ([COMPANY], [PERSON], [DAI], [JIE], [YU]) VALUES (N'B', N'赵六', CAST(10.00 AS Numeric(18, 2)), CAST(1.00 AS Numeric(18, 2)), NULL)
  33. GO

  34. WITH C AS(
  35. SELECT [COMPANY]
  36.       ,[PERSON]
  37.       ,[JIE]
  38.       ,[DAI]
  39.       ,[YU]
  40.           ,SUM([JIE]) OVER(PARTITION BY COMPANY,PERSON
  41.                                                                 ORDER BY COMPANY,PERSON
  42.                                                                 ROWS BETWEEN UNBOUNDED PRECEDING
  43.                                                                 AND CURRENT ROW) RunSumJIE
  44.           ,SUM(DAI) OVER(PARTITION BY COMPANY,PERSON ORDER BY COMPANY,PERSON) SumDai

  45.           ,SUM([JIE]) OVER(PARTITION BY COMPANY,PERSON
  46.                                                                 ORDER BY COMPANY,PERSON
  47.                                                                 ROWS BETWEEN UNBOUNDED PRECEDING
  48.                                                                 AND CURRENT ROW)
  49.           - SUM(DAI) OVER(PARTITION BY COMPANY,PERSON ORDER BY COMPANY,PERSON) CHA

  50.   FROM [dbo].[KBOrder]
  51.   )
  52.   SELECT C.COMPANY,C.PERSON,C.JIE,C.DAI,C.YU,C.RunSumJIE,C.SumDai,C.CHA
  53.   ,CASE WHEN C.CHA <= 0 THEN 0 WHEN C.CHA > 0 AND C.CHA < C.JIE THEN C.CHA WHEN C.CHA > 0 AND C.CHA > C.JIE THEN C.JIE END
  54.   FROM C
复制代码


电话/微信:18049989370 QQ:857188287
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则  允许回帖邮件提醒楼主

快表软件是国内较早研究表格类软件开发平台的团队之一,迄今已有十多年的行业经验.致力于为企事业单位提供实用可靠的数字化平台。
  • 微信公众号

  • 微信小商店

  • 微信客服

  • Powered by Discuz! X3.4 | Copyright © 2022-2024, XiRong Soft. | 快表软件
  • 沪ICP备13033196号 | 营业执照 |上海西戎软件科技有限公司|沪公网安备31011502002146号|沪ICP备13033196号 |