How to get the data within the same day? - mysql

I'm doing a query to calculate the verified order within the same day. Regarding how to calculate, I can do it but to limit the time, can you help me?
For example: TIMESTAMPDIFF(HOUR, enddate, startdate) <= '24',
the time will be within 1 day, but it can be from one day to next day.
Then how can get the data within the same day?
My original query is:
SELECT COUNT(*),
1.0 * count(CASE WHEN TIMESTAMPDIFF(HOUR, `Created Datetime`, `Order Verification Date`) <= '2' THEN `BOB Sales Order Item` ELSE NULL END) / COUNT(`BOB Sales Order Item`) AS within_2hours_rate,
1.0 * count(CASE WHEN TIMESTAMPDIFF(DAY, `Created Datetime`, `Order Verification Date`) <= '24' THEN `BOB Sales Order Item` ELSE NULL END) / COUNT(`BOB Sales Order Item`) AS within_the_sameday_rate
FROM data.oms
WHERE `Created Datetime` BETWEEN '2017-10-29 00:00:00' AND '2017-11-04 23:59:59' AND `Order Verification Status` = 'finance_verified'
Thanks in advance!

If you're looking to check that the two times are on the same calendar day then you can do DATE(`Created Datetime`) = DATE(`Order Verification Date`).

Related

Is there anybody who can help me with mysql query?

SELECT
(SELECT SUM(kwh) FROM energy_logger WHERE TIME >= '05:30:00' AND TIME < '18:30:00' GROUP BY Date) as daytime,
(SELECT SUM(kwh) FROM energy_logger WHERE TIME >= '18:30:00' AND TIME < '22:30:00' GROUP BY Date) as peaktime,
(SELECT SUM(kwh) FROM energy_logger WHERE TIME >= '22:30:00' OR TIME < '05:30:00' GROUP BY Date) offpeaktime,
Date
FROM energy_logger
GROUP BY Date
ORDER BY ID DESC
This is my query!
And I always have an issue of this, "Subquery returns more than 1 row".....
I think that you are looking for conditional aggregataion:
select
date,
sum(case when time '05:30:00' and time < '18:30:00' then kwh end) daytime,
sum(case when time '18:30:00' and time < '22:30:00' then kwh end) peaktime,
sum(case when time '22:30:00' or time < '05:30:00' then kwh end) offpeaktime
from energy_logger
group by date
order by date
This will give you, for each day, the sum of kwh over the 3 distinct time slots (daytime, peaktime, offpeaktime).

How to combine the results of two MySQL queries into one?

Suppose I have a table like this:
How can I count the number of data that occur at the day of 2018-09-07 for each person, and the number of data that occur at the month 2018-09 for each person?
I mean I want to create a table like this:
I know that
SELECT
name,
COUNT(*) AS day_count_2018_09_07
FROM data_table
WHERE
arrive_time >= '2018-09-07 00:00:00'
AND
arrive_time <= '2018-09-07 23:59:59'
GROUP BY name;
can generate the number of data that occur at the day of 2018-09-07 for each person, and
SELECT
name,
COUNT(*) AS month_count_2018_09
FROM data_table
WHERE
arrive_time >= '2018-09-01 00:00:00'
AND
arrive_time <= '2018-09-30 23:59:59'
GROUP BY name;
can generate the number of data that occur at the month 2018-09 for each person.
But I don't know how to combine the above two queries so that day_count_2018_09_07 and month_count_2018_09 columns can be created in one query.
Here's the SQL fiddle where you can directly get the data in my question.
You can use conditional aggregation to get both results from the same query:
SELECT name,
SUM(CASE WHEN SUBSTR(DATE(arrive_time),1,7)='2018-09' THEN 1 ELSE 0 END) AS month_count_2018_09,
SUM(CASE WHEN DATE(arrive_time)='2018-09-07' THEN 1 ELSE 0 END) AS day_count_2018_09_07
FROM data_table
GROUP BY name
Output:
name month_count_2018_09 day_count_2018_09_07
Ben 3 0
Jane 1 1
John 3 2
Try to combine them like that:
Select DayCounter.name, DayCounter.day_count_2018_09_07, MonthCounter.month_count_2018_09
from
(SELECT
name,
COUNT(*) AS day_count_2018_09_07
FROM data_table
WHERE
arrive_time >= '2018-09-07 00:00:00'
AND
arrive_time <= '2018-09-07 23:59:59'
GROUP BY name) as DayCounter
Inner Join
(SELECT
name,
COUNT(*) AS month_count_2018_09
FROM data_table
WHERE
arrive_time >= '2018-09-01 00:00:00'
AND
arrive_time <= '2018-09-30 23:59:59'
GROUP BY name) as MonthCounter
On DayCounter.name = MonthCounter.name
What about something like this:
SELECT
name,
SUM(CASE WHEN (arrive_time BETWEEN '2018-09-07 00:00:00' AND '2018-09-07 23:59:59') THEN 1 ELSE 0 END) AS day_count_2018_09_07,
SUM(CASE WHEN (arrive_time BETWEEN '2018-09-01 00:00:00' AND '2018-09-30 23:59:59') THEN 1 ELSE 0 END) AS month_count_2018_09
FROM
data_table
GROUP BY
name;

SQL query summary issue

I'm new to SQL and trying to create a total summary of a working SQL query. It's listing the total results from one month of data.
Now I need the total values of the outcome of the query.
So I created a 'query in a query' piece of SQL, but it ain't working because my lack of SQL knowledge. I guess it's an easy fix for you pro's :-)
The working SQL query with the daily outcome of one month:
SELECT
DATE_FORMAT(date, '%d/%m/%y') AS Datum,
COUNT(*) AS Berichten,
SUM(CASE WHEN virusinfected>0 THEN 1 ELSE 0 END) AS Virus,
SUM(CASE WHEN (virusinfected=0 OR virusinfected IS NULL) AND isspam>0 THEN 1 ELSE 0 END) AS Ongewenst,
SUM(CASE WHEN (virusinfected=0 OR virusinfected IS NULL) AND (isspam=1) AND isrblspam>0 THEN 1 ELSE 0 END) AS RBL,
SUM(size) AS Grootte
FROM
maillog
WHERE
1=1
AND (1=1)
AND
date < '2017-04-01'
AND
date >= '2017-03-01'
AND
to_domain = 'domain1.nl'
OR
date < '2017-04-01'
AND
date >= '2017-03-01'
AND
to_domain = 'domain2.nl'
GROUP BY
Datum
ORDER BY
date
The incorrect query trying to create the monthly totals:
SELECT Datum,
SUM(Berichten) AS Berichten,
SUM(Virus) AS Virus,
SUM(Ongewenst) AS Ongewenst,
SUM(RBL) AS RBL,
SUM(Grootte) AS Grootte,
FROM ( SELECT
DATE_FORMAT(date, '%d/%m/%y') AS Datum,
COUNT(*) AS Berichten,
SUM(CASE WHEN virusinfected>0 THEN 1 ELSE 0 END) AS Virus,
SUM(CASE WHEN (virusinfected=0 OR virusinfected IS NULL) AND isspam>0 THEN 1 ELSE 0 END) AS Ongewenst,
SUM(CASE WHEN (virusinfected=0 OR virusinfected IS NULL) AND (isspam=1) AND isrblspam>0 THEN 1 ELSE 0 END) AS RBL,
SUM(size) AS Grootte
FROM
maillog
WHERE
1=1
AND (1=1)
AND
date < '2017-04-01'
AND
date >= '2017-03-01'
AND
to_domain = 'domain1.nl'
OR
date < '2017-04-01'
AND
date >= '2017-03-01'
AND
to_domain = 'domain2.nl'
GROUP BY
Datum
ORDER BY
date
) t
GROUP BY Datum;
Thanks in advance.
What you want can be done with just a little addition to your first SQL statement: add with rollup after the group by clause:
GROUP BY Datum WITH ROLLUP
It will run more efficiently than the version with sub-query, although it could work that way, but you should then remove the outer group by clause and not select Datum there, since you don't want the totals per date any more, but overall.
Still, you will lose the details and only get the overall totals then. You would have to use a union with your original query to get both levels of totals. You can imagine that the with rollup modifier will do the job more efficiently.

Select data between two dates exclude some days

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

Replace sub select counts with a more efficient method

I appear to be having a little spazzy moment today and can't seem to come up with a better way of doing sub select counts on a table.
Basically what I need to do is for each distinct supplier I then need 2 counts (from the same table) one for the total records assigned to that supplier and one for disputed records for that supplier. At the moment I have the query below which is technically correct but slow as hell on a table with 1 mill + records. I'm sure there's a better way of doing it but I can't for the life of me work it out this morning.
SELECT
DISTINCT tblsuppliers.SupplierName,
(SELECT COUNT(*) FROM tblmovements a WHERE m.Supplier=a.Supplier AND a.TicketStatus IN(0,1) AND a.DateRequired>='2013-09-01 00:00:00' AND a.DateRequired<='2013-11-30 23:59:59') as 'Total Tickets',
(SELECT COUNT(*) FROM tblmovements b WHERE m.Supplier=b.Supplier AND b.TicketStatus IN(0,1) AND b.DateRequired>='2013-09-01 00:00:00' AND b.DateRequired<='2013-11-30 23:59:59' AND (b.SuppIsDisputed=1 OR b.SuppDisputeClearedBy>0)) as 'Total Disputed'
FROM tblmovements m
INNER JOIN tblsuppliers ON m.Supplier=tblsuppliers.ID
ORDER BY tblsuppliers.SupplierName ASC
The joined table is just to give me an supplier name as opposed to the supplier ID which is stored in the movements table. Any suggestions are greatly appreciated.
Try this:
SELECT s.SupplierName,
SUM(CASE WHEN m.TicketStatus IN(0,1) AND m.DateRequired>='2013-09-01 00:00:00' AND
m.DateRequired<='2013-11-30 23:59:59' THEN 1 ELSE 0
END) AS 'Total Tickets',
SUM(CASE WHEN m.TicketStatus IN(0,1) AND m.DateRequired>='2013-09-01 00:00:00' AND
m.DateRequired<='2013-11-30 23:59:59' AND
(m.SuppIsDisputed=1 OR m.SuppDisputeClearedBy>0) THEN 1 ELSE 0
END) AS 'Total Disputed'
FROM tblmovements m
INNER JOIN tblsuppliers s ON m.Supplier=s.ID
GROUP BY s.ID
ORDER BY s.SupplierName ASC
SELECT
tblsuppliers.SupplierName,
SUM(IF(TicketStatus IN(0,1) AND DateRequired>='2013-09-01 00:00:00' AND DateRequired<='2013-11-30 23:59:59', 1, 0)) as 'Total Tickets',
SUM(IF(TicketStatus IN(0,1) AND DateRequired>='2013-09-01 00:00:00' AND DateRequired<='2013-11-30 23:59:59' AND (SuppIsDisputed=1 OR SuppDisputeClearedBy>0), 1, 0)) as 'Total Disputed'
FROM tblmovements m
INNER JOIN tblsuppliers ON m.Supplier=tblsuppliers.ID
GROUP BY tblsuppliers.SupplierName