| 68 | |
| 69 | |
| 70 | == Advanced Reports: Dynamic Variables == |
| 71 | For more flexible reports, Trac supports the use of ''dynamic variables'' in report SQL statements. |
| 72 | In short, dynamic variables are ''special'' strings that are replaced by custom data before query execution. |
| 73 | |
| 74 | === Using Variables in a Query === |
| 75 | The syntax for dynamic variables is simple, any upper case word beginning with '$' is considered a variable. |
| 76 | |
| 77 | Example: |
| 78 | {{{ |
| 79 | SELECT id AS ticket,summary FROM ticket WHERE priority='$PRIORITY' |
| 80 | }}} |
| 81 | |
| 82 | To assign a value to $PRIORITY when viewing the report, you must define it as an argument in the report URL, leaving out the the leading '$'. |
| 83 | |
| 84 | Example: |
| 85 | {{{ |
| 86 | http://projects.edgewall.com/trac/reports/14?PRIORITY=high |
| 87 | }}} |
| 88 | |
| 89 | |
| 90 | === Special/Constant Variables === |
| 91 | There is one ''magic'' dynamic variable to allow practical reports, its value automatically set without having to change the URL. |
| 92 | |
| 93 | * $USER -- Username of logged in user. |
| 94 | |
| 95 | Example (''List all tickets assigned to me''): |
| 96 | {{{ |
| 97 | SELECT id AS ticket,summary FROM ticket WHERE owner='$USER' |
| 98 | }}} |
| 99 | |
| 100 | |
| 101 | ---- |
| 102 | |
143 | | |
144 | | ---- |
145 | | |
146 | | == Sample Reports == |
147 | | '''status and summary for all tickets''' |
148 | | |
149 | | {{{ |
150 | | SELECT id as ticket, status, summary FROM ticket |
151 | | }}} |
152 | | |
153 | | ---- |
154 | | '''all active tickets, sorted by priority and time''' |
155 | | |
156 | | {{{ |
157 | | SELECT id AS ticket, status, severity, priority, owner, |
158 | | time as created, summary FROM ticket |
159 | | WHERE status IN ('new', 'assigned', 'reopened') |
160 | | ORDER BY priority, time |
161 | | }}} |
162 | | |
163 | | ---- |
164 | | '''active tickets, grouped by milestone and sorted by priority''' |
165 | | |
166 | | {{{ |
167 | | SELECT id AS ticket, milestone, status, severity, |
168 | | priority, component, owner, summary |
169 | | FROM ticket |
170 | | WHERE status IN ('new', 'assigned', 'reopened') |
171 | | ORDER BY milestone, |
172 | | (CASE priority |
173 | | WHEN 'highest' THEN 0 |
174 | | WHEN 'high' THEN 1 |
175 | | WHEN 'normal' THEN 2 |
176 | | WHEN 'low' THEN 3 |
177 | | ELSE 4 |
178 | | END), severity, time |
179 | | }}} |
180 | | |
181 | | |