Group by 2 field in mysql [duplicate] - mysql

This question already has answers here:
multiple query same table but in different columns mysql
(4 answers)
Closed 6 years ago.
how to group by data with 2 column but the result is in 1 row (like when we join it).
this is the table 'jembatan'
id nama tahun jumlah
-----------------------------
1 A 2011 12
2 B 2011 10
3 A 2011 23
4 B 2012 11
i want the result like this:
id totalA totalB tahun
---------------------------------
25 10 2011
0 11 2012
how to do like that?

You want conditional aggregation:
select sum(case when nama = 'A' then jumlah else 0 end) as TotalA,
sum(case when nama = 'B' then jumlah else 0 end) as TotalB,
tahun
from t
group by tahun;

Related

Join multiple rows in SQL tables based one column value [duplicate]

This question already has an answer here:
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 2 years ago.
I have a SQL table defined like so:
id time data
1 1 11
1 2 12
1 3 13
2 1 21
2 2 22
2 3 23
3 1 31
3 2 32
3 3 33
I would like to create a new table that combines all the rows with the same time value like so:
time data data data
1 11 21 31
2 12 22 32
3 13 23 33
I have tried selecting the time on based on time, but I get a lot of duplicate rows with different column orders.
What is the best way to do this in SQL
You can do:
select
time,
sum(case when id = 1 then data end) as data,
sum(case when id = 2 then data end) as data,
sum(case when id = 3 then data end) as data
from t
group by time

Transpose a table

I am using Mysql and I want count distinct value and then make the distinct values as the column name.
I have a table Students like this
ID Names Age
1 Tim 12
2 James 14
3 White 13
4 John 13
5 Annie 11
6 Judy 13
I want to find how many people in each age. My expected result is:
11 12 13 14
1 1 3 1
I tried the query: "Select count(age), age from Students group by age;"
It gives out:
count(age) age
1 11
1 12
3 13
1 14
How can I take a "transpose" to the table?
If you are using MySQL you will need to know age in advance:
SELECT
SUM(CASE WHEN (age='11') THEN 1 ELSE 0 END) AS 11,
SUM(CASE WHEN (age='12') THEN 1 ELSE 0 END) AS 12,
SUM(CASE WHEN (age='13') THEN 1 ELSE 0 END) AS 13,
SUM(CASE WHEN (age='14') THEN 1 ELSE 0 END) AS 14
FROM
Students

laravel sum and crosstab

hi i am trying to solve this problem from 2 days please help. I have a table like below. i am trying to get crosstab display
expenditures
id name
1 exp1
2 exp2
3 exp3
4 exp4
5 exp5
stations
id name
1 station1
2 station2
3 station3
4 station4
expenses
expenditure_id station_id year month expense
1 1 2015 1 10
1 2 2015 1 20
1 3 2015 1 15
1 4 2015 1 10
1 1 2015 2 20
1 1 2015 3 30
1 1 2015 4 40
2 1 2015 1 20
2 1 2015 2 10
2 1 2015 3 20
I am trying get result like this
for year 2015
expenditure station1 station2 station3 station4
exp1 100 20 15 0
exp2 50 0 0 0
where value of station1 station2 will be the sum of month expense
This is the mysql query as your requirement, use Laravel query builder to run this query as raw query, if you donot know how to run raw query in laravel let me know, in the mean while run the query in phpmyadmin/mysql client and see whether its give you the desired output .
SELECT y.expenditure,
MAX(CASE y.station WHEN 'station1' THEN y.totalexpense END) 'station1',
MAX(CASE y.station WHEN 'station2' THEN y.totalexpense END) 'station2',
MAX(CASE y.station WHEN 'station3' THEN y.totalexpense END) 'station3',
MAX(CASE y.station WHEN 'station4' THEN y.totalexpense END) 'station4'
FROM (
SELECT exp.name AS expenditure,st.name AS station,x.totalexpense
FROM expenditures AS exp
LEFT JOIN
(SELECT expenditure_id,station_id,SUM(expense) as totalexpense
FROM expenses
WHERE year = '2015'
GROUP by expenditure_id,station_id
)
AS x ON exp.id = x.expenditure_id
LEFT JOIN stations AS st ON x.station_id = st.id
) AS y
GROUP BY y.expenditure
I assume you have only 4 stations, if stations are unknown numbers then you can use a prepared statement and create this dynamically:

Get latest result in SQL [duplicate]

This question already has answers here:
Returning the 'last' row of each 'group by' in MySQL
(6 answers)
Closed 7 years ago.
I have following table structure:
id customer_id version_num
1 1 1
2 1 2
3 2 1
4 2 2
5 3 1
6 3 2
7 3 3
I want results to be displayed as:
id customer_id version_num
2 1 2
4 2 2
7 3 3
I'm able to get the following results:
id customer_id version_num
1 1 1
3 2 1
5 3 1
Query that I used:
select * from TABLE group by customer_id
I don't know how to make use of latest(version_num) in my query.
SELECT Y.id, Y.customer_id, Y.version_num
FROM YourTable Y
JOIN (SELECT customer_id, MAX(version_num) max_v
FROM YourTable
GROUP customer_id
) T
ON Y.customer_id = T.customer_id
AND Y.version_num = T.max_v;

quarter year wise filteration in sql

I have a table having structure as follows
id cust_id target month year fiscal_ID
1 234 50 4 2013 1
2 234 50 5 2013 1
3 234 50 6 2013 1
4 234 150 7 2013 1
5 234 150 8 2013 1
6 234 150 9 2013 1
I need to get the result as follows
cust_id target quarter year fiscal_ID
234 150 Q1 2013 1
234 450 Q2 2013 1
months 4,5,6 in Q1, 7,8,9 in Q2 etc
Since you are storing the month and year in separate columns, one way you can get the result is to use a derived table that references the month and quarter and you join to that data:
select t.cust_id,
sum(target) target,
d.qtr,
t.year,
t.fiscal_id
from yourtable t
inner join
(
select 4 mth, 'Q1' qtr union all
select 5 mth, 'Q1' qtr union all
select 6 mth, 'Q1' qtr union all
select 7 mth, 'Q2' qtr union all
select 8 mth, 'Q2' qtr union all
select 9 mth, 'Q2'
) d
on t.month = d.mth
group by t.cust_id, d.qtr, t.year, t.fiscal_id;
See SQL Fiddle with Demo.