SQL Query return zero if is NOT in list - mysql

I have the followin
SELECT
au.country as country_code,
COALESCE(SUM(uwm.amount), 0) as amountInbound
FROM user_wallet_movement uwm
LEFT JOIN user_wallet uw ON uwm.wallet_id = uw.id
LEFT JOIN app_user au ON uw.user_id = au.id
WHERE
status = 'execute'
and direction = 'inbound'
and mov_date > '2020-07-01'
and au.country IN ('AD', 'AC', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'xA', 'xE', 'xF', 'XK', 'xN', 'xO', 'xS', 'YE', 'YT', 'ZA', 'ZM', 'ZW')
GROUP BY country_code
ORDER BY country_code
Which should return me the amount of money spent in each country in the list.
My output is
AE 0.35365110000000016
AR 1.0367374499999995
AT 0.11195171000000001
AU 1.7345992
BE 1.9242438800000006
BG 5.043282479999997
CA 0.5906319000000001
CH 0.5082077999999999
CO 0.14248785
CR 0.036722840000000014
CU 0.11325390999999999
CY 0.18752883999999997
CZ 0.11454307999999999
DE 8.057752660000036
DO 0.8858295500000001
EE 0.7410690900000001
ES 31.125371000000023
FR 0.4851664099999999
GB 1.44115391
HR 0.023154
HU 1.0131190899999998
IE 0.3229343799999997
IN 0.026833529999999984
IT 2199.1061043693944
KE 0.21115987
KR 0.161765
LU 0.20279967
MC 0.2127708600000001
MT 0.028277630000000005
MX 0.45381685
NL 0.1408655
PE 0.00108554
QA 1.8347713
RO 7.0233499800000105
RS 0.25260947000000006
RU 0.16577983
SE 3.4979126399999947
SH 1.1328741000000002
SI 0.00178069
SK 0.04637177
SZ 0.3603625199999996
US 2.41114205
VE 0.53491791
So, as you can see, there are countries in the list which not appear in the output because the amount is null.
How can I include them in the list with the value 0?
Thank you
EDIT:
Not all countries in the list are in the table; I also would like to be in the output countries that are not in the table but are in the list

the problem is if you don't have data in wallet tables for given table join will return nothing. instead you can use left join :
SELECT
au.country as country_code,
COALESCE(SUM(uwm.amount), 0) as amountInbound
FROM
app_user au
left join user_wallet uw ON uw.user_id = au.id
left JOIN user_wallet_movement uwm ON uwm.wallet_id = uw.id
WHERE
status = 'execute'
and direction = 'inbound'
and mov_date > '2020-07-01'
au.country IN ('AD', 'AC', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'xA', 'xE', 'xF', 'XK', 'xN', 'xO', 'xS', 'YE', 'YT', 'ZA', 'ZM', 'ZW')
GROUP BY country_code
ORDER BY country_code

Probably your inner joins are limiting the rows in your output, try using outer joins:
SELECT
au.country as country_code,
COALESCE(SUM(uwm.amount), 0) as amountInbound
FROM user_wallet_movement uwm
LEFT OUTER JOIN user_wallet uw ON uwm.wallet_id = uw.id
LEFT OUTER JOIN app_user au ON uw.user_id = au.id
WHERE
status = 'execute'
and direction = 'inbound'
and mov_date > '2020-07-01'
and au.country IN (...)
GROUP BY country_code
ORDER BY country_code

If your data has all the countries, then a simple fix is to use conditional aggregation:
SELECT au.country as country_code,
SUM(CASE WHEN status = 'execute' and direction = 'inbound' and mov_date > '2020-07-01' THEN uwm.amount ELSE 0 END) as amountInbound
FROM user_wallet_movement uwm JOIN
user_wallet uw
ON uwm.wallet_id = uw.id JOIN
app_user au
ON uw.user_id = au.id
WHERE au.country IN ('AD', 'AC', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'xA', 'xE', 'xF', 'XK', 'xN', 'xO', 'xS', 'YE', 'YT', 'ZA', 'ZM', 'ZW')
GROUP BY country_code
ORDER BY country_code;
Otherwise, you will need to use a LEFT JOIN. For that purpose, it is better to start with a countries table of some sort. Do you have such a table?

If you would like to have all the countries from table A to show up even though table b has less number of countries then your join should be LEFT instead of Inner.
This way rows with no amount will be null in cases where the countries doesn't exist in table B. This null can be replaced using COALESCE()
COALESCE returns the first non-null value. if both are not null then it returns the first one.
SELECT
au.country as country_code,
COALESCE(SUM(uwm.amount), 0) as amountInbound
FROM user_wallet_movement uwm
LEFT JOIN user_wallet uw
ON uwm.wallet_id = uw.id
LEFT JOIN app_user au
ON uw.user_id = au.id
WHERE
status = 'execute'
and direction = 'inbound'
and mov_date > '2020-07-01'
and au.country IN ('ALL THE COUNTRIES')
GROUP BY
country_code
ORDER BY
country_code

Related

How to calculate difference between all current date records total and previous dates records total using GROUP BY and INNER JOIN?

I have a table below:
SELECT * FROM reports;
# id, date, o_type, quantity, vendor
'1', '2020-04-05', '2511', '200', 'apple'
'2', '2020-04-05', '5120', '350', 'apple'
'3', '2020-04-05', '2520', '150', 'apple'
'4', '2020-04-05', '5114', '400', 'apple'
'5', '2020-04-05', 'HG851', '200', 'google'
'6', '2020-04-05', 'HG851A', '400', 'google'
'7', '2020-04-05', 'MA5620G', '9000', 'google'
'8', '2020-04-05', 'OT550', '7000', 'google'
'9', '2020-04-05', 'OT925', '2000', 'google'
'10', '2020-04-05', 'OT928', '2000', 'google'
'11', '2020-04-06', '2520', '150', 'apple'
'12', '2020-04-06', 'HG851', '200', 'google'
'13', '2020-04-06', 'HG851', '200', 'google'
'14', '2020-04-06', 'HG851A', '400', 'google'
'15', '2020-04-07', '2511', '200', 'apple'
'16', '2020-04-07', '5120', '350', 'apple'
'17', '2020-04-07', '2520', '150', 'apple'
'18', '2020-04-07', '5114', '400', 'apple'
'19', '2020-04-07', 'G-440G-A', '200', 'NOKIA'
'20', '2020-04-07', '1240GA', '400', 'NOKIA'
'21', '2020-04-07', '1440GP', '9000', 'NOKIA'
'22', '2020-04-07', 'B-0404G-B', '7000', 'NOKIA'
'23', '2020-04-07', 'B2404GP', '2000', 'NOKIA'
'24', '2020-04-07', 'G-881G-A', '2000', 'NOKIA'
'25', '2020-04-08', 'G-881G-B', '150', 'NOKIA'
'26', '2020-04-08', 'HG851', '200', 'google'
'27', '2020-04-08', 'HG851A', '400', 'google'
I have a below query as per my project requirement:
SELECT Date(a.date), a.vendor, a.o_type, a.quantity, b.total FROM reports a
INNER JOIN (
SELECT vendor, date, SUM(quantity) as total
FROM reports WHERE date >= '2020-04-06' AND date <= '2020-04-08'
GROUP BY vendor, date) b ON a.date = b.date AND a.vendor = b.vendor
# Date(a.date), vendor, o_type, quantity, total
'2020-04-06', 'apple', '2520', '150', '150'
'2020-04-06', 'google', 'HG851', '200', '800'
'2020-04-06', 'google', 'HG851', '200', '800'
'2020-04-06', 'google', 'HG851A', '400', '800'
'2020-04-07', 'apple', '2511', '200', '1100'
'2020-04-07', 'apple', '5120', '350', '1100'
'2020-04-07', 'apple', '2520', '150', '1100'
'2020-04-07', 'apple', '5114', '400', '1100'
'2020-04-07', 'NOKIA', 'G-440G-A', '200', '20600'
'2020-04-07', 'NOKIA', '1240GA', '400', '20600'
'2020-04-07', 'NOKIA', '1440GP', '9000', '20600'
'2020-04-07', 'NOKIA', 'B-0404G-B', '7000', '20600'
'2020-04-07', 'NOKIA', 'B2404GP', '2000', '20600'
'2020-04-07', 'NOKIA', 'G-881G-A', '2000', '20600'
'2020-04-08', 'NOKIA', 'G-881G-B', '150', '150'
'2020-04-08', 'google', 'HG851', '200', '600'
'2020-04-08', 'google', 'HG851A', '400', '600'
I have to add an extra column DIFFERENCE to the above INNER JOIN query. How to calculate the difference between the current date and previous dates vendor column's total.
Example1:
2020-04-06 ---> apple ---> total(150)
2020-04-07 ---> apple ---> total(1100) Here difference equals to -950 (150-1100)
Example2:
2020-04-07 ---> apple ---> total(1100)
2020-04-08 ---> apple ---> total(0) Here difference equals to -1100 (0-1100)
Example3:
2020-04-07 ---> NOKIA ---> total(20600)
2020-04-08 ---> apple ---> total(150) Here difference equals to -20450 (150-20600)
Please guide me on how to proceed further? or if any other details required from my end kindly let me know.

SELECT from table AND 'archive' table at once

To reduce the size of one of our tables (currently over 4 million rows), I've created an 'archive' table which will only be accessed by certain users.
The archive table is a replica of the main table, except it has OLD rows in it that are rarely accessed.
However, sometimes I'll need to query both tables:
SELECT
t1.*,
t6.*
FROM (
SELECT
COUNT(DISTINCT up.lesson_id) AS assessment_count,
MIN(up.date) AS first_date,
MAX(up.date) AS last_date
FROM
cdu_user_progress up
WHERE
(up.game_id = '0') AND
(up.uid = '150') AND
(up.lesson_id IN ('65', '1112', '66', '67', '68', '69', '1114', '70', '71', '72', '73', '74', '75', '1113', '77', '424', '423', '1115', '93', '94', '95', '420', '421', '422', '96', '97', '98', '99', '100', '101', '1015', '102', '415', '104', '106', '105', '107', '108', '1016', '109', '110', '160', '111', '113', '112', '738', '739', '1050', '1051', '116', '117', '118', '119', '120', '121', '1017', '123', '124', '125', '130', '1018', '131', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '267', '268', '269', '270', '271', '272', '273', '274', '1019', '278', '775', '279', '280', '281', '282', '283', '284', '285', '286', '1161', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '318', '319', '302', '303', '304', '305', '306', '307', '308', '309', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '1054', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '377', '378', '379', '380', '479', '480', '481', '482', '431', '432', '433', '434', '435', '436', '437', '438', '439', '440', '441', '442', '443', '444', '445', '446', '448', '447', '449', '450', '451', '452', '453', '454', '456', '455', '457', '458', '459', '460', '461', '462', '463', '464', '465', '466', '467', '468', '469', '470', '471', '472', '473', '474', '475', '476', '477', '478', '1090')) AND
(up.score > '-1')
) t1
INNER JOIN (
SELECT
AVG(c1.score) AS avg_first_scores,
SUM(c1.score) AS sum_first_scores
FROM
cdu_user_progress c1
LEFT OUTER JOIN cdu_user_progress c2 ON c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1
WHERE
(c1.game_id = '0') AND
(c1.uid = '150') AND
(c1.lesson_id IN ('65', '1112', '66', '67', '68', '69', '1114', '70', '71', '72', '73', '74', '75', '1113', '77', '424', '423', '1115', '93', '94', '95', '420', '421', '422', '96', '97', '98', '99', '100', '101', '1015', '102', '415', '104', '106', '105', '107', '108', '1016', '109', '110', '160', '111', '113', '112', '738', '739', '1050', '1051', '116', '117', '118', '119', '120', '121', '1017', '123', '124', '125', '130', '1018', '131', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '267', '268', '269', '270', '271', '272', '273', '274', '1019', '278', '775', '279', '280', '281', '282', '283', '284', '285', '286', '1161', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '318', '319', '302', '303', '304', '305', '306', '307', '308', '309', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '1054', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '377', '378', '379', '380', '479', '480', '481', '482', '431', '432', '433', '434', '435', '436', '437', '438', '439', '440', '441', '442', '443', '444', '445', '446', '448', '447', '449', '450', '451', '452', '453', '454', '456', '455', '457', '458', '459', '460', '461', '462', '463', '464', '465', '466', '467', '468', '469', '470', '471', '472', '473', '474', '475', '476', '477', '478', '1090')) AND
(c1.score > '-1') AND
(c2.date IS '')
) t6
The solutions I've already thought of - replace SELECT cdu_user_progress with a nested-query:
SELECT * FROM cdu_user_progress UNION SELECT * FROM cdu_user_progress_archive
But this would create a huge temporary table in memory.
I tried UNION of each query (nested queries above), but this results in multiple rows returned, where it used to return a single row.
SELECT
...
FROM (
SELECT
...
FROM
cdu_user_progress up
WHERE
...
UNION SELECT
...
FROM
cdu_user_progress_archive up
WHERE
...
) t1
INNER JOIN (
SELECT
...
FROM
cdu_user_progress c1
LEFT OUTER JOIN cdu_user_progress c2 ON c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1
WHERE
...
UNION SELECT
...
FROM
cdu_user_progress_archive c1
LEFT OUTER JOIN cdu_user_progress_archive c2 ON c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1
WHERE
...
) t6
Any other ideas???
Thanks!
Those suq-queries contain aggregates.
So a simple solution is to sin against the DRY principle (Don't Repeat Yourself).
I.e. copy&paste those queries, change to the archive tables and union them.
But that list of lesson_id's is rather long.
Probably better to put those in a temp table so it can be reused.
SELECT t1.*, t6.*
FROM (
SELECT 'current' as Src,
COUNT(DISTINCT up.lesson_id) AS assessment_count,
MIN(up.date) AS first_date,
MAX(up.date) AS last_date
FROM cdu_user_progress up
INNER JOIN temp_lessonid_list lst
ON lst.lesson_id = up.lesson_id
WHERE up.game_id = 0
AND up.uid = 150
AND up.score > -1
UNION ALL
SELECT 'archive' as Src,
COUNT(DISTINCT up.lesson_id) AS assessment_count,
MIN(up.date) AS first_date,
MAX(up.date) AS last_date
FROM cdu_user_progress_archive up
INNER JOIN temp_lessonid_list lst
ON lst.lesson_id = up.lesson_id
WHERE up.game_id = 0
AND up.uid = 150
AND up.score > -1
) t1
INNER JOIN
(
SELECT 'current' as Src,
AVG(c1.score) AS avg_first_scores,
SUM(c1.score) AS sum_first_scores
FROM cdu_user_progress c1
INNER JOIN temp_lessonid_list lst
ON lst.lesson_id = c1.lesson_id
LEFT JOIN cdu_user_progress c2
ON (c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1)
WHERE c1.game_id = 0
AND c1.uid = 150
AND c1.score > -1
AND c2.date IS ''
UNION ALL
SELECT 'archive' as Src,
AVG(c1.score) AS avg_first_scores,
SUM(c1.score) AS sum_first_scores
FROM cdu_user_progress_archive c1
INNER JOIN temp_lessonid_list lst
ON lst.lesson_id = c1.lesson_id
LEFT JOIN cdu_user_progress_archive c2
ON (c1.uid = c2.uid AND c1.lesson_id = c2.lesson_id AND c1.game_id = c2.game_id AND c1.level = c2.level AND c1.date > c2.date AND c2.score > -1)
WHERE c1.game_id = 0
AND c1.uid = 150
AND c1.score > -1
AND c2.date IS ''
) t6 ON t6.Src = t1.Src;
You could also research table partitioning.
you can write 2 separate queries for each table and use the mysql UNION keyword to join them together
eg
(SELECT * FROM table1 WHERE col="value")
UNION
(SELECT * FROM table2 WHERE col="value");
https://dev.mysql.com/doc/refman/8.0/en/union.html

How do we select non-aggregate columns in a query with a GROUP BY clause, which is not functionally dependent on columns in GROUP BY clause?

My SQL
SELECT
`tbl_order`.`order_id`
FROM `tbl_order`
LEFT JOIN `tbl_rooms` ON `tbl_order`.`room_name` = `tbl_rooms`.`room_id`
LEFT JOIN `tbl_orderdetails` ON `tbl_order`.`order_key` = `tbl_orderdetails`.`orderdetail_orderkey`
LEFT JOIN `tbl_guestcatlang` ON `tbl_order`.`order_product_type`=`tbl_guestcatlang`.`guestcatlang_guestcat_id`
and `tbl_guestcatlang`.`guestcatlang_lang_id`="en"
JOIN `tbl_sitesettings` ON `tbl_order`.`order_setting_id` = `tbl_sitesettings`.`setting_id`
WHERE
(
(
tbl_sitesettings.delayedBrekfast = 1 AND
`tbl_order`.`order_product_type` = "BREAKFAST" AND
(
`tbl_order`.`order_required_time` > "2018-02-28 18:30:00" AND
`tbl_order`.`order_required_time` < "2018-03-01 18:29:59" AND
tbl_sitesettings.delayTime - (unix_timestamp(convert_tz(now(),("UTC"), tbl_sitesettings.timeZone))-unix_timestamp()) < "26100"
)
) OR
(
`tbl_sitesettings`.`delayedBrekfast` != 1 OR
`tbl_order`.`order_status` = "Complete" OR
tbl_order.order_product_type != "BREAKFAST"
)
) AND
`tbl_order`.`order_status` IN('Open') AND
`tbl_order`.`order_product_type` IN('BREAKFAST', 'Room Service', 'AMENITIES', 'GIFT SHOP', 'Internet Plan', 'Device Management',
'System Message', '70', '69', '67', '68', '75', '76', '77', '78',
'93', '94', '95', '97', '98', '101', '102', '103', '104', '105',
'106', '107', '108', '109', '110', '111', '112', '113', '114', '115',
'116', '117', '118', '119', '120', '121', '122', '123', '124', '127',
'128', '135') AND
`tbl_order`.`order_product_type` != 'Recomended Product' AND
`tbl_order`.`order_setting_id` = '7'
GROUP BY `tbl_order`.`order_key`
ORDER BY `tbl_order`.`ordered_time` DESC
And this was the error Message
Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'ebdb_production.tbl_order.order_id' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
Any solution ??? :(
Try execute the below query. This will remove the restriction of such.
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));

How to set all 50 states to a country code as 'US'

Okay so I'm using Google Charts API to create a map that displays sales based on location. Some code in my chart are countries so France is displayed in my CSV file as FR. However, the API ONLY does Countries so my data in the file that are states such as NC, CA, NY etc... need to be stored as US. Would a case statement for each state be the best way to go?
States I need these states to be set equal to 'US'
StateID
-------
AL
CA
HI
NY
etc...
try this sql...
SELECT
CASE WHEN
CUST_STATE_CD in ('AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY')
THEN
'US'
ELSE
CUST_STATE_CD
END as state,
count(CUST_NM) as totalCust
FROM
sales_filev1
GROUP BY
CASE WHEN
CUST_STATE_CD in ('AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY')
THEN
'US'
ELSE
CUST_STATE_CD
END

Magento Database query hogging resources

I have a magento store on version 1.8.1.0 hosted with siteground. I've been notified by their system administrator that my database queries are hogging up too much resoruces. Here are what they've sent me. I'm at a complete loss in how to tackle this issue, any pointer would be greatly appreciated.
1 Executed 9h 26m 48s ago for 16.454657 sec on Database --> DB_production
Date: 2015-10-05 22:41:18 Query_time: 16.454657 Rows_examined: 349107: Rows_sent 1 Lock_time: 0.000301
SELECT count(DISTINCT e.entity_id)
FROM report_event AS report_table_views
INNER JOIN catalog_product_entity AS e ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4
INNER JOIN catalog_product_entity_int AS at_visibility_default ON (at_visibility_default.entity_id = e.entity_id) AND (at_visibility_default.attribute_id = '102') AND at_visibility_default.store_id = 0
LEFT JOIN catalog_product_entity_int AS at_visibility ON (at_visibility.entity_id = e.entity_id) AND (at_visibility.attribute_id = '102') AND (at_visibility.store_id = 4)
INNER JOIN catalog_product_entity_int AS at_status_default ON (at_status_default.entity_id = e.entity_id) AND (at_status_default.attribute_id = '96') AND at_status_default.store_id = 0
LEFT JOIN catalog_product_entity_int AS at_status ON (at_status.entity_id = e.entity_id) AND (at_status.attribute_id = '96') AND (at_status.store_id = 4)
WHERE (report_table_views.event_type_id = 1) AND (IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) = '4') AND (IF(at_status.value_id > 0, at_status.value, at_status_default.value) = 1);
2 Executed 9h 26m 48s ago for 16.381442 sec on Database --> DB_production
Date: 2015-10-05 22:41:18 Query_time: 16.381442 Rows_examined: 349107: Rows_sent 1 Lock_time: 0.000517
SELECT count(DISTINCT e.entity_id)
FROM report_event AS report_table_views
INNER JOIN catalog_product_entity AS e ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4
INNER JOIN catalog_product_entity_int AS at_visibility_default ON (at_visibility_default.entity_id = e.entity_id) AND (at_visibility_default.attribute_id = '102') AND at_visibility_default.store_id = 0
LEFT JOIN catalog_product_entity_int AS at_visibility ON (at_visibility.entity_id = e.entity_id) AND (at_visibility.attribute_id = '102') AND (at_visibility.store_id = 4)
INNER JOIN catalog_product_entity_int AS at_status_default ON (at_status_default.entity_id = e.entity_id) AND (at_status_default.attribute_id = '96') AND at_status_default.store_id = 0
LEFT JOIN catalog_product_entity_int AS at_status ON (at_status.entity_id = e.entity_id) AND (at_status.attribute_id = '96') AND (at_status.store_id = 4)
WHERE (report_table_views.event_type_id = 1) AND (IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) = '4') AND (IF(at_status.value_id > 0, at_status.value, at_status_default.value) = 1);
=== TOP 10 of 1854 (total) Slow Queries for the past 24 hours ==========
[36;1m 1. Executed 13h 30m 41s ago for 9.566494 sec on Database --> [33;1mDB_production[0m [0m
[36mDate: 2015-10-06 21:35:51 Query_time: 9.566494 Rows_examined: 234795: Rows_sent 68398 Lock_time: 0.011157[0m
SELECT t_d.entity_id, t_d.attribute_id, t_d.value AS default_value, t_s.value AS store_value, IF(t_s.value_id IS NULL, t_d.value, t_s.value) AS value FROM catalog_product_entity_varchar AS t_d LEFT JOIN catalog_product_entity_varchar AS t_s ON t_s.attribute_id = t_d.attribute_id AND t_s.entity_id = t_d.entity_id AND t_s.store_id = 4 WHERE (t_d.entity_type_id = 4) AND (t_d.entity_id IN (1678, 1682, 1828, 1830, 1834, 1837, 1839, 1926, 1942, 1982, 1983, 1985, 1987, 1989, 2316, 2325, 3299, 1510, 2055, 2172, 2282, 2283, 2328, 2333, 1075, 1771, 1780, 1818, 1849, 1850, 1853, 2569, 2570, 2572, 2573, 2574, 2575, 2576, 2577, 2578, 2579, 2728)) AND (t_d.attribute_id IN ('75', '76', '120')) AND (t_d.store_id = 0) UNION ALL SELECT t_d.entity_id, t_d.attribute_id, t_d.value AS default_value, t_s.value AS store_value, IF(t_s.value_id IS NULL, t_d.value, t_s.value) AS value FROM catalog_product_entity_datetime AS t_d LEFT JOIN catalog_product_entity_datetime AS t_s ON t_s.attribute_id = t_d.attribute_id AND t_s.entity_id = t_d.entity_id AND t_s.store_id = 4 WHERE (t_d.entity_type_id = 4) AND (t_d.entity_id IN (1678, 1682, 1828, 1830, 1834, 1837, 1839, 1926, 1942, 1982, 1983, 1985, 1987, 1989, 2316, 2325, 3299, 1510, 2055, 2172, 2282, 2283, 2328, 2333, 1075, 1771, 1780, 1818, 1849, 1850, 1853,
====== I Truncated a lot of the entity ID for this post=======
3456, 3457, 3458, 3459, 3460, 3461, 3462, 3463, 3464, 3465, 3466, 3467, 3468, 2545, 2546, 2547, 2548, 2549, 2550, 2551, 2552, 2553, 2554, 2555, 2558, 2559, 2560, 2561, 2562, 2563, 2564, 2565, 2566, 2567, 2568, 2569, 2570, 2572, 2573, 2574, 2575, 2576, 2577, 2578, 2579, 2728)) AND (t_d.attribute_id IN ('96', '122', '124', '126', '127', '128', '129', '132', '134')) AND (t_d.store_id = 0);
I have the same with magento and Siteground. They block my site because there are slow queries, but resource usage is normal - CPU time and account executions are not over limitations ;/ They suggest me to upgrade plan to Cloud or Dedicated server. But at this time my site is not fully finish and there are no visitors and don't know why they block my site only for that there are slow queries ;/ i'm using GrowBig shared hosting at this time, and here are the queries:
"I carefully checked for you and I can see that there are still some slow queries. You can check the results from here:
Code:
I carefully checked for you and I can see that there are still some slow queries. You can check the results from here:
Code:
7. Executed 8h 38m 41s ago for 3.789867 sec on Database --> devakorp_mage878
Date: 2015-10-12 19:41:24 Query_time: 3.789867 Rows_examined: 101030: Rows_sent 173 Lock_time: 0.000547
SELECT e.*, IF(at_is_active.value_id > 0, at_is_active.value, at_is_active_default.value) AS is_active, IF(at_include_in_menu.value_id > 0, at_include_in_menu.value, at_include_in_menu_default.value) AS include_in_menu, mg_core_url_rewrite.request_path FROM mg_catalog_category_entity AS e INNER JOIN mg_catalog_category_entity_int AS at_is_active_default ON (at_is_active_default.entity_id = e.entity_id) AND (at_is_active_default.attribute_id = '42') AND at_is_active_default.store_id = 0 LEFT JOIN mg_catalog_category_entity_int AS at_is_active ON (at_is_active.entity_id = e.entity_id) AND (at_is_active.attribute_id = '42') AND (at_is_active.store_id = 1) INNER JOIN mg_catalog_category_entity_int AS at_include_in_menu_default ON (at_include_in_menu_default.entity_id = e.entity_id) AND (at_include_in_menu_default.attribute_id = '67') AND at_include_in_menu_default.store_id = 0 LEFT JOIN mg_catalog_category_entity_int AS at_include_in_menu ON (at_include_in_menu.entity_id = e.entity_id) AND (at_include_in_menu.attribute_id = '67') AND (at_include_in_menu.store_id = 1) LEFT JOIN mg_core_url_rewrite ON (mg_core_url_rewrite.category_id=e.entity_id) AND (mg_core_url_rewrite.is_system=1 AND mg_core_url_rewrite.product_id IS NULL AND mg_core_url_rewrite.store_id='1' AND id_path LIKE 'category/%') WHERE (e.entity_type_id = '3') AND (e.entity_id IN('3', '180', '181', '182', '183', '184', '185', '186', '187', '219', '220', '221', '222', '223', '224', '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '211', '212', '213', '216', '217', '218', '27', '151', '152', '28', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '29', '172', '173', '174', '175', '176', '177', '178', '179', '30', '31', '16', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '14', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '108', '109', '110', '111', '139', '140', '141', '142', '143', '144', '188', '189', '190', '191', '192', '193', '38', '199', '62', '63', '64', '65', '66', '67', '17', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '145', '146', '147', '148', '149', '150', '6', '87', '88', '89', '90', '91', '92', '93', '94', '198', '18', '197', '214', '215', '200', '230', '231', '234', '235', '236', '237', '232', '233')) AND (IF(at_is_active.value_id > 0, at_is_active.value, at_is_active_default.value) = '1') AND (IF(at_include_in_menu.value_id > 0, at_include_in_menu.value, at_include_in_menu_default.value) = '1');
Executed 3h 18m 25s ago for 3.449602 sec on Database --> devakorp_mage878
Date: 2015-10-13 01:01:40 Query_time: 3.449602 Rows_examined: 93735: Rows_sent 0 Lock_time: 0.000094
INSERT INTO mg_catalog_product_index_eav (entity_id, attribute_id, store_id, value) SELECT mg_catalog_product_index_eav_idx.entity_id, mg_catalog_product_index_eav_idx.attribute_id, mg_catalog_product_index_eav_idx.store_id, mg_catalog_product_index_eav_idx.value FROM mg_catalog_product_index_eav_idx ON DUPLICATE KEY UPDATE entity_id = VALUES(entity_id), attribute_id = VALUES(attribute_id), store_id = VALUES(store_id), value = VALUES(value);
Executed 8h 41m 52s ago for 3.428078 sec on Database --> devakorp_mage878
Date: 2015-10-12 19:38:13 Query_time: 3.428078 Rows_examined: 0: Rows_sent 0 Lock_time: 0.000000
commit;
Executed 1h 50m 14s ago for 3.172525 sec on Database --> devakorp_mage878
Date: 2015-10-13 02:29:51 Query_time: 3.172525 Rows_examined: 0: Rows_sent 0 Lock_time: 0.003677
DELETE FROM mg_catalog_product_index_price_tmp;
i have changed host to another. in siteground i paid 14e pro month, now i pay 20e, i have VPS and two magento shop with 10 000 SKU and resources are minimal and site is more faster. i am disapointed with siteground and surprised with new hosting www.netdc.pl (polish hosting).