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`
Related
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
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.
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'
-------------------------------------------------
SELECT trtm,
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
(SELECT
(SUM( tshares ) AS total WHERE trtm = 'TM'),
(SUM( tshares ) AS total1 WHERE trtm = 'TR'))
FROM transfer_file
where t_date between '10/1/1992' and '10/2/1992'
-----------------------------------------------
How to get the sum of values for specific fields,i am getting the count of value but not getting the sum of value,what to do? help me if any body knows
You should use conditional aggregation inside the SUM too:
SELECT trtm,
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
SUM(CASE WHEN trtm = 'TM' THEN tshares END) AS Total_Transmission,
SUM(CASE WHEN trtm = 'TR' THEN tshares END) AS Total_Transfer
FROM transfer_file
WHERE t_date between '10/1/1992' and '10/2/1992'
How do I get the Total value of Yes, No, Other fields of each username?
I like to add Total field.
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE NULL END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE NULL END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE NULL END) as Other
//How to get total of Yes/No/Other
FROM table
WHERE source = 'CompanyName' ";
Also the highest Total goes at the top order.
use 0 instead of NULL, add the missing group by and use COUNT(*) to get the total of each group and order the result:
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other,
COUNT(*) as TOTAL
FROM table
WHERE source = 'CompanyName'
group by Username
order by TOTAL desc;
This assumes that type can only be 'Yes', 'No' or ''.
Use a sub query to sum up your results and add a sort:
select yes, no, other, yes + no + other as Total
from (
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other
FROM table
WHERE source = 'CompanyName'
)
order by (yes + no + other) desc
Don't use SUM(null), the SUM of (1,1,1,null) = null, not 3.
SELECT s.*, s.yes+s.no+s.other as all FROM (
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other
FROM table
WHERE source = 'CompanyName'
GROUP BY Username
) s
ORDER BY all DESC