[英]SQL query with case and different conditions
I have a SQL database with a table looking more or less like this one: 我有一个SQL数据库,其中的表或多或少看起来像这样:
channel type I1 I2
1 A 34 10
2 A 27 9
3 B 9 21
1 A 29 11
2 B 8 19
3 A 27 9
1 B 11 20
2 B 9 22
3 A 31 8
I would like to query the table using different criteria in case of type=A
or type=B
, however I would like to group the results by channel, showing the total hits. 在type=A
或type=B
情况下,我想使用不同的条件查询表,但是我想按渠道对结果进行分组,以显示总点击数。
My last approach to the problem is this: (not working) 我对这个问题的最后解决方法是:(不起作用)
SELECT channel, count(channel)
case
when type="A" then
WHERE I1>30 and I2<10
when type="B" then
WHERE I1>10 and I2<20
end
from channelResults
group by channel
The expected output would be so simple as: 预期的输出将非常简单:
channel count
1 9
2 20
3 3
So as an example, the output would show that in channel 1 there are 9 rows (type A or type B it does not matter) that satisfy each condition, that means, for example we have 4 typeA that satisfy condition for typeA + 5 typeB that satisfy condition for typeB, so in total 9. 因此,例如,输出将显示通道1中有9个行(分别是A型或B型)可以满足每个条件,这意味着,例如,我们有4个typeA满足typeA + 5 typeB满足类型B的条件,所以总共9。
Try this 尝试这个
select channel,
count(channel)
from channelResults
where type = 'A' and I1 > 30 and I2 < 10 or
type = 'B' and I1 > 10 and I2 < 20
group by channel
but still i'm not sure if this is what you want as output, if it is not please be more specific. 但我仍然不确定这是否是您想要的输出,如果不是,请更具体。
SELECT channel, --count(channel)
case when type="A" and I1>30 and I2<10 then count(channel)
when type="B" and I1>10 and I2<20 then count(channel)
end as count
from channelResults
group by channel
There's multiple ways to write this. 有多种写方法。 One way would be to split the query into 2 parts as that's really what your problem states -- one set of criteria for A, another for B. So following that line of thinking: 一种方法是将查询分为两部分,因为这实际上就是您的问题所陈述的内容-一组针对A的条件,另一组针对B的条件。因此遵循以下思路:
select channel, count(channel) from (
--countables type A
select * from channelResults where type='A' and I1>30 and I2<10
union all
--countables type B
select * from channelResults where type='B' and I1>10 and I2<20
) data
GROUP BY data.channel
Another way would be to output the count value (0 or 1) using a case, a bit like what you were doing, and then count (or sum) that colum: 另一种方法是使用某种情况输出计数值(0或1),有点像您正在做的事情,然后对那个列进行计数(或求和):
SELECT
channel,
count( --or sum()
CASE
WHEN [type]='A' and I1>30 and I2<10 then 1
WHEN [type]='B' and I1>10 and I2<20 then 1
ELSE NULL
END
) CountValue
FROM channelResults
GROUP BY channel
If this is a bit hard to check/decipher, you could also do this in multiple steps: 如果很难检查/解密,也可以分多个步骤进行:
WITH count_data AS (
SELECT
*,
CASE
WHEN [type]='A' and I1>30 and I2<10 then 1
WHEN [type]='B' and I1>10 and I2<20 then 1
ELSE NULL
END AS CountValue
FROM channelResults
)
SELECT channel, COUNT(CountValue) CountValue
FROM count_data
GROUP BY channel
You can then initially write the bottom query as: 然后,您可以首先将底部查询写为:
select * from count_data
Then once you're happy with how the raw data and how case works, you can then group it as I showed above. 然后,一旦您对原始数据的方式和案例的工作方式感到满意,就可以将其分组,如上所示。
Hope that helps. 希望能有所帮助。
Here is a fiddle to play around with ... I was testing my SQL while all the other answered ;) 这是一个小玩意儿...我正在测试我的SQL,而其他所有答案都;)
http://sqlfiddle.com/#!9/fd13e/3 http://sqlfiddle.com/#!9/fd13e/3
You can try this:- 您可以尝试以下方法:
SELECT channel, count(case when type="A" and I1>30 and I2<10 then channel end as count_channel
when type="B" and I1>10 and I2<20 then channel end as count_channel)
from channelResults
group by channel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.