I need to make query that selects data between two dates but exclude weekdays and some other days that are for example public holidays.
SELECT
`tblproduction`.`OperaterID`, `tblproduction`.`OperationID`,
SUM(`tblproduction`.`TotalProduced`) AS TotalProduced,
SUM(`tblproduction`.`TotalProducedOperator`) AS TotalProducedOp,
'Normal working day' AS DayType
FROM `tblproduction`
WHERE tblproduction.StartDateTime >= '2015-02-01 00:00:00' AND (tblproduction.StartDateTime <= '2015-02-28 23:59:59') AND tblproduction.OperaterID = 10
AND (DAYOFWEEK(tblproduction.StartDateTime) IN (1,7)) AND (DATE(tblproduction.StartDateTime) NOT IN (SELECT HolidayDate FROM tblholidays))
GROUP BY `tblproduction`.`OperaterID`, `tblproduction`.`OperationID`
UNION ALL
SELECT
`tblproduction`.`OperaterID`, `tblproduction`.`OperationID`,
SUM(`tblproduction`.`TotalProduced`) AS TotalProduced,
SUM(`tblproduction`.`TotalProducedOperator`) AS TotalProducedOp,
'Weekend' AS DayType
FROM `tblproduction`
WHERE tblproduction.StartDateTime >= '2015-02-01 00:00:00' AND (tblproduction.StartDateTime <= '2015-02-28 23:59:59') AND tblproduction.OperaterID = 10
AND (DAYOFWEEK(tblproduction.StartDateTime) NOT IN (1,7)) AND (DATE(tblproduction.StartDateTime) NOT IN (SELECT HolidayDate FROM tblholidays))
GROUP BY `tblproduction`.`OperaterID`, `tblproduction`.`OperationID`
For that purpose i have table with predefined dates that represents public holidays named tblHolidays. One of criteria in sql query is avoid counting pieces made during the holiday date. By running this query is steel includes those dates that are holidays. What should I change in query?
Maybe start with this...
SELECT p.OperaterID
, p.OperationID
, SUM(p.TotalProduced) TotalProduced
, SUM(p.TotalProducedOperator) TotalProducedOp
, CASE WHEN DAYOFWEEK(p.startdatetime) IN (1,7) THEN 'Normal working day' ELSE 'Weekend' END DayType
FROM tblproduction p
WHERE p.StartDateTime >= '2015-02-01 00:00:00' AND p.StartDateTime <= '2015-02-28 23:59:59'
AND p.OperaterID = 10
AND DATE(p.StartDateTime) NOT IN (SELECT HolidayDate FROM tblholidays)
GROUP
BY p.OperaterID
, p.OperationID
, CASE WHEN DAYOFWEEK(p.startdatetime) IN (1,7) THEN 'Normal working day' ELSE 'Weekend' END
Related
i want to change 2016-11-20 with current date
SELECT services, loket,
SUM( CASE WHEN STATUS IN ('7','4') THEN 1 ELSE 0 END ) AS Total_queue,
SUM( CASE WHEN STATUS = '4' THEN 1 ELSE 0 END ) AS balance_queue, SUM( CASE WHEN STATUS = '7' THEN 1 ELSE 0 END ) AS nomer_queue
FROM tabelqueue
WHERE STATUS IN ('7','4')
AND get_ticket BETWEEN '2016-11-21 00:00:01' AND '2016-11-21 23:59:59'
GROUP BY services
Write the condition as:
WHERE STATUS IN ('7', '4') AND
get_ticket >= CURDATE() AND get_ticket < DATE_ADD(CURDATE(), interval 1 day)
Your original condition should probably be written as:
WHERE STATUS IN ('7', '4') AND
get_ticket >= DATE('2016-11-21') AND
get_ticket < DATE_ADD(DATE('2016-11-21'), interval 1 day)
Notes:
Don't use times when you only want dates.
You could do DATE(get_ticket) = CURDATE(). This is easy to read but the query cannot take advantage of an index on get_ticket, if that is available and appropriate.
If STATUS is a number, do not use quotes around the constants. Quoted numbers are misleading both for people and for the optimizer.
Use the GETDATE() function; for example:
WHERE STATUS IN ('7','4')
AND get_ticket BETWEEN GETDATE() AND '2016-11-21 23:59:59'
GROUP BY services
This gives the current date and time; if you want it to start at midnight today you can use:
CONVERT(VARCHAR(12), GETDATE(), 102)
select .....
where
a.DateCreated between '2014-01-01 00:00:00' and '2015-01-31 23:59:59'
group by a.StoreID , b.ProductID , DATE_FORMAT(a.DateCreated, '%m')
and
select ....
where
a.DateCreated between '2014-01-01 00:00:00' and '2015-01-31 23:59:59'
group by a.StoreID , b.ProductID , DATE_FORMAT(a.DateCreated, '%Y-%m')
the first is just month
and you get a maximum of 12 rows for each combination of a.StoreID ,
b.ProductID in the result
the second is year and month (this is probably better by the way)
and you get 12 monthly rows for each year and each combination of a.StoreID ,b.ProductID in the result
however!
do NOT use between for date rage filtering, and do NOT use 23:59:59 as the end of the day because it isn't, a much better way is:
where a.DateCreated >= '2014-01-01 00:00:00'
and a.DateCreated < '2016-01-01 00:00:00'
I'm trying to do a query that fetches data per the last hour and the two last hours. It's just a hits counter.
So I would like to get the resultset as follows:
id, page_url, last_hour_hits, two_last_hours_hits
The table is very simple:
id (autonumber)
page_url
time_stamp
I tried the query below:
SELECT
page_url,
COUNT(page_url) AS last_hour_hits
FROM stats
WHERE time_stamp > '2015-08-01 00:00:00'
GROUP BY page_url
('2015-08-01 00:00:00' is calculated for the last hour)
It works fine, but I have now idea how to add the 'two_last_hours' counter.
Thank you in advance.
With MySQL you can use the fact that boolean expression returns 1 for true and simplify the query to this:
SELECT
page_url
,sum(time_stamp > (now() - interval 1 hour)) as last_hour_hits
,sum(time_stamp > (now() - interval 2 hour)) as two_last_hours_hits
FROM stats
GROUP BY page_url;
This uses now() to get the current time and subtracts the interval as needed.
Just put your WHERE clause with two hours before ('2015-07-31 23:00:00') and a CASE WHEN to count for the last hour ('2015-08-01 00:00:00') :
SELECT
page_url,
COUNT(DISTINCT CASE WHEN time_stamp > '2015-08-01 00:00:00' THEN id ELSE NULL END) as last_hour_hits
COUNT(*) AS two_last_hours_hits
FROM stats
WHERE time_stamp > '2015-07-31 23:00:00'
GROUP BY page_url;
you can use join between two result sets- one for the one hour and the other for the two hours:
SELECT stats.page_url, last_hour_hits, last_two_hours_hits from
stats inner join (
SELECT page_url, COUNT(page_url) AS last_hour_hits
FROM stats WHERE time_stamp > '2015-08-01 00:00:00'
GROUP BY page_url ) as last_hour
on stats.page_url = last_hour.page_url
inner join (
SELECT page_url, COUNT(page_url) AS last_two_hours_hits
FROM stats WHERE time_stamp > '2015-07-31 23:00:00'
GROUP BY page_url ) as last_two_hours
on stats.page_url = last_two_hours.page_url
I am new here and tried to look up the answer to my question but couldn't find anything on it. I am currently learning how to work with SQL queries and am wondering how I can count the amount of unique values that appear in two time intervals?
I have two columns; one is the timestamp while the other is a customer id. What I want to do is to check, for example, the amount of customers that appear in time interval A, let's say January 2014 - February 2014. I then want to see how many of these also appear in another time interval that i specify, for example February 2014-April 2014. If the total sample were 2 people who both bought something in january while only one of them bought something else before the end of April, the count would be 1.
I am a total beginner and tried the query below but it obviously won't return what I want because each entry only having one timestamp makes it not possible to be in two intervals.
SELECT
count(customer_id)
FROM db.table
WHERE time >= date('2014-01-01 00:00:00')
AND time < date('2014-02-01 00:00:00')
AND time >= date('2014-02-01 00:00:00')
AND time < date('2014-05-01 00:00:00')
;
Try this.
select count(distinct t.customer_id) from Table t
INNER JOIN Table t1 on t1.customer_id = t.customer_id
and t1.time >= '2014-01-01 00:00:00' and t1.time<'2014-02-01 00:00:00'
where t.time >='2014-02-01 00:00:00' and t.time<'2014-05-01 00:00:00'
Here's one method of doing this with conditional grouping in an inner-select.
Select Case
When GroupBy = 1 Then 'January - February 2014'
When GroupBy = 2 Then 'February - April 2014'
End As Period,
Count (Customer_Id) As Total
From
(
SELECT Customer_Id,
Case
When Time Between '2014-01-01' And '2014-02-01' Then 1
When Time Between '2014-02-01' And '2014-04-01' Then 2
Else -1
End As GroupBy
From db.Table
) D
Where GroupBy <> -1
Group By GroupBy
Edit: Sorry, misread the question. This will show you those that overlap those two time ranges:
Select Count(Customer_Id)
From db.Table t1
Where Exists
(
Select Customer_Id
From db.Table t2
Where t1.customer_id = t2.customer_id
And t2.Time Between '2014-02-01' And '2014-04-01'
)
And t1.Time Between '2014-01-01' And '2014-02-01'
i have a table stores activity
a want to generate a overview selecting sum(total) based on the date (unixtime)
like Select SUM(total) from list where date between 123456789 and 2000000
somethings like this ..
but i would like to get more date ranges out with one query
to have a listable result like
sum(total) of 2 days ago
sum(total) of yesterday
sum(total) of today
and so on ...
You don't need a UNION. Something like this should work:
select SUM(total) as Total,
SUM(
case when date >= DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 2 DAY)
and date < DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY) then total end
) TwoDaysAgoTotal,
SUM(
case when date >= DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)
and date < CONCAT(CURDATE(), ' 00:00:00') then total end
) as OneDayAgoTotal,
SUM(
case when date >= CONCAT(CURDATE(), ' 00:00:00') then total end
) as TodayTotal
from list
where date between 123456789 and 2000000
Union statement don't solve your case? http://dev.mysql.com/doc/refman/4.1/pt/union.html