简体   繁体   English

将多个sql语句合并到列中

[英]Combining multiple sql statements into columns

I'm pretty new to SQL and I'm running something like this to pull out some data. 我对SQL还是很陌生,我正在运行类似的东西来提取一些数据。 But Currently I am running them one statement at a time and copying them to excel. 但是目前,我一次只运行一次它们,然后将它们复制到excel。 Is there a way to run all these together? 有没有办法将所有这些一起运行? Level would be the same for each statement, but count(ID) will be different. 每个语句的级别相同,但是count(ID)不同。 There might also be some days where a level does not have any data. 可能还有些日子,某个级别没有任何数据。

select 
    count(ID), level 
from table1 
where createat >= "2013-11-22 00:00:00" and createat <= "2013-11-22 23:59:59" 
group by level;

select 
    count(ID), level 
from table1 
where createat >="2013-11-23 00:00:00" and createat <= "2013-11-23 23:59:59" 
group by level;

select 
    count(ID), level 
from table1 
where createat >= "2013-11-24 00:00:00" and createat <= "2013-11-24 23:59:59" 
group by level;

Thanks for the help! 谢谢您的帮助! I'm hoping to learn more about how to write these. 我希望了解更多有关如何编写这些内容的信息。

SELECT
  level,
  COUNT(CASE WHEN createat>='2013-11-22' and createat<'2013-11-23' THEN id ELSE NULL END)   AS day_1_count,
  COUNT(CASE WHEN createat>='2013-11-23' and createat<'2013-11-24' THEN id ELSE NULL END)   AS day_2_count,
  COUNT(CASE WHEN createat>='2013-11-24' and createat<'2013-11-25' THEN id ELSE NULL END)   AS day_3_count
FROM
  table1
GROUP BY
  level
WHERE
  createat>='2013-11-22' and createat<'2013-11-25'

Although, it would probably make more sense to join on a table with the dates that you need, rather than hard code them. 虽然,将表与所需的日期连接起来而不是对它们进行硬编码可能更有意义。

Also, note that SQL works on a fixed number of columns, this does not work if you want one query for 3 days and then to reuse it for 5 days. 另外,请注意,SQL适用于固定数量的列,如果您希望一次查询3天然后重用5天,则此方法将不起作用。 For that you need to keep it as one result per Row. 为此,您需要将其保留为每行一个结果。

Try this: 尝试这个:

SELECT t.level, SUM(CASE WHEN DATE(createat) = '2013-11-22' THEN 1 ELSE 0 END) AS day1,
       SUM(CASE WHEN DATE(createat) = '2013-11-23' THEN 1 ELSE 0 END) AS day2,
       SUM(CASE WHEN DATE(createat) = '2013-11-24' THEN 1 ELSE 0 END) AS day3
FROM table1 t
WHERE DATE(t.createat) >= "2013-11-22" AND DATE(t.createat) <= "2013-11-24" 
GROUP BY t.level;

OR 要么

SELECT t.level, SUM(CASE WHEN t.createat >= "2013-11-22 00:00:00" AND t.createat <= "2013-11-22 23:59:59" THEN 1 ELSE 0 END) AS day1,
       SUM(CASE WHEN t.createat >= "2013-11-23 00:00:00" AND t.createat <= "2013-11-23 23:59:59" THEN 1 ELSE 0 END) AS day2,
       SUM(CASE WHEN t.createat >= "2013-11-24 00:00:00" AND t.createat <= "2013-11-24 23:59:59" THEN 1 ELSE 0 END) AS day3
FROM table1 t
WHERE t.createat >= "2013-11-22 00:00:00" AND t.createat <= "2013-11-24 23:59:59" 
GROUP BY t.level;

The simple edit to the query you posted is: 对您发布的查询的简单编辑是:

select count(ID), level 
from table1 
where createat >= "2013-11-22 00:00:00" and createat <= "2013-11-22 23:59:59" 
group by level
union
select count(ID), level 
from table1 
where createat >="2013-11-23 00:00:00" and createat <= "2013-11-23 23:59:59" 
group by level
union
select count(ID), level 
from table1 
where createat >= "2013-11-24 00:00:00" and createat <= "2013-11-24 23:59:59" 
group by level

However if you are trying to get the count, level value and date for each day then you could use a query like this: 但是,如果您尝试获取每天的计数,级别值和日期,则可以使用如下查询:

SELECT COUNT(*), level, CAST(createat AS DATE)
FROM table1
GROUP BY level, CAST(createat AS DATE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM