Add another different column based on previous column - mysql

Let's say i have a table like this in database
Name Day VALUE1 VALUE2
========================
A mon 1 2
A fri 2 2
and i want it like
Name VALUE1(mon) VALUE2(mon) VALUE1(fri) VALUE2(fri)
=====================================================
A 1 2 2 2
can i make something like this? if i can, pls tell me how to make this with query

You may use a pivot query:
SELECT
Name,
MAX(CASE WHEN DAY = 'mon' THEN VALUE1 END) AS "Value1(mon)",
MAX(CASE WHEN DAY = 'mon' THEN VALUE2 END) AS "Value2(mon)",
MAX(CASE WHEN DAY = 'fri' THEN VALUE1 END) AS "Value1(fri)",
MAX(CASE WHEN DAY = 'fri' THEN VALUE2 END) AS "Value2(fri)"
FROM yourTable
GROUP BY Name;

You can do self full outer join as follows:
Select coalesce(m.day,f.day) as day,
M.value1 as monval1,
M.value2 as monval2,
F.value1 as frival1,
F.value2 as frival2
From your_table m full outer join your_table f
On m.name = f.name
And m.day = 'mon' and f.day = 'fri'

Related

View doesn't show same result as command with subquery on mariadb

Dears,
I tried to create a view that sumarize transaction per category (row) and months (columns) on one year as this :
SELECT counterpart.id_category as catid, category AS Category, (
SELECT COALESCE(SUM(amount), 0)
FROM transaction
INNER JOIN counterpart
ON transaction.id_counterpart = counterpart.id_counterpart
WHERE YEAR(date) = YEAR(now())
AND MONTH(date) = 1
AND id_category = catid
) AS Jan, [...]
FROM transaction
INNER JOIN counterpart
ON transaction.id_counterpart = counterpart.id_counterpart
INNER JOIN category
ON counterpart.id_category = category.id_category
WHERE transaction.id_user = 2
AND YEAR(date) = YEAR(now())
GROUP BY category
I don't know if it's a good way or not, at least it works as single command but not as view.
On view, the WHERE id_category = catid for subquery doesn't work properly.
Thank you.
I suspect conditional aggregation would be more appropriate and quicker than a lot of sub queries.
for example
SELECT counterpart.id_category as catid, category AS Category,
sum(case when month(date) = 1 then 1 else 0 end) as Jan,
sum(case when month(date) = 2 then 1 else 0 end) as feb,
sum(case when month(date) = 3 then 1 else 0 end) as Mar
FROM transaction
INNER JOIN counterpart
ON transaction.id_counterpart = counterpart.id_counterpart
INNER JOIN category
ON counterpart.id_category = category.id_category
WHERE transaction.id_user = 2
AND YEAR(date) = YEAR(now())
GROUP BY counterpart.id_category,category

Join not summing CASE WHEN

What I'm looking to do is show my current performance for this month, compared with expected scheduled wins to come in and then display the total expected amount, by product type.
For clarity, I have two sub-products that I'm grouping under the same name.
My issue is that for my 'Charged' amount, it's keeping the two sub-products separate, where as the 'Scheduled' amount is working fine.
The table should look like:
Type | Charged | Scheduled | Expected
A 3 2 5
B 1 1 2
What's actually showing is:
Type | Charged | Scheduled | Expected
A 2 1 3
A 1 1 2
B 1 1 2
The code is as follows:
select
t2.product,
t1.Charged,
t2.Scheduled,
t1.charged + t2.scheduled as 'expected'
from(
select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as 'Type',
SUM(charged) as 'Scheduled'
from
table
where
month(date) = month(now())
and
year(date) = year(now())
and status like 'scheduled'
group by 1
order by 2 desc) t2 join
(select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as 'Type',
sum(charged) as 'Charged'
FROM table
WHERE (status = 'Complete'
AND str_to_date(concat(date_format(date, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))
GROUP BY user_type
ORDER BY user_type ASC) as t1 on t1.type = t2.type
I appreciate I might not be explaining this incredibly well (and that my code is probably quite clunky - I'm still fairly new!) so any help/direction would be appreciated.
Thanks!
Just some suggestion
you have a column product in main select but you have type in subquery and not product
you should not use sigle quote around column name
ad you have group by user_type but you need group by type for charged
select
t2.type,
t1.Charged,
t2.Scheduled,
t1.charged + t2.scheduled as 'expected'
from(
select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
SUM(charged) as Scheduled
from
table
where
month(date) = month(now())
and
year(date) = year(now())
and status like 'scheduled'
group by 1
order by 2 desc) t2 join
(select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
sum(charged) as Charged
FROM table
WHERE (status = 'Complete'
AND str_to_date(concat(date_format(date, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))
GROUP BY Type
ORDER BY Type ASC) as t1 on t1.type = t2.type

Appending data from different tables - mysql

I have the following tables Job description and Candidate.
Each job description can be related to n candidates. The candidates are obtained from different sources(a, b, c d) - candidate.source.
I want a single query to list me the JD ids, with count of candidates for each source for each JD, as shown below:
JD id | candidate name | count of candidates - source a | count of candidates - source b | count of candidates - source | c..............
Try using the query as a template:
select
JobDescriptionName,
SUM(ACount) CountOfCandidatesOfA ,
SUM(BCount) CountOfCandidatesOfB,
SUM(CCount) CountOfCandidatesOfC ,
SUM(DCount) CountOfCandidatesOfD
from
( select JobDescriptionID, (CASE WHEN Source = 'a' THEN 1 ELSE 0 END) AS ACount,
(CASE WHEN Source = 'b' THEN 1 ELSE 0 END) AS BCount,
(CASE WHEN Source = 'c' THEN 1 ELSE 0 END) AS CCount,
(CASE WHEN Source = 'd' THEN 1 ELSE 0 END) AS DCount
from Candidate) AS DerivedCandidate
inner join JobDescription ON JobDescription.JobDescriptionID = DerivedCandidate.JobDescriptionID group by JobDescriptionID;
I know there is already an accepted answer but in the effort of learning myself more about SQL here is an alternative way applying the case directly in the initial select without the sub-select:
Select
jd.id,
jd.name,
sum(case when c.source = 'a' then 1 else 0 end) as sourceA,
sum(case when c.source = 'b' then 1 else 0 end) as sourceB,
sum(case when c.source = 'c' then 1 else 0 end) as sourceC,
sum(case when c.source = 'd' then 1 else 0 end) as sourceD
from JobDescription as jd
join Candidate as c on c.jobId = jd.id
group by jd.id, jd.name
SQL Fiddle Demo
SELECT JD id, COUNT(Candidate), source
FROM table1
GROUP BY JD id,source

SQL query: Separate column where IDs are the same but type is different

Basically I get this from a simple select query:
SELECT Site.RefID, SiteName, Dates.Date, Dates.Type
FROM Site, Dates
WHERE Site.RefID = Dates.RefID;
RefID | SiteName | Date | Type
1 Sydney 06-12-15 OPENED
1 Sydney 08-12-15 CLOSED
2 Mel 17-12-15 OPENED
2 Mel 19-12-15 CLOSED
But I want to seperate it so tge result is similar to this:
RefID | SiteName | DateOPENED | DateCLOSED
1 Sydney 06-12-15 | 08-12-15
Basically I want to compare the data tracking details
Apologies in advance if this question isn't structured very well :/ I'm a complete beginner
I was thinking maybe a select within a select or possible case when's, but I can't seem to get either working
Try the following approach, using case expression:
select s.RefID
, s.Name
, min(case when d.Type == 'OPENED' then d.Date end) as DateOPENED
, min(case when d.Type == 'CLOSED' then d.Date end) as DateCLOSED
from Site s
join Dates d on s.RefID = d.RefID
group by s.RefID, s.Name
You can use conditional aggregation to get expected result:
SELECT Site.RefID, SiteName,
MIN(CASE WHEN Dates.Type = 'OPENED' THEN Dates.Date END) DateOPENED,
MAX(CASE WHEN Dates.Type = 'CLOSED' THEN Dates.Date END) DateCLOSED
FROM Site
INNER JOIN Dates ON Site.RefID = Dates.RefID
GROUP BY Site.RefID, SiteName
Also, it is always preferable to use explicit instead of implicit join syntax.
Add a GROUP BY to your current query, use MIN for opening date, and MAX for en closing date.
SELECT Site.RefID, SiteName, MIN(Dates.Date) as DateOPENED, MIN(Dates.Date) as DateCLOSED
FROM Site
JOIN Dates ON Site.RefID = Dates.RefID
group by fID, SiteName
Alternatively, JOIN once for opening and once for closing:
SELECT Site.RefID, SiteName, do.Date as DateOPENED, dc.Date as DateCLOSED
FROM Site
LEFT JOIN (select Refid, Date from Dates where Type = 'OPENED') do ON Site.RefID = do.RefID
LEFT JOIN (select Refid, Date from Dates where Type = 'CLOSED') dc ON Site.RefID = dc.RefID
SELECT A.RefId, A.SiteName, A.Date DateOpened, B.Date DateClosed
FROM #tbl A JOIN #tbl B
ON A.RefId = B.RefID
AND A.Type = 'OPENED'
AND B.Type = 'CLOSED'
For the sake of simplicity, have replaced the query with #tbl(you can deal with it howsoever you'd like to).

Selecting in GroupBy

I have a following query
select YEAR(t1.date) as 'year'
, MONTHNAME(t1.date) as 'month'
, COUNT(*) as total
, if (t1.Sex = 1, 'male','female') as sex
from outpatients t1
where YEAR(t1.date) = 2015
group by MONTH(t1.date), t1.Sex
order by t1.date, t1.Sex
So the output will look like this:
I would like to write a query to see female and male as the columns.
So the output will look like.
I can't find the way to group the data
You can use SUM function to sum the male and female
select YEAR(t1.date) as year, MONTHNAME(t1.date) as month, COUNT(*) as total
, SUM(CASE WHEN sex = 'male' THEN 1 ELSE 0 END) AS male
, SUM(CASE WHEN sex = 'female' THEN 1 ELSE 0 END) AS female
from outpatients t1
where YEAR(t1.date) = 2015
group by MONTH(t1.date), t1.Sex
order by t1.date, t1.Sex