How to SUM up columns in MySQL - mysql

I would like to group by one column and sum up another column by doing so.
If I ask
SELECT * FROM Name_Data WHERE Name = 'Tim'
I will get a column named 'Count' that shows me how much Tim's there are per ZIPcode
ZIPcode Place Year Name Count
3042 Kleinpolder 1993 Tim 7
3042 Kleinpolder 2003 Tim 6
3051 Kleiwegkwart 1983 Tim 14
3051 Kleiwegkwart 1993 Tim 9
3059 Nesselande 1993 Tim 8
3059 Nesselande 2003 Tim 10
3068 Ommoord/z 1983 Tim 24
3068 Ommoord/z 2003 Tim 15
3065 s-Gravenland 1993 Tim 21
3011 Weena 2003 Tim 5
I would like to use GROUP BY ZIPcode and by doing so also sum up all the Count numbers. I have read how to use the SUM command but I can't figure out how to sum up the Count column.
So
3042 should be 7 + 6 = 13
, 3051 should be 14 + 9 = 23 and so on..

select sum(`Count`) as `Count`,Name,ZIPcode
from
Name_Data
WHERE Name = 'Tim'
group by ZIPcode

Related

MYSQL : Grouping rows by dates and making fields in to column names

Have have table like this
id date user call_count
1 2020-09-15 Tim 4
2 2020-09-14 Tim 6
3 2020-09-04 Jamie 1
4 2020-09-02 Tim 2
5 2020-09-07 Tim 5
6 2020-09-01 Jamie 1
7 2020-09-01 Tim 5
8 2020-09-10 Jamie 4
9 2020-09-12 Tim 5
10 2020-09-22 Tim 44
11 2020-09-22 Tony 32
I'd like to have a single query that groups by date, putting each user as the name of a column and displaying their count for that date. Right now I am accomplishing this by a few loops of selects, but it is ugly and not efficient.
Result should look like
Date Tim Tony Jamie
2020-09-22 44 32
2020-09-15 4
2020-09-14 6
2020-09-12 5
2020-09-10 4
2020-09-07 5
2020-09-04 1
2020-09-02 2
2020-09-01 5 1
I've searched quite a bit for something like this, but not finding much. Thank you
Use conditional aggregation:
SELECT
Date,
SUM(CASE WHEN user = 'Tim' THEN call_count END) AS Tim,
SUM(CASE WHEN user = 'Tony' THEN call_count END) AS Tony,
SUM(CASE WHEN user = 'Jamie' THEN call_count END) AS Jamie
FROM yourTable
GROUP BY
Date
ORDER BY
Date;

Can't find out expected result from Left Join Where & group by

I have two table receivable & receive.
table: receivable
tranId roll month amount
1 1111 October-2019 10
2 1112 October-2019 10
3 1113 October-2019 10
4 1114 October-2019 10
5 1115 October-2019 10
Table: receive
tranId roll month amount
1 1111 October-2019 10
2 1111 September-2019 10
3 1113 October-2019 10
4 1114 October-2019 10
5 1115 October-2019 10
6 1116 October-2019 10
7 1117 October-2019 10
8 1118 October-2019 10
9 1119 October-2019 10
10 1120 October-2019 10
In both table month columns are string & table receive roll+month column is unique. i want to create a receivable & receive statement report of a student (Where roll=1111) from those table like.
Expected result:
month rcvamount rcvvmonth rcvvamount
---------------------------------------------------------
October-2019 10 October-2019 10
---------------------------------------------------------
September-2019 10 - -
my query is:
SELECT receive.month, sum(receive.amount) AS rcvamount, receivable.month AS rcvvmonth,
receivable.amount AS rcvvamount
FROM receive
LEFT JOIN receivable ON receive.month = receivable.month
WHERE receive.roll = 1111
GROUP BY receive.month
and resutl is:
month rcvamount rcvvmonth rcvvamount
----------------|-------|----------------------------------
October-2019 | 50 | October-2019 10
----------------|-------|----------------------------------
September-2019 10 - -
revamount in October-2019 should be 10 from receive table.
You can aggregate before joining. Or use union all and aggregate:
SELECT month, SUM(ramount) AS rcvamount,
SUM(raamount) AS rcvvamount
FROM ((SELECT month, amount as ramount, null as raamount
FROM receive
WHERE roll = 1111
) UNION ALL
(SELECT month, NULL as ramount, amount as raamount
FROM receiveable
WHERE roll = 1111
)
) r
GROUP BY month;
One nice feature of UNION ALL is that it handles missing data in either table.

How to create a SQL query that calculate monthly grow in population

I want to create a SQL query that count the number of babies born in month A, then it should count the babies born in month B but the second record should have the sum of month A plus B. For example;
Month | Number
--------|---------
Jan | 5
Feb | 7 <- Here were 2 babies born but it have the 5 of the previous month added
Mar | 13 <- Here were 6 babies born but it have the 7 of the two previous months added
Can somebody maybe please help me with this, is it possible to do something like this?
I have a straight forward table with babyID, BirthDate, etc.
Thank you very much
Consider using a subquery that calculates a running count. Both inner and outer query would be aggregate group by queries:
Using the following sample data:
babyID Birthdate
1 2015-01-01
2 2015-01-15
3 2015-01-20
4 2015-02-01
5 2015-02-03
6 2015-02-21
7 2015-03-11
8 2015-03-21
9 2015-03-27
10 2015-03-30
11 2015-03-31
SQL Query
SELECT MonthName(BirthDate) As BirthMonth, Count(*) As BabyCount,
(SELECT Count(*) FROM BabyTable t2
WHERE Month(t2.BirthDate) <= Month(BabyTable.BirthDate)) As RunningCount
FROM BabyTable
GROUP BY Month(BirthDate)
Output
BirthMonth BabyCount RunningCount
January 3 3
February 3 6
March 5 11

mysql select sum of count group by weekdays

Mysql query I have following tables
Company table:-
id company_name company_createddate
1 ABC 2015-01-01 12:45:23
2 LMN 2015-01-11 09:45:23
3 PQR 2015-02-16 23:45:23
User table:-
id username
1 John
2 Mary
Order table:-
id company_id user_id order_name order_createddate
1 1 1 Sales 2015-02-16 09:45:23
2 1 1 Sales3 2015-02-20 09:45:23
3 1 1 Marketing 2015-02-24 09:45:23
4 2 1 Sales2 2015-02-17 09:45:23
5 1 2 M1 2015-02-16 09:45:23
6 2 2 M2 2015-02-23 09:45:23
7 2 2 P1 2015-02-26 09:45:23
Output required:-
Week day user name Pipeline Orders
16 Feb - 22 Feb John 2 (ABC), 1(LMN)
23 Feb - 01 Mar John 3 (ABC), 1(LMN)
16 Feb - 22 Feb Mary 1 (ABC)
23 Feb - 01 Mar Mary 1 (ABC), 2(LMN)
Week days is order_createddate
In 16Feb-22Feb Week, 2 orders created for company ABC
And in 23Feb-01Mar Week, 1 order created for company ABC, I want to show total number of order generated till 01 Mar Week, So i need output of 3.
Please help!
select concat(STR_TO_DATE(concat('2015 ',wk,' 1'), '%Y %u %w'), ' to ',
STR_TO_DATE(concat('2015 ',wk,' 0'), '%Y %u %w')),
User.username, GROUP_CONCAT(som) from (
select DATE_FORMAT(order_createddate,'%u') wk,user_id,
concat(Company.company_name ,'(',CONVERT(count(1),CHAR),')') som from `Order`
join Company on `Order`.company_id = Company.id
group by wk,user_id,company_name ) t
join User on t.user_id = User.id
group by wk, User.username
the time limit should be in sub select clause t.
if you dont like the week format, change them by modify the first line's date format
sql fiddle here

Show years person has not been a part of

I have a table like this.
id Person year
1 4 2001
2 7 2001
3 5 2001
4 9 2001
5 4 2001
6 7 2002
7 2 2002
8 5 2002
9 4 2002
10 6 2002
11 6 2003
12 4 2003
13 9 2003
14 2 2004
15 3 2004
16 7 2004
17 5 2004
18 7 2004
19 9 2005
20 8 2005
I would like for it to print all the years that person '9' has not been a part of.
I thought this was as simple as simply putting WHERE person!=9 GROUP by year, but that doesn't work, as if it ignores my where command.
It does however work the other way around when I say person=9 - then I only get the years that he has been a part of.
I have tried joining the table with itself. But no luck.
try:
... WHERE year NOT IN (SELECT year WHERE person = 9) ...
select distinct yr.year
from your_table as yr
left join
(
select distinct year
from your_table
where person=9
) as not_exist
on yr.year=not_exist.year
where not_exist.year is null;
OR
select distinct year from your_table
where year not in (select distinct year from your_table where person=9);