SQlite3 - Select showing all cols with sub total after each category group
2 Attachment(s)
Hi,
In MySql there is a way to do this without too much hassle. See attachment Target.txt. The best I can produce in SQlite3 is: See attachment Actual.txt. Can someone tell me what query I can use to accomplish Target.txt format? Thanks in advance R. |
If I understand your question, you could do that with your existing query by simply changing what you select for those columns in the subtotal query of the UNION, something like this:
Code:
SELECT date, description, category, amount FROM rdata |
Maybe
Code:
SELECT date, description, category, amount FROM rdata |
1 Attachment(s)
Thanks folks,
We're getting there. I tried this query and got: see attachment getttingThere.png Code:
SELECT date, description, category, amount FROM rdata Any other ideas? |
Sorry. The alias sets the column heading.
Try Code:
SELECT date, description, category, amount FROM rdata |
Quote:
Code:
SELECT date, description, category, amount FROM rdata However, if you (correctly) alias "Sub Total" as category, the string "Sub Total" will indeed appear in the category column, but the order by clause will put it elsewhere in the row ordering, as would an empty category field in that row. Hence my original comment that you would need to add some ordering criteria to keep it in place, perhaps adding a separate column for maintaining order. In general, when I need more complex subtotals and totals for some result set for which ROLLUP (not supported by SQLite as far as I know) is not appropriate, I would select the categories and their subtotals into a temporary table with suitable ordering and an auto_increment column, then select the final result with grand total from the temporary table, ordered by the auto_increment column which need not be selected in the final result set. However, using what you have, something like this might work for you: Code:
SELECT date, description, category, amount, "" as Total FROM rdata |
You attach 2 files so why not .dump the table?
then we can try it out. |
1 Attachment(s)
Sorry, great idea,
see attached SQL file showSQL.txt |
Then this should produce something like you want, including a grand total row:
Code:
SELECT date, description, category, amount Here is the result set I get (I did this in mysql with suitable substitutions of concat operator and table/column names). Code:
MariaDB [test]> \. query2.sql |
Running your query with copy & paste to avoid typos got this result from sqlite:
Quote:
Your result is exactly what I am looking for. Nice job. Now have to get working in SQlite. |
The problem with the concat operator may be that I used double quotes in my query. That is acceptable for MySQL, but apparently not for SQLite:
Quote:
As for the order by statement, a quick look at the docs inidcates SQLite may not allow compound expressions in the order by terms, only column names. My order by uses a descending boolean value as the first ordering criteria (category != 'GRAND TOTAL'), so all other rows evaluate to 1, while the last row evaluates to zero. If SQLite does not allow that, then see if you can work out another way to do the same thing. Hint: A purpose-made ordering column which you can de-select with an enclosing SELECT clause. |
Almost there. All the data is there so I could mamipulate it with PHP so Grand Total is at the bottom.
Quote:
|
Reckon this could be what you need, maybe, if your brain doesn't explode :)
https://sqlite.org/lang_with.html edit: the recursive bit |
Thanks all, for your time in helping me!!!!
R |
All times are GMT -5. The time now is 11:27 AM. |