How do I edit my MySQL request max case outputs? - mysql

When I run the following query.
SELECT tblhostingaddons.id as id,
MAX(CASE WHEN (tblcustomfields.fieldname = 'boyut') THEN tblcustomfields.fieldoptions ELSE NULL END) AS quota,
MAX(CASE WHEN (tblcustomfields.fieldname = 'adet') THEN tblcustomfields.fieldoptions ELSE NULL END) AS pc,
MAX(CASE WHEN (tblcustomfields.fieldname = 'gonderimlimiti') THEN tblcustomfields.fieldoptions ELSE NULL END) AS send,
MAX(CASE WHEN (tblcustomfields.fieldname = 'turu') THEN tblcustomfields.fieldoptions ELSE NULL END) AS type
FROM `tblhostingaddons`
LEFT JOIN tbladdons ON tbladdons.id = tblhostingaddons.addonid
LEFT JOIN tblcustomfields ON tblcustomfields.relid = tbladdons.id
WHERE tblhostingaddons.hostingid = '88'
AND tblhostingaddons.status = 'Active'
GROUP BY tblhostingaddons.id
ORDER BY tblhostingaddons.id
The following result comes up. But I just want the type to come in lines that are epostapro.
id quota pc send type
74 5120 1 200 epostapro
75 NULL NULL NULL NULL

Try using having clause:
SELECT tblhostingaddons.id as id,
MAX(CASE WHEN (tblcustomfields.fieldname = 'boyut') THEN tblcustomfields.fieldoptions ELSE NULL END) AS boyut,
MAX(CASE WHEN (tblcustomfields.fieldname = 'adet') THEN tblcustomfields.fieldoptions ELSE NULL END) AS adet,
MAX(CASE WHEN (tblcustomfields.fieldname = 'gonderimlimiti') THEN tblcustomfields.fieldoptions ELSE NULL END) AS gonderimlimiti,
MAX(CASE WHEN (tblcustomfields.fieldname = 'turu') THEN tblcustomfields.fieldoptions ELSE NULL END) AS turu
FROM `tblhostingaddons`
LEFT JOIN tbladdons ON tbladdons.id = tblhostingaddons.addonid
LEFT JOIN tblcustomfields ON tblcustomfields.relid = tbladdons.id
WHERE tblhostingaddons.hostingid = '88'
AND tblhostingaddons.status = 'Active'
GROUP BY tblhostingaddons.id
having MAX(CASE WHEN (tblcustomfields.fieldname = 'turu') THEN tblcustomfields.fieldoptions ELSE NULL END)='epostapro'
ORDER BY tblhostingaddons.id
OR you can remove id from selection list as well as group by
like below:
SELECT
MAX(CASE WHEN (tblcustomfields.fieldname = 'boyut') THEN tblcustomfields.fieldoptions ELSE NULL END) AS boyut,
MAX(CASE WHEN (tblcustomfields.fieldname = 'adet') THEN tblcustomfields.fieldoptions ELSE NULL END) AS adet,
MAX(CASE WHEN (tblcustomfields.fieldname = 'gonderimlimiti') THEN tblcustomfields.fieldoptions ELSE NULL END) AS gonderimlimiti,
MAX(CASE WHEN (tblcustomfields.fieldname = 'turu') THEN tblcustomfields.fieldoptions ELSE NULL END) AS turu
FROM `tblhostingaddons`
LEFT JOIN tbladdons ON tbladdons.id = tblhostingaddons.addonid
LEFT JOIN tblcustomfields ON tblcustomfields.relid = tbladdons.id
WHERE tblhostingaddons.hostingid = '88'
AND tblhostingaddons.status = 'Active'

Related

MySQL Correct WHERE clause with a value pair table like Wordpress usermeta

I am trying to select data from a wordpress usermeta table of some members.
I have managed to use this SQL and this works by listing all the users
SELECT
wpdg_usermeta.user_id,
MAX(CASE when wpdg_usermeta.meta_key = 'firm_name' THEN wpdg_usermeta.meta_value ELSE NULL END) AS firm_name,
MAX(CASE when wpdg_usermeta.meta_key = 'member_name' THEN wpdg_usermeta.meta_value ELSE NULL END) AS member_name,
MAX(CASE when wpdg_usermeta.meta_key = 'firm_address' THEN wpdg_usermeta.meta_value ELSE NULL END) AS firm_address,
MAX(CASE when wpdg_usermeta.meta_key = 'firm_city' THEN wpdg_usermeta.meta_value ELSE NULL END) AS firm_city,
MAX(CASE when wpdg_usermeta.meta_key = 'member_email' THEN wpdg_usermeta.meta_value ELSE NULL END) AS member_email,
MAX(CASE when wpdg_usermeta.meta_key = 'member_directory' THEN wpdg_usermeta.meta_value ELSE NULL END) AS member_directory
FROM wpdg_usermeta
GROUP BY wpdg_usermeta.user_id
However I want to filter only those members that agreed to appear in the member directory, so I tried to add this
WHERE
wpdg_usermeta.meta_key = 'member_directory' AND wpdg_usermeta.meta_value IN ('Yes')
However, then I get 2 rows which do have member_directory = Yes, which is correct, but all the other fields in the results except user_id are Null
also tried
wpdg_usermeta.meta_key = 'member_directory' AND wpdg_usermeta.meta_value = 'Yes'
Same result
Sorry, haven't tried selecting from a value pair table before and I'm a bit stumped
You can use a having clause instead:
having member_directory is not null

How do I combine multiple rows with matching ID's into separate columns in SQL?

I'm not too experienced with SQL, so not sure if a similar question has already been answered (I'm sure there is, but I'm way out of my depth here). I used to be great at SQL many years back, but it's been so long that mind is foggy.
I'm migrating customers from one CMS to a different CMS.
Site A = Old CMS
Site B = New CMS
In Site A's database, we have a table "p20kx_users" which contains the basic info - unique ID, username and email.
We then have a table "p20kx_virtuemart_userinfos" which contains in essence, two sets of data; shipping addresses and billing addresses.
In the "p20kx_virtuemart_userinfos" table, some users have just one row (the billing address), however most users have two rows (shipping AND billing addresses). There is a column in the "p20kx_virtuemart_userinfos" table named "address_type" that determines whether the row is Billing (BT) or Shipping (ST). There are also instances where users have neither, so that row shows up as NULL.
I've written a basic query that appears to retrieve all of the details I need, however I'm looking for a way so that the shipping and billing addresses appear on one row, in separate columns, so that I can export the results of the query directly to CSV and import to Site B.
Here's my query (I'm running this via PHPMyAdmin on a MariaDB 10.2 DB):
SELECT
p20kx_users.id,
p20kx_users.name,
p20kx_users.username,
p20kx_users.email,
p20kx_virtuemart_userinfos.address_type,
p20kx_virtuemart_userinfos.company,
p20kx_virtuemart_userinfos.title,
p20kx_virtuemart_userinfos.first_name,
p20kx_virtuemart_userinfos.last_name,
p20kx_virtuemart_userinfos.phone_1,
p20kx_virtuemart_userinfos.phone_2,
p20kx_virtuemart_userinfos.address_1,
p20kx_virtuemart_userinfos.address_2,
p20kx_virtuemart_userinfos.city,
p20kx_virtuemart_userinfos.zip
FROM p20kx_users
LEFT JOIN p20kx_virtuemart_userinfos
ON p20kx_users.id=p20kx_virtuemart_userinfos.virtuemart_user_id
I've had a good old google, and attempted the below which returns one result with mixed up details from other users.
SELECT
p20kx_users.id,
p20kx_users.name,
p20kx_users.username,
p20kx_users.email,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.company ELSE NULL END) Billing_Company,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.title ELSE NULL END) Billing_Title,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.first_name ELSE NULL END) Billing_Fname,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.last_name ELSE NULL END) Billing_LName,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.phone_1 ELSE NULL END) Billing_Phone1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.phone_2 ELSE NULL END) Billing_Phone2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.address_1 ELSE NULL END) Billing_Add1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.address_2 ELSE NULL END) Billing_Add2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.city ELSE NULL END) Billing_City,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'BT' THEN p20kx_virtuemart_userinfos.zip ELSE NULL END) Billing_Zip,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.company ELSE NULL END) Shipping_Company,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.title ELSE NULL END) Shipping_Title,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.first_name ELSE NULL END) Shipping_Fname,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.last_name ELSE NULL END) Shipping_LName,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.phone_1 ELSE NULL END) Shipping_Phone1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.phone_2 ELSE NULL END) Shipping_Phone2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.address_1 ELSE NULL END) Shipping_Add1,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.address_2 ELSE NULL END) Shipping_Add2,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.city ELSE NULL END) Shipping_City,
MAX(CASE WHEN p20kx_virtuemart_userinfos.address_type = 'ST' THEN p20kx_virtuemart_userinfos.zip ELSE NULL END) Shipping_Zip
FROM p20kx_users
LEFT JOIN p20kx_virtuemart_userinfos
ON p20kx_users.id=p20kx_virtuemart_userinfos.virtuemart_user_id
Can someone tell me where I am going wrong, and how I should address this - I've spent half a day looking at this. The longer I look, the more confused I get.
Your query is close. You need a GROUP BY:
SELECT u.id, u.name, u.username, u.email,
MAX(CASE WHEN ui.address_type = 'BT' THEN ui.company END) as Billing_Company,
. . .
FROM p20kx_users u LEFT JOIN
p20kx_virtuemart_userinfos ui
ON u.id = ui.virtuemart_user_id
GROUP BY u.id, u.name, u.username, u.email;
I also introduced table aliases, so the query is easier to write and to read. And I removed the ELSE NULL, because that is redundant.

MySQL Workbench : Create group ID and pivot table

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

How to fetch data in one row using case condition

I have two tables and i wanted data should come in specific format.
My query
SELECT
DISTINCT ur.suid,
(CASE WHEN ur.sceneID = '1' THEN ur.data ELSE 0 END) AS Scenario1data,
(CASE WHEN ur.sceneID = '1' THEN ss.score ELSE 0 END) AS Scenario1score,
(CASE WHEN ur.sceneID = '2' THEN ur.data ELSE 0 END) AS Scenario2,
(CASE WHEN ur.sceneID = '3' THEN ur.data ELSE 0 END) AS Scenario3,
(CASE WHEN ur.sceneID = '4' THEN ur.data ELSE 0 END) AS Scenario4
FROM tbluserresults AS ur LEFT JOIN tbluserscenescores AS ss ON ss.suid = ur.suid;
Result i wanted
suid Scenario1data Scenario1score Scenario2 Scenario3 Scenario4
1 'abc show1' 334 abc show2 abc show3 abc show 4
2 0 0 0 0.
Check my SQLFIDDLE
There are probably other better ways to do this, but I believe the following does what you want
SELECT results.suid as suid,
(CASE WHEN results.Scenario1data IS NOT NULL THEN results.Scenario1data ELSE 0 END) AS Scenario1data,
(CASE WHEN results.Scenario1score IS NOT NULL THEN results.Scenario1score ELSE 0 END) AS Scenario1score,
(CASE WHEN results.Scenario2 IS NOT NULL THEN results.Scenario2 ELSE 0 END) AS Scenario2,
(CASE WHEN results.Scenario3 IS NOT NULL THEN results.Scenario3 ELSE 0 END) AS Scenario3,
(CASE WHEN results.Scenario4 IS NOT NULL THEN results.Scenario4 ELSE 0 END) AS Scenario4
FROM
(SELECT DISTINCT(suid), #suid:= suid,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '1') AS Scenario1data,
(SELECT ss.score FROM tbluserscenescores ss WHERE ss.suid = #suid AND ss.sceneID = '1') AS Scenario1score,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '2') AS Scenario2,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '3') AS Scenario3,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '4') AS Scenario4
FROM tbluserscenescores) AS results

convert mysql output from rows to columns

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`