ÇóÒ»SQLÓï¾ä - MS-SQL Server / »ù´¡Àà
ÓбíÈçÏÂ
a b c
001 ÊÕÈë 5000
001 ·ÑÓÃ 2000
001 ºÏ¼Æ 3000
002 ÊÕÈë 6000
002 ·ÑÓÃ 3000
002 ºÏ¼Æ 3000
003 ÊÕÈë 4000
003 ·ÑÓÃ 1000
003 ºÏ¼Æ 2000
...
ºÏ¼Æ=ÊÕÈë-·ÑÓÃ
ÇóÒ»Óï¾ä ²éÕÒ³öµ±b=ÊÕÈëʱc×ֶβ»ÕýÈ·µÄ¼Ç¼
select ²»ÕýÈ·¼Ç¼=x.a from ta x join ta y on x.ºÏ¼Æ<>y.ÊÕÈë-y.·ÑÓà where x.b='ÊÕÈë'
SQL code:
select a,sum(case b when 'ÊÕÈë' then c else -c end)
from your_table
group by a
having sum(case b when 'ÊÕÈë' then c else -c end) <> 0
SQL code:
use PracticeDB
go
if exists (select 1 from sysobjects where name='tb_a')
drop table tb_a
go
create table tb_a (a varchar(10), b varchar(10),c numeric(10))
go
insert into tb_a
select '001', 'ÊÕÈë' ,5000 union all
select '001', '·ÑÓÃ' ,2000 union all
select '001', 'ºÏ¼Æ' ,3000 union all
select '002', 'ÊÕÈë' ,6000 union all
select '002', '·ÑÓÃ' ,3000 union all
select '002', 'ºÏ¼Æ' ,3000 union all
select '003', 'ÊÕÈë' ,4000 union all
select '003', '·ÑÓÃ' ,1000 union all
select '003', 'ºÏ¼Æ' ,2000
ºÏ¼Æ=ÊÕÈë-·ÑÓÃ
ÇóÒ»Óï¾ä ²éÕÒ³öµ±b=ÊÕÈëʱc×ֶβ»ÕýÈ·µÄ¼Ç¼
select * from tb_a
;with t
as
(
select a,sum(case b when 'ÊÕÈë' then c else 0 end) [ÊÕÈë],
Ïà¹ØÎÊ´ð£º
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jas ......
×Ö¶Î1,×Ö¶Î2.....×Ö¶ÎN,Status,ParentID
1,Name1....test1,1,99
1,Name1....test1,3,99
1,Name2....test2,1,101
1,Name2....test2,3,101
1,Name3....test3,2,101
1,Name1....test1,4,101
ÏëÒªµÄ½á¹ûÊÇ:
1,Na ......
±íÊý¾Ý
COL1 COL2 COL2 COL4 COL5
----------------------------------------------------------------------------------------------
2010-05-05 00:00 ......
Ôõô°ÑMDFÊý¾Ýתµ½MySQL£¿
ÖÁÉٵø½¼Óµ½sqlserverÉÏ
¾ßÌåÔõôŪ°¡ ÎҵıÏÒµÉè¼ÆµÄÊý¾Ý¿âÊÇ´ÓÍøÉÏdownÏÂÀ´µÄ£¿£¿
Ä㻹²»ÈçÖ±½ÓÓÃsqlserverÄØ
º¯Êý¡¢´æ´¢¹ý³Ì¶¼µÃ¸ÄÁË¡£
sqlserverûÔõôÓùý¡£Ò²Ã»Óа²× ......
select o_customer,o_price from orders having o_price >=avg(o_price)
select o_customer,o_price from orders where o_price >=(select avg(o_price) from orders)
ÎҸоõûÓÐÇø±ð°¡£¬ÔõôÔÚmysql»áÓÐ ......