MySQL Workbench : Create group ID and pivot table - mysql

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

Related

Represent MYSQL result with specific condition

i need help for represent my query result into specific report with condition
Here's my query result
after get the result i need represent it as report with some condition, it would be like this:
City A - Company A
City A - Company B
City A - Company C - Service A
I've no idea how to do it since I'm really new to MySQL.
I want to put the query into PHP.
Thanks in Advance
You can do it this way:
City A - Company A:
select pick_date, sum(Weight),
sum(case when site = 'SiteA' then 1 else 0 end) as SiteA,
sum(case when site = 'SiteB' then 1 else 0 end) as SiteB,
sum(case when site = 'SiteC' then 1 else 0 end) as SiteC
From tbl
where company_name = 'CompanyA'
group by pick_date
For City A - Company B, replace 'CompanyA' with CompanyB in the query
For Company C - Service A:
select pick_date, sum(Weight),
sum(case when site = 'SiteA' then 1 else 0 end) as SiteA,
sum(case when site = 'SiteB' then 1 else 0 end) as SiteB,
sum(case when site = 'SiteC' then 1 else 0 end) as SiteC
From tbl
where company_name = 'CompanyC' and service_name = 'ServiceA'
group by pick_date
This should do the trick. If your original query is also from SQL, put it into a temp table, and then using the following query will get you 3 answers.
SELECT
pickup_date
,SUM( CASE WHEN [site] = 'SiteA' THEN 1 ELSE 0 END) as [SiteA]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteB]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteC]
FROM
tbl
WHERE
[city] = 'CityA'
AND
[company_name] = 'CompanyA'
GROUP BY
pickup_date
SELECT
pickup_date
,SUM( CASE WHEN [site] = 'SiteA' THEN 1 ELSE 0 END) as [SiteA]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteB]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteC]
FROM
tbl
WHERE
[city] = 'CityA'
AND
[company_name] = 'CompanyB'
GROUP BY
pickup_date
SELECT
pickup_date
,SUM( CASE WHEN [site] = 'SiteA' THEN 1 ELSE 0 END) as [SiteA]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteB]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteC]
FROM
tbl
WHERE
[city] = 'CityA'
AND
[company_name] = 'CompanyC'
AND
[service_name] = 'ServiceA'
GROUP BY
pickup_date

SQL Query: Adding Percentage

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

Transform solved - now wanting to add two additional table joins

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.

How to use the CONCAT() function in MySQL?

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

SUM for more fields in mysql

I have a query,which give me wrong result.Here is my query.
SELECT sum(open1) open1, sum(closed1) closed,sum(pending1) NotSpecified,
status,type,sub_status,created_date,bystatus,lst_type
FROM (
SELECT agent_1_id,status,type,sub_status,created_date,'Open' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
count(*) as open1, 0 closed1, 0 pending1
FROM crm_mydeals
where status = 1 AND agent_1_id>0 and is_active=1
GROUP BY status
UNION ALL
SELECT agent_1_id,status,type,sub_status,created_date,'Closed' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
0 open1, count(*) as closed1, 0 pending1
FROM crm_mydeals
where status = 2 AND agent_1_id>0 and is_active=1
GROUP BY status
UNION ALL
SELECT agent_1_id,status,type,sub_status,created_date,'NotSpecified' as bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type,
0 open1, 0 closed1, count(*) as pending1
FROM crm_mydeals
where status = 3 AND agent_1_id>0 and is_active=1
GROUP BY status
) s
WHERE DATE(created_date) BETWEEN '2013-11-22' AND '2014-2-22'
GROUP BY status
If you simplify the query, it might help to find the correct answer?
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0) AS open1,
SUM(CASE WHEN status = 2 THEN 1 ELSE 0) AS closed,
SUM(CASE WHEN Status = 3 THEN 1 ELSE 0) AS NotSpecified,
status,
type,
sub_status,
created_date,
(CASE WHEN status = 1 THEN 'Open'
WHEN status = 2 THEN 'Closed'
WHEN status = 3 THEN 'NotSpecified') AS bystatus,
(CASE WHEN type = 1 THEN 'Rent'
WHEN type = 2 THEN 'Sale'
ELSE 'Not Specified' END) as lst_type
FROM crm_mydeals
WHERE status IN (1,2,3)
AND agent_1_id > 0
AND is_active=1
AND DATE(created_date) BETWEEN '2013-11-22' AND '2014-2-22'
GROUP BY status, type, sub_status, created_date