我想忽略生产的第一天是否有任何零值。
SELECT D_DATE, PRODUCE FROM PRODUCTION
数据集
Date Produce
1/1/2015 0
1/2/2015 0
1/3/2015 0
1/4/2015 6
1/5/2015 5
1/6/2015 2
1/7/2015 0
1/8/2015 1
1/9/2015 1
前三天是我想在结果中忽略的零,但第7天不应该被忽略
期望的结果
Date Produce
1/4/2015 6
1/5/2015 5
1/6/2015 2
1/7/2015 0
1/8/2015 1
1/9/2015 1
答案 0 :(得分:3)
为简单起见,我假设至少有一天有产品> 0
SELECT d_date, produce
FROM production
WHERE
d_date >= (
SELECT MIN(d_date)
FROM production
WHERE
produce != 0
)
;
答案 1 :(得分:1)
您可以使用SUM作为分析函数来计算产生的累积总和并过滤大于零的那些。
select d_date, produce
from (
select
d_date,
produce,
sum(produce) over (order by d_date) cuml_produce
from production
)
where cuml_produce > 0
order by d_date;
答案 2 :(得分:0)
试试这个
SELECT date,
produce
FROM
(
SELECT date,
produce,
row_number() over (order by date) r1,
row_number() over (order by produce, date) r2
FROM production
) A
WHERE r1 != r2
答案 3 :(得分:-1)
使用相关子查询。
SELECT Date, Produce,
ROW_NUMBER() OVER (ORDER BY DATE) RN
INTO #Temp
FROM tbl
SELECT t.Date, t.Produce FROM #Temp t
WHERE
EXISTS(
SELECT 1 FROM #Temp t1
WHERE t1.rn < t.rn
AND t1.PRODUCE !=0)
OR T.Produce != 0
答案 4 :(得分:-2)
假设您的日期在DATETIME,这会对您有所帮助吗?
SELECT D_DATE, PRODUCE
FROM PRODUCTION
where Date >= (select TOP 1 Date from PRODUCTION where PRODUCE > 0 Order by DATE)