两条经典SQL语句(2)_Mssql数据库教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:浅谈SQL server2000中char和varchar字符型数据类型的区别
char和varchar的长度都在1到8000之间,它们的区别在于char是定长字符数据,而varchar是变长字符数据。所谓定长就是长度是固定的,当输入的数据长度没有达到指定的长度时将自动以英文空格在其后面填充,使长度达到相应的长度;而变长字符数据则不会以空格填充


  2004-03-16 00:00:00 NULL NULL 30
  2004-03-17 00:00:00 NULL NULL 30
  2004-03-18 00:00:00 NULL NULL 30
  2004-03-19 00:00:00 NULL NULL 30
  2004-03-20 00:00:00 NULL NULL 30
  2004-03-21 00:00:00 NULL NULL 30
  2004-03-22 00:00:00 NULL NULL 30
  2004-03-23 00:00:00 NULL NULL 30
  2004-03-24 00:00:00 NULL NULL 30
  2004-03-25 00:00:00 NULL NULL 30
  2004-03-26 00:00:00 NULL NULL 30
  2004-03-27 00:00:00 NULL NULL 30
  2004-03-28 00:00:00 NULL NULL 30
  2004-03-29 00:00:00 NULL NULL 30
  2004-03-30 00:00:00 NULL NULL 30
  2004-03-31 00:00:00 NULL NULL 30

 

  答案:


 
  SELECT Y.[日期], tbl.[收入], tbl.[支出], (
  SELECT SUM(ISNULL(tbl.[收入], 0)-ISNULL(tbl.[支出], 0)) FROM tbl WHERE [日期]<=Y.[日期]) AS [余额]
  FROM tbl RIGHT JOIN (
  SELECT DATEADD(dd, N.i, DATEADD(dd, 1-DAY(m.MinDay), m.MinDay)) AS [日期]
  FROM (
  SELECT 0 AS i




 
  UNION ALL SELECT 1
  UNION ALL SELECT 2
  UNION ALL SELECT 3
  UNION ALL SELECT 4
  UNION ALL SELECT 5
  UNION ALL SELECT 6
  UNION ALL SELECT 7
  UNION ALL SELECT 8
  UNION ALL SELECT 9
  UNION ALL SELECT 10
  UNION ALL SELECT 11
  UNION ALL SELECT 12
  UNION ALL SELECT 13
  UNION ALL SELECT 14
  UNION ALL SELECT 15
  UNION ALL SELECT 16
  UNION ALL SELECT 17
  UNION ALL SELECT 18
  UNION ALL SELECT 19
  UNION ALL SELECT 20
  UNION ALL SELECT 21
  UNION ALL SELECT 22
  UNION ALL SELECT 23
  UNION ALL SELECT 24
  UNION ALL SELECT 25
  UNION ALL SELECT 26
  UNION ALL SELECT 27
  UNION ALL SELECT 28
  UNION ALL SELECT 29
  UNION ALL SELECT 30
  UNION ALL SELECT 31
  ) N,
  (
  SELECT MIN(日期) AS MinDay
  FROM tbl
  GROUP BY DATEDIFF(month, 0, 日期)
  ) M
  WHERE DATEDIFF(mm, DATEADD(dd, N.i, DATEADD(dd, 1-DAY(m.MinDay), m.MinDay)), M.MinDay)=0) AS Y
  ON tbl.[日期]=Y.日期

分享:浅析SQL server存储过程
一直用ASP+ACCESS来编写网页和公司的内部应用系统,内部应用系统也就是大家说的OA吧,这个我也不知道,公司又叫它ERP,反正不管什么,它是用来帮助公司处理日常工作的,简化劳动的.从来没用过存储过程,所有的添加,删除,修改都是在ASP里面写程序执行,代码很多.数据

共2页上一页12下一页
来源:模板无忧//所属分类:Mssql数据库教程/更新时间:2010-04-13
相关Mssql数据库教程