sql合并两个查询并截断日期

发布于 2024-09-09 07:32:18 字数 589 浏览 6 评论 0原文

我正在尝试截断日期以仅获取年/月,而不是它们的形式,即年/月/日.time
我想要做的是计算每月销售的所有汽车和每月销售的所有 SUV,类似于:

// counts cars    
select SellDate, count(*) 
from category 
where machineIdentification = 1 
GROUP BY SellDate  

// counts suv's  
select SellDate, count(*) 
from category 
where machineIdentification = 2 
GROUP BY SellDate  

单独运行每个查询会给出一个日期列表(年/月/日/第二个时间) )和数字 1,因为在该时间只有 1 辆汽车或 SUV 售出,但是我尝试按 SellDate 进行分组并截断日期,因此它只显示每个月的总数。

我想做的是结合查询并最终得到如下值:

2009-01 23 10  
2009-02 13 14  
2009-03 29 7  

第一列是年份。月份,第二列是销售的汽车数量,第三列是销售的SUV数量

I am trying to truncate dates to only get year/month as opposed to the form they are in which is year/month/day.time
What I want to do is count all of the cars that sold each month and all of the suvs that sold each month, having something like:

// counts cars    
select SellDate, count(*) 
from category 
where machineIdentification = 1 
GROUP BY SellDate  

// counts suv's  
select SellDate, count(*) 
from category 
where machineIdentification = 2 
GROUP BY SellDate  

Separately running each query gives me a list of dates (y/m/d/time to the second) and the number 1 because only 1 car or suv sold that exact time, however I am trying to group by SellDate and truncate the date so it only shows me the total numbers each month.

what I am trying to do is combine the queries and end up with values like so:

2009-01 23 10  
2009-02 13 14  
2009-03 29 7  

With the first column being the year.month, the second being # of cars sold and the third being # of suv's sold

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-09-16 07:32:18
select
   date(SellDate, 'start of month'),
   SUM (CASE machineIdentification = 1 THEN 1 ELSE 0 END) AS carcount ,
   SUM (CASE machineIdentification = 2 THEN 1 ELSE 0 END) AS suvcount 
from category 
where machineIdentification IN (1, 2 )
GROUP BY date(SellDate, 'start of month')

我会在客户端代码中连接年/月,这样您就可以使用“2010 年 7 月”

(我无法在 SQLLite 中进行测试,抱歉,但是除了输出年/月格式之外,这应该很接近)

select
   date(SellDate, 'start of month'),
   SUM (CASE machineIdentification = 1 THEN 1 ELSE 0 END) AS carcount ,
   SUM (CASE machineIdentification = 2 THEN 1 ELSE 0 END) AS suvcount 
from category 
where machineIdentification IN (1, 2 )
GROUP BY date(SellDate, 'start of month')

I'd concatenate the year/month in the client code so you can have "July 2010" for example

(I can't test in SQLLite, sorry, but this should be close except for output year/month format)

乖乖兔^ω^ 2024-09-16 07:32:18

它可能涉及子查询中的联合和日期格式,针对 SQLLite 重新格式化:

SELECT SellDate, COUNT(CARS) AS Cars, COUNT(SUVS) AS SUVS
FROM         
 (SELECT STRFTIME('%Y-%m',SellDate) AS SellDate,
         MachineIdentification AS CARS, null AS SUVS
     FROM          Category
     where machineIdentification = 1 
     UNION
     SELECT STRFTIME('%Y-%m',SellDate) AS SellDate, 
         null AS CARS, MachineIdentification AS SUVS
     FROM Category
     where machineIdentification = 2 
  )
GROUP BY SellDate

It could involve a union in a subquery and a format on the date, reformatted for SQLLite:

SELECT SellDate, COUNT(CARS) AS Cars, COUNT(SUVS) AS SUVS
FROM         
 (SELECT STRFTIME('%Y-%m',SellDate) AS SellDate,
         MachineIdentification AS CARS, null AS SUVS
     FROM          Category
     where machineIdentification = 1 
     UNION
     SELECT STRFTIME('%Y-%m',SellDate) AS SellDate, 
         null AS CARS, MachineIdentification AS SUVS
     FROM Category
     where machineIdentification = 2 
  )
GROUP BY SellDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文