Подсчитайте один и тот же столбец дважды в зависимости от условия - PullRequest
0 голосов
/ 16 июня 2020
• 1000 не используется в том же запросе, где учетная запись fkey равна нулю. Итак, результат будет:
amount2 CountUsed CountUnused   
25      3         1
50      10        0
100     5         2

Спасибо

Ответы [ 3 ]

2 голосов
/ 16 июня 2020

Вы можете использовать условное агрегирование

select Amount as amount2
    , sum( case when  Account_Fkey is not null 
            and Amount is not null then 1 else 0 end)  CountUsed  
    , sum( case when  Account_Fkey is null 
            and Amount is not null then 1 else 0 end)  CountNotUsed  
    from tblGiftCards
group by Amount 
1 голос
/ 16 июня 2020

Я бы упростил logi c и express следующим образом:

select Amount as amount2, count(Account_Fkey) as CountUsed,
       (count(*) - count(Account_Fkey)) as CountNotUsed
from tblGiftCards
where Amount is not null
group by Amount 
1 голос
/ 16 июня 2020

Попробуйте

select tab.amount2 , CountUsed , CountUnUsed   from 
(select Amount as amount2, Count(Amount) as CountUsed  from tblGiftCards
where Account_Fkey is not null
group by Amount 
) tab, 
(select Amount as amount2, Count(Amount) as CountUnUsed  from tblGiftCards
where Account_Fkey is null
group by Amount 
)tab2
where tab.amount2 = tab2.amount2
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...