SQLITE两次查询同一个表并将这些查询放入两个不同的列中?

时间:2015-07-20 13:55:55

标签: sqlite

我的问题是如何做两个select语句并将结果放在单独的colummns中?

例如,

我的陈述是

Select count(*) as "JAN" from tickets where created_at BETWEEN "2015-01-01" AND "2015-01-31"
Select count(*) as "FEB" from tickets where created_at BETWEEN "2015-02-01" AND "2015-02-25"

我尝试过放置一个逗号并将第二个语句放在括号中,但这也没有用。我不确定我到底做错了什么。谁能看看这个?感谢您提前的时间。

2 个答案:

答案 0 :(得分:1)

我认为您甚至不需要加入,因为SQLite支持CASE expression

select
sum(case 
    when created_at between '2015-01-01' and '2015-01-31' then 1
    else 0 end) as "JAN",
sum(case
    when created_at between '2015-02-01' and '2015-02-25' then 1
    else 0 end) as "FEB"
from tickets

答案 1 :(得分:1)

您可以将SELECT放入标量子查询中:

SELECT (SELECT COUNT(*)
        FROM tickets
        WHERE created_at GLOB '2015-01*-') AS "JAN",
       (SELECT COUNT(*)
        FROM tickets
        WHERE created_at GLOB '2015-02-*') AS "FEB";