我有一个SQL Server 2008表,其中的列代表每12个月。我想找到连续2个月有负余额的行。
编辑:我需要将所有正列相加,所以我使用了它。如果有人知道更简单的方式让我知道。
SELECT SUM(case when Oct > 0 then Oct else 0 end) +
SUM(case when Nov > 0 then Nov else 0 end) +
SUM(case when Dec > 0 then Dec else 0 end) +
SUM(case when Jan > 0 then Jan else 0 end) +
SUM(case when Feb > 0 then Feb else 0 end) +
SUM(case when Mar > 0 then Mar else 0 end) +
SUM(case when Apr > 0 then Apr else 0 end) +
SUM(case when May > 0 then May else 0 end) +
SUM(case when Jun > 0 then Jun else 0 end) +
SUM(case when Jul > 0 then Jul else 0 end) +
SUM(case when Aug > 0 then Aug else 0 end) +
SUM(case when Sep > 0 then Sep else 0 end) as 'Sum of positive months'
答案 0 :(得分:2)
你可以直截了当地做到:
select * from tablename
where (Oct < 0 and Nov < 0) or
(Nov < 0 and Dec < 0) or
(Dec < 0 and Jan < 0) or
(Jan < 0 and Feb < 0) or
...