I having two table with mysql as below?
TimeSchedule
subID | date | venue | timeslot
1 | 8-12 | ABC | 10 - 12
2 | 8-12 | ABC | 2 - 4
subject
subID | name
1 | Games
2 | Music
I want to display these two table data as following?
Date | 10 - 12 | 2 - 4 |Venue
8-12 | Game | Music | ABC
Try this:
SELECT t.date,
MAX(CASE WHEN t.timeslot = '10 - 12' THEN s.name ELSE '' END) AS `10 - 12`,
MAX(CASE WHEN t.timeslot = '2 - 4' THEN s.name ELSE '' END) AS `2 - 4`,
t.venue
FROM TimeSchedule t
INNER JOIN `subject` s ON t.subID = s.subID
GROUP BY t.date;
Related
I have 3 tables like the following.
Table "mansioni":
id_mansione | desc_mansione
1 | production
2 | office
3 | transport
Table "dipendente": store id, name and surname:
id_dip | nome_dip | cognome_dip
1 | piero | rossi
2 | marco | rossi
Table dipendenti_iddip: store the association between "dipendente" and table "mansioni"
iddip_mansione | num_mansione | id_mansione
1 | 1 | 1
1 | 2 | 2
2 | 1 | 2
2 | 2 | 3
Now I need a query that give me a result like this:
id_dip | nome_dip | cognome_dip | mansione1 | mansione2 | mansione3
1 | piero | rossi | production| office |
2 | marco | rossi | office | transport |
I arrived to the following query but with this I can only see the "id_mansione" and not the "desc mansione" field
select i.id_dip,
i.nome_dip,
i.cognome_dip,
max(case when t.num_mansione='1' then t.id_mansione end) Mansione1,
max(case when t.num_mansione='2' then t.id_mansione end) Mansione2,
max(case when t.num_mansione='3' then t.id_mansione end) Mansione3
from dipendente i
left join dipendenti_iddip t
on i.id_dip = t.iddip_mansione
group by i.id_dip, i.nome_dip, i.cognome_dip
How can I arrive to my result?
Thanks...
Add join on mansioni and replace t.id_mansione with m.desc_mansione
select i.id_dip,
i.nome_dip,
i.cognome_dip,
max(case when t.num_mansione = '1' then m.desc_mansione end) Mansione1,
max(case when t.num_mansione = '2' then m.desc_mansione end) Mansione2,
max(case when t.num_mansione = '3' then m.desc_mansione end) Mansione3
from dipendente i
join dipendenti_iddip t
on i.id_dip = t.iddip_mansione
join mansioni m on m.id_mansione = t.id_mansione
group by i.id_dip
I have these 2 tables:
Table Users:
id | name
---------
1 | Joe
2 | Sara
3 | Michael
Table Sales:
id | product | user_id
------------------------
1 | Car | 2
2 | Truck | 3
3 | motorcycle| 1
4 | Car | 2
5 | Truck | 1
6 | Car | 3
7 | Car | 2
8 | Truck | 3
9 | Car | 2
10 | Car | 3
I want the following:
User Name | Car | Truck | Motorcycle
Joe | 0 | 1 | 1
Sara | 4 | 0 | 0
Michael | 2 | 2 | 0
Any help would be appreciated
--For a small number of products this works,
Select username
, Sum(case when product = 'Car' then 1 else 0 End) Cars
, Sum(case when product = 'Truck' then 1 else 0 End) Truck
, Sum(case when product = 'motorcycle' then 1 else 0 End) motorcycle
From Sales s join users u on s.userid = u.userid
group by username
SELECT Users.name,
SUM(Case
WHEN product='Car' then 1
ELSE 0
) AS Car,
SUM(Case
WHEN product='Truck' then 1
ELSE 0
) AS Truck ,
SUM(Case
WHEN product='Motorcycle' then 1
ELSE 0
) AS Motorcycle,
FROM Users
LEFT JOIN Sales ON Users.id = Sales.user_id
GROUP BY Users.name
SELECT
name as UserName ,
SUM(product= 'Car')AS Car,
SUM(product= 'Truck')AS Truck,
SUM(product= 'motorcycle') AS motorcycle
FROM Users join Sales
on Users.id = Sales.user_id
GROUP BY name
hope it helps but still this is a standard solution you should check pivot Tables.
This question already has answers here:
MySQL row into number of columns and sum
(3 answers)
Closed 5 years ago.
I have a table with records such as:
ID | Car_num | Service | Price | Payment
---+---------+---------+-------+-------+-
1 | 001 | shower | 10 | card
2 | 002 | TV | 5 | cash
3 | 001 | TV | 5 | cash
How to write an SQL query to get the following output?
ID |Car_num | shower | TV
---+--------+------------+---
1 | 001 | 10 (card) | 5 (cash)
2 | 002 | | 5 (cash)
Use conditional aggregation:
SELECT MIN(t.id) as id,
t.car_num,
MAX(CASE WHEN t.service = 'shower' THEN t.price END) as shower,
MAX(CASE WHEN t.service = 'TV' THEN t.price END) as TV
FROM YourTable t
GROUP BY t.car_num
If you want the columns to actually appear like 10 (card) and not 10 (which is not recommended at all), then change it to this:
MAX(CASE WHEN t.service = 'shower' THEN concat(t.price,'(',t.payment,')') END) as shower,
MAX(CASE WHEN t.service = 'TV' THEN concat(t.price,'(',t.payment,')') END) as TV
Using MySQL.
Below is my table structure.
batch_admissions
------------------------+
batchId | studentId |
----------------------- +
1 | 1 |
1 | 2 |
1 | 3 |
2 | 1 |
2 | 2 |
------------------------+
attendance_master
----------------------------+
attendance | studentId |
----------------------------+
P | 1 |
P | 2 |
P | 3 |
----------------------------+
desire result if batchId=2 as below as attendance_master only contain record of batchId=1
----------------------------+
attendance | studentId |
----------------------------+
| 1 |
| 2 |
----------------------------+
But currently I am getting all record back from attendance_master irrespective of change in batchId.
What wrong in my query? I think left join should do the job. but not working
SELECT
a.attendanceId,
a.attendanceDate,
a.attendance,
a.Remarks,
CONCAT(b.studentFirstName, ' ', COALESCE(b.studentMiddleName,'') , ' ', b.studentLastName) as studentName ,
c.classRollNum,
SUBSTRING_INDEX(SUBSTRING_INDEX( a.attendanceDate , '-', 3 ),'-',-1) AS attDay,
CASE WHEN DAYNAME(a.attendanceDate) = 'Monday' THEN 'Mon'
WHEN DAYNAME(a.attendanceDate) = 'Tuesday' THEN 'Tue'
WHEN DAYNAME(a.attendanceDate) = 'Wednesday' THEN 'Wed'
WHEN DAYNAME(a.attendanceDate) = 'Thursday' THEN 'Thu'
WHEN DAYNAME(a.attendanceDate) = 'Friday' THEN 'Fri'
WHEN DAYNAME(a.attendanceDate) = 'Saturday' THEN 'Sat'
WHEN DAYNAME(a.attendanceDate) = 'Sunday' THEN 'Sun'
END as attDayName
,CONCAT(SUBSTRING_INDEX(SUBSTRING_INDEX( a.attendanceDate , '-', 3 ),'-',-1),'.',c.classRollNum) as Idx
FROM attendance_master a
LEFT JOIN student_master b ON a.studentId = b.studentId
LEFT JOIN batch_admissions c ON c.studentId = a.studentId AND c.batchId=1
WHERE a.attendanceDate BETWEEN '2016-03-01' AND '2016-03-31'
ORDER BY c.classRollNum ASC
-------------
Basically I trying to avoid triggering two queries and want result in single query.
batch_admissions table holds series of batch with N numbers student in it.
attendance_master table holds attendance of students for all batch.
On web page I am displaying table grid report, per batch wise.
What I am trying to achieve,
case 1 : when attendance_master NOT contain attendance for batchId for specific period. Still want list of student for that batch
-------------------------------------------------------
BatchId |studentId | Mon | Tue | Wed | Thus |
------------------------------------------------------
1 | 11 | | | | |
1 | 12 | | | | |
.. | .. | | | | |
Case 2: when attendance_master contain attendance for batchId for specific period.
-------------------------------------------------------
BatchId |studentId | Mon | Tue | Wed | Thus |
------------------------------------------------------
2 | 1 | P | P | P | P |
2 | 2 | P | A | P | P |
.. | .. | P | P | P | P |
Alternate I can trigger two queries to achieve this logically. One for get of student for batch, and then getting attendance detail for all those student.
ok... so return all records from batch admissions and the related student_master data (which there will always be records) and the associated attendance master data...
FROM batch_admissions c
INNER JOIN student_master b
ON a.studentId = c.studentId
LEFT JOIN attendance_master a
ON c.studentId = a.studentId
and a.attendanceDate BETWEEN '2016-03-01' AND '2016-03-31'
WHERE c.batchId=1
ORDER BY c.classRollNum ASC
I have a query which creates a crosstab. The results are a count of the txn_id for branda, and the count of txn_id for brandb.
The txn_id is NOT UNIQUE. This is an example of the transactions table.:
txn_id | nationality_id | sku | sales | units
1 | 1 | 1 | 20 | 2
1 | 1 | 2 | 15 | 1
2 | 4 | 1 | 20 | 2
3 | 2 | 1 | 10 | 1
4 | 3 | 2 | 15 | 1
5 | 4 | 1 | 10 | 1
There are 2 other tables (products) - (sku, brand, product name), and (nationalities) - (nationality_id, nationality).
I would like to add a third column which gets me the count of txn_id where BOTH brands are purchased
The output should be
nationality | branda | brandb | combined
1 | 1 | 1 | 1
2 | 1 | 0 | 0
3 | 0 | 1 | 0
4 | 2 | 0 | 0
Current query.
SELECT
nationalities.nationality,
COUNT((CASE brand WHEN 'branda' THEN txn_id ELSE NULL END)) AS branda,
COUNT((CASE brand WHEN 'brandb' THEN txn_id ELSE NULL END)) AS brandb
<I want my 3rd column here>
FROM
transaction_data
INNER JOIN
products USING (sku)
INNER JOIN
nationalities USING (nationality_id)
GROUP BY nationality
ORDER BY branda DESC
LIMIT 20;
I have tried using:
COUNT((CASE brand WHEN 'brandb' OR 'brandb' THEN txn_id ELSE NULL END)) AS combined - however this obviously returns too many (returns branda or brandb regardless of whether they were purchased together). I know I can't use AND, because obviously no single cell is going to be both branda AND brandb.
I have also tried using:
COUNT((CASE brand WHEN IN('branda', 'brandb') THEN txn_id ELSE NULL END)) AS combined - However this isn't valid syntax.
I feel that I should be using a HAVING clause, but I'm not sure how this would work in the column list.
I think you are going to need two levels of aggregation:
SELECT n.nationality,
sum(branda), sum(brandb), sum(branda * brandb)
FROM (SELECT t.txn_id, n.nationality,
MAX(CASE brand WHEN 'branda' THEN 1 ELSE 0 END) AS branda,
MAX(CASE brand WHEN 'brandb' THEN 1 ELSE 0 END) AS brandb
FROM transaction_data t INNER JOIN
products p
USING (sku) INNER JOIN
nationalities n
USING (nationality_id)
GROUP BY t.txn_id, n.nationality
) tn
GROUP BY n.nationality
ORDER BY max(txn_id) DESC
LIMIT 20;