Currently, my final working transform in MySQL is working as needed. See SQL Fiddle with query below:
SELECT idt.fldDRCClientID
, idt.fldLastName
, COALESCE(grp.fldCreditorName1, '') as fldCreditorName1
, COALESCE(grp.fldDebtAccountNumber1, '') as fldDebtAccountNumber1
, COALESCE(grp.fldEnrolledDebt1, '') as fldEnrolledDebt1
, COALESCE(grp.fldCreditorName2, '') as fldCreditorName2
, COALESCE(grp.fldDebtAccountNumber2, '') as fldDebtAccountNumber2
, COALESCE(grp.fldEnrolledDebt2, '') as fldEnrolledDebt2
, COALESCE(grp.fldCreditorName3, '') as fldCreditorName3
, COALESCE(grp.fldDebtAccountNumber3, '') as fldDebtAccountNumber3
, COALESCE(grp.fldEnrolledDebt3, '') as fldEnrolledDebt3
, COALESCE(grp.fldCreditorName4, '') as fldCreditorName4
, COALESCE(grp.fldDebtAccountNumber4, '') as fldDebtAccountNumber4
, COALESCE(grp.fldEnrolledDebt4, '') as fldEnrolledDebt4
, COALESCE(grp.fldCreditorName5, '') as fldCreditorName5
, COALESCE(grp.fldDebtAccountNumber5, '') as fldDebtAccountNumber5
, COALESCE(grp.fldEnrolledDebt5, '') as fldEnrolledDebt5
, COALESCE(grp.fldCreditorName6, '') as fldCreditorName6
, COALESCE(grp.fldDebtAccountNumber6, '') as fldDebtAccountNumber6
, COALESCE(grp.fldEnrolledDebt6, '') as fldEnrolledDebt6
FROM tblClients idt
LEFT JOIN (
SELECT d.IndividualNumber as IndividualNumber
, MAX(CASE WHEN row = 1 THEN d.fldCreditorName END) AS fldCreditorName1
, MAX(CASE WHEN row = 1 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber1
, MAX(CASE WHEN row = 1 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt1
, MAX(CASE WHEN row = 2 THEN d.fldCreditorName END) AS fldCreditorName2
, MAX(CASE WHEN row = 2 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber2
, MAX(CASE WHEN row = 2 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt2
, MAX(CASE WHEN row = 3 THEN d.fldCreditorName END) AS fldCreditorName3
, MAX(CASE WHEN row = 3 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber3
, MAX(CASE WHEN row = 3 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt3
, MAX(CASE WHEN row = 4 THEN d.fldCreditorName END) AS fldCreditorName4
, MAX(CASE WHEN row = 4 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber4
, MAX(CASE WHEN row = 4 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt4
, MAX(CASE WHEN row = 5 THEN d.fldCreditorName END) AS fldCreditorName5
, MAX(CASE WHEN row = 5 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber5
, MAX(CASE WHEN row = 5 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt5
, MAX(CASE WHEN row = 6 THEN d.fldCreditorName END) AS fldCreditorName6
, MAX(CASE WHEN row = 6 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber6
, MAX(CASE WHEN row = 6 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt6
FROM
(
SELECT #row := CASE WHEN tblClients_fldDRCClientID = #id
THEN #row + 1 ELSE 1 END as row
, #id := tblClients_fldDRCClientID as IndividualNumber
, inf.fldCreditorName
, inf.fldDebtAccountNumber
, inf.fldEnrolledDebt
FROM (SELECT #row := 0, #id := 0) v
, tblDebtAccountNumber as inf
ORDER BY inf.tblClients_fldDRCClientID
) d
GROUP BY d.IndividualNumber
) grp
ON grp.IndividualNumber = idt.fldDRCClientID
;
However, I want to take this one step further. I need to include/join two additional tables (T3 and T4) to the mix. These two tables are both foreign key tables to the tblClients where:
idt.fldDRCClientID is the PK of tblClients;
tblClients_fldDRCClientID is foreign key for both T3 and T4.
I have tried some additional inner joins and sub selects but they both fail. Looking for the proper places to add the syntax for T3 and T4 to get this functioning. Any ideas or suggestions would be appreciated.
Related
I have a query that works but in addition to what I already have I want to add one extra column for each category free,reduced,paid and certified free with the percentage compared to the total number of students. Can anyone help me?
select
count(case when Lunchstatus = 'P' then 1 else null end) as Paid
, count(case when LunchStatus = 'R' then 1 else null end) as Reduced
, count(case when LunchStatus = 'F' then 1 else null end) as Free
, count(case when LunchStatus = 'fdc' then 1 else null end) as CertifiedFree
, count(case when LunchStatus = 'P' then 1
when LunchStatus = 'fdc' then 1
when LunchStatus = 'R' then 1
when LunchStatus = 'F' then 1
else null
end) as Total
from students
where enroll_status = 0
and schoolid = %param1%
Treat you existing query as a derived table, cross join for the total count, then calculate your percentages:
select
d.*
, d.Paid * 100.0 / cj.totcount as paid_pct
... more like that
from (
select
count(case when Lunchstatus = 'P' then 1 else null end) as Paid
, count(case when LunchStatus = 'R' then 1 else null end) as Reduced
, count(case when LunchStatus = 'F' then 1 else null end) as Free
, count(case when LunchStatus = 'fdc' then 1 else null end) as CertifiedFree
, count(case when LunchStatus = 'P' then 1
when LunchStatus = 'fdc' then 1
when LunchStatus = 'R' then 1
when LunchStatus = 'F' then 1
else null
end) as Total
from students
where enroll_status = 0
and schoolid = %param1%
) d
CROSS JOIN (
select count(*) as totcount
from students
where enroll_status = 0
and schoolid = %param1%
) cj
I have two tables in a one to many relationship. In table 2, I have three columns I am looking to transform into row data. Why? these will be used to merge with another process (as a data source) for producing documents. Please see the attachment for the breakdown and the related output.
Here is the first successful query having the accurate information needed.
SELECT
tblClients.fldDRCClientID,
fldLastName,
e1.fldCreditorName,
e1.fldDebtAccountNumber,
e1.fldEnrolledDebt
From tblClients
Left Join tblDebtAccountNumber as e1 ON e1.tblClients_fldDRCClientID = tblClients.fldDRCClientID;
First query results:
(Posted on behalf of the question author to move it from the comments).
Here is my query:
SELECT idt.fldDRCClientID
, idt.fldLastName
, COALESCE(grp.fldCreditorName1, '') as fldCreditorName1
, COALESCE(grp.fldDebtAccountNumber1, '') as fldDebtAccountNumber1
, COALESCE(grp.fldEnrolledDebt1, '') as fldEnrolledDebt1
, COALESCE(grp.fldCreditorName2, '') as fldCreditorName2
, COALESCE(grp.fldDebtAccountNumber2, '') as fldDebtAccountNumber2
, COALESCE(grp.fldEnrolledDebt2, '') as fldEnrolledDebt2
, COALESCE(grp.fldCreditorName3, '') as fldCreditorName3
, COALESCE(grp.fldDebtAccountNumber3, '') as fldDebtAccountNumber3
, COALESCE(grp.fldEnrolledDebt3, '') as fldEnrolledDebt3
, COALESCE(grp.fldCreditorName4, '') as fldCreditorName4
, COALESCE(grp.fldDebtAccountNumber4, '') as fldDebtAccountNumber4
, COALESCE(grp.fldEnrolledDebt4, '') as fldEnrolledDebt4
, COALESCE(grp.fldCreditorName5, '') as fldCreditorName5
, COALESCE(grp.fldDebtAccountNumber5, '') as fldDebtAccountNumber5
, COALESCE(grp.fldEnrolledDebt5, '') as fldEnrolledDebt5
, COALESCE(grp.fldCreditorName6, '') as fldCreditorName6
, COALESCE(grp.fldDebtAccountNumber6, '') as fldDebtAccountNumber6
, COALESCE(grp.fldEnrolledDebt6, '') as fldEnrolledDebt6
FROM tblClients idt
LEFT JOIN (
SELECT d.IndividualNumber as IndividualNumber
, MAX(CASE WHEN row = 1 THEN d.fldCreditorName END) AS fldCreditorName1
, MAX(CASE WHEN row = 1 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber1
, MAX(CASE WHEN row = 1 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt1
, MAX(CASE WHEN row = 2 THEN d.fldCreditorName END) AS fldCreditorName2
, MAX(CASE WHEN row = 2 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber2
, MAX(CASE WHEN row = 2 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt2
, MAX(CASE WHEN row = 3 THEN d.fldCreditorName END) AS fldCreditorName3
, MAX(CASE WHEN row = 3 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber3
, MAX(CASE WHEN row = 3 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt3
, MAX(CASE WHEN row = 4 THEN d.fldCreditorName END) AS fldCreditorName4
, MAX(CASE WHEN row = 4 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber4
, MAX(CASE WHEN row = 4 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt4
, MAX(CASE WHEN row = 5 THEN d.fldCreditorName END) AS fldCreditorName5
, MAX(CASE WHEN row = 5 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber5
, MAX(CASE WHEN row = 5 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt5
, MAX(CASE WHEN row = 6 THEN d.fldCreditorName END) AS fldCreditorName6
, MAX(CASE WHEN row = 6 THEN d.fldDebtAccountNumber END) AS fldDebtAccountNumber6
, MAX(CASE WHEN row = 6 THEN d.fldEnrolledDebt END) AS fldEnrolledDebt6
FROM
(
SELECT #row := CASE WHEN tblClients_fldDRCClientID = #id
THEN #row + 1 ELSE 1 END as row
, #id := tblClients_fldDRCClientID as IndividualNumber
, inf.fldCreditorName
, inf.fldDebtAccountNumber
, inf.fldEnrolledDebt
FROM (SELECT #row := 0, #id := 0) v
, tblDebtAccountNumber as inf
ORDER BY inf.tblClients_fldDRCClientID
) d
GROUP BY d.IndividualNumber
) grp
ON grp.IndividualNumber = idt.fldDRCClientID
;
SQL Fiddle
I have the following table with some records.
Table: tbl_group_test
CREATE TABLE tbl_group_test
(
YearID varchar(20),
ProID varchar(20),
CodeSecID varchar(20),
CodeMerID int,
val int,
cid varchar(20)
);
Records Insertion:
INSERT INTO tbl_group_test VALUES
('2017000001','4PO251','1IJ25',1,0,'a'),
('2017000002','4PO241','1IJ25',1,0,'a'),
('2017000003','4PO272','1IJ25',1,0,'a'),
('2017000004','4PO243','1IJ25',1,0,'a'),
('2017000005','4PO276','1IJ25',1,0,'a'),
('2017000006','4PO251','1IJ25',1,0,'a'),
('2017000007','4PO249','1IJ25',1,0,'a'),
('2017000008','4PO278','1IJ25',1,0,'a'),
('2017000009','4PO240','1IJ25',1,0,'a'),
('2017000010','4PO290','1IJ25',1,0,'a'),
('2017000011','4PO251','1IJ25',1,0,'b'),
('2017000012','4PO241','1IJ25',1,0,'b'),
('2017000013','4PO272','1IJ25',1,0,'b'),
('2017000014','4PO243','1IJ25',1,0,'b'),
('2017000015','4PO276','1IJ25',1,0,'b'),
('2017000016','4PO251','1IJ25',1,0,'b'),
('2017000017','4PO241','1IJ25',1,0,'b'),
('2017000018','4PO272','1IJ25',1,0,'b'),
('2017000019','4PO243','1IJ25',1,0,'b'),
('2017000020','4PO276','1IJ25',1,0,'b');
Note: Now i want to create a single group ID for every 10 records and want to show a pivot table based on that group id.
Expected Result:
Group ID YearID1 YearID2 ..... YearID10 CodeSecID CodeMerID val cid
1 2017000001 2017000002 2017000010 1IJ25 1 0 a
2 2017000011 2017000012 2017000020 1IJ25 1 0 b
select
max(case when s.col = 1 then s.yearid else '' end) as yrid1,
max(case when s.col = 2 then s.yearid else '' end) as yrid2,
max(case when s.col = 3 then s.yearid else '' end) as yrid3,
max(case when s.col = 4 then s.yearid else '' end) as yrid4,
max(case when s.col = 5 then s.yearid else '' end) as yrid5,
max(case when s.col = 6 then s.yearid else '' end) as yrid6,
max(case when s.col = 7 then s.yearid else '' end) as yrid7,
max(case when s.col = 8 then s.yearid else '' end) as yrid8,
max(case when s.col = 9 then s.yearid else '' end) as yrid9,
max(case when s.col = 10 then s.yearid else '' end) as yrid10,
max(codesecid) codesecid,
max(codemerid) codemerid,
max(val) val,
max(cid) cid
from
(
select t.* ,
if(#rn > 9, #rn := 1,#rn:=#rn +1) col,
if(#rn = 1, #block:=#block + 1,#block:=#block) block
from (select #block:=0,#rn:=0) rn, tbl_group_test t
order by yearid
) s
group by s.block
I try to make a query which can INSERT data quickly, before I execute query with INSERT statement I build a query which contains SELECT SUM CASE and INNER JOIN.
And my code contains error. Here's my code :
SELECT pkg_guru.id_pkg ,
pkg_guru.id_guru ,
guru.nama_guru ,
guru.mapel_diampu ,
pkg_guru.id_penilai ,
penilai.nama_penilai ,
pkg_guru.tahun ,
pkg_guru.sem ,
pkg_guru.kompetensi
SUM(CASE WHEN pkg_guru.kompetensi = "1" THEN pkg_guru.jawaban
ELSE 0
END) AS nilaikp1 ,
SUM(CASE WHEN pkg_guru.kompetensi = "2" THEN pkg_guru.jawaban
ELSE 0
END) AS nilaikp2 ,
SUM(CASE WHEN pkg_guru.kompetensi = "3" THEN pkg_guru.jawaban
ELSE 0
END) AS nilaikp3 ,
SUM(CASE WHEN pkg_guru.kompetensi = "4" THEN pkg_guru.jawaban
ELSE 0
END) AS nilaikp4 ,
SUM(pkg_guru.jawaban) AS total_skor
FROM pkg_guru
INNER JOIN guru ON pkg_guru.id_guru = guru.id_guru
INNER JOIN penilai ON pkg_guru.id_penilai = penilai.id_penilai
GROUP BY pkg_guru.id_pkg ,
pkg_guru.id_guru ,
guru.nama_guru ,
guru.mapel_diampu ,
pkg_guru.id_penilai ,
penilai.nama_penilai
Can you tell me what's wrong in this query code?
There's a comma missing after pg.kompetensi and you haven't included all of the required columns in your GROUP BY:
SELECT pg.id_pkg ,
pg.id_guru ,
g.nama_guru ,
g.mapel_diampu ,
pg.id_penilai ,
p.nama_penilai ,
pg.tahun ,
pg.sem ,
pg.kompetensi ,
SUM(CASE WHEN pg.kompetensi = "1" THEN pg.jawaban
ELSE 0
END) AS nilaikp1 ,
SUM(CASE WHEN pg.kompetensi = "2" THEN pg.jawaban
ELSE 0
END) AS nilaikp2 ,
SUM(CASE WHEN pg.kompetensi = "3" THEN pg.jawaban
ELSE 0
END) AS nilaikp3 ,
SUM(CASE WHEN pg.kompetensi = "4" THEN pg.jawaban
ELSE 0
END) AS nilaikp4 ,
SUM(pg.jawaban) AS total_skor
FROM pkg_guru pg
INNER JOIN guru g ON pg.id_guru = g.id_guru
INNER JOIN penilai p ON pg.id_penilai = p.id_penilai
GROUP BY pg.id_pkg ,
pg.id_guru ,
g.nama_guru ,
g.mapel_diampu ,
pg.id_penilai ,
p.nama_penilai ,
pg.tahun ,
pg.sem ,
pg.kompetensi
You missed comma after pkg_guru.kompetensi at the third line. And pkg_guru.tahun,pkg_guru.sem,pkg_guru.kompetensi in GROUP BY section.
I have the MySQL query:
SELECT
transporttype,
concat(MONTHNAME(STR_TO_DATE(month, '%m')), ' ', year) AS `month`,
count(loadnumber) AS loads
FROM v2ReportingTable
WHERE (transporttype not in ('Extrusions-LongDistance','Extrusions-Shuttle') and urgent='no')
GROUP BY (concat(MONTHNAME(STR_TO_DATE(month, '%m')),' ',year)),transporttype
ORDER BY (concat(MONTHNAME(STR_TO_DATE(month, '%m')),' ',year)), transporttype
that produces the below output:
I want to display the results in column format, as below:
How can I modify my query to display the results as such.
Assistance is appreciated as always
SELECT `month`,
MAX(CASE WHEN transporttype = 'Inbound' THEN loads ELSE NULL END) `Inbound`,
MAX(CASE WHEN transporttype = 'LocalGauteng' THEN loads ELSE NULL END) `LocalGauteng`,
MAX(CASE WHEN transporttype = 'LongDistance' THEN loads ELSE NULL END) `LongDistance`,
MAX(CASE WHEN transporttype = 'Shuttle-company1' THEN loads ELSE NULL END) `Shuttle-company1`,
MAX(CASE WHEN transporttype = 'Shuttle-company2' THEN loads ELSE NULL END) `Shuttle-company2`,
MAX(CASE WHEN transporttype = 'stores' THEN loads ELSE NULL END) `stores`,
MAX(CASE WHEN transporttype = 'returns' THEN loads ELSE NULL END) `returns`,
MAX(CASE WHEN transporttype = 'localkzn' THEN loads ELSE NULL END) `localkzn`
FROM
(
SELECT transporttype,
CONCAT(MONTHNAME(STR_TO_DATE(month, '%m')), ' ', year) AS `month`,
COUNT(loadnumber) AS loads
FROM v2ReportingTable
WHERE transporttype not in ('Extrusions-LongDistance','Extrusions-Shuttle') AND
urgent='no'
GROUP BY (concat(MONTHNAME(STR_TO_DATE(month, '%m')),' ',year)), transporttype
) aa
GROUP BY `month`