Query 2 sum in 1 table - sql

table a
column id : a a b b
column total : 1 2 1 3
how can i show? in one table without use compute
a 3 7
b 4 7

Do group by to sum each id's total. Do a sub-select to count total:
select id,
sum(total) as total,
(select sum(total) from a) as totalall
from a
group by id

Using window functions with a distinct, it can be simply expressed like this:
select distinct id,
sum(Total) over(partition by id) total,
Sum(Total) over () total_all
from mytable
SQL Fiddle

One way is to use OUTER APPLY. You could also set a variable to the sum of the table and call that variable.
select a.id, sum(a.total) as total, b.Grand as GrandTotal
from tablea a
outer apply
(select sum(total) as Grand from tablea) b
group by a.id

Related

SQL Sum of Counts and Group By

I have a table with 2 columns:
ID Status
-- -------
1 OK
2 OK
3 NOTOK
4 NOTOK
5 OK
I want to do a count and group by status and also have the sum of the count on each row. The count works but I do not know how to do the sum to appear on each row like this:
Status Count TotalCount
------- ----- ------
OK 3 5
NOTOK 2 5
SELECT COUNT(*) AS Count, SUM(*) AS Sum FROM *your table* GROUP BY Status.
You can use AS to rename whatever you want. I think sum will appear now.
select distinct Status, count(*) occurrences, count(*) over(partition by Status) total_count from table group by Status
You can use two windowed counts:
select distinct status,
Count(*) over(partition by status) as "count",
Count(*) over() as "Sum"
from t;
without window functions you could take various approaches, one would be
with c as (
select status, Count(*) as "Count"
from t
group by status
)
select *, (select Sum("count") from c) as "Sum"
from c
And in case CTE's are not supported...
select *, (select Count(*) from t) as "Sum"
from (
select status, Count(*) as "Count"
from t
group by status
)c
Use a subquery.
SELECT Status, COUNT(*) as Count, (SELECT COUNT(*) FROM my_table) AS TotalCount FROM my_table GROUP BY Status;
SQL Fiddle

SQL - Group by group property?

I've got a table of consignments (simplified of course)
CONSIGNMENT_NR CUSTOMER
1 1
2 1
3 2
4 2
5 2
I can easily select, for each customer, how many consignments they have:
SELECT CUSTOMER, COUNT(*) AS 'Count'
FROM CONSIGNMENT
GROUP BY CUSTOMER
Which will give me (with that example data):
CUSTOMER Count
1 2
2 3
But what I want is to get how many customers made x amount of consignments.
The data I want would look like this:
Amount No of Customers
2 1
3 1
I can't quite figure out how.
Wrap your query up as a derived table. GROUP BY its result:
select Amount, count(*) as No_of_Customers
from
(
SELECT COUNT(*) AS Amount
FROM CONSIGNMENT
GROUP BY CUSTOMER
) dt
group by Amount
you can try below - using subquery
select count(distinct customer) as noofcustomer, 'Count'
from
(
SELECT CUSTOMER, COUNT(*) AS 'Count'
FROM CONSIGNMENT
GROUP BY CUSTOMER
)A group by 'Count'

how to achieve count(distinct) without group by in hive

I want to find the count(distinct column name) wihtout using group by in hive.
my input is :
name id
a 2
a 3
a 4
b 1
c 4
c 4
d 7
d 9
my expected output is
name count
a 3
b 1
c 1
d 2
can some tell me how to achieve this without using group by. please help
A canonical solution with no explicit group by is select distinct with window functions:
select distinct name, count(distinct id) over (partition by name)
from t;
In your case, I strongly recommend the group by version:
select name, count(distinct id)
from t
group by name;
You can use subquery :
select distinct t.name,
(select count(distinct id) from table t1 where t1.name = t.name) as count
from table t;
However, GROUP BY is really appropriate way to do this.
just use count aggregate function with distinct keyword
select name,count(distinct id) as cnt from table
group by name

How to combine two tables into own this the same columns?

I have two tables A and B. A has two columns: id, amount. B also has two columns: id, amount.
I hope to combine A and B to create a new table C, with same two columns:id, amount. How can I do it using SQL?
For example:
A
('A1',1)
('A2',5)
('A3',2)
('A4',5)
('A5',2)
('A6',7)
B
('A1',3)
('A3',2)
('A4',7)
('A5',4)
('A8',2)
('A9',10)
so C should be:
C
('A1',4)
('A2',5)
('A3',4)
('A4',12)
('A5',6)
('A6',7)
('A8',2)
('A9',10)
SELECT ID, SUM(Amount) total
FROM
(
SELECT ID, Amount FROM A
UNION ALL
SELECT ID, AMount FROM B
) s
GROUP BY ID
SQLFiddle Demo
You can create a table base on the result from the query.
CREATE TABLE C
AS
SELECT ID, SUM(Amount) total
FROM
(
SELECT ID, Amount FROM A
UNION ALL
SELECT ID, AMount FROM B
) s
GROUP BY ID;
SQLFiddle Demo
the answer above works absolutely fine. Just to add to it an order by clause that will sort by ID.
SELECT ID, SUM(Amount) as total
FROM
(
SELECT ID, Amount FROM A
UNION ALL
SELECT ID, AMount FROM B
) s
GROUP by ID
order by ID

SUM of grouped COUNT in SQL Query

I have a table with 2 fields:
ID Name
-- -------
1 Alpha
2 Beta
3 Beta
4 Beta
5 Charlie
6 Charlie
I want to group them by name, with 'count', and a row 'SUM'
Name Count
------- -----
Alpha 1
Beta 3
Charlie 2
SUM 6
How would I write a query to add SUM row below the table?
SELECT name, COUNT(name) AS count
FROM table
GROUP BY name
UNION ALL
SELECT 'SUM' name, COUNT(name)
FROM table
OUTPUT:
name count
-------------------------------------------------- -----------
alpha 1
beta 3
Charlie 2
SUM 6
SELECT name, COUNT(name) AS count, SUM(COUNT(name)) OVER() AS total_count
FROM Table GROUP BY name
Without specifying which rdbms you are using
Have a look at this demo
SQL Fiddle DEMO
SELECT Name, COUNT(1) as Cnt
FROM Table1
GROUP BY Name
UNION ALL
SELECT 'SUM' Name, COUNT(1)
FROM Table1
That said, I would recomend that the total be added by your presentation layer, and not by the database.
This is a bit more of a SQL SERVER Version using Summarizing Data Using ROLLUP
SQL Fiddle DEMO
SELECT CASE WHEN (GROUPING(NAME) = 1) THEN 'SUM'
ELSE ISNULL(NAME, 'UNKNOWN')
END Name,
COUNT(1) as Cnt
FROM Table1
GROUP BY NAME
WITH ROLLUP
Try this:
SELECT ISNULL(Name,'SUM'), count(*) as Count
FROM table_name
Group By Name
WITH ROLLUP
all of the solution here are great but not necessarily can be implemented for old mysql servers (at least at my case). so you can use sub-queries (i think it is less complicated).
select sum(t1.cnt) from
(SELECT column, COUNT(column) as cnt
FROM
table
GROUP BY
column
HAVING
COUNT(column) > 1) as t1 ;
Please run as below :
Select sum(count)
from (select Name,
count(Name) as Count
from YourTable
group by Name); -- 6
The way I interpreted this question is needing the subtotal value of each group of answers. Subtotaling turns out to be very easy, using PARTITION:
SUM(COUNT(0)) OVER (PARTITION BY [Grouping]) AS [MY_TOTAL]
This is what my full SQL call looks like:
SELECT MAX(GroupName) [name], MAX(AUX2)[type],
COUNT(0) [count], SUM(COUNT(0)) OVER(PARTITION BY GroupId) AS [total]
FROM [MyView]
WHERE Active=1 AND Type='APP' AND Completed=1
AND [Date] BETWEEN '01/01/2014' AND GETDATE()
AND Id = '5b9xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' AND GroupId IS NOT NULL
GROUP BY AUX2, GroupId
The data returned from this looks like:
name type count total
Training Group 2 Cancelation 1 52
Training Group 2 Completed 41 52
Training Group 2 No Show 6 52
Training Group 2 Rescheduled 4 52
Training Group 3 NULL 4 10535
Training Group 3 Cancelation 857 10535
Training Group 3 Completed 7923 10535
Training Group 3 No Show 292 10535
Training Group 3 Rescheduled 1459 10535
Training Group 4 Cancelation 2 27
Training Group 4 Completed 24 27
Training Group 4 Rescheduled 1 27
You can use union to joining rows.
select Name, count(*) as Count from yourTable group by Name
union all
select "SUM" as Name, count(*) as Count from yourTable
For Sql server you can try this one.
SELECT ISNULL([NAME],'SUM'),Count([NAME]) AS COUNT
FROM TABLENAME
GROUP BY [NAME] WITH CUBE
with cttmp
as
(
select Col_Name, count(*) as ctn from tab_name group by Col_Name having count(Col_Name)>1
)
select sum(ctn) from c
You can use ROLLUP
select nvl(name, 'SUM'), count(*)
from table
group by rollup(name)
Use it as
select Name, count(Name) as Count from YourTable
group by Name
union
Select 'SUM' , COUNT(Name) from YourTable
I am using SQL server and the following should work for you:
select cast(name as varchar(16)) as 'Name', count(name) as 'Count'
from Table1
group by Name
union all
select 'Sum:', count(name)
from Table1
I required having count(*) > 1 also. So, I wrote my own query after referring some the above queries
SYNTAX:
select sum(count) from (select count(`table_name`.`id`) as `count` from `table_name` where {some condition} group by {some_column} having count(`table_name`.`id`) > 1) as `tmp`;
Example:
select sum(count) from (select count(`table_name`.`id`) as `count` from `table_name` where `table_name`.`name` IS NOT NULL and `table_name`.`name` != '' group by `table_name`.`name` having count(`table_name`.`id`) > 1) as `tmp`;
You can try group by on name and count the ids in that group.
SELECT name, count(id) as COUNT FROM table group by name
After the query, run below to get the total row count
select ##ROWCOUNT
select sum(s) from
(select count(Col_name) as s from Tab_name group by Col_name having count(*)>1)c