假设,2017年5月,我想获得2017年4月的记录。
我的查询会记录2017年4月的记录,但我也希望包含空日期。
因此,所需的输出将如下:
----------------------
id | date | count
----------------------
1 | 01/04/17 | 0 // 0 because this dates does not have any tickets.
2 | 02/04/17 | 0 // 0 because this dates does not have any tickets.
3 | 03/04/17 | 0 // 0 because this dates does not have any tickets.
4 | 04/04/17 | 0 // 0 because this dates does not have any tickets.
5 | 05/04/17 | 0 // 0 because this dates does not have any tickets.
6 | 06/04/17 | 0 // 0 because this dates does not have any tickets.
7 | 07/04/17 | 0 // 0 because this dates does not have any tickets.
8 | 08/04/17 | 0 // 0 because this dates does not have any tickets.
直到30/04/2017
15/04/17
显示2个计数,因为它包含2个记录,我的查询也说明了这一点
但
如何从01/04/17
- 30/04/2017
查询是:
Ticket.includes(:line_items).where('tickets.created_at > ? AND
tickets.created_at < ?', Date.today.last_month.beginning_of_month,
Date.today.beginning_of_month).where.not(line_items: {id: nil}).count
输出是:
SELECT COUNT(DISTINCT "tickets"."id") FROM "tickets" LEFT OUTER JOIN
"line_items" ON "line_items"."ticket_id" = "tickets"."id" WHERE
(tickets.created_at > '2017-04-01' AND tickets.created_at < '2017-05-01') AND
("line_items"."id" IS NOT NULL)
所需的输出应显示如下记录:
同样,如果用户选择过去7天,它也应显示最近7天的记录。
应对它的任何解决方法?
答案 0 :(得分:1)
假设您将用户输入存储在名为“no_of_days”的变量中。你可以做点什么
data = Ticket.includes(:line_items).where('tickets.created_at > ? AND tickets.created_at < ?', Time.now.beginning_of_day - (no_of_days).days , Time.now.beginning_of_day).where.not(line_items: {id: nil}) #retrieves all data in a single query
(0..no_of_days).map do |day_from_now|
todays_data = data.select{|item| item.created_at < (Time.now.beginning_of_day - (day_from_now).days) && item.created_at > (Time.now.beginning_of_day - (day_from_now - 1).days)}
return { "day": (Date.today - day_from_now.days).strftime("%A %d-%m"),
"ticket_count": todays_data.size,
"total_sales": todays_data.inject(0){|sum, item| sum + item.line_item.quantity * item.line_item.rate }
end
答案 1 :(得分:1)
在 SQL-Server 中,您可以实现以下目标:
DECLARE @MinDate DATE = '20170401',
@MaxDate DATE = '20170430';
SELECT t.Id, d.[Date], COALESCE(t.[Count],0) AS [Count]
FROM (
SELECT TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
) d
LEFT JOIN yourTbl AS t ON d.Date = t.Date
答案 2 :(得分:1)
回答PostgreSQL,但不是RoR,因为我不知道:
SELECT dat::date, count(DISTINCT tic."id")
FROM generate_series('2017-04-01'::date, '2017-04-30'::date, interval '1 day') AS dat
LEFT JOIN (SELECT "tickets"."id", "tickets"."created_at"
FROM "tickets"
JOIN "line_items"
ON "line_items"."ticket_id" = "tickets"."id") tic
ON tic."created_at" = dat::date
GROUP BY dat
ORDER BY dat
或
SELECT dat::date, count(DISTINCT "tickets"."id")
FROM generate_series('2017-04-01'::date, '2017-04-30'::date, interval '1 day') AS dat
LEFT JOIN "tickets" ON "tickets"."created_at" = dat::date
AND EXISTS(SELECT 1 FROM "line_items"
WHERE "line_items"."ticket_id" = "tickets"."id" LIMIT 1)
GROUP BY dat
ORDER BY dat
generate_series
创建的行集从param1
到param2
,间隔为param3
。