Hello guys I need help with this function, I'm using MySQL database server
version: 5.5.47-0ubuntu0.14.04.1 - (Ubuntu).
I want to be able to get how many "weeks" it took a worker to get hired. i have a table that stores log data whenever the worker's status change.
logid workerid statusid timestamp
1000 10 1(available) 2016-04-10
1001 10 2(Hired) 2016-04-24
what i want to have is :
It took worker 10 two weeks to get hired.
I already looked at DATEDIFF(first date, second date) function but I have to use two different where conditions for each date, I have no idea how to do this?
You need to join same table and to use DATEDIFF():
SELECT
e1.workerid,
DATEDIFF(e2.`timestamp`, e1.`timestamp`) daysDiff,
ROUND(DATEDIFF(e2.`timestamp`, e1.`timestamp`) / 7, 0) weeksDiff
FROM
employees e1
INNER JOIN employees e2 ON e1.workerid = e2.workerid
WHERE
e1.statusid = 1
AND e2.statusid = 2
Output:
+----------+----------+-----------+
| workerid | daysDiff | weeksDiff |
+----------+----------+-----------+
| 10 | 14 | 2 |
+----------+----------+-----------+
1 row in set
Related
So I've got two tables:
ic:
id | created
---|--------------------
1 | 2016-03-31 16:20:03
2 | 2016-03-31 16:25:18
3 | 2016-03-31 16:28:09
status:
id | ic_id | timestamp
---|--------------------
1 | 1 | 2016-03-31 16:20:03
2 | 5 | 2016-03-31 16:25:18
3 | 5 | 2016-03-31 16:28:09
I now want to find the average difference between the ic.created and the status.timestamp of the first corresponding record in the status table. I started out with this:
SELECT status.`timestamp` - informed_consent.created as difference
FROM status
WHERE status.`timestamp` > '2017-06-19'
AND status.ic_id IN (
SELECT informed_consent.id
FROM informed_consent
WHERE informed_consent.id = status.ic_id;
);
But I immediately get an error in my mysql syntax. I guess I can also use a left join, but I'm kinda lost here.
Could anybody help me out in the right direction?
I think you can use inner join instead of sub query due to informed_consent.created is not present in outer sql,it's in the sub query
SELECT status.`timestamp`,status.`timestamp` - informed_consent.created as difference
FROM status
JOIN informed_consent ON informed_consent.id = status.ic_id
WHERE status.`timestamp` > '2017-06-19'
I Have 2 tables in my DB and I want to compare values of 2 select queries Ive made on each one
Table 1: click_log
Query table 1:
SELECT *
FROM click_log
Table 2: km_articles
Query table 2:
SELECT km_article_no
FROM km_articles
WHERE km_article_date <= "2017-10-31" AND km_article_status = "Published" AND km_article_view_count <= "5"
The columns I want to compare are table link_clicked for table 1 with km_article_no and I know I will find repeated matched, nevertheless from those repeated matches I want to find the latest one that I want to get from another column in table 1 called "when_clicked" that contains data information, not sure How can i put together those to queries and then narrow them down.
this is how the tables look like:
Table 1:
|link_clicked|when_clicked
KB00001 | 2017-08-02
KB00001 | 2017-12-02
KB00002 | 2017-08-02
KB00002 | 2017-09-02
KB00003 | 2017-09-02
KB00003 | 2017-09-02
Table 2:
km_article_no|km_article_ti|km_article_status|km_article_view_count|km_article_date
KB00001 |outlook IOS | Published | 5 | 2017-01-02
KB00002 |outlook CSS | Published | 4 | 2017-01-05
KB00003 |outlook ZTE | Retired | 3 | 2017-01-09
If I understand correctly, you want to show all km_articlesrows, each with the latest related click_log.when_clicked date. So aggregate your click_log per link_clicked and find the maximum when_clicked. Then join this to km_articles.
select kma.*, cl.last_clicked
from km_articles kma
join
(
select link_clicked, max(when_clicked) as last_clicked
from click_log
group by link_clicked
) cl on cl.link_clicked = kma.km_article_no
where kma.km_article_date <= date '2017-10-31'
and kma.km_article_status = 'Published'
and kma.km_article_view_count <= 5;
(If you also want to show km_articles rows that have no match in click_log, then change join to left join.)
i have a tbl_remit where i need to get the last remittance.
I'm developing as system wherein I need to get the potential collection of each Employer using the Employer's last remittance x 12. Ideally, Employers should remit once every month. But there are cases where an Employer remits again for the same month for the additional employee that is newly hired. The Mysql Statement that I used was this.
SELECT Employer, MAX(AP_From) as AP_From,
MAX(AP_To) as AP_To,
MAX(Amount) as Last_Remittance,
(MAX(Amount) *12) AS LastRemit_x12
FROM view_remit
GROUP BY PEN
Result
|RemitNo.| Employer | ap_from | ap_to | amount |
| 1 | 1 |2016-01-01 |2016-01-31 | 2000 |
| 2 | 1 |2016-02-01 |2016-02-28 | 2000 |
| 3 | 1 |2016-03-01 |2016-03-31 | 2000 |
| 4 | 1 |2016-03-01 |2016-03-31 | 400 |
By doing that statement, i ended up getting the wrong potential collection.
What I've got:
400 - Last_Remittance
4800 - LastRemit_x12 (potential collection)
What I need to get:
2400 - Last_Remittance
28800 - LastRemit_x12 (potential collection)
Any help is greatly appreciated. I don't have a team in this project. this may be a novice question to some but to me it's really a complex puzzle. thank you in advance.
You want to filter the data for the last time period. So, think where rather than group by. Then, you want to aggregate by employer.
Here is one method:
SELECT Employer, MAX(AP_From) as AP_From, MAX(AP_To) as AP_To,
SUM(Amount) as Last_Remittance,
(SUM(Amount) * 12) AS LastRemit_x12
FROM view_remit vr
WHERE vr.ap_from = (SELECT MAX(vr2.ap_from)
FROM view_remit vr2
WHERE vr2.Employer = vr.Employer
)
GROUP BY Employer;
EDIT:
For performance, you want an index on view_remit(Employer, ap_from). Of course, that assumes that view_remit is really a table . . . which may be unlikely.
If you want to improve performance, you'll need to understand the view.
I am trying to create high charts time line graph for my school attendance but I get slightly different records appearing in my result.
The table that stores attendance is :
+++++++++++++++++++++++++++++++++
Id | Date | AttendTime(varchar)
--------------------------------
1 | 20160815 | 7:51
2 | 20160815 | 7:53
3 | 20160815 | 8:01
-------------------------------
I am using following sql to get the data
SELECT
COUNT(Id) as Total
FROM Attendance
WHERE Date = '20160815' AND
HOUR(STR_TO_DATE(AttendTime, '%H:%i')) <= HOUR(STR_TO_DATE('7:30', '%H:%i'))
And I should get 0 as there are none but I get 3 records. What am I doing wrong?
Thank you.
If you are simply trying to see how many attendances were before 7:30, you do not need the HOUR function:
SELECT
COUNT(Id) as Total
FROM Attendance
WHERE Date = '20160815'
AND STR_TO_DATE(AttendTime, '%H:%i') <= STR_TO_DATE('7:30', '%H:%i')
I have a simple database that keeps track of transactions from multiple players in a game.
When a game is started between two players, a unique game_id is generated and stored in a separate database called game_ids. In the database I am querying (game_transactions) I need the following to happen...
Table game_transactions sample data:
game_id | home_id | visiting_id | timestamp | ...
3 1 2 00000001
4 1 3 00000001
3 2 1 00000002
3 1 2 00000003
4 1 3 00000002
3 2 1 00000004
4 1 3 00000003
As you can see, when one player completes a turn their game is added to this table. This goes on until the game is completed. What I am trying to do is get all rows when searching by the user_id "WHERE home_id = user_id OR visiting_id = user_id". However this returns ALL the rows for that specific user, I want to limit the query to give the row of data for the MOST RECENT game_id by the given timestamp. The timestamp is an actual UNIX time stamp, but for example purposes this is more readable.
When said and done, this query needs to return the following when user_id = 1:
game_id | home_id | visiting_id | timestamp | ...
3 2 1 00000004
4 1 3 00000003
Please let me know if I need to clarify further.
Thank you.
On a side note, I will be purging the database of entries older than x days. But this will be a cron that runs once a day, trying to keep queries to a minimum!
SELECT gt.game_id, gt.home_id, gt.visiting_id, gt.timestamp
FROM game_transactions gt
INNER JOIN (SELECT game_id, MAX(timestamp) AS MaxTimestamp
FROM game_transactions
WHERE 1 IN (home_id, visiting_id)
GROUP BY game_id) q
ON gt.game_id = q.game_id
AND gt.timestamp = q.MaxTimestamp