我遇到了一个小问题,希望得到任何帮助。
我的表是这样的:
var count = 0
// DOES NOTHING FOR 1 SEC
setTimeout(function () {
var interval = setInterval(function () {
$("#blinker").toggleClass(function(){
count++
return "blink"
});
// Reason it is 8 because it is counting the fadeIn and FadeOut
if (count == 8)
clearInterval(interval);
}, 600)
}, 1000);
我的目标是CASH | CREDIT CARD | DEBIT CARD | ACCOUNT | OTHER
-------------------------------------------------
0.00 0.00 0.00 0.00 0.00
1.00 0.00 0.00 0.00 0.00
2.00 1.00 0.00 0.00 0.00
上述任何有多列的行> 0
因此,在此场景中将使用上表选择第三行。
答案 0 :(得分:10)
SELECT
[CASH], [CREDIT CARD], [DEBIT CARD], [ACCOUNT], [OTHER]
FROM table
WHERE
CASE WHEN [CASH] > 0 THEN 1 ELSE 0 END+
CASE WHEN [CREDIT CARD] > 0 THEN 1 ELSE 0 END+
CASE WHEN [DEBIT CARD] > 0 THEN 1 ELSE 0 END+
CASE WHEN [ACCOUNT] > 0 THEN 1 ELSE 0 END+
CASE WHEN [OTHER] > 0 THEN 1 ELSE 0 END >= 2
答案 1 :(得分:1)
我更喜欢t-clausen's answer但是作为练习,我决定将其作为UNPIVOT
后跟PIVOT
进行尝试,以便我们可以使用更多常规SQL工具编写它:
declare @t table (SomeID int,Cash money,Credit money,Debit money,Account money,Other money)
insert into @t(SomeID,Cash,Credit,Debit,Account,Other) values
(1,0.00,0.00,0.00,0.00,0.00),
(2,1.00,0.00,0.00,0.00,0.00),
(3,2.00,1.00,0.00,0.00,0.00)
;With Unpiv as (
select *,SUM(CASE WHEN MoneyValue > 0.00 THEN 1 ELSE 0 END) OVER (PARTITION BY SomeID) as cnt
from @t t
unpivot (MoneyValue for MoneyType in (Cash,Credit,Debit,Account,Other)) x
), Repiv as (
select *
from Unpiv u
pivot (SUM(MoneyValue) for MoneyType in (Cash,Credit,Debit,Account,Other)) x
where
cnt >= 2
)
select * from Repiv
这假设您有另一列(此处称为SomeID
),通过该列可以唯一标识每一行。
结果:
SomeID cnt Cash Credit Debit Account Other
----------- ----------- --------------------- --------------------- --------------------- --------------------- ---------------------
3 2 2.00 1.00 0.00 0.00 0.00
希望上述内容可能更适合某些需求变体。
答案 2 :(得分:1)
SELECT
[CASH], [CREDIT CARD], [DEBIT CARD], [ACCOUNT], [OTHER]
FROM table
WHERE (
SELECT COUNT(*)
FROM (VALUES ([CASH]),([CREDIT CARD]),([DEBIT CARD]),([ACCOUNT]),([OTHER])) t(value)
WHERE value > 0
) >= 2