sql group by with count and add totals row - count

So my situation. Got a table with few thousand entries, I took one column and counted the number of the same entries.
SELECT mycolumn, COUNT(*)
FROM mytable
WHERE myid = 6867
GROUP BY mycolumn
ORDER BY COUNT(*) DESC
Outputs:
6 885
1 715
4 562
5 557
2 232
3 181
I get the desired results. Now all I need is to add a bottom row with a sum of all counted entries.

Typically you would do this in some reporting tool, like SSRS, not SQL.
To do it in SQL, add a UNION statement:
UNION ALL
SELECT 0, COUNT(*)
FROM mytable
WHERE myid = 6867;

Related

SQLITE sum sales by customer and by year

I have a sqlite database with this info
id
pcs
dollars
year
10
25
150
2021
10
20
160
2021
10
22
120
2022
11
12
130
2021
11
10
100
2022
I want to get this
id
pcs2021
dollars2021
pcs2022
dollars2022
10
45
310
22
120
11
12
130
10
100
I got this:
SELECT id, SUM(pcs), SUM(dollars) FROM Table GROUP BY id
But I can't find the way to get the SUM of each year separately.
I tried something like:
SELECT id, (SELECT SUM(pcs) FROM Table WHERE id=id AND year=2021) AS pcs2021, (SELECT SUM(dollars) FROM Table WHERE id=id AND year=2021) AS dollars2021, (SELECT SUM(pcs) FROM Table WHERE id=id AND year=2022) AS pcs2022, (SELECT SUM(dollars) FROM Table WHERE id=id AND year=2022) AS dollars2022, FROM Table GROUP BY id
but it doesn't work.....
Thank you for your help.
Using :-
SELECT id, SUM(pcs) AS pcs2021, SUM(dollars) AS dollars2021 FROM `Table` WHERE year = 2021 GROUP BY id;
results in :-
note the above assumes year is an integer so you may have to enclose the literal in single quotes.
Additional (re comment)
My problem is when I want to put different columns with SUM of values for each year
An issue you are having is that id=id will always be true and thus you will sum all rows, irrespective of the id, for the year and get the result (when the syntax is correct):-
That is id is the id of the sub query when you want to compare against the id currently being processed by the main query.
To do this you can give the main query an alias using the AS clause and then refer to the id of the alias so (where the main query is given the alias of a):-
SELECT
id,
(SELECT sum(pcs) FROM `table` WHERE id=a.id AND year='2021') AS pcs2021,
(SELECT sum(dollars) FROM `table` WHERE id=a.id AND year='2021') AS dollars2021,
(SELECT sum(pcs) FROM `table` WHERE id=a.id AND year='2022') AS pcs2022,
(SELECT sum(dollars) FROM `table` WHERE id=a.id AND year='2022') AS dollars2022
FROM `table` AS a
GROUP BY id
;
The result being :-
Note the use of Table as a table name will result in a syntax error if it is not enclosed as Table is an SQLite keyword.

SQLITE get next row after ORDERBY

I need to get the next row from an ORDERBY query
I have 2 columns, ID(Primary key), Age(float) in a table T and I need something like the following
SELECT ID FROM T WHERE !> (inputted ID) + 1 rowID/Next row <! ORDERBY Age (then primary key, but I suspect if the Age values are the same SQLite would default to order by primary key anyway) LIMIT 1
Essentially it would select the next row after the inputted ID in the ordered table, its the next row / rowID + 1 I am not sure how to get.
As suggested here is a data set as an example
https://dbfiddle.uk?rdbms=sqlite_3.27&fiddle=19685ac20cc42041a59d318a01a2010f
ID Age
1 12.2
2 36.8
3 22.5
4 41
5 16.7
I am attempting to get the the following row from the ordered (by age) list given a specific ID
ID Age
1 12.2
5 16.7
3 22.5
2 36.8
4 41
Something similar to
SELECT ID FROM OrderedInfo WHERE ID = 5 ORDER BY Age ASC LIMIT 1 OFFSET 1;
My expected result would be '3' from the example data above
I have expanded the data set to include duplicate entries as I didn't implicitly state it could have such data - as such forpas answer works for the first example with no duplicate entries - thanks for your help
https://dbfiddle.uk?rdbms=sqlite_3.27&fiddle=f13d7f5a44ba414784547d9bbdf4997e
Use a subquery for the ID that you want in the WHERE clause:
SELECT *
FROM OrderedInfo
WHERE Age > (SELECT Age FROM OrderedInfo WHERE ID = 5)
ORDER BY Age LIMIT 1;
See the demo.
If there are duplicate values in the column Age use a CTE that returns the row that you want and join it to the table so that you expand the conditions:
WITH cte AS (SELECT ID, Age FROM OrderedInfo WHERE ID = 5)
SELECT o.*
FROM OrderedInfo o INNER JOIN cte c
ON o.Age > c.Age OR (o.Age = c.Age AND o.ID > c.ID)
ORDER BY o.Age, o.ID LIMIT 1;
See the demo.

How to query a table with a Where clause that loops through a different table?

I have 2 tables in my database
Table 1
DC Item Day
6006 123 May 1
6006 123 May 2
7036 456 May 6
Table 2
DC Item Day ShippedCases Label Type
6006 123 May 1 100 A
6006 123 May 2 200 A
6006 123 May 2 500 B
7036 456 May 2 300 B
7036 456 May 6 400 A
Table 1 has all the unique records that I am interested in but it doesnt contain ShippedCases or Label Type. How can i write a query to select all the records from Table 2 that match the records in Table 1?
In this case I want to select the DC, Item, Day fields in Table 2 that Match Table 1 and sum the Shipped Cases where Label Type = A.
Result View
DC Item Day Shipped Cases Label Type
6006 123 May 1 100 A
6006 123 May 2 200 A
7036 456 May 6 400 A
I think this is a simple Select statement but I am not where how to setup my where clause with my first 3 parameters being the Table 1 fields and a 4th parameter of Table 2 Label Type = A.
Any advice would be appreciated!
You want to use an INNER JOIN here and aggregation with SUM() and a GROUP BY
SELECT
table1.DC,
table1.Item,
table1.Day,
Sum(table2.ShippedCases) as ShippedCases,
Table2.LabelType
FROM
Table1
INNER JOIN Table2
ON Table1.DC = Table2.DC
AND Table1.Item = Table2.Item
AND Table1.Day = Table2.Day
WHERE
Table2.LabelType = 'A'
GROUP BY 1,2,3,5
If you have items in Table1 that don't appear in Table2, then you'll want to convert this to a LEFT OUTER JOIN:
SELECT
table1.DC,
table1.Item,
table1.Day,
Sum(table2.ShippedCases) as ShippedCases,
Table2.LabelType
FROM
Table1
LEFT OUTER JOIN Table2
ON Table1.DC = Table2.DC
AND Table1.Item = Table2.Item
AND Table1.Day = Table2.Day
AND Table2.LabelType = 'A'
GROUP BY 1,2,3,5
Notice that we move the WHERE clause up into the ON clause of the LEFT OUTER JOIN to insure that only items in Table2 are restricted for that LabelType. In other words, Teradata will filter Table2 BEFORE joining to Table1. BEcause it's a LEFT OUTER JOIN you will get ALL records from Table1, and then only those from Table2 that pass the filter and match the ON conditions.

Teradata - OLAP Functions - filter rows

I'm wondering if I can use an OLAP Function to filter irrelevant rows like this:
If I have one matching value (the fourth fields) all the rows with the same key ( the first 3 fields) must not be displayed
In this example, the matching value would be 'C':
Entities product ID Solde
997 0050 123 D
997 0050 123 D
997 0050 123 C
899 0124 125 D
899 0124 125 D
So here My key is composed by entities/product/ID, regarding the value of "Solde" I need to display or not.
Here the the undesired value is Solde = C.
In this example only the last row should be diplayed, because the key 899/0124/125 has only rows with solde = 'D'
The key 997/0050/123 has one row with solde = 'C' so I don't want to display it
Thanks in advance for your helping
Christophe
Updated answer
The more traditional way to solve this is to first select the Entities/Product/ID records that you DON'T want.
SELECT Entities, Product, ID FROM table WHERE Solde<>'D';
Use that result in a subquery in your WHERE clause to exclude those:
SELECT DISTINCT Entities, Product, ID, Solde
FROM table
WHERE (Entities, Product, ID) NOT IN ( SELECT Entities, Product, ID FROM table WHERE Solde<>'D');
Alternatively using a HAVING clause and aggregating
SELECT Entities, Product, ID
FROM table
COUNT(*) = SUM(CASE WHEN Solde = 'D' THEN 1 ELSE 0 END)
GROUP BY 1,2,3
I guess you are looking for answer as the below:
SELECT Solde
FROM yourtable
QUALIFY COUNT(*) OVER (PARTITION BY Entities, Product, ID, Solde) = 1;

Sqlite Increment column value based on previous row's value

How do I increment a column value based on previous column value in Sqlite? I need to do this for 1000+ rows. I have data in the first row say 100. I need to increment the next 1000 rows by 2.
Row# ColName
1 100
2 102
3 104
4 106
I tried something like this:
Update Table SET ColName = (Select max(ColName ) from Table ) + 2 but this puts 102 in all columns.
Assuming that this table has a rowid column, it is possible to count how many previous rows there are:
UPDATE MyTable
SET ColName = (SELECT MAX(ColName)
FROM MyTable
) +
(SELECT COUNT(*)
FROM MyTable AS Previous
WHERE Previous.rowid < MyTable.rowid
) * 2
WHERE ColName IS NULL

Resources