The following table has the time when the user enters/quit on the company:
ID | user_id | day_unix | time
10 1 1459220400 1459293745
9 1 1459220400 1459293711
8 1 1459220400 1459293689
7 1 1459220400 1459293678
6 1 1459220400 1459293669
11 1 1459220400 1459293761
day_unix consists in the first second of the day, this way, GROUP BY can be easily used in future.
time consists in the time that the user click's on the button to start/stop working. Time also can be changed to a native DATETIME column.
I want to create a SQL query capable of summing the time between the entries. So the query must to jump "odd" entries that can be considered as the beginnig of a small coffee break and right after the "odd" entries the user has started working again, so the query must to sum the working time, excluding the coffee breaks.
Any idea? Here is what I've already got:
SELECT * FROM table GROUP BY day_unix ORDER BY time ASC
Related
Working on the DB design of a Badge System which will work synchronously(request/response) for variant criteria for more than 1000+ badges I am wondering how the badges will be checked when a user does a request.
A first DB Schema I am thinking is:
Badge
id name
1 Check-in 10 Beers, of X Manufacturer the last 30 days
2 Check-in 5 Organic Beers from England
3 Comment on 5 different check-ins
Rules
id key
1 count_of
2 manufacturer_of
3 last_x_days
4 comment_x_diff_checkins
5 from_country_x
BadgeRules
badge_id rule_id value
1 1 10 (check-in 10 beers)
1 2 122 (manufacturer_id)
1 3 30 (days)
The think is that the User makes a check-in and the system should check the different variants for different badges.
As an example need to check on the above DB data that the User made 10 check-ins of Manufacturer with ID 122 the last 30 days. Obviously makes no sense to check 1000+ badges for each request but somehow can be checked only the Badges that the User can win for that check-in.
I was thinking to save for user's state in a different db table but I dont think that this can work for all the rules like last_x_days
UserState
id user_id key value
1 1 checkins 8
2 1 comments 12
P.S. I have seen the Untappd app which has thousands of Badges and it assign them on users request.
I would be grateful for a proposal.
I was not able to find an answer while reading similar questions, so I'll ask my question here, appreciate any help on this matter.
In MySQL DB have a table with articles:
id date is_active title text
3 2017-01-20 1 New payment system goes live some articl
5 2017-01-21 1 Library v.2.5 released some articl
6 2017-01-22 1 New skins and themes some articl
7 2017-01-25 0 Terms and Conditions updated some articl
8 2017-01-26 1 Don't forget to subscribe some articl
10 2017-01-22 0 Support Chat beta release some articl
11 2017-01-30 1 Maintenance window next Sunday some articl
12 2017-01-28 1 Refer a friend and get a bonus some articl
13 2017-01-26 1 Follow us in social networks some articl
14 2017-01-22 1 Video sharing feature is now live some articl
I have 2 web pages:
a list of all active articles ordered by date (important: it is possible that several articles can have same date).
This works fine, no issue here.
single article read page, additionally I have "Next" and "Previous" links on that page
This is an issue.
I would like to show 'next' and 'prev' links as a href="article.php?id=8". So I would like to get next record ID from DB according to the query like this:
SELECT id FROM articles WHERE date > (SELECT date FROM news WHERE id = 6) and isactive = 1 ORDER BY date ASC LIMIT 1;
However my query does not work properly when it comes to defining next article id with the same date. When user stays on the article id=3 and clicks 'next' multiple times I would expect the following order of loading articles:
id title date is_active
5 Library v.2 2017-01-21 1
6 New skins a 2017-01-22 1
14 Video shari 2017-01-22 1
8 Don't forge 2017-01-26 1
13 Follow us i 2017-01-26 1
12 Refer a fri 2017-01-28 1
11 Maintenance 2017-01-30 1
But what I get is: 5, 6, 8, 12, 11. So 2 records are missing in this sequence (ids: 14 and 13) because they have same date.
I tried playing with different queries, but all results are not 100% right (in some cases it keeps returning same numbers, ex.: 5, 6, 14, 6, 14....., etc. )
Is is possible to get the 'next' IDs based on current ID and desired order via MySQL query(ies)? I am not against doing several queries or working with nested queries.
As a workaround, of course I can just retrieve an ordered array of all ids like this:
SELECT id FROM articles WHERE isactive=1 ORDER BY date
and then define 'next' and 'previous' using this array, however I don't like this solution.
If you can point me to some similar topics or other possible ways to solve this, please do.
Thank you.
SELECT id
FROM articles
WHERE date >= (SELECT date -- changed
FROM news
WHERE id = 6)
AND isactive = 1
AND id NOT IN(6) -- added, maybe id!=6 or id<>6
ORDER BY date ASC
LIMIT 1
-- (all id date >= date) - (id=6)
Why you do this, why not use
SELECT id FROM news ORDER BY date ASC LIMIT 6,1 -- possition, count
If SQL query caching, this is may be faster.
I'm generally pretty self reliant on fudging something together that works but I have run into a brick wall on this one and am eventually reaching out for a nod in teh right direction..
my query:
$post_views = (int)$wpdb->get_var("
SELECT SUM(count) AS views
FROM ".$wpdb->prefix."post_views
WHERE id IN (".$post_id.") AND type = 0"
The database table looks like this :
id type period count
------- ------- ----------- -------
32310 0 20141023 8
32310 0 20141022 68
32310 1 201443 76
32310 2 201410 76
32310 3 2014 76
32310 4 total 76
The type 0 are the ones I'm interested in, I just want the sum of the COUNT column for the most recent 7 type 0 entries
I have been trying with things based around "ORDER BY period DESC LIMIT 7 " on the end of the query - to no avail, I generally get returns of 0 doing this.
a new type 0 row will be generated for each article every day, so thats why I need to only get the last 7
any help here would be massively appreciated, totally stuck for the first time ever with this.
SELECT SUM(count)
FROM (SELECT count
FROM wp_post_views
WHERE type = 0
AND id IN (684,42,7)
ORDER BY period DESC
LIMIT 7)
Or just determine the date a week ago first and use that to filter, but a subquery like this will work fine as well.
I got a complicated question.
I have a query that combine data from several tables. for example:
traffic: id, traffic_source_id, first_hit_date
goals: id, goal_type, goal_date, goal_value
traffic_sources: id, source_name
goal_type could be "contact form", "demo download", "purchase"
goal_value is numeric. and display the number of times the user reach the goal on the same date (day).
I'm trying to show one row for each traffic_id, and then sum every goal for this traffic_id. When a user comes for the first time, he/she gets a traffic id that stays with him (cookie). he/she can reach a goal a week later after the first hit date, and then other goal 2 days later, for example.
I want to be able to query from date > to date and show the correct sum values of the goals for that speific range.
When I try to use SUM(CASE WHEN ...) I can't sum just the range withing the goals table.
example:
traffic
from: 1-feb to 28-feb
3 | google adwords | 0 contact | 1 demo download | 1 purchase
4 | facebook | 1 contact | 3 demo download | 3 purchase
but when I want to change the range from 1-feb to 14-feb
3 | google adwords | 0 contact | 1 demo download | 0 purchase
4 | facebook | 0 contact | 2 demo download | 2 purchase
hope I'm clear enough...
any advice will be much appreciated.
Update:
query example of what I have now:
SELECT traffic.traffic_id as original_traffic_id hit_date, referrer, referrer_url, keyword, ip,
SUM(CASE goals.goal_type WHEN 'Contact' THEN 1 ELSE 0 END) goal_contact,
SUM(CASE goals.goal_type WHEN 'Download' THEN 1 ELSE 0 END) goal_download,
SUM(CASE goals.goal_type WHEN 'Signup' THEN 1 ELSE 0 END) goal_signup,
SUM(CASE goals.goal_type WHEN 'Purchase' THEN 1 ELSE 0 END) goal_purchase
FROM traffic
LEFT JOIN goals ON goals.traffic_id = traffic.traffic_id
WHERE traffic.traffic_id=100 AND hit_date >= '$from_date' AND hit_date <= '$to_date'
(where $from_date and $to_date are mysql date formats)
(this is not the real query since the original query much larger and includes about 7 more tables that joins in)
This query actually sums all the goals without having into cound goal_date. I want to be able to limit the SUM to the range of $from_date and $to_date
Hope it clears it a bit more.
I don't have the answer for this yet, but I what I did is to summerize all goals with another query prior to the first one.
The array looks like $goals_array[traffic_id][goal_type] -> value
And while looping through the main sql, just fetch the value from the $goals_array.
It works fast, and altough I wish to make this on one query, that's ok for now.
I have this table:
id track_name datetime weight
1 aName 2010-06-01 09:00:00 1
2 theName3 2010-07-01 11:00:00 2
3 heyThere 2010-08-01 16:00:00 3
4 abcd 2010-08-01 22:44:00 4
5 g123go 2010-08-01 22:00:50 5
6 foobar 2010-09-01 13:11:00 6
7 barfoo 2010-11-01 12:00:55 7
8 barbar 2010-12-01 11:11:00 8
The weight determines the row record order. It is used for ordering a playlist. And the user can move items up and down, thus reordering in the simple fashion, in which works great.
Now I wonder if there is possible to write a single query that can change the 'weight' value based on the 'date' column, ordering by either DESC or ASC. The same for the 'track_name' column.
Example pseudo query:
UPDATE table SET weight (start from 1) ORDER BY datetime ASC
My alternative is to fetch all rows and process each and everyone of them on the web server, which I doubt is the most effecient way, if there are thousands of records.
I don't think you can do it with a single query. You need a counter, this means you need a loop. If you want to do it with MySQL only, you can create a stored procedure. If not, just write a PHP script (witch might will be a bit slower). Logic is the same:
Get all the data from the table;
Loop through every record in the
order you need and update weight
parameter.
You could use a temporary table with an auto incremented id and select insert into it using your order.