LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2019, 01:24 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
SQlite3 - Select showing all cols with sub total after each category group


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.
Attached Files
File Type: txt Actual.txt (1.5 KB, 53 views)
File Type: txt Target.txt (691 Bytes, 51 views)
 
Old 12-12-2019, 02:01 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
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
UNION ALL
SELECT date, "" as description, category, SUM(amount) FROM rdata GROUP BY category ORDER BY category
I have not actually run this in SQLite but it should support aliasing in the results set. Because you are ordering by category you will need to retain it to keep the total with the items it is total of, perhaps adding an ordering clause to keep the empty description row after non-empty rows.

Last edited by astrogeek; 12-12-2019 at 02:17 PM. Reason: added comment
 
Old 12-12-2019, 06:41 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,734

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Maybe
Code:
SELECT date, description, category, amount FROM rdata
UNION ALL
SELECT " ", " " , category as "Sub Total", SUM(amount) FROM rdata GROUP BY category ORDER BY category
also untested.
 
Old 12-12-2019, 09:06 PM   #4
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks folks,

We're getting there.

I tried this query and got: see attachment getttingThere.png

Code:
SELECT date, description, category, amount FROM rdata
UNION ALL
SELECT " ", " " , category as "Sub Total", SUM(amount) FROM rdata GROUP BY category ORDER BY category
I don't understand why the alias 'Sub Total' didn't work. However, just having the first two fields blank helps.
Any other ideas?
Attached Thumbnails
Click image for larger version

Name:	gettingThere.png
Views:	75
Size:	80.2 KB
ID:	32089  
 
Old 12-12-2019, 09:53 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,734

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Sorry. The alias sets the column heading.
Try
Code:
SELECT date, description, category, amount FROM rdata
UNION ALL
SELECT " ", " " , "Sub Total", SUM(amount) FROM rdata GROUP BY category ORDER BY category
Still not tested...not even at my desktop right now.
 
Old 12-12-2019, 09:58 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by pizzipie View Post
Thanks folks,

We're getting there.

I tried this query and got: see attachment getttingThere.png

Code:
SELECT date, description, category, amount FROM rdata
UNION ALL
SELECT " ", " " , category as "Sub Total", SUM(amount) FROM rdata GROUP BY category ORDER BY category
I don't understand why the alias 'Sub Total' didn't work. However, just having the first two fields blank helps.
Any other ideas?
That isn't how alias works, it would be just the reverse of that, like this:

Code:
SELECT date, description, category, amount FROM rdata
UNION ALL
SELECT " ", " " , "Sub Total" as category, SUM(amount) FROM rdata GROUP BY category ORDER BY category
The number and name of the columns must be the same in both queries, so the UNION of each of those results has the same columns. When you alias category as "Sub Total" you are creating a fifth column named "Sub Total" which has the contents of category as its value, but it doesn't show because the first query in the UNION does not include such a column.

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
UNION ALL
SELECT " ", "", category, "Sub Total", SUM(amount) as Total FROM rdata GROUP BY category ORDER BY category, Total
That should work by adding a fifth column, Total, empty for each item but the total amount in the grouped result rows, and last in each group because of the ordering clause. Just in case that turns out to be first in each group change the ordering by using DESC.

Last edited by astrogeek; 12-12-2019 at 10:09 PM.
 
Old 12-13-2019, 06:38 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
You attach 2 files so why not .dump the table?
then we can try it out.
 
Old 12-13-2019, 01:15 PM   #8
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Sorry, great idea,

see attached SQL file showSQL.txt
Attached Files
File Type: txt showSQL.txt (2.4 KB, 28 views)

Last edited by pizzipie; 12-13-2019 at 01:26 PM.
 
Old 12-13-2019, 03:17 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Then this should produce something like you want, including a grand total row:

Code:
SELECT date, description, category, amount 
    FROM rdata
UNION ALL
SELECT " ", "", category||" Sub Total" as category, SUM(amount) as amount 
    FROM rdata GROUP BY category
UNION ALL
SELECT " ", "", 'GRAND TOTAL' as category, SUM(amount) as amount 
    FROM rdata 
ORDER BY category!='GRAND TOTAL' DESC, category;
Note that this is all done using the tip I suggested originally about use of additional ordering clauses to put the subtotals and totals in the proper place. In this query the ORDER BY operates on the UNION of all the queries, i.e. after all the rows have been assembled.

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
+------------+---------------------------------------+--------------------------+----------+
| datex      | description                           | category                 | amount   |
+------------+---------------------------------------+--------------------------+----------+
| 04/03/2019 | Kitchen Sink And Faucet               | appliances               |   438.50 |
| 04/01/2019 | Kitchen Fixtures Stove - Ect          | appliances               |   862.82 |
| 05/17/2019 | Kitchen Lights                        | appliances               |   113.67 |
| 05/03/2019 | Washer Dryer                          | appliances               |  1102.10 |
| 04/21/2019 | Refrigerator And BR Light             | appliances               |  1567.55 |
|            |                                       | appliances Sub Total     |  4084.64 |
| 04/26/2019 | House Cleanup                         | cleaning                 |   130.00 |
|            |                                       | cleaning Sub Total       |   130.00 |
| 11/19/2018 | Gas In Kitchen – Off-  Rm Lr Heater   | demo                     |    99.00 |
| 11/19/2018 | Remove Sinks And Toilets – Baths      | demo                     |   206.85 |
| 11/12/2018 | Remove Wallpaper                      | demo                     |   650.00 |
|            |                                       | demo Sub Total           |   955.85 |
| 08/16/2019 | Outdoor Lights                        | fixtures                 |    88.59 |
| 04/20/2019 | Tub Enclosures                        | fixtures                 |   372.80 |
|            |                                       | fixtures Sub Total       |   461.39 |
| 07/08/2019 | Laminate Flooring                     | floor-covering           | 10973.00 |
| 04/27/2019 | Living Room Rugs                      | floor-covering           |   375.37 |
| 04/13/2019 | Kitchen Rugs                          | floor-covering           |    81.61 |
|            |                                       | floor-covering Sub Total | 11429.98 |
| 07/03/2019 | Guestroom And Master Br               | furniture                |   487.49 |
|            |                                       | furniture Sub Total      |   487.49 |
| 08/16/2019 | Front Door Lock Set                   | hardware                 |   134.51 |
|            |                                       | hardware Sub Total       |   134.51 |
| 05/03/2019 | Window Coverings                      | interior                 |  1300.00 |
| 06/04/2019 | Window Coverings                      | interior                 |  1370.70 |
|            |                                       | interior Sub Total       |  2670.70 |
| 07/19/2019 | Sprinklers On/Tested R&R Spray Units  | irrigation               |   310.00 |
|            |                                       | irrigation Sub Total     |   310.00 |
| 06/17/2019 | Furnish And Install Cedar Mulch       | landscape                |   450.00 |
| 06/17/2019 | Furnish And Install Cedar Mulch       | landscape                |   300.00 |
| 05/08/2019 | Landscape                             | landscape                |   220.00 |
| 04/20/2019 | Clean Up Landscaping From Winter      | landscape                |   400.00 |
| 04/20/2019 | Landscape                             | landscape                |   400.00 |
| 10/07/2018 | Landscape – Rmv Trees – Misc          | landscape                |   410.00 |
| 06/24/2019 | Remove Vegetation At Elec Trans       | landscape                |   526.00 |
| 06/22/2019 | Furnish And Install Cedar Mulch       | landscape                |   500.00 |
|            |                                       | landscape Sub Total      |  3206.00 |
| 09/23/2019 | Garage Door                           | maint                    |   445.60 |
| 08/22/2019 | Garage Door                           | maint                    |    83.19 |
| 08/23/2019 | House Cleanup                         | maint                    |   100.00 |
| 04/25/2019 | House Cleanup                         | maint                    |   130.00 |
|            |                                       | maint Sub Total          |   758.79 |
| 08/21/2019 | TV Cabinet Materials                  | materials                |   110.09 |
|            |                                       | materials Sub Total      |   110.09 |
| 01/07/2019 | Misc                                  | misc                     |     8.63 |
| 05/23/2019 | Misc                                  | misc                     |    63.88 |
| 05/04/2019 | Misc                                  | misc                     |    22.29 |
| 02/09/2019 | Misc                                  | misc                     |     5.65 |
| 01/12/2019 | Misc                                  | misc                     |    25.55 |
| 01/11/2019 | Misc                                  | misc                     |     3.05 |
|            |                                       | misc Sub Total           |   129.05 |
|            |                                       | GRAND TOTAL              | 24868.49 |
+------------+---------------------------------------+--------------------------+----------+
52 rows in set, 1 warning (0.00 sec)

Last edited by astrogeek; 12-13-2019 at 04:06 PM. Reason: Broke SQL lines for clarity
 
Old 12-13-2019, 06:52 PM   #10
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Running your query with copy & paste to avoid typos got this result from sqlite:

Quote:
1st ORDER BY term does not match any column in the result set: SELECT date, description, category, amount
FROM show <== note: I created a new small database with a table called show for simplicity
UNION ALL
SELECT " ", "", category||" Sub Total" as category, SUM(amount) as amount
FROM show GROUP BY category
UNION ALL
SELECT " ", "", 'GRAND TOTAL' as category, SUM(amount) as amount
FROM show
ORDER BY category!='GRAND TOTAL' DESC, category;
I don't understand the underlined bold pieces of the query. Is this documented somewhere that I could read about?

Your result is exactly what I am looking for. Nice job. Now have to get working in SQlite.
 
Old 12-13-2019, 08:38 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
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:
A string constant is formed by enclosing the string in single quotes ('). A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal. C-style escapes using the backslash character are not supported because they are not standard SQL.
Try that and let me know if it works.

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.

Last edited by astrogeek; 12-13-2019 at 09:14 PM.
 
Old 12-14-2019, 05:10 PM   #12
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Almost there. All the data is there so I could mamipulate it with PHP so Grand Total is at the bottom.

Quote:
Sat 14 Dec 2019 15:43:32

CREATE TABLE `show` (
`Id` INTEGER PRIMARY KEY AUTOINCREMENT,
`Date` TEXT NOT NULL,
`Description` TEXT NOT NULL,
`Category` TEXT NOT NULL,
`Amount` REAL
);

SELECT date, description, category, amount FROM show
UNION ALL
SELECT " ", " ", category || " Sub Total" as category, SUM(amount) as amount FROM show GROUP BY category
UNION ALL
SELECT " ", " ", 'GRAND TOTAL' as category, SUM(amount) as amount FROM show ORDER BY category


Date Description Category Amount
---------- ----------- ----------- ----------
GRAND TOTAL 24868.49
04/03/2019 Kitchen Sin appliances 438.5
04/21/2019 Refrigerato appliances 1567.55
05/03/2019 Washer Drye appliances 1102.1
05/17/2019 Kitchen Lig appliances 113.67
04/01/2019 Kitchen Fix appliances 862.82
appliances 4084.64
04/26/2019 House Clean cleaning 130.0
cleaning 130.0
11/12/2018 Remove Wall demo 650.0
11/19/2018 Remove Sink demo 206.85
11/19/2018 Gas In Kitc demo 99.0
demo Sub 955.85 <------ DB browser puts out Sub Total[/B]
04/20/2019 Tub Enclosu fixtures 372.8 apparently .column produces short column and the rest is truncated
08/16/2019 Outdoor Lig fixtures 88.59
fixtures 461.39
04/13/2019 Kitchen Rug floor-cover 81.61
04/27/2019 Living Room floor-cover 375.37
07/08/2019 Laminate Fl floor-cover 10973.0
floor-cover 11429.98
07/03/2019 Guestroom A furniture 487.49
furniture 487.49
08/16/2019 Front Door hardware 134.51
hardware 134.51
05/03/2019 Window Cove interior 1300.0
06/04/2019 Window Cove interior 1370.7
interior 2670.7
07/19/2019 Sprinklers irrigation 310.0
irrigation 310.0 <------------
10/07/2018 Landscape – landscape 410.0
04/20/2019 Landscape landscape 400.0
04/20/2019 Clean Up La landscape 400.0
05/08/2019 Landscape landscape 220.0
06/17/2019 Furnish And landscape 300.0
06/17/2019 Furnish And landscape 450.0
06/22/2019 Furnish And landscape 500.0
06/24/2019 Remove Vege landscape 526.0
landscape 3206.0 <----------
04/25/2019 House Clean maint 130.0
08/23/2019 House Clean maint 100.0
08/22/2019 Garage Door maint 83.19
09/23/2019 Garage Door maint 445.6
maint Sub 758.79
08/21/2019 TV Cabinet materials 110.09
materials 110.09 < ------------
01/07/2019 Misc misc 8.63
01/11/2019 Misc misc 3.05
01/12/2019 Misc misc 25.55
02/09/2019 Misc misc 5.65
05/04/2019 Misc misc 22.29
05/23/2019 Misc misc 63.88
misc Sub 129.05
 
Old 12-16-2019, 07:36 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Reckon this could be what you need, maybe, if your brain doesn't explode

https://sqlite.org/lang_with.html

edit: the recursive bit
 
Old 12-16-2019, 10:27 AM   #14
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks all, for your time in helping me!!!!

R
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] (Python 3.4.3,SQLite3):SELECT command returns 'no such column' error A/S-H Programming 14 04-28-2017 07:58 AM
Shell Script to compare folders,Sub-Folders and Sub-Sub-Folders unix_72427 Programming 8 08-08-2012 02:51 PM
Multi booting sub category under Linux Newbie inspiron_Droid LQ Suggestions & Feedback 9 05-28-2008 08:19 PM
php and sqlite3 can select but not insert file is set rw gruessle Programming 2 01-26-2007 05:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration