I need to group the sum by different columns:
SUM1 group by Banco_Recarga
SUM2 group by BancoRetiro
SELECT Banco_Recarga, BancoRetiro, bancos_recargas.banco,
SUM(CASE WHEN Tipo='1' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS SUM1,
SUM(CASE WHEN Tipo='2' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS SUM2
FROM transaccionesrr
INNER JOIN bancos_recargas ON (transaccionesrr.Banco_Recarga = bancos_recargas.id)
WHERE Fecha_Recarga BETWEEN '2017-11-01' AND '2017-11-10' GROUP BY Banco_Recarga ORDER BY SUM1;
I have been looking for hours but i can't find a solution
Try this:
SELECT
CASE
WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN Banco_Recarga
WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN BancoRetiro
ELSE 'Unexpected'
END AS banco
, SUM(CASE WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS sum1
, SUM(CASE WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS sum2
FROM transaccionesrr
INNER JOIN bancos_recargas ON (transaccionesrr.Banco_Recarga = bancos_recargas.id)
WHERE Fecha_Recarga BETWEEN '2017-11-01' AND '2017-11-10'
GROUP BY
CASE
WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN Banco_Recarga
WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN BancoRetiro
ELSE 'Unexpected'
END
ORDER BY sum1, sum2
You may need to add conditions into the where clause.
Related
How to make one row in different rows and column
SELECT
internal_id, store_id, user_id, shift_info,
DATE(log_datetime) dates,
(case when log_action = '0' then TIME(log_datetime) end) as time_in,
(case when log_action = '0' then CONCAT(log_lat, ",", log_lng) end) as loc_in,
(case when log_action = '1' then TIME(log_datetime) end) as time_out,
(case when log_action = '1' then CONCAT(log_lat, ",", log_lng) end) as loc_out
FROM
attendance_store_user
WHERE
user_id = "A4CBD64F-D21C-5612-CCF5-497892B62E76"
i want result like this :
You could try using a join of the same table filter for null time_out and time_in
select a.dates, a.store_id, a,time_in, b.time_out
FROM attendance_store_user a
INNER JOIN attendance_store_user b on a.dates = b.dates
and a.user_id = b.user_id
and a.time_out is null
and b.time_in is null
WHERE a.user_id = "A4CBD64F-D21C-5612-CCF5-497892B62E76"
There is I am doing mistake in my mysql query. can anyone help me out??
select * from user_errors where user_id = '2'
IF( type = 'custom_type' ) second_user_id != '2' ELSE second_user_id = '2'
END IF
There is no necessary to use if else, anyway try this;)
select * from user_errors
where user_id = '2'
and ((type = 'custom_type' and second_user_id != '2') or second_user_id = '2')
Or you must use it, try this;)
select * from user_errors
where user_id = '2'
and case when type = 'custom_type' then second_user_id != '2' else second_user_id = '2' end
Use like this( You don't need to use if else condition)
select * from user_errors where user_id = '2'
and ((type = 'custom_type' and second_user_id != '2')
or (type != 'custom_type' and second_user_id = '2'))
try this
select * from user_errors
where user_id = '2'
IF(type = 'custom_type', second_user_id != '2', second_user_id = '2' )
SELECT * FROM user_errors
WHERE user_id = '2'AND ( case when type = 'custom_type' then second_user_id != '2' else second_user_id = '2' end)
I have huge amount of MySQL record data and must be tranformed like this :
My question is, how to extract datetime value (month, week, hour) and insert it into exact column with binary value (0 or 1) as image above.
PROBLEM SOLVED, i just figured out the easiest way, here for the example:
UPDATE TABLE
SET h1 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '1' THEN 1
ELSE '0'
END
, h2 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '2' THEN '1'
ELSE '0'
END
, h3 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '3' THEN '1'
ELSE '0'
END
, h4 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '4' THEN '1'
ELSE '0'
END
, h5 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '5' THEN '1'
ELSE '0'
END
, h6 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '6' THEN '1'
ELSE '0'
END
, h7 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '7' THEN '1'
ELSE '0'
END
;
PROBLEM SOLVED, i just figured out the easiest way, here for the example:
UPDATE TABLE
SET h1 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '1' THEN 1
ELSE '0'
END
, h2 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '2' THEN '1'
ELSE '0'
END
, h3 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '3' THEN '1'
ELSE '0'
END
, h4 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '4' THEN '1'
ELSE '0'
END
, h5 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '5' THEN '1'
ELSE '0'
END
, h6 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '6' THEN '1'
ELSE '0'
END
, h7 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '7' THEN '1'
ELSE '0'
END
;
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
Hi friends can any one please help me to optimize this query ,it takes more than 30 sec.
any suggestion is warmly welcome.
Query :
SELECT
CONCAT(CCD.CONTACT_FIRST_NAME, ' ', CCD.CONTACT_LAST_NAME) AS NAME,
A.EMAIL_IDS,
CCD.UNSUBSCRIBE,
IFNULL(CSL.LOG_DATE, '') AS LOG_DATE,
CSL.IP_ADDRESS,
IF(CSL.BROWSER IS NULL, '', CSL.BROWSER) AS BROWSER,
(CASE
WHEN (UNSUBSCRIBE = 0 OR UNSUBSCRIBE IS NULL) THEN 'Opted In'
ELSE (CASE
WHEN (UNSUBSCRIBE = 1) THEN 'Opted Out'
ELSE (CASE
WHEN
((UNSUBSCRIBE = 2 OR UNSUBSCRIBE = 3)
AND CCD.CONTACT_ID NOT IN (SELECT
CONTACT_ID
FROM
CM_SUBSCRIPTION_MAIL_DATA
WHERE
IS_MAIL_SENT = 'Y'))
THEN
'Opt-In Request not Sent'
ELSE 'Opt-In Pending'
END)
END)
END) AS CURR_SUB,
(CASE
WHEN (SUBSCRIBE_FROM = 0) THEN 'Opted In'
ELSE (CASE
WHEN (SUBSCRIBE_FROM = 1) THEN 'Opted Out'
ELSE (CASE
WHEN (SUBSCRIBE_FROM = 2 OR SUBSCRIBE_FROM = 3) THEN 'Opt-In Pending'
ELSE ''
END)
END)
END) AS SUB_FROM,
SUBSCRIBE_FROM,
(CASE
WHEN (SUBSCRIBE_TO = 0) THEN 'Opted In'
ELSE (CASE
WHEN (SUBSCRIBE_TO = 1) THEN 'Opted Out'
ELSE (CASE
WHEN (SUBSCRIBE_TO = 2 OR SUBSCRIBE_TO = 3) THEN 'Opt-In Pending'
ELSE ''
END)
END)
END) AS SUB_TO,
SUBSCRIBE_TO
FROM
CM_CONTACT_DETAILS CCD
LEFT JOIN
ADDRESS A ON CCD.CONTACT_ID = A.FOREIGN_ID
LEFT JOIN
CM_SUBSCRIPTION_LOGS CSL ON CSL.CONTACT_ID = CCD.CONTACT_ID
WHERE
1 = 1
GROUP BY CSL.LOG_DATE , CCD.CONTACT_ID
ORDER BY NAME DESC , LOG_DATE DESC
LIMIT 0 , 20
Your case/when can be simplified, but I believe the killer of your query was the correlated NOT IN SELECT clause for your Opt-in request not sent.
I've change your query to do a left-join to your mailing table based on the contact AND the mailed status of "Y". So this simplifies your case to only have to check for IS NULL of the mailing table contact ID.
You can't do too much on performance since your Group by is by columns from different tables that won't take advantage of any index optimization, then all that ordered by the name makes it more of an issue... but the correlated subquery per the field being executed every time to a left-join SHOULD help.
SELECT
CONCAT(CCD.CONTACT_FIRST_NAME, ' ', CCD.CONTACT_LAST_NAME) AS NAME,
A.EMAIL_IDS,
CCD.UNSUBSCRIBE,
IFNULL(CSL.LOG_DATE, '') AS LOG_DATE,
CSL.IP_ADDRESS,
IF(CSL.BROWSER IS NULL, '', CSL.BROWSER) AS BROWSER,
CASE WHEN UNSUBSCRIBE = 0 OR UNSUBSCRIBE IS NULL THEN 'Opted In'
WHEN UNSUBSCRIBE = 1 THEN 'Opted Out'
WHEN UNSUBSCRIBE IN (2,3) AND SMD.CONTACT_ID IS NULL THEN 'Opt-In Request not Sent'
ELSE 'Opt-In Pending' END AS CURR_SUB,
CASE WHEN SUBSCRIBE_FROM = 0 THEN 'Opted In'
WHEN SUBSCRIBE_FROM = 1 THEN 'Opted Out'
WHEN SUBSCRIBE_FROM = 2 OR SUBSCRIBE_FROM = 3 THEN 'Opt-In Pending'
ELSE '' END AS SUB_FROM,
SUBSCRIBE_FROM,
CASE WHEN SUBSCRIBE_TO = 0 THEN 'Opted In'
WHEN SUBSCRIBE_TO = 1 THEN 'Opted Out'
WHEN SUBSCRIBE_TO = 2 OR SUBSCRIBE_TO = 3 THEN 'Opt-In Pending'
ELSE '' END AS SUB_TO,
SUBSCRIBE_TO
FROM
CM_CONTACT_DETAILS CCD
LEFT JOIN ADDRESS A
ON CCD.CONTACT_ID = A.FOREIGN_ID
LEFT JOIN CM_SUBSCRIPTION_LOGS CSL
ON CSL.CONTACT_ID = CCD.CONTACT_ID
LEFT JOIN CM_SUBSCRIPTION_MAIL_DATA SMD
ON CCD.CONTACT_ID = SMD.CONTACT_ID
AND IS_MAIL_SENT = 'Y'
WHERE
1 = 1
GROUP BY
CSL.LOG_DATE,
CCD.CONTACT_ID
ORDER BY
NAME DESC,
LOG_DATE DESC
LIMIT
0, 20