问题
I have trac installed for managing my online projects. I have also installed a date plugin DateFieldPlugin in it. I am unable to show this date filed in the custom reports.
The following code generates the normal report.
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,reporter AS _reporter
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
and I want something like this:
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter, c.value AS Duedate
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority', ticket_custom c
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
The above code is showing me the due date column though it is showing one task row as multiple times.
Can anybody help me in this?
回答1:
Derek is quit there. Nevertheless, here is the working one:
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter, c.value AS Duedate
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT OUTER JOIN ticket_custom c ON t.id = c.ticket AND c.name = 'due_date'
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
Only you will have to replace the c.name value to match with your date fields name, if it's not 'due_date' like in my configuration.
Background: The 'ticket_custom' table definition is just one key-value-pair per row, as the call for the schema reveals:
sqlite> .schema ticket_custom
CREATE TABLE ticket_custom (
ticket integer,
name text,
value text,
UNIQUE (ticket,name)
);
Please note the primary key is not just the ticket number, but ticket (number) and (custom column) name combined. Hence an additional WHERE clause is required on the JOIN or you'll still get multiple rows - one for each custom field of all matching tickets. This is in contrast to the main table 'ticket', where all information for one ticket is in a single row.
回答2:
You aren't specifying a join condition on ticket_custom
. Therefore, it's doing a CROSS JOIN
, which joins every record from ticket_custom
to every record in the rest of your query. So if your ticket left join enum
originally returned 100 rows, and ticket_custom
has 20, you'll have 2000 resulting rows in your 2nd query.
You need to determine how ticket_custom relates to the other tables and provide an appropriate predicate. Most likely, it's something like this:
SELECT p.value AS __color__,
owner AS __group__,
id AS ticket, severity, summary, component, milestone, t.type AS type, time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter, c.value AS Duedate
FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT JOIN ticket_custom c ON t.id = c.ticket_id
WHERE status = 'assigned'
ORDER BY owner, p.value, t.type, time
(See new LEFT JOIN line). The columns are a guess - you'll need to substitute whichever columns correctly relate the 2 tables.
来源:https://stackoverflow.com/questions/7120472/how-to-show-due-date-column-in-the-trac-reports