34,591
社区成员
发帖
与我相关
我的任务
分享
;WITH Goods([Year],Goods,Sales) AS (
SELECT 2000,'酒',100 UNION ALL
SELECT 2001,'酒',110 UNION ALL
SELECT 2002,'酒',120 UNION ALL
SELECT 2003,'酒',130 UNION ALL
SELECT 2004,'酒',140 UNION ALL
SELECT 2005,'酒',150 UNION ALL
SELECT 2000,'饼干',50 UNION ALL
SELECT 2001,'饼干',60 UNION ALL
SELECT 2002,'饼干',70 UNION ALL
SELECT 2003,'饼干',80 UNION ALL
SELECT 2004,'饼干',90 UNION ALL
SELECT 2005,'饼干',85 UNION ALL
SELECT 2000,'电视',100 UNION ALL
SELECT 2001,'电视',200 UNION ALL
SELECT 2002,'电视',300 UNION ALL
SELECT 2003,'电视',400 UNION ALL
SELECT 2004,'电视',500 UNION ALL
SELECT 2005,'电视',600 UNION ALL
SELECT 2000,'冰箱',50 UNION ALL
SELECT 2001,'冰箱',60 UNION ALL
SELECT 2002,'冰箱',70 UNION ALL
SELECT 2003,'冰箱',65 UNION ALL
SELECT 2004,'冰箱',80 UNION ALL
SELECT 2005,'冰箱',90
),
Tab2 AS (
SELECT
a.Year,
a.Goods,
SUM(a.Sales) AS Sales,
LEAD(SUM(a.Sales)) OVER(PARTITION BY a.Goods ORDER BY a.year) AS NextYear
FROM
Goods a
GROUP BY
a.Year,a.Goods
)
SELECT a.Goods
FROM Goods a
WHERE NOT EXISTS(
SELECT
*
FROM
Tab2 b
WHERE
b.Sales > ISNULL(b.NextYear,b.Sales)
AND b.Goods = a.Goods
)
GROUP BY a.Goods
Goods
电视
酒
--create table sales
--(
-- years int,
-- goods varchar(10),
-- sales int
--)
--insert into sales
--select 2000,'酒',100 union all
--select 2001,'酒',110 union all
--select 2002,'酒',120 union all
--select 2003,'酒',130 union all
--select 2004,'酒',140 union all
--select 2005,'酒',150 union all
--select 2000,'饼干',50 union all
--select 2001,'饼干',60 union all
--select 2002,'饼干',70 union all
--select 2003,'饼干',80 union all
--select 2004,'饼干',90 union all
--select 2005,'饼干',85 union all
--select 2000,'电视',100 union all
--select 2001,'电视',200 union all
--select 2002,'电视',300 union all
--select 2003,'电视',400 union all
--select 2004,'电视',500 union all
--select 2005,'电视',600 union all
--select 2000,'冰箱',50 union all
--select 2001,'冰箱',60 union all
--select 2002,'冰箱',70 union all
--select 2003,'冰箱',65 union all
--select 2004,'冰箱',80 union all
--select 2005,'冰箱',90
DECLARE @v NVARCHAR(max);
DECLARE @v1 NVARCHAR(max);
DECLARE @v2 NVARCHAR(max);
DECLARE @sql NVARCHAR(max);
SET @v=stuff((SELECT DISTINCT ',['+goods+'] as ['+goods+']' FROM sales FOR XML PATH('')),1,1,'')
SET @v1=stuff((SELECT DISTINCT ',['+goods+']' FROM sales FOR XML PATH('')),1,1,'')
SET @v2=stuff((SELECT DISTINCT ',replicate(''='',['+goods+']/10.0) as ['+goods+']' FROM sales FOR XML PATH('')),1,1,'')
SET @sql='
;with cte as (
SELECT years,'+@v+' FROM sales pivot (max(sales) for goods in ('+@v1+') ) pt
)
select years,'+@v2+' from cte
'
EXEC(@sql)
绘个条形图出来一看就看出来了。create table sales
(
years int,
goods varchar(10),
sales int
)
insert into sales
select 2000,'酒',100 union all
select 2001,'酒',110 union all
select 2002,'酒',120 union all
select 2003,'酒',130 union all
select 2004,'酒',140 union all
select 2005,'酒',150 union all
select 2000,'饼干',50 union all
select 2001,'饼干',60 union all
select 2002,'饼干',70 union all
select 2003,'饼干',80 union all
select 2004,'饼干',90 union all
select 2005,'饼干',85 union all
select 2000,'电视',100 union all
select 2001,'电视',200 union all
select 2002,'电视',300 union all
select 2003,'电视',400 union all
select 2004,'电视',500 union all
select 2005,'电视',600 union all
select 2000,'冰箱',50 union all
select 2001,'冰箱',60 union all
select 2002,'冰箱',70 union all
select 2003,'冰箱',65 union all
select 2004,'冰箱',80 union all
select 2005,'冰箱',90
;with cte as(
select
ROW_NUMBER() over(partition by goods order by years) as yearsNum,
ROW_NUMBER() over(partition by goods order by sales) as salesNum,
*
from sales
)
select distinct goods
from sales
where goods not in (
select goods
from cte
where yearsNum<>salesNum
)
goods
----------
电视
酒
(2 行受影响)