读书人

这样的查询怎么写

发布时间: 2014-01-01 18:12:08 作者: rapoo

这样的查询如何写
------------------------------
品名 单价 地域别
a 10 1
a 11 2
b 5 1
b 4 2
c 3 1
d 6 2

想要的结果:
如果给条件是True则显示:
------------------------------
品名 单价 地域别
a 10 1
b 5 1
c 3 1
d 6 2
如果给条件是False则显示:
------------------------------
品名 单价 地域别
a 11 2
b 4 2
c 3 1
d 6 2

就是说:①如果条件为真,同一种品名有两种单价就显示地域别为1的单价,
一种品名只有一种单价就不考虑地域别
②如果条件为假,同一种品名有两种单价就显示地域别为2的单价,
一种品名只有一种单价就不考虑地域别
[解决办法]


--true
;with a1 (品名,单价,地域别) as
(
select 'a',10,1 union all
select 'a',11,2 union all
select 'b',5,1 union all
select 'b',4,2 union all
select 'c',3,1 union all
select 'd',6,2
)
,a2 as
(
select *,row_number() over(partition by 品名 order by 地域别) re from a1
)
select 品名,单价,地域别
from a2
where re=1

--False
;with a1 (品名,单价,地域别) as
(
select 'a',10,1 union all
select 'a',11,2 union all
select 'b',5,1 union all
select 'b',4,2 union all
select 'c',3,1 union all
select 'd',6,2
)
,a2 as
(
select *,row_number() over(partition by 品名 order by 地域别 desc) re from a1
)
select 品名,单价,地域别
from a2
where re=1

[解决办法]

create table kc
(品名 varchar(5),单价 int,地域别 int)

insert into kc
select 'a',10,1 union all
select 'a',11,2 union all
select 'b',5,1 union all
select 'b',4,2 union all
select 'c',3,1 union all
select 'd',6,2


-- 如果给条件是True
declare @x varchar(10)
select @x='True'

select a.*
from kc a
where a.地域别=case @x when 'True' then 1
when 'False' then 2 end
or not exists
(select 1 from kc b where b.品名=a.品名 and b.地域别!=a.地域别)

/*
品名 单价 地域别
----- ----------- -----------
a 10 1
b 5 1
c 3 1
d 6 2

(4 row(s) affected)
*/


-- 如果给条件是False
declare @x varchar(10)
select @x='False'

select a.*
from kc a
where a.地域别=case @x when 'True' then 1
when 'False' then 2 end
or not exists
(select 1 from kc b where b.品名=a.品名 and b.地域别!=a.地域别)

/*
品名 单价 地域别


----- ----------- -----------
a 11 2
b 4 2
c 3 1
d 6 2

(4 row(s) affected)
*/


[解决办法]
引用:
Quote: 引用:

你用的是2000,还是2005呢


sql 2005


试试这个:

create table kc
(品名 varchar(5),单价 int,地域别 int)

insert into kc
select 'a',10,1 union all
select 'a',11,2 union all
select 'b',5,1 union all
select 'b',4,2 union all
select 'c',3,1 union all
select 'd',6,2
go


declare @condition varchar(10)

set @condition='true' --按照你的需要,输入true或者false

select 品名,单价,地域别
from
(
select *,
ROW_NUMBER() over(partition by 品名
order by case when @condition = 'true' and 地域别 = 1
then 0
when @condition = 'false' and 地域别 = 2
then 0
else 1
end) as rownum
from kc
)t
where rownum = 1
/*
品名单价地域别
a101
b51
c31
d62
*/

[解决办法]
引用:

create table kc
(品名 varchar(5),单价 int,地域别 int)

insert into kc
select 'a',10,1 union all
select 'a',11,2 union all
select 'b',5,1 union all
select 'b',4,2 union all
select 'c',3,1 union all
select 'd',6,2


-- 如果给条件是True
declare @x varchar(10)
select @x='True'

select a.*
from kc a
where a.地域别=case @x when 'True' then 1
when 'False' then 2 end
or not exists
(select 1 from kc b where b.品名=a.品名 and b.地域别!=a.地域别)

/*
品名 单价 地域别
----- ----------- -----------
a 10 1
b 5 1
c 3 1
d 6 2

(4 row(s) affected)
*/


-- 如果给条件是False
declare @x varchar(10)
select @x='False'

select a.*
from kc a
where a.地域别=case @x when 'True' then 1
when 'False' then 2 end
or not exists
(select 1 from kc b where b.品名=a.品名 and b.地域别!=a.地域别)

/*
品名 单价 地域别
----- ----------- -----------
a 11 2


b 4 2
c 3 1
d 6 2

(4 row(s) affected)
*/



引用:
Quote: 引用:

Quote: 引用:

你用的是2000,还是2005呢


sql 2005


试试这个:

create table kc
(品名 varchar(5),单价 int,地域别 int)

insert into kc
select 'a',10,1 union all
select 'a',11,2 union all
select 'b',5,1 union all
select 'b',4,2 union all
select 'c',3,1 union all
select 'd',6,2
go


declare @condition varchar(10)

set @condition='true' --按照你的需要,输入true或者false

select 品名,单价,地域别
from
(
select *,
ROW_NUMBER() over(partition by 品名
order by case when @condition = 'true' and 地域别 = 1
then 0
when @condition = 'false' and 地域别 = 2
then 0
else 1
end) as rownum
from kc
)t
where rownum = 1
/*
品名单价地域别
a101
b51
c31
d62
*/
涨知识啦, 两位大牛的写法真是不一般
[解决办法]
IF OBJECT_ID(N'kc',N'U') IS NOT NULL
DROP TABLE kc

CREATE TABLE kc
(
品名 VARCHAR(5) ,
单价 INT ,
地域别 INT
)
insert into kc
select 'a',10,1 union all
select 'a',11,2 union all
select 'b',5,1 union all
select 'b',4,2 union all
select 'c',3,1 union all
select 'd',6,2

-- 如果给条件是True
DECLARE @C varchar(10)
DECLARE @Sql NVARCHAR(MAX)
SET @C='True'

-- 如果给条件是False
--SET @C='False'

SET @Sql='SELECT c.品名,c.单价,c.地域别 FROM
(
SELECT CASE '''+@C+''' WHEN ''True'' THEN MIN(ID) ELSE MAX(ID) END ID,品名 FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY 品名) AS ID ,* FROM kc)a GROUP BY 品名)b,(SELECT ROW_NUMBER() OVER(ORDER BY 品名) AS ID ,* FROM kc)c
WHERE b.ID=c.ID'
EXEC(@sql)

品名 单价 地域别
----- ----------- -----------
a 10 1
b 5 1
c 3 1
d 6 2

(4 行受影响)

读书人网 >SQL Server

热点推荐