Skip to content

Instantly share code, notes, and snippets.

@raychew13
Last active February 13, 2018 23:23
Show Gist options
  • Save raychew13/6af80d3420bfdf9e5dd70c69c40758cd to your computer and use it in GitHub Desktop.
Save raychew13/6af80d3420bfdf9e5dd70c69c40758cd to your computer and use it in GitHub Desktop.
Useful SQL
ALTER TABLE forecast.workstream_data
ADD timezone VARCHAR(40);
-- Copying data - specific column from one table to another based on row condition
UPDATE forecast.workstream_data set timezone = (SELECT o.timezone FROM roster.organisation as o where o.id=workstream_data.organisation_id)
WHERE user_id is NULL;
-- Copying data - specific column from one table to another based on row condition
UPDATE forecast.workstream_data set workstream_data.timezone = (
SELECT t.timezone
FROM roster.user as u, roster.team t
WHERE u.id=workstream_data.user_id
AND u.team_id=t.id
);
-- Dump specific tables to SQL File
mysqldump -u db_user -p db_schema tableA tableB tableC > my_tables.sql
-- Another useful QUERY use `STR_TO_DATE`, `CONCAT`, `LPAD`, `IF`
SELECT
date_format( ForecastQuarterHourVolume.start_time, '%Y-%m-%d %H:00:00') as start_timeNN,
SUM(ForecastQuarterHourVolume.volume) as total_volume,
ceil(AVG(ForecastQuarterHourVolume.average_handle_time)) as aht,
SUM(ForecastQuarterHourVolume.volume) * AVG(ForecastQuarterHourVolume.average_handle_time) as workload
FROM
(
-- Join Daily Forecast Data to Quarter Hour workload percent table to calculate Quarter Hour Volume
SELECT
STR_TO_DATE(CONCAT(DATE_FORMAT(start_time, '%Y-%m-%d'),
' ',
LPAD(h.ws_hour, 2, '0'),
':',
LPAD(h.ws_min, 2, '0')
), '%Y-%m-%d %H:%i') AS start_time,
(IF(fp.adjusted_volume<0,0,fp.adjusted_volume) * h.daily_volume_percent) AS volume,
h.aht AS average_handle_time,
(IF(fp.adjusted_volume<0,0,fp.adjusted_volume) * h.daily_volume_percent * h.aht) AS workload,
h.workstream_id AS workstream_id
FROM forecast_data.historical_workstream_percent AS h
RIGHT JOIN forecast_data.forecast_point AS fp
ON h.weekday_short = substring(DAYNAME(fp.start_time), 1, 3)
AND h.workstream_id = fp.workstream_id
WHERE h.ws_timezone = 'UTC'
AND fp.published_forecast_id = 188
AND fp.start_time >= '2017-10-01 00:00:00' AND fp.start_time <= '2017-10-18 00:00:00'
) as ForecastQuarterHourVolume
GROUP BY DATE_FORMAT(ForecastQuarterHourVolume.start_time, '%Y-%m-%d %H')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment