Subtotal (rollup?) values only from 2nd column - sql

I'm having difficulty pinning down the proper syntax to get a total I need from Oracle 11g. I need to produce a total based on values in column 2, independent of the values in column 1. I've tried several types of rollup, cube, grouping sets, but I keep getting totals for each grade level (values in column 1).
Here is the 'group by' without any subtotaling:
... sql select statement ...
...
GROUP BY grade, inc_count
ORDER BY grade
which produces
9 714
10 550
11 445
12 296
And here is my goal output:
9 714
10 550
11 445
12 296
2005
Which permutation of rollup/group sets/or something else will get me to my goal output?

You could try doing a UNION ALL to this SELECT 'SUM', SUM(value) from TABLE

For the example you gave:
select grade, sum(cnt)
from t
group by cube(grade)
order by sum(cnt);
group by cube is "syntactic sugar" for:
group by grouping sets ((grade),())

Related

SQL Query to get the Average age from multiple 'date' values

I'm needing to get a SQL Query which returns the average age of multiple data inputs in my table
Households
User
Dates
1
2002-01-01
2
2004-06-10
I want to grab both User 1 and 2 date of births and return the average age of them.
Managed to get the age from the date of births using
SELECT *, DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), Date)), '%Y') + 0 AS age
FROM Households;
I just can't get the rest of it working to then average the ages out.
Assuming that you are running MySQL, as the syntax of your SQL code suggests.
For starts, I would recommend simplifying the age computation. MySQL provides timestampdiff(), which we can use like so:
select user_id, user_date,
timestampdiff(year, user_date, current_date) age
from houshold
user_id
user_date
age
1
2002-01-01
20
2
2004-06-10
18
3
2004-11-10
17
To compute the average age over all rows of the table, we can use aggregate function avg():
select avg(timestampdiff(year, user_date, current_date)) avg_age
from houshold
avg_age
18.3333
Here is a small demo based on your sample data. Note that I renamed the columns so they do not clash with meaningful SQL names.

Calculate grand total in oracle sql developer

How to calculate grand total in query in SQL developer in oracle.
Break on report
Compute Sum is not working in SQL developer.It works only in SQL plus.
Don't want to use group function also.
Is there any way to run a query in SQL developer and show the total at the end of the results/rows.
Also want to use it in jdbc java the query
For Example , i need to select a table and list all records matching certain criteria and show the sum at the end of the records
Select volume, value , *.t1 from table1 t1 where trunc(create_date) = trunc(sysdate)
This will display say 50 records..i want to display at the end of all records under column volume and value ,the grand total of these 2 columns.
Volume value xxx yy zzzz
25 123.5 aa bb cc
35 10 a c b
50 100 c nn xc
-- --
110 233.5
You can use the below query to achieve the result -
SELECT volume,
value ,
t1.*
FROM table1 t1
WHERE TRUNC(create_date) = TRUNC(SYSDATE)
UNION ALL
SELECT SUM(volume),
SUUM(value),
NULL,
NULL,
... -- Add other NULLS according to your column list in table1.
FROM table1;
"I don't want" is a poor reason.
How come you can & want to use SQL*Plus' commands (which won't work elsewhere), but standard SQL capabilities are out of your sight? I'd suggest you to reconsider what you said & thought.
If you do, rollup might be a nice option for you.
SQL> select deptno,
2 sum(sal) sum_sal,
3 sum(comm) sum_comm
4 from emp
5 group by rollup(deptno);
DEPTNO SUM_SAL SUM_COMM
---------- ---------- ----------
10 8750
20 10875
30 9400 2200
29025 2200
SQL>
You can use ROLLUP FUNCTION here.
SELECT SUM (volume) volume,
SUM (value) value,
xxx
FROM table1 t1
WHERE TRUNC (create_date) = TRUNC (SYSDATE)
GROUP BY ROLLUP (xxx)

SQL EXTRACT(YEAR FROM MYDATE) not a GROUP BY expression

I have table MYTABLE with columns mydate and quantity of VARCHAR2 type.
|mydate| |quantity|
10/15/2010 15
01/20/2010 20
05/16/2005 30
04/29/2005 50
03/30/2008 5
I want to get:
|year| |quantity|
2010 35
2005 80
2008 5
I try:
SELECT
to_char(mydate,'yyyy') YEAR,
SUM(to_number(quantity))
FROM MYTABLE
GROUP BY
to_char(mydate,'yyyy');
But I get an error
ORA-00979: not a GROUP BY expression
What did I do wrong?
You must put all columns of the SELECT in the GROUP BY or use functions on them which compress the results to a single value (like MIN, MAX or SUM).
A simple example to understand why this happens: Imagine you have a database like this:
FOO BAR
0 A
0 B
and you run SELECT * FROM table GROUP BY foo. This means the database must return a single row as result with the first column 0 to fulfill the GROUP BY but there are now two values of bar to chose from. Which result would you expect - A or B? Or should the database return more than one row, violating the contract of GROUP BY?
Try this
select extract(year from mydate),sum(to_number(quant)) from mytable
group by extract(year from mydate);
SQLFiddle Example

SQL Select SUM if ID is the same [duplicate]

This question already has answers here:
SQL Server : SUM() of multiple rows including where clauses
(6 answers)
Closed 8 years ago.
I know this is probably a simple question since I'm still learning, but I am trying to find a SQL Select statement that will calculate the SUM of each row's pieces and weight IF their secondary IDs are the same. So the select is collapsing and getting the SUM of rows with the same secondary ID, while also returning the unique rows as well.
ID_a ID_b pieces weight
--------------------------
1 1 10 20
2 1 20 30
3 2 40 40
Will result in
ID_b: 1 pieces: 30 weight: 50
ID_b: 2 pieces: 40 weight: 40
Thank you.
Group by the id_b and then you can use aggregate functions to sum up the values for each group
select id_b, sum(pieces), sum(weight)
from your_table
group by id_b
select id_b,sum(pieces),sum(weight)
from table
group by id_b
Here is the query you're looking for:
SELECT TID_b
,SUM(pieces) AS [totalPieces]
,SUM(weight) AS [totalWeight]
FROM yourTable T
GROUP BY T.ID_b
Hope this will help you.

SQL Select Command Syntax Help

Hopefully this is pretty simple, but I'm having some trouble. I have a table that has multiple fields but basically the two that matter are ID and year. A single ID can exist in many years. How do I set up a select statement (Which I'm ultimately using in a join in another statement) so that I can retrieve all of the distinct IDS with no duplicates for the top year that they exist for?
If there is a set of records like this:
ID - Year
55 - 2000
55 - 2001
56 - 2000
56 - 2002
So basically I want something like this returned:
55 - 2001
56 - 2002
Help?
SELECT ID, MAX(Year) FROM MyTable GROUP BY ID
References:
SELECT (Transact-SQL)
GROUP BY (Transact-SQL)
Aggregate Functions (Transact-SQL)
MAX (Transact-SQL)