当前位置: 代码迷 >> Sql Server >> 急请相助修改,希望得到结果是正确的
  详细解决方案

急请相助修改,希望得到结果是正确的

热度:68   发布时间:2016-04-24 09:52:20.0
急!请帮助修改,希望得到结果是正确的!
--建立数据表
if object_id('tempdb.dbo.#tb1') is not null drop table #tb1
go 
create table #tb1([ID] [int] IDENTITY(1,1) NOT NULL,[no1] int,[no2] int,[no3] int,[no4] int,[no5] int)
insert #tb1 
select '12','13','17','20','24'
--建立数据表
if object_id('tempdb.dbo.#tb2') is not null drop table #tb2
go 
create table #tb2([ID] [int] IDENTITY(1,1) NOT NULL,[no1] int,[no2] int,[no3] int,[no4] int,[no5] int)
insert #tb2 
select '12','13','17','20','24' union all
select '12','14','17','20','24' union all
select '12','15','17','20','24' union all
select '12','16','17','20','24' union all
select '12','17','18','20','24' union all
select '12','17','19','20','24' union all
select '12','17','20','21','24' union all
select '12','17','20','22','24' union all
select '12','17','20','23','24' union all
select '05','12','17','20','24' union all
select '06','12','17','20','24' union all
select '12','17','20','24','35'
---进行查询
Select ID as KRID,no1,no2,no3,no4,no5,
((Case when NO1 in (Select [no1],[no2],[no3],[no4],[no5]from #tb1 ) then 1 else 0 end)+
 (Case when NO2 in (Select [no1],[no2],[no3],[no4],[no5]from #tb1 ) then 1 else 0 end)+
 (Case when NO3 in (Select [no1],[no2],[no3],[no4],[no5]from #tb1 ) then 1 else 0 end)+
 (Case when NO4 in (Select [no1],[no2],[no3],[no4],[no5]from #tb1 ) then 1 else 0 end)+
 (Case when NO5 in (Select [no1],[no2],[no3],[no4],[no5]from #tb1 ) then 1 else 0 end)) as KRpNumber  
From  #tb2  order by KRpNumber desc

执行结果是
引用
(1 行受影响)

(12 行受影响)
消息 116,级别 16,状态 1,第 16 行
当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。
消息 116,级别 16,状态 1,第 16 行
当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。
消息 116,级别 16,状态 1,第 16 行
当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。
消息 116,级别 16,状态 1,第 16 行
当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。
消息 116,级别 16,状态 1,第 16 行
当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。


请帮助修改,希望得到结果
KRID	no1	no2	no3	no4	no5	KRpNumber
12 12 17 20 24 35 5
1 12 13 17 20 24 4
2 12 14 17 20 24 4
3 12 15 17 20 24 4
4 12 16 17 20 24 4
5 12 17 18 20 24 4
6 12 17 19 20 24 4
7 12 17 20 21 24 4
8 12 17 20 22 24 4
9 12 17 20 23 24 4
10 5 12 17 20 24 4
11 6 12 17 20 24 4

------解决思路----------------------


WITH CTE AS (
select QTY from #tb1 
unpivot (QTY for TYPE in(no1,no2,no3,no4,no5)) as p)
Select ID as KRID,no1,no2,no3,no4,no5,
((Case when NO1 in (Select *from CTE ) then 1 else 0 end)+
 (Case when NO2 in (Select *from CTE ) then 1 else 0 end)+
 (Case when NO3 in (Select *from CTE  ) then 1 else 0 end)+
 (Case when NO4 in (Select*from CTE  ) then 1 else 0 end)+
 (Case when NO5 in (Select *from CTE  ) then 1 else 0 end)) as KRpNumber  
From  #tb2  order by KRpNumber desc

--结果
KRID        no1         no2         no3         no4         no5         KRpNumber
----------- ----------- ----------- ----------- ----------- ----------- -----------
1           12          13          17          20          24          5
2           12          14          17          20          24          4
3           12          15          17          20          24          4
  相关解决方案