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 |
Related
I have a table like this
userID time NoOfVisits
1 2014 50
2 2015 60
3 2016 70
4 2017 80
5 2018 90
6 2019 100
I need to write a sql query which will print time and average of past 3 years NoOfVisits for a particular site.
output should be as
userID time NoOfVisits
1 2014 50.0000
2 2015 55.0000
3 2016 60.0000
4 2017 70.0000
5 2018 80.0000
6 2019 90.0000
Explanation :
For user Id 6 (80+90+100)/3=90.0000
Please help me to solve this problem.
You can use a cumulative average, available in MySQL 8+:
select t.*,
avg(visits) over (order by time rows between 2 preceding and current row) as avg_visits_3
from t;
Assuming there are no gaps between the years (like your sample data), you can self join the table and group by userid, time to get the average:
select
t.userid, t.time, avg(tt.noofvisits) NoOfVisits
from tablename t inner join tablename tt
on tt.time between t.time - 2 and t.time
group by t.userid, t.time
See the demo.
Results:
| userid | time | NoOfVisits |
| ------ | ---- | ---------- |
| 1 | 2014 | 50 |
| 2 | 2015 | 55 |
| 3 | 2016 | 60 |
| 4 | 2017 | 70 |
| 5 | 2018 | 80 |
| 6 | 2019 | 90 |
I have a table XX. i need to get the records which are 20 days consecutive gap .below is my table look
ID ISmen Date
1 0 2013-05-2
2 0 2013-05-2
3 0 2014-04-2
4 1 2014-05-2
5 1 2014-05-2
6 0 2014-05-2
7 0 2014-05-2
8 0 2014-05-2
9 1 2014-05-25
10 1 2014-05-25
11 0 2014-05-26
12 1 2014-05-27
13 0 2014-05-28
From the above table i need to get the records which are ismen is 1 and the next record ismen is also 1 (i.e 4,5 and 9,10 but not 12).and one more thing 4,5 and 9,10 should have 20 days gap
i am getting the records which are 4,5 and 9,10 ..but i can't able to check date difference between the records .i know we can achieve in the loop but i am trying to get in MySQL is it possible or not.I try below query.thanks in advance for help
SELECT *
FROM XX t1,
XX t2
WHERE (t1.ID=t2.ID+1
OR t1.ID=t2.ID-1)
AND t1.Ismen=1
AND t2.Ismen=1
There is a 23 day gap between |4|5| to |9|10| but ignoring the sample data precision, this result:
| ISMEN | T1ID | T2ID | T1DATE | T2DATE |
|-------|------|------|----------------------------|----------------------------|
| 1 | 4 | 9 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
| 1 | 5 | 9 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
| 1 | 4 | 10 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
| 1 | 5 | 10 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
was produced by this query:
select
t1.ismen
, t1.id as t1id
, t2.id as t2id
, t1.`date` as t1date
, t2.`date` as t2date
from table1 as t1
inner join table1 as t2 on t1.ismen = t2.ismen
and t1.`date` + INTERVAL 23 DAY = t2.`date`
The wanted gap between records can be defined in the join conditions (change to 20 or whatever). But do note there is nothing to stop 4 relating to 9 and 10 or 5 to 9 & 10 so you get 4 records in total.
see: http://sqlfiddle.com/#!9/8d941/1
You could reduce that result by some means (e.g. using row_number() but I don't know if that is required.
I have a table that produces the following results:
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 6 2013 2013-11-26 14:50
61 1 763 ABCD Brazil 6 2013 2013-11-26 14:50
54 1 200 ABCDE US 12 2013 2013-11-26 21:02
32 2 200 ABCDE US 12 2013 2013-11-26 21:03
I want to be able to show the latest 'no' per row. I have tried using max(dateupdated), but still shows all the records. I've also tried grouping, but also no luck.
I only need to see the latest record determined by the dateupdated.
Can anyone please assist me?
I'm not sure I understand your question, but you might be helped by something like this:
select * from mytable where dateupdated = (select max(dateupdated) from mytable);
EDIT:
Based on your clarification, you need in that case a correlated subquery. I haven't tried this, but perhaps something like:
select *
from mytable a
where dateupdated = (select max(dateupdated) from mytable where no = a.no);
Or this:
SELECT *
FROM table
ORDER BY dateupdated DESC
LIMIT 1;
Last for every no: (Is this what you want ?)
SELECT *
FROM table t1
WHERE NOT EXISTS(SELECT * FROM table t2
WHERE t1.no = t2.no
AND t2.dateupdated > t1.dateupdated)
Query:
SQLFIDDLEExample
SELECT t1.*
FROM Table1 t1
LEFT JOIN Table1 t2 ON t2.dateupdated > t1.dateupdated
AND t1.no = t2.no
WHERE t2.no IS NULL
Result:
| USERID | STATUSID | NO | NAME | AREA | MONTH | YEAR | DATEUPDATED |
|--------|----------|-----|-------|--------|-------|------|---------------------------------|
| 62 | 2 | 763 | ABCD | Brazil | 6 | 2013 | November, 26 2013 15:28:00+0000 |
| 62 | 2 | 869 | ABC | Brazil | 6 | 2013 | November, 26 2013 15:28:00+0000 |
| 32 | 2 | 200 | ABCDE | US | 12 | 2013 | November, 26 2013 21:03:00+0000 |
Tag along a LIMIT 1; at the end of your query and sort descending.
employee makes entry in the following table when starting new task
from home or office
[tablename=CHECK]
c_id c_sdate c_emp c_task
-------------------------------------------------
1 2013-05-01 01:01:00 1 26 //date 01 from home-----
2 2013-05-01 08:11:00 1 27 //date 01 from office--- Present
3 2013-05-02 03:41:00 1 28 //date 02 from home---
4 2013-05-02 09:12:00 1 29 //date 02 from office-
5 2013-05-02 22:32:00 1 30 //date 02 from home---Present
6 2013-05-03 01:43:00 1 31 //date 03 from home
7 2013-06-03 23:25:00 1 32 //date 03 from home----------Homework
8 2013-06-03 02:15:00 2 33 //other employee
an employe will be considered as present if there 1 or many records where time between 8am and 8pm
an employe will be considered as workedFromHome if there 1 or many records where time NOT between 8am and 8pm, and not present on that day
note: do not count a day as workedFromHome if there is any record time between 8am and 8pm (means workedFromHome is only counted if he is not resent on that day)
I want to display monthly report of a employee eg. c_emp=1 for month eg. 5
like this in 1 query
c_emp presentCount HW_Count
1 3 1
or separatly query 1
c_emp presentCount
1 3
and query 2
c_emp HW_Count
1 1
I have tried for counting present working fine
select count(distinct(date_format(c_sdate,'%e'))) as count
from ita_check
where date_format(c_sdate,'%m')=5
and c_emp=1
and date_format(c_sdate,'%H%i')>=800
and date_format(c_sdate,'%H%i')<=2000
and for counting fromHome giving wrong count
select count(distinct(date_format(c_sdate,'%e'))) as count
from ita_check
where date_format(c_sdate,'%m')=5
and c_eid=1
and c_id not in (
select c_id
from ita_check
where date_format(c_sdate,'%m')=5
and c_eid=1
and (date_format(c_sdate,'%H%i')<=800 or date_format(c_sdate,'%H%i')>=2000)
)
and date_format(c_sdate,'%H%i')<800
or date_format(c_sdate,'%H%i')>2000
in above query for counting Working
the sub query returns 1 and 2
while the outer eliminate c_id=2 but not c_id=1
Try this query
SELECT c_emp,
sum(if(cnt>=1,1,0)) as Office,
count(*)-sum(if(cnt>=1,1,0)) as WFH from (
select c_emp, Date(c_sdate),
sum(if(c_sdate BETWEEN Date(c_sdate) + interval 8 hour
AND Date(c_sdate) + interval 20 hour, 1, 0)) as cnt
from table1
group by c_emp, Date(c_sdate)) tmp
group by c_emp
SQL FIDDLE:
| C_EMP | OFFICE | WFH |
------------------------
| 1 | 2 | 2 |
| 2 | 0 | 1 |
For monthly report
SELECT c_emp, date_format(c_date, '%c %Y') as Mnth,
sum(if(cnt>=1,1,0)) as Office,
count(*)-sum(if(cnt>=1,1,0)) as WFH from (
select c_emp, Date(c_sdate) as c_date,
sum(if(c_sdate BETWEEN Date(c_sdate) + interval 8 hour
AND Date(c_sdate) + interval 20 hour, 1, 0)) as cnt
from table1
group by c_emp, Date(c_sdate)) tmp
group by c_emp,Mnth
SQL FIDDLE:
| C_EMP | MNTH | OFFICE | WFH |
---------------------------------
| 1 | 5 2013 | 2 | 1 |
| 1 | 6 2013 | 0 | 1 |
| 2 | 6 2013 | 0 | 1 |
I want to calculate total hrs spend by an employee between 09:00am and 18:00pm.
My database look like this.
How can I do this??
AttendanceId EmpId CheckTime CheckType
-------------------------------------------------------------------------
3 5 2013-01-03 09:00:15.000 1 (Login)
4 5 2013-01-03 11:00:00.000 2 (Logout)
5 5 2013-01-03 11:30:00.000 1
6 5 2013-01-03 13:00:00.000 2
7 5 2013-01-03 13:30:00.000 1
8 5 2013-01-03 16:00:00.000 2
9 5 2013-01-03 16:30:00.000 1
10 5 2013-01-03 18:00:00.000 2
Since your Login/Logout values are in the same column, this might be easier to PIVOT the login/logout times first then get the datediff to determine the total amount of time an employee is present.
The PIVOT portion of the query is this:
select empid, [1], [2]
from
(
select empid, checktime, checktype,
row_number() over(partition by empid, checktype order by checktime) rn
from yourtable
) src
pivot
(
max(checktime)
for checktype in ([1], [2])
) piv
See SQL Fiddle with Demo
The result of this is:
| EMPID | 1 | 2 |
---------------------------------------------------------------------------
| 5 | January, 03 2013 09:00:15+0000 | January, 03 2013 11:00:00+0000 |
| 5 | January, 03 2013 11:30:00+0000 | January, 03 2013 13:00:00+0000 |
| 5 | January, 03 2013 13:30:00+0000 | January, 03 2013 16:00:00+0000 |
| 5 | January, 03 2013 16:30:00+0000 | January, 03 2013 18:00:00+0000 |
Once the data is in this structure, you can easily get the difference in the time by applying the DateDiff() function.
The final query to generate the amount of time an employee is logged in is:
select empid, sum(SecondsDiff) / 3600 as TotalHours
from
(
select empid, datediff(ss, [1], [2]) SecondsDiff
from
(
select empid, checktime, checktype,
row_number() over(partition by empid, checktype order by checktime) rn
from yourtable
) src
pivot
(
max(checktime)
for checktype in ([1], [2])
) piv
) src
group by empid
See SQL Fiddle with Demo
And the result is:
| EMPID | TOTALHOURS |
----------------------
| 5 | 7 |
To get the difference between two dates, you use the DATEDIFF function:
http://msdn.microsoft.com/en-us/library/ms189794.aspx
I think you'd need to do this row-by-row, though. Because of the structure of your table, you can't just do a simple query.