您的位置:程序门 -> ms-sql server -> 应用实例



再次请教(续昨日)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


再次请教(续昨日)[已结贴,结贴人:fuwuchao]
发表于:2007-07-21 14:29:37 楼主
昨日说道:如下
我是想把整个表都查询出来,并不是要看那一列的和。    
应该说,当我这个表中的某列的和达到了一个值,且在某2个时间段内时的查询结果。

比如:表table有a,b,c三列
a为时间,b为一个数字列,c为备注。

当b列的和达到50时,且a列在2007-6-1和2007-6-30之间时。

是要查询出这三列不是说b那一列。

今天想就上面问题在进一步说明
有一表table有a,b,c,d四列
a为时间,b为一个数字列,c为备注。(a列是随系统时间变的)

能不能判断当c在当月累计达到一个值的查询结果。
发表于:2007-07-21 15:19:131楼 得分:0
lz的數據貼出來,並把結果也貼出來,這樣大家才能更明的你的意思
发表于:2007-07-21 15:27:442楼 得分:0
能不能判断当c在当月累计达到一个值的查询结果。
------------
c为备注它為什么值?
发表于:2007-07-21 15:56:083楼 得分:0
哎呀我都不知道要什么说好了

表table
a                             b           c
2007-01-01           1           10
2007-01-01           2           20
2007-01-01           3           30
2007-01-10           1           40
2007-01-10           2           50
2007-01-20           1           60
2007-01-20           2           70
2007-01-25           1           80

就这样的3列当b累计从1号开始计算达到200时就显示出来的一个查询结果(a是随系统变的,b是一序号列代表那一天中有几笔业务的发生)

当b随a累计加到200时,即2007-01-20的第一笔数据时,就要查询出来。
发表于:2007-07-21 16:03:004楼 得分:30
樓主說的是這個意思?

declare   @t   table(
a   varchar(10),
b   int,
c   int)

insert   @t   select   '2007-05-01 ',   30,   5
union   all   select   '2007-06-01 ',   20,   3
union   all   select   '2007-06-12 ',   15,   6
union   all   select   '2007-06-14 ',   30,   2
union   all   select   '2007-06-28 ',   40,   6
union   all   select   '2007-07-03 ',   12,   7
union   all   select   '2007-07-13 ',   53,   3
union   all   select   '2007-07-15 ',   52,   5


select   *   from   @t
where   a   between   '2007-06-01 '   and   '2007-06-30 '
and   exists(select   1   from   @t
where   a   between   '2007-06-01 '   and   '2007-06-30 '
having   sum(b)   >   50)


/*
a                     b                       c
----------   -----------   -----------  
2007-06-01   20                     3
2007-06-12   15                     6
2007-06-14   30                     2
2007-06-28   40                     6

(所影响的行数为   4   行)
*/
发表于:2007-07-21 16:05:315楼 得分:0
...

猜錯了
发表于:2007-07-21 16:07:156楼 得分:0
到底是b列累計到200還是c列累計到200??

2007-01-20的第一筆應該是c列超過200才對啊
发表于:2007-07-21 16:18:467楼 得分:0
create   table   t

(a   varchar(10),                             b     int,       c   int)
insert   into   t
select   '2007-01-01 ',           1,           10   union   all
select   '2007-01-01 ',           2,           20   union   all
select   '2007-01-01 ',           3,           30   union   all
select   '2007-01-10 ',           1,           40   union   all
select   '2007-01-10 ',           2,           50   union   all
select   '2007-01-20 ',           1,           60   union   all
select   '2007-01-20 ',           2,           70   union   all
select   '2007-01-25 ',           1,           80


select   top   1   a.*   from   t   a
where   exists   (select   sum(c)   from   t  
where   id> =a.id   and   a.a   between   '2007-01-01 '   and   '2007-01-25 '  
having   sum(c)> 200)
order   by   id   desc


a                     b                       c                       id                    
----------   -----------   -----------   -----------  
2007-01-20   1                       60                     6

(1   row(s)   affected)
发表于:2007-07-21 16:19:588楼 得分:0
這是顯示一條,如果你要顯示到這條為止的所有記錄   呢


a                     b                       c                       id                    
----------   -----------   -----------   -----------  
2007-01-01   1                       10                     1
2007-01-01   2                       20                     2
2007-01-01   3                       30                     3
2007-01-10   1                       40                     4
2007-01-10   2                       50                     5
2007-01-20   1                       60                     6

(6   row(s)   affected)
发表于:2007-07-21 16:20:099楼 得分:0
对对是c我打错了
发表于:2007-07-21 16:20:2210楼 得分:0
select     a.*   from   t   a
where   exists   (select   sum(c)   from   t  
where   id> =a.id   and   a.a   between   '2007-01-01 '   and   '2007-01-25 '  
having   sum(c)> 200)
发表于:2007-07-21 16:26:4511楼 得分:0
這里我借助了id   自動編號


快速检索

最新资讯
热门点击