无人问津的帖子又来了...
时间:2011-12-13
来源:互联网
--> 测试数据:[获取表]
if object_id('[获取表]') is not null drop table [获取表]
create table [获取表]([卡号] int,[获取值] int,[年份] int,[月份] int)
insert [获取表]
select 1102,460,2010,11 union all
select 1102,230,2010,11 union all
select 1102,530,2010,11 union all
select 1102,265,2010,11 union all
select 1102,530,2010,11 union all
select 1102,230,2010,12 union all
select 1102,265,2010,12 union all
select 1102,530,2010,12 union all
select 1102,530,2010,12 union all
select 1102,230,2011,1 union all
select 1102,230,2011,1 union all
select 1102,460,2011,1 union all
select 1102,690,2011,1 union all
select 1102,1150,2011,1 union all
select 1102,1700,2011,1 union all
select 1102,265,2011,1 union all
select 1102,1325,2011,1 union all
select 1102,1325,2011,1 union all
select 1102,1060,2011,2 union all
select 1102,530,2011,3 union all
select 1102,530,2011,3 union all
select 1102,530,2011,4 union all
select 1102,795,2011,4 union all
select 1102,795,2011,4 union all
select 1102,530,2011,4 union all
select 1102,530,2011,5 union all
select 1102,530,2011,5 union all
select 1102,133,2011,5 union all
select 1102,266,2011,5 union all
select 1102,340,2011,9 union all
select 1102,340,2011,10 union all
select 1102,340,2011,10 union all
select 1102,680,2011,11
--> 测试数据:[兑换表]
if object_id('[兑换表]') is not null drop table [兑换表]
create table [兑换表]([卡号] int,[兑换值] int,[年份] int,[月份] int)
insert [兑换表]
select 1102,1920,2010,12 union all
select 1102,1512,2011,1 union all
select 1102,8550,2011,3 union all
select 1102,975,2011,4 union all
select 1102,3870,2011,9 union all
select 1102,430,2011,10 union all
select 1102,916,2011,11 union all
select 1102,550,2011,12
******
查询结果
卡号 总获取 总兑换 剩余 年份 月份
1102 17174 12957 4217 2011 8
1102 17514 12957 4557 2011 9
1102 18194 16827 1367 2011 10
if object_id('[获取表]') is not null drop table [获取表]
create table [获取表]([卡号] int,[获取值] int,[年份] int,[月份] int)
insert [获取表]
select 1102,460,2010,11 union all
select 1102,230,2010,11 union all
select 1102,530,2010,11 union all
select 1102,265,2010,11 union all
select 1102,530,2010,11 union all
select 1102,230,2010,12 union all
select 1102,265,2010,12 union all
select 1102,530,2010,12 union all
select 1102,530,2010,12 union all
select 1102,230,2011,1 union all
select 1102,230,2011,1 union all
select 1102,460,2011,1 union all
select 1102,690,2011,1 union all
select 1102,1150,2011,1 union all
select 1102,1700,2011,1 union all
select 1102,265,2011,1 union all
select 1102,1325,2011,1 union all
select 1102,1325,2011,1 union all
select 1102,1060,2011,2 union all
select 1102,530,2011,3 union all
select 1102,530,2011,3 union all
select 1102,530,2011,4 union all
select 1102,795,2011,4 union all
select 1102,795,2011,4 union all
select 1102,530,2011,4 union all
select 1102,530,2011,5 union all
select 1102,530,2011,5 union all
select 1102,133,2011,5 union all
select 1102,266,2011,5 union all
select 1102,340,2011,9 union all
select 1102,340,2011,10 union all
select 1102,340,2011,10 union all
select 1102,680,2011,11
--> 测试数据:[兑换表]
if object_id('[兑换表]') is not null drop table [兑换表]
create table [兑换表]([卡号] int,[兑换值] int,[年份] int,[月份] int)
insert [兑换表]
select 1102,1920,2010,12 union all
select 1102,1512,2011,1 union all
select 1102,8550,2011,3 union all
select 1102,975,2011,4 union all
select 1102,3870,2011,9 union all
select 1102,430,2011,10 union all
select 1102,916,2011,11 union all
select 1102,550,2011,12
******
查询结果
卡号 总获取 总兑换 剩余 年份 月份
1102 17174 12957 4217 2011 8
1102 17514 12957 4557 2011 9
1102 18194 16827 1367 2011 10
作者: admczy 发布时间: 2011-12-13
你怎么知道这帖子无人问津的?
作者: qianjin036a 发布时间: 2011-12-13
SQL code
create table [获取表]([卡号] int,[获取值] int,[年份] int,[月份] int) insert [获取表] select 1102,460,2010,11 union all select 1102,230,2010,11 union all select 1102,530,2010,11 union all select 1102,265,2010,11 union all select 1102,530,2010,11 union all select 1102,230,2010,12 union all select 1102,265,2010,12 union all select 1102,530,2010,12 union all select 1102,530,2010,12 union all select 1102,230,2011,1 union all select 1102,230,2011,1 union all select 1102,460,2011,1 union all select 1102,690,2011,1 union all select 1102,1150,2011,1 union all select 1102,1700,2011,1 union all select 1102,265,2011,1 union all select 1102,1325,2011,1 union all select 1102,1325,2011,1 union all select 1102,1060,2011,2 union all select 1102,530,2011,3 union all select 1102,530,2011,3 union all select 1102,530,2011,4 union all select 1102,795,2011,4 union all select 1102,795,2011,4 union all select 1102,530,2011,4 union all select 1102,530,2011,5 union all select 1102,530,2011,5 union all select 1102,133,2011,5 union all select 1102,266,2011,5 union all select 1102,340,2011,9 union all select 1102,340,2011,10 union all select 1102,340,2011,10 union all select 1102,680,2011,11 create table [兑换表]([卡号] int,[兑换值] int,[年份] int,[月份] int) insert [兑换表] select 1102,1920,2010,12 union all select 1102,1512,2011,1 union all select 1102,8550,2011,3 union all select 1102,975,2011,4 union all select 1102,3870,2011,9 union all select 1102,430,2011,10 union all select 1102,916,2011,11 union all select 1102,550,2011,12 go select distinct 卡号, (select sum(获取值) from 获取表 where 卡号=a.卡号 and (年份<a.年份 or (年份=a.年份 and 月份<=a.月份))) 总获取, (select sum(兑换值) from 兑换表 where 卡号=a.卡号 and (年份<a.年份 or (年份=a.年份 and 月份<=a.月份))) 总兑换, 年份,月份 FROM ( select distinct 卡号,2011 as 年份,8 as 月份 from 获取表 union select distinct 卡号,2011,9 from 获取表 union select distinct 卡号,2011,10 from 获取表 )a /* 卡号 总获取 总兑换 年份 月份 ----------- ----------- ----------- ----------- ----------- 1102 17174 12957 2011 8 1102 17514 16827 2011 9 1102 18194 17257 2011 10 (3 行受影响) */ go drop table 兑换表,获取表
作者: qianjin036a 发布时间: 2011-12-13

作者: Beirut 发布时间: 2011-12-13
????????
作者: rucypli 发布时间: 2011-12-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28