I need to show a timeline from MySQL table. Basically retrieve count of records by each hour. My TimeSigned column is DateStamp
Login
Id TimeSigned MarkedBy
1. 2016-03-14 05:12:17 James
2. 2016-03-14 05:30:10 Mark
3. 2016-03-14 06:10:00 James
4. 2016-03-14 07:30:10 Mary
I am using following query but it brings wrong results.
SELECT COUNT(Id) From Logins WHERE HOUR(TimeSigned) > 5 AND HOUR(TimeSigned) < 6
I was expecting it to return a count of 2 (i.e. 1 and the 2 record are within the 5-6 time range) but it brings back 0.
I have created a sqlfiddle here SQL Fiddle
Use = in your first condition. there is nothing between 5 and 6 so it will give count 0
SELECT COUNT(Id) From Logins WHERE HOUR(TimeSigned) >= 5 AND HOUR(TimeSigned) < 6
HOUR() returns the hour part so it's whole numbers.
There are no whole numbers that are greater than 5 and less than 6.
I think you want to just look for the hour is equal to 5
SELECT COUNT(Id) From Logins WHERE HOUR(TimeSigned) = 5
Or if you want you could return counts for each hour by doing
SELECT COUNT(Id) as Count,HOUR(TimeSigned) as Hour From Logins GROUP BY HOUR(TimeSigned)
Related
I've got database in MySQL with Output like that.
DATE_ADD | ID_PRODUCT | NOTE
That is TimeLine status for id_product. It could be more than 100 duplicate record by every Id_product in 1 week. What I need to find : only this RECORD without New NOTES in last 21 days. (>20 days)
Means: nobody is changing status of produckt for long time.
How to write the query to find only this record without new note in last 21 days.
And show the note order by date_add desc.
Thanks in advance for help me in that issue.
Right now I wrote smg like that:
Select
N.id_product,
N.date_add,
S.transaction,
N.note
from Sale S
left join note N on N.id_product=S.id_product
Where
(...)
and (N.date_add > DATE_SUB(CURDATE(), INTERVAL 20 DAY)) is null
Group By N.id_product
order by N.date_add desc
I have a following table structure:
page_id view_count date
1 30 2018-08-30
1 33 2018-08-31
1 1 2018-09-01
1 5 2018-09-02
...
View count is reset on 1st of every month, and it's current value is stored on a daily basis, so on 31st of August it was increased by 3 (because 33-30).
What I need to do is to retrieve the view count (difference) between two dates through SQL query. To retrieve view count between two dates in same month would be simple, by just subtracting bigger date with the lower date, but retrieving between two dates that are in different months is what's not sure to me how to achieve. If I wanted to retrieve data between 2018-08-13 and 2018-09-13 I would have to get difference between 2018-08-31 and 2018-08-13, and add it to the value of 2018-09-13.
Also, I would like to do it for all page_id at once, between the same dates if possible within a single query.
assuming that the counter is unique per page and that the page_id counter is inserted daily into the table, I think that such a solution would work
The dates are based on the example,
and should be replaced by the relevant parameters
SELECT
v1.view_count + eom.view_count - v2.view_count
FROM
view_counts v1
INNER JOIN view_counts v2 ON v2.page_id = v1.page_id AND v2.`date` = '2018-08-13'
INNER JOIN view_counts eom ON v2.page_id = v.page_id AND eom.`date` = LAST_DAY(DATE_ADD(v.`date`, INTERVAL -1 MONTH))
WHERE
`date` = '2018-09-13'
I have a table with a lot of rows and i need to get the count of them grouped by ID and date bimonthly
For example
**ID Date**
15 2016/01/01
15 2016/01/04
15 2016/01/05
15 2016/01/22
15 2016/01/30
15 2016/02/01
15 2016/02/16
15 2016/03/01
15 2016/03/16
15 2016/03/22
Expected results:
**Count ID Date**
3 15 2016/01/01
2 15 2016/01/15
1 15 2016/02/01
1 15 2016/02/15
1 15 2016/03/01
2 15 2016/03/15
Currently i have this:
SELECT count(*) as '#', ID, from_unixtime(Date, '%Y-%m-%d') as 'Date'
FROM table
GROUP BY country,WEEK(FROM_UNIXTIME(Date))
ORDER BY Date
Which indeed groups by week but starting from the first input and so on (whici is not what i want but is as close as i have gotten)
EDIT: Changed the term to bimonthly
The 1st thru 15th and 16th thru 30/31 groups are not "biweekly". That grouping would be referred to as bimonthly. (I'd prefer the term "dimonthly" if that were a word.)
It's odd that you would want to return a value of the 15th, for the group that would not contain the 15th, rather than returning the 16th.
You could use expressions to get you the year and month, and then the 1st or 15th.
GROUP BY CONCAT(DATE_FORMAT(t.date,'%Y-%m-'),IF(DAY(t.date)<16,'01','15'))
+ INTERVAL 0 DAY
You could use the same expression in the SELECT list to return the date value.
(My personal preference would be to return a date value of yyyy-mm-16, rather than -15.)
Note that this won't return a "zero count" for a bimonthly period where there aren't any rows. A row for such a period would be "missing".
GROUP BY clauses can be arbitrary expressions, e.g.
GROUP BY (DAY(FROM_UNIXTIME(Date)) <= 15)
which would split things up into 1-15 and 16->
I have a table that contains records of different transaction that is needed to be updated monthly. Once the record for a specific month has been successfully updated, it will insert a new record to that table to indicate that it is already updated. Let's take this example.
**date_of_transaction** **type**
2015-04-21 1 //A deposit record
2015-04-24 2 //A withdrawal record
2015-04-29 1
2015-04-30 2
2015-04-30 3 //3, means an update record
2015-05-14 1
2015-05-22 1
2015-05-27 2
2015-05-30 2
2015-06-09 1
2015-06-12 2
2015-06-17 2
2015-06-19 2
Let's suppose that the day today is July 23, 2015. I can only get the data one month lower than the current month, so only the data that I can get are june and downwards records.
As you can see, there is an update performed in the month of April because of the '3' in the type attribute, but in the month of May and June, there are no updates occurred, how can I get the month that is not yet updated?
This will return you months, which has no type=3 rows
SELECT MONTH([trans-date]) FROM [table] GROUP BY MONTH([trans-date]) HAVING MAX([trans-type])<3
Note: this will not work if 3 is not max value in the column
My approach would be to find all the months first, then find the months whose records were updated. Then select only those months from all months whose records werent updated (A set minus operation).
Mysql query would be something like this
select extract(MONTH,data_of_transaction) from your_table_name where month not in (select extract(MONTH,data_of_transaction) from table where type=3);
You can try this;
select *
from tbl
where date_of_transaction < 'July 23, 2015'
and
date_format(date_of_transaction, '%M-%Y') in (
select
date_format(date_of_transaction, '%M-%Y')
from tbl
group by date_format(date_of_transaction, '%M-%Y')
having max(type) != 3
)
date_format(date_of_transaction, '%M-%Y') will take month-year in consideration and filter the data having type = 3.
I have a webapplication linked to a mysql database with the following fields:
field 1:trip_id
field 2:trip_destination
field 3:trip_description
field 4:trip_duration
In the webapplication I have a listbox based on the following:
ListBox value =1: trip duration 1 - 5 days
ListBox value =2: trip duration 6 - 10 days
Listbox value =3: trip duration 11 -20 days
ListBox value =4: trip duration over 20 days
How do I put this in the sql select statement?
SELECT * FROM trip_table WHERE trip_duration BETWEEN start_day-1 AND end_day+1;
You would then need to replace start_day and end_day with your periods e.g. start_day = 6 end_day=10.
Hope this helps.
in its simplest form, from your internally controlled values of the listbox ranges (and I'm not a PHP programmer to fill in the blanks), but a query could be.
select *
from TripTable
where trip_duration >= ?MinimumDays
AND trip_duration <= ?MaximumDays
If you are trying to get all trips, and have them pre-stamped with a 1-4 classification, I would apply a CASE WHEN
select *,
case
when trip_duration >= 1 and trip_duration <=5 then 1
when trip_duration >= 6 and trip_duration <=10 then 2
when trip_duration >= 11 and trip_duration <=20 then 3
when trip_duration > 20 then 4
end as TripDurationType
from
TripTable