由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - sql query question
相关主题
parameterized queries with no inputsa complex sql query, high hand help!!!
sql面试题目求指点What is optimizer statistics of a table and what is access plan of a query
怎么写这个query呢?请教ACCESS问题, 如何把REPORT的结果(如SUM)写回TABLE中?
问个SQL的问题如何完成这个sql?
请教一个SQL query该怎么写请教一个ACCESS的土问题
请教2个sql query 问题Re: 求教: Join 两个query (data) tables 出错
一个求和的有日期限制的sql query 问题.菜鸟求教 MS Access pass through query
change year format in Access by SQL query (转载)如果比较两个table?再access里边
相关话题的讨论汇总
话题: 200705话题: qty话题: sum话题: a01话题: 200704
进入Database版参与讨论
1 (共1页)
c***n
发帖数: 921
1
Table A: 3 attributes (id, qty, date) e.g. row (a001, 50, 200705)
Now want to produce a table B which contains 3 attributes (id, sum1, sum2)
sum1= sum(qty) between 200705 and 200804
sum2 = sum(qty) between 200605 and 200704
Is there any smart method to get table B?
B*****g
发帖数: 34098
2
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.
db2.udb.doc/admin/r0005646.htm

D

【在 c***n 的大作中提到】
: Table A: 3 attributes (id, qty, date) e.g. row (a001, 50, 200705)
: Now want to produce a table B which contains 3 attributes (id, sum1, sum2)
: sum1= sum(qty) between 200705 and 200804
: sum2 = sum(qty) between 200605 and 200704
: Is there any smart method to get table B?

c***n
发帖数: 921
3
怎么用case 呢? 我也考虑过, 可是不会用.请多提示一下.

【在 B*****g 的大作中提到】
: http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.
: db2.udb.doc/admin/r0005646.htm
:
: D

B*****g
发帖数: 34098
4
select id,
sum(case when date between 200705 and 200804 then qty else 0),
sum(case when date between 200605 and 200704 then qty else 0)
from table a

【在 c***n 的大作中提到】
: 怎么用case 呢? 我也考虑过, 可是不会用.请多提示一下.
c***n
发帖数: 921
5
这个我试验了,可是不行. 让我下周再试试.
不过我写的是 sum(case....) as newName

【在 B*****g 的大作中提到】
: select id,
: sum(case when date between 200705 and 200804 then qty else 0),
: sum(case when date between 200605 and 200704 then qty else 0)
: from table a

B*****g
发帖数: 34098
6
select id,
sum(case when date between 200705 and 200804 then qty else 0 end),
sum(case when date between 200605 and 200704 then qty else 0 end)
from table a

【在 c***n 的大作中提到】
: 这个我试验了,可是不行. 让我下周再试试.
: 不过我写的是 sum(case....) as newName

s*****c
发帖数: 24
7
select id,
sum(case when date between 200705 and 200804 then qty else 0 end),
sum(case when date between 200605 and 200704 then qty else 0 end)
from table a
group by id;
c***n
发帖数: 921
8
200705 是 date, 不是id.
id qty date
a01 50 200601
a01 55 200604
a01 60 200605
a01 50 200701
a01 55 200702
a01 60 200704
a01 45 200805
a01 55 200806
a02 50 200603
a02 55 200604
a02 60 200608
a02 50 200703
a02 55 200704
a02 60 200705
a02 45 200801
a02 60 200804
a02 55 200806
a03 10 200807
期望得到
id sum1(200605-200704 sum) sum2(200705-200804 sum)
a01 225 null (or 0)
a
c***n
发帖数: 921
9
运行成功了.

【在 s*****c 的大作中提到】
: select id,
: sum(case when date between 200705 and 200804 then qty else 0 end),
: sum(case when date between 200605 and 200704 then qty else 0 end)
: from table a
: group by id;

b*****e
发帖数: 364
10
This one is the right answer.

【在 s*****c 的大作中提到】
: select id,
: sum(case when date between 200705 and 200804 then qty else 0 end),
: sum(case when date between 200605 and 200704 then qty else 0 end)
: from table a
: group by id;

1 (共1页)
进入Database版参与讨论
相关主题
如果比较两个table?再access里边请教一个SQL query该怎么写
mysql maximum columns <=1000?请教2个sql query 问题
有没有可能将在Access里做好的东西导入到Oracle里去一个求和的有日期限制的sql query 问题.
请教一个SQL Querychange year format in Access by SQL query (转载)
parameterized queries with no inputsa complex sql query, high hand help!!!
sql面试题目求指点What is optimizer statistics of a table and what is access plan of a query
怎么写这个query呢?请教ACCESS问题, 如何把REPORT的结果(如SUM)写回TABLE中?
问个SQL的问题如何完成这个sql?
相关话题的讨论汇总
话题: 200705话题: qty话题: sum话题: a01话题: 200704