简体   繁体   English

在查询结果中添加空日期行

[英]Add empty date row to query result

There are 6 kind of ticket types. 门票类型有6种。 User will enter starting date and ending date. 用户将输入开始日期和结束日期。 Then I have to show how many each type of tickets are on each days between those 2 date. 然后,我必须显示在这两个日期之间的每一天中每种类型的票数。

ticket table: id (int), type_id (int), created_at (date), and more fields 工单表: id(int),type_id(int),created_at(日期)和更多字段

Input: date_begin, date_end 输入: date_begin,date_end
Output: rows > dates. 输出:行>日期。 columns > date, total_amount, and each types 列>日期,total_amount和每种类型

My current query: 我当前的查询:

SELECT
    DATE(ticket.created_date) as date,
    SUM(IF(ticket.created_date, 1, 0)) as total,
    SUM(IF(ticket.type_id is Null, 1, 0)) as total0,
    SUM(IF(ticket.type_id = 1, 1, 0)) as total1,
    SUM(IF(ticket.type_id = 2, 1, 0)) as total2,
    SUM(IF(ticket.type_id = 3, 1, 0)) as total3,
    SUM(IF(ticket.type_id = 4, 1, 0)) as total4,
    SUM(IF(ticket.type_id = 5, 1, 0)) as total5
FROM
    ticket
JOIN 
    ticket_type ON ticket_type.id = ticket.type_id
WHERE
    DATE(ticket.created_date) BETWEEN '2015-06-12' AND '2015-06-22'
GROUP BY
    DATE(ticket.created_date)

Current output: 电流输出:

在此处输入图片说明

Desired output: 所需的输出:
在此处输入图片说明

As you see today is 2015-06-19. 如您所见,今天是2015-06-19。 So my database currently have data only until right now (today). 因此,我的数据库目前仅具有数据,直到现在(今天)。 But if user's end_date is in future, i want get rows until that day, and datas will be 0. 但是,如果用户的end_date是将来的日期,则我要在该天之前获取行,并且数据将为0。

First of all you must create the funcion "explodeDates". 首先,您必须创建函数“ explodeDates”。 You have an example of definition on the link on my previous coment. 您在我之前的评论中的链接上有一个定义示例。 However, if you are using SqlServer, you can use this code: 但是,如果使用的是SqlServer,则可以使用以下代码:

CREATE FUNCTION explodeDates(@firstDate DATE, @secondDate DATE)
RETURNS 
@mytable TABLE 
(
    mydate DATE
)
AS
BEGIN
    WHILE @firstDate < @secondDate 
    BEGIN
        INSERT INTO @mytable(mydate) VALUES (@firstDate);

        SET @firstDate = DATEADD(day, 1, @firstdate);
    END

    RETURN 
END
GO

When you had defined that function, you can call it like this: 定义该函数后,可以这样调用它:

SELECT * FROM dbo.explodeDates('01/01/2015', '31/01/2015')

After that you, only have to define your initial query like this: 之后,您只需要像这样定义初始查询即可:

SELECT
    d.mydate as date,
    SUM(IF(ticket.created_date, 1, 0)) as total,
    SUM(IF(ticket.type_id is Null, 1, 0)) as total0,
    SUM(IF(ticket.type_id = 1, 1, 0)) as total1,
    SUM(IF(ticket.type_id = 2, 1, 0)) as total2,
    SUM(IF(ticket.type_id = 3, 1, 0)) as total3,
    SUM(IF(ticket.type_id = 4, 1, 0)) as total4,
    SUM(IF(ticket.type_id = 5, 1, 0)) as total5
FROM
    dbo.explodeDates('12/06/2015', '22/06/2015') d
    LEFT JOIN ticket t ON t.created_date = d.mydate
    LEFT JOIN ticket_type ON ticket_type.id = ticket.type_id

GROUP BY
    d.mydate

Firstly, This has to be validated at the fronted. 首先,这必须在前端进行验证。 Also, if ever such a query comes, I think you output is valid. 另外,如果出现这样的查询,我认为您的输出是有效的。 How can you show tickets of future date (Which are not created). 如何显示将来日期的票证(未创建)。 I assume these are not movie tickets.. :P 我认为这些不是电影票。.:P

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

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