SQL数据比对查询

zhongguohld 2009-04-23 04:38:14
原始数据
ITEM COSTTY COST
001 0 10
001 5 10
002 0 15
002 5 15
003 0 9
003 5 10
004 0 11
004 5 13


结果数据
ITEM COST0 COST5
003 9 10
004 11 13

就是要每条数据的COST的0和5不一值的,并且差超过10%的然后以行的形式显示出来!
...全文
389 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
meheartfly 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 HEROWANG 的回复:]
SQL codeselect s.item,s.cost,t.cost from tb s,tb t
where s.ITEM =t.ITEM and s.costty=0 and t.costty=5
and abs(1.0*(s.cost-t.cost)/s.cost)>0.1

item cost cost
003 9 10
004 11 13
[/Quote]
漂亮
  • 打赏
  • 举报
回复
select s.item,s.cost,t.cost from tb s,tb t 
where s.ITEM =t.ITEM and s.costty=0 and t.costty=5
and abs(1.0*(s.cost-t.cost)/s.cost)>0.1

item cost cost
003 9 10
004 11 13
gsk09 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ws_hgo 的回复:]
SQL codecreate table #tb(ITEM varchar(3), COSTTY int, COST int )
go

insert #tb select '001' , 0 , 10
insert #tb select '001' , 5 , 10
insert #tb select '002' , 0 , 15
insert #tb select '002' , 5 , 15
insert #tb select '003' , 0 , 9
insert #tb select '003' , 5 , 10
insert #tb select '004' , 0 , 11
insert #tb select '00…
[/Quote]


select item,sum((5-COSTTY)/5*COST) COST0,sum(COSTTY/5*COST) COST5 from tb group by item having max(cost)*1.0/min(cost)>1.1
zhongguohld 2009-04-24
  • 打赏
  • 举报
回复
还有点问题,我的数据还有可能是COST5比COST0大呢!
gsk09 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 meheartfly 的回复:]
引用 14 楼 HEROWANG 的回复:
SQL codeselect s.item,s.cost,t.cost from tb s,tb t
where s.ITEM =t.ITEM and s.costty=0 and t.costty=5
and abs(1.0*(s.cost-t.cost)/s.cost)>0.1

item cost cost
003 9 10
004 11 13

漂亮
[/Quote]

It is wrong !


create table tb(ITEM varchar(3), COSTTY int, COST int )
go

insert tb select '001' , 0 , 10
insert tb select '001' , 5 , 10
insert tb select '002' , 0 , 15
insert tb select '002' , 5 , 15
insert tb select '003' , 0 , 10
insert tb select '003' , 5 , 9
insert tb select '004' , 0 , 11
insert tb select '004' , 5 , 13

go
select item,sum((5-COSTTY)/5*COST) COST0,sum(COSTTY/5*COST) COST5 from tb group by item having max(cost)*1.0/min(cost)>1.1

select s.item,s.cost,t.cost from tb s,tb t
where s.ITEM =t.ITEM and s.costty=0 and t.costty=5
and abs(1.0*(s.cost-t.cost)/s.cost)>0.1


select s.item,s.cost,t.cost,abs(1.0*(s.cost-t.cost)/s.cost) as test1,abs(1.0*(s.cost-t.cost)/t.cost) as test2 from tb s,tb t
where s.ITEM =t.ITEM and s.costty=0 and t.costty=5

go
drop table tb

item COST0 COST5
---- ----------- -----------
003 10 9
004 11 13


item cost cost
---- ----------- -----------
004 11 13


item cost cost test1 test2
---- ----------- ----------- --------------------------------------- ---------------------------------------
001 10 10 0.000000000000 0.000000000000
002 15 15 0.000000000000 0.000000000000
003 10 9 0.100000000000 0.111111111111
004 11 13 0.181818181818 0.153846153846
htl258_Tony 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhongguohld 的回复:]
另外请教一下!
火狐/yynet <zhongguohld@163.com> 19:13:55
select item,min(cost) COST0,max(cost) COST5 from tb group by item having max(cost)*1.0/min(cost)>1.1
火狐/yynet <zhongguohld@163.com> 19:14:05
这里为什么要乘以1.0
[/Quote]
隐式转化为 float型,否则为int型。
htl258_Tony 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhongguohld 的回复:]
我要的是+-10%不知道能不能解决呢!
[/Quote]
不需要正负了。
不管你哪个上,哪个下,如果都是一样的。
select item,min(cost) COST0,max(cost) COST5 
from tb
group by item
having max(cost)*1.0/min(cost)>1.1
go

这是三楼的语句,可行的。因为这里使用了聚合,所以顺序不是问题。
zhongguohld 2009-04-23
  • 打赏
  • 举报
回复
另外请教一下!
火狐/yynet<zhongguohld@163.com> 19:13:55
select item,min(cost) COST0,max(cost) COST5 from tb group by item having max(cost)*1.0/min(cost)>1.1
火狐/yynet<zhongguohld@163.com> 19:14:05
这里为什么要乘以1.0
zhongguohld 2009-04-23
  • 打赏
  • 举报
回复
我要的是+-10%不知道能不能解决呢!
ws_hgo 2009-04-23
  • 打赏
  • 举报
回复
create table #tb(ITEM varchar(3),    COSTTY int,   COST int )
go

insert #tb select '001' , 0 , 10
insert #tb select '001' , 5 , 10
insert #tb select '002' , 0 , 15
insert #tb select '002' , 5 , 15
insert #tb select '003' , 0 , 9
insert #tb select '003' , 5 , 10
insert #tb select '004' , 0 , 11
insert #tb select '004' , 5 , 13

select * from #tb t where exists (select * from #tb where ITEM=t.ITEM and COST<t.COST)

ITEM COSTTY COST
---- ----------- -----------
003 5 10
004 5 13

(2 行受影响)
nzperfect 2009-04-23
  • 打赏
  • 举报
回复
有没有5比0小的情况?
htl258_Tony 2009-04-23
  • 打赏
  • 举报
回复
楼上反应很快
claro 2009-04-23
  • 打赏
  • 举报
回复
帮顶。
sdhdy 2009-04-23
  • 打赏
  • 举报
回复
create table tb(ITEM varchar(3),    COSTTY int,   COST int )
go

insert tb select '001' , 0 , 10
insert tb select '001' , 5 , 10
insert tb select '002' , 0 , 15
insert tb select '002' , 5 , 15
insert tb select '003' , 0 , 9
insert tb select '003' , 5 , 10
insert tb select '004' , 0 , 11
insert tb select '004' , 5 , 13

go
select item,min(cost) COST0,max(cost) COST5 from tb group by item having max(cost)*1.0/min(cost)>1.1
go
drop table tb
/*
item COST0 COST5
---- ----------- -----------
003 9 10
004 11 13
*/
sdhdy 2009-04-23
  • 打赏
  • 举报
回复
select item,min(cost) COST0,max(cost) COST5 from  tb group by item having max(cost)*1.0/min(cost)>1.1
mugua604 2009-04-23
  • 打赏
  • 举报
回复
理解能力越来越差了~

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧