Select specific records and change its values - mysql

I have a table filled with (DUTCH) holidays in the past and future and I need to change the name and date for a specific holiday. Since we don't have a Queen anymore, but a King, Queensday is no longer a holiday. Now its Kingsday. AND it is celebrated on a different date.
This is my current table:
68 NL 2014-04-30 Queensday
77 NL 2015-04-30 Queensday
88 NL 2016-04-30 Queensday
97 NL 2017-04-30 Queensday
106 NL 2018-04-30 Queensday
115 NL 2019-04-30 Queensday
124 NL 2020-04-30 Queensday
134 NL 2021-04-30 Queensday
I want to change all records where description='Queensday' into description='Kingsday' AND date=date-3days (since it is celebrated each April 27th) but only where year of the date is greater than 2013.

update table
set
description = 'Kingsday',
<yourdateField> = date_sub(<yourdateField>, interval 3 DAY)
where description = 'Queensday'
and year(<yourdateField>) > 2013
bonus (?)
update country
set
politicalSystem = 'democracy',
comment = 'easier to manage holidays'
where
politicalSystem = 'royalty'

Related

mysql join rows with from only 1 id

I am attempting to query two tables to create a monthly employee time report. When I join the tables using INNER JOIN (emp_nr is the same in both tables) then I get the following. Please excuse me if I have not the correct formatting.
Table employee
id | emp_nr | name | vorname | address | city .....bla bla bla
Table zeit (szeit and ezeit are datetime Y-m-d H:i:s)
id | emp_nr | szeit | ezeit |
my query
$result = mysqli_query($conn,"
SELECT e.vorname
, e.name
, e.emp_nr
, a.szeit
, a.ezeit
FROM employee e
JOIN zeit a
WHERE a.szeit BETWEEN '$jahr-$monat-01' AND '$jahr-$monat-01' + INTERVAL 1 MONTH - INTERVAL 1 DAY
AND e.emp_nr = 57
ORDER
BY e.emp_nr
, a.szeit
");
my results are the following: Here I get all rows of all employees with the name and emp_nr from only one
Juli Zimmerm 57 2018-02-01 09:45:37 2018-02-01 14:15:08
Juli Zimmerm 57 2018-02-01 18:00:00 2018-02-01 22:30:00
Juli Zimmerm 57 2018-02-01 19:33:00 2018-02-01 19:44:00
Juli Zimmerm 57 2018-02-02 10:31:25 2018-02-02 14:30:25
In the code when I change
ON e.emp_nr = 57
to
ON a.emp_nr = 57
I get the following results.
Everyone listed individually but all the times are the same and the requested emp_nr is not there.
Frank Cas 01 2018-02-01 19:33:00 2018-02-01 19:44:00
Julie Cas 03 2018-02-01 19:33:00 2018-02-01 19:44:00
Lisa Ket 15 2018-02-01 19:33:00 2018-02-01 19:44:00
Anja Fis 22 2018-02-01 19:33:00 2018-02-01 19:44:00
What I am trying to get is a report where only the correct ID
with the correct name comes out.
for the simplest SELECT
SELECT emp_nr, szeit, ezeit
FROM zeit
WHERE szeit BETWEEN '2018-02-01' AND '2018-02-01' +
INTERVAL 1 MONTH - INTERVAL 1 DAY
AND emp_nr = 57
The result is without the corresponding name from table employee
57 2018-02-01 19:33:00 2018-02-01 19:44:00
When using JOINS you need to specify the relationship between the 2 tables using ON clause
SELECT e.vorname, e.name, e.emp_nr, a.szeit, a.ezeit
FROM zeit a
JOIN employee e ON a.emp_nr = e.emp_nr
WHERE a.szeit BETWEEN '2018-02-01' AND '2018-02-01' +
INTERVAL 1 MONTH - INTERVAL 1 DAY
AND e.emp_nr = 57
ORDER BY e.emp_nr, a.szeit

Join Tables Based on Time and ID

I have two tables of time series data that I am trying to query and don't know how to properly do it.
The first table is time series data of device measurements. Each device is associated with a source and the data contains an hourly measurement. In this example there are 5 devices (101-105) with data for 5 days (June 1-5).
device_id date_time source_id meas
101 2016-06-01 00:00 ABC 105
101 2016-06-01 01:00 ABC 102
101 2016-06-01 02:00 ABC 103
...
101 2016-06-05 23:00 ABC 107
102 2016-06-01 00:00 XYZ 102
...
105 2016-06-05 23:00 XYZ 104
The second table is time series data of source measurements. Each source has three hourly measurements (meas_1, meas_2 and meas_3).
source_id date_time meas_1 meas_2 meas_3
ABC 2016-06-01 00:00 100 101 102
ABC 2016-06-01 01:00 99 100 105
ABC 2016-06-01 02:00 104 108 109
...
ABC 2016-06-05 23:00 102 109 102
XYZ 2016-06-01 00:00 105 106 103
...
XYZ 2016-06-05 23:00 103 105 101
I am looking for a query to get the data for a specified date range that grabs the device's measurements and its associated source's measurements. This example is the result for querying for device 101 from June 2-4.
device_id date_time d.meas s.meas_1 s.meas_2 s.meas_3
101 2016-06-02 00:00 105 100 101 102
101 2016-06-02 01:00 102 99 100 105
101 2016-06-02 02:00 103 104 108 109
...
101 2016-06-04 23:00 107 102 109 102
The actual data set could get large with lets say 100,000 devices and 90 days of hourly measurements. So any help on properly indexing the tables would be appreciated. I'm using MySQL.
UPDATE - Solved
Here's the query I used:
SELECT d.device_id, d.date_time, d.meas, s.meas_1, s.meas_2, s.meas_3
FROM devices AS d
JOIN sources AS s
ON d.source_id = s.source_id AND d.date_time = s.date_time AND d.device_id = '101' AND d.date_time >= '2016-06-02 00:00' AND d.date_time <= '2016-06-04 23:00'
ORDER BY d.date_time;
For what its worth, it also worked with the filters in a WHERE clause rather than in the JOIN but it was slower performing. Thanks for the help.

Mysql: Calculating running average for each Employee by day Monthly

I have this table that has the name of the employee and their phone time duration in mysql. The table looks like this:
Caller Emplid Calldate Call_Duration
Jack 333 1/1/2016 43
Jack 333 1/2/2016 45
Jack 333 1/3/2016 87
Jack 333 2/4/2016 44
Jack 333 2/5/2016 234
jack 333 2/6/2016 431
Jeff 111 1/1/2016 23
Jeff 111 1/2/2016 54
Jeff 111 1/3/2016 67 48
I am trying to calculate the running Daily average of each employee total_Duration by day each month. Suppose I have daily running average for the month of April, then the running average for the May should start from 1st of may and end on 31st of that month. I have tried doing many ways and mysql does not have pivot and partition function like sql server. The total employee who made the call changes daily, I need something that dynamically takes care of no of employees that makes call.
The output should look like this:
Caller Emplid Calldate Call_Duration Running_avg
Jack 333 1/1/2016 43 43
Jack 333 1/2/2016 45 44
Jack 333 1/3/2016 87 58.33333333
Jack 333 2/4/2016 44 44
Jack 333 2/5/2016 234 139
Jack 333 2/6/2016 431 236.3333333
Jeff 111 1/1/2016 23 23
Jeff 111 1/2/2016 54 38.5
Jeff 111 1/3/2016 67 48
This is the query that I started below:
SELECT row_number,Month_Year,Callername,Calldate,Caller_Emplid,`Sum of Call`,`Sum of Call`/row_number as AvgCall,
#`sum of call`:=#`sum of call`+ `sum of call` overallCall,
#row_number:=row_number overallrow_number,
#RunningTotal:=#`sum of call`/#row_number runningTotal
FROM
(SELECT
#row_number:=
CASE WHEN #CallerName=CallerName and date_format(calldate,'%d') = date_format(calldate,'%d') and
date_format(calldate,'%m') = date_format(calldate,'%m')
THEN #row_number+1
ELSE 1 END AS row_number,#CallerName:=CallerName AS Callername,Calldate,Caller_Emplid,Month_Year,`Sum of Call`
FROM transform_data_2, (SELECT #row_number:=0,#CallerName:='') AS t
ORDER BY callername) a
JOIN (SELECT #`Sum of call`:= 0) t

SSRS - How to total a row

Report Month Booked Month Hours Available Hours Reported Hours
2015-12-01 00:00:00 2015-10-01 00:00:00 3.5 97 172
2015-12-01 00:00:00 2015-10-01 00:00:00 0.75 97 172
2015-12-01 00:00:00 2015-10-01 00:00:00 6.85 97 172
2015-12-01 00:00:00 2015-10-01 00:00:00 3 97 172
2015-11-01 00:00:00 2015-10-01 00:00:00 3.5 97 185
2015-11-01 00:00:00 2015-10-01 00:00:00 0.75 97 185
2015-11-01 00:00:00 2015-10-01 00:00:00 6.85 97 185
2015-11-01 00:00:00 2015-10-01 00:00:00 3 97 185
In my SSRS report i want to total the Reported Hours Column. This person booked 172 hours in December and 185 hours in November so his total for the year should be 185 + 172 = 357.
In my current report it adds together all the rows. How do i get it to just pick one row of reported hours per month?
Thanks
There's not a set way to do this in SSRS.
Usually you would wrap your query in another query that uses ROW_NUMBER and group by months. Then only add the hours if the row_numnber = 1.
How do I use ROW_NUMBER()?
Unfortunately that's not always possible. I wrote some code to add up unique records based on another field. I made it to get check amounts for various vendors.
For the expression, you would use:
=Code.SUMVendors(Report_Month & "|" & Fields!Reported_Hours.Value)
Add the Visual BASIC to the CODE of the report:
FUNCTION SUMVendors(BYVAL ITEMS AS OBJECT()) AS DECIMAL
'Returns Sum of unique vendors. Use as Code.SUMVendors(Vendor & "|" & Amount)
IF ITEMS IS NOTHING THEN
RETURN NOTHING
GOTO EXITSUB
END IF
DIM TEMP AS STRING
DIM VENDORS AS STRING = ""
DIM VENDOR AS STRING = ""
DIM VALUE AS DECIMAL
FOR EACH ITEM AS OBJECT IN ITEMS
TEMP = CONVERT.TOSTRING(ITEM)
VENDOR = "|" & LEFT(TEMP, InStr(TEMP, "|"))
VALUE = CDEC(MID(TEMP, InStr(TEMP, "|") + 1, LEN(TEMP)))
IF InStr(VENDORS, VENDOR) = 0 Then
VENDORS = VENDORS + VENDOR
SUMVendors = SUMVendors + VALUE
End If
NEXT
EXITSUB:
END FUNCTION

MySql subquerying to pull data from another table

I am currently using phpMyAdmin supporting MySql 5.0.
I have the following two tables:
Master Facebook Insights
------------------------
Client ID Client Name Date Top Likes By Gender
--------- ----------- ------ --------------------
1 Client1 2012-01-01
1 Client1 2012-02-01
2 Client2 2012-01-01
2 Client2 2012-02-01
...etc. (the dates are always the beginning of each month for however many months
& clients exist)
Likes By Demographic
----------------
Date Demographic1 Demographic2 Demographic3 Client
------ ------------ ------------ ------------ ------
0000-00-00 M.35-44 M.45-54 M.25-34 1
2012-01-01 53 44 28 1
2012-01-02 53 46 29 1
...etc.
0000-00-00 M.18-24 M.35-44 M.25-34 1
2012-02-01 374 221 194 1
2012-02-02 374 222 195 1
...etc.
0000-00-00 F.35-44 F.25-34 M.35-44 2
2012-01-01 194 182 83 2
2012-01-02 194 182 83 2
...etc.
0000-00-00 F.35-44 F.25-34 M.35-44 2
2012-02-01 196 180 83 2
2012-02-02 197 180 83 2
...etc.
For the Likes By Demographic table:
All days of each month are listed per client; when a new month begins,
there is a new set of demographics that may or may NOT be listed exactly the same
as the previous month. This is because the data was imported from a CSV that put the demographics in order of highest 'likes,' so this usually changes month to month. This is also the reason that the individual demographics are not the column headers (because they are inconsistent).
My problem is the following:
I wish to list the top 3 demographics per month for each client in the 'Top Likes By Gender' column of the first table, like so:
Client ID Client Name Date Top Likes By Gender
--------- ----------- ------ --------------------
1 Client1 2012-01-01 M.35-44, M.45-54, M.25-34
1 Client1 2012-02-01 M.18-24, M.35-44, M.25-34
2 Client2 2012-01-01 F.35-44, F.25-34, M.35-44
2 Client2 2012-02-01 F.35-44, F.25-34, M.35-44
The use of subqueries is confusing me. The following is the (incorrect) code I have been trying to fix. The problem is that is just extracts the first three demographics for the first client (M.35-44, M.45-54, M.25-34) and repeats them down the entire column for all clients and dates.
Am I on the right track, or is there a much simpler/more correct way to do this?
UPDATE `Master Facebook Insights`
SET `Top Likes By Gender` =
(select * from
(SELECT DISTINCT CONCAT(`Demographic1`,', ',`Demographic2`,', ',`Demographic3`)
FROM `Likes By Demographic` t1
JOIN `Master Facebook Insights` t2
ON t1.`Client` = t2.`Client ID`
WHERE t1.`Date` = '0000-00-00'
AND t2.`Date` =
(
SELECT MIN(`Date`)
FROM `Likes By Demographic`
WHERE `Date` > '0000-00-00'
AND `Client` = t2.`Client ID`
GROUP BY `Date`
ORDER BY `Date` ASC
LIMIT 1
)
)inner1 limit 1)
Thank you so much for the help!
Just using the original Likes_By_Demographic table, you can do:
select
client_id,
date,
concat(demo1,', ',demo2,', ',demo3) `top_likes`
from likes_by_demographic
group by client_id, date
order by client_id, date;
...which will yield (pseudo-data):
| CLIENT_ID | DATE | TOP_LIKES |
-----------------------------------------------------------------------
| 1 | January, 01 2012 00:00:00-0800 | M.35-44, M.25-34, 195 |
| 1 | February, 01 2012 00:00:00-0800 | M.25-34, M.35-44, 195 |
| 1 | March, 01 2012 00:00:00-0800 | M.25-34, 195, 169 |
| 2 | January, 01 2012 00:00:00-0800 | F.45-54, 210, 195 |
| 2 | February, 01 2012 00:00:00-0800 | F.45-54, 210, 75 |