sql合并两个查询并截断日期
我正在尝试截断日期以仅获取年/月,而不是它们的形式,即年/月/日.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在客户端代码中连接年/月,这样您就可以使用“2010 年 7 月”
(我无法在 SQLLite 中进行测试,抱歉,但是除了输出年/月格式之外,这应该很接近)
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)
它可能涉及子查询中的联合和日期格式,针对 SQLLite 重新格式化:
It could involve a union in a subquery and a format on the date, reformatted for SQLLite: