I have two database. One oracle and one sql server.
For oracle.
WITH cycleset AS (
SELECT
c.CYCLE_OID
, UPPER(m.NAME) AS MACHINE_NAME, c.PAYLOAD
FROM mshist.CYCLE c
INNER JOIN msmodel.MACHINE m ON m.machine_oid = c.primarymachine
WHERE (c.CYCLE_OID BETWEEN 1705497113111 AND 1705497596716)),
eventset AS (
SELECT cs.* ,
CAST((
EXTRACT(SECOND FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) +
EXTRACT(MINUTE FROM ( a.END_TIME_UTC- a.START_TIME_UTC) DAY TO SECOND) * 60 +
EXTRACT(HOUR FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 +
EXTRACT(DAY FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 * 24
) AS NUMBER(19,0)) AS DURATION
FROM cycleset cs
INNER JOIN mshist.CYCLEACTIVITYCOMPONENT a ON cs.CYCLE_OID = a.OID
WHERE a.NAME NOT IN ('Machine.Delay')
UNION ALL
SELECT cs.* ,
CAST((
EXTRACT(SECOND FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) +
EXTRACT(MINUTE FROM ( a.END_TIME_UTC- a.START_TIME_UTC) DAY TO SECOND) * 60 +
EXTRACT(HOUR FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 +
EXTRACT(DAY FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 * 24
) AS NUMBER(19,0)) AS DURATION
FROM cycleset cs
INNER JOIN mshist.CYCLEDELAY a ON cs.CYCLE_OID = a.OID)
SELECT MACHINE_NAME, SUM(DURATION) AS EVENT_DURATION, SUM(PAYLOAD) AS PAYLOAD
FROM eventset
GROUP BY
MACHINE_NAME
For SQL.
SELECT
UPPER(MACH_NAME) AS MACHINE_NAME
, CAST(SUM(f.SPLIT_DURATION) AS NUMERIC(19,0) ) AS DURATION,
SUM(f.EVENT_PAYLOAD) AS PAYLOAD
FROM [mssol2015pdw].[dbo].[F_CYCLE_SHIFT_HR_EVENT_LU] f
INNER JOIN [mssol2015pdw].[dbo].D_MACHINE m ON f.MACHINE_1_DIM_KEY = m.DIMENSION_KEY
INNER JOIN [mssol2015pdw].[dbo].D_CYCLE c ON f.[CYCLE_DIM_KEY] = c.DIMENSION_KEY
WHERE m.DIMENSION_KEY > 0
AND c.CYCLE_OID BETWEEN 1705497113111 AND 1705497596716
GROUP BY UPPER(MACH_NAME)
The data should get sorted out by machine name, it is getting sorted out in oracle but not in sql.
I am trying to compare the data in both the columns are same or not.
Also the count of data is not same for both the queries.
just add Order by MACHINE_NAME to the end of your both queries, you can also add Order by 1 at the end of queries, as MACHINE_NAME is first field
Related
I need to create a frequency table that displays the revenue per bin. The bin has a range of 500. The code shown below almost do the work except it is displaying a different values:
SELECT IF(rng='1 - 500','0 - 500',rng)AS Revenue,
IFNULL(B.rngcount,0)AS Count
FROM
(
SELECT '1 - 500' rng UNION
SELECT '501 - 1000' UNION
SELECT '1001 - 1500' UNION
SELECT '1501 - 2000' UNION
SELECT '2001 - 2500'
) A
LEFT JOIN (SELECT CONCAT(FLOOR((product.price * line_item.quantity)/500)*500+1,' - ',FLOOR((product.price * line_item.quantity)/500)*500+500) rng,
COUNT(1) rngcount
FROM line_item, product
GROUP BY rng) B USING (rng);
SELECT CONCAT(ranges.lo, ' - ', ranges.hi) Revenue,
COUNT(*) `Count`
FROM line_item
JOIN product USING (product_id)
JOIN ( SELECT 1 lo, 500 hi UNION
SELECT 501 , 1000 UNION
SELECT 1001 , 1500 UNION
SELECT 1501 , 2000 UNION
SELECT 2001 , 2500 ) ranges ON product.price * line_item.quantity BETWEEN ranges.lo AND ranges.hi
GROUP BY ranges.lo, ranges.hi;
fiddle
As I have commented - the relation between product and line_item must be specified. Common column for them is product_id - so according joining condition added.
And ranges table is used in a form from-to, this simplifies checking what range the revenie is posessed in.
is it possible to sum the Count column, and place the total.
Easily.
SELECT CONCAT(ranges.lo, ' - ', ranges.hi) Revenue,
COUNT(*) `Count`
FROM line_item
JOIN product USING (product_id)
JOIN ( SELECT 1 lo, 500 hi UNION
SELECT 501 , 1000 UNION
SELECT 1001 , 1500 UNION
SELECT 1501 , 2000 UNION
SELECT 2001 , 2500 ) ranges ON product.price * line_item.quantity BETWEEN ranges.lo AND ranges.hi
GROUP BY Revenue WITH ROLLUP;
Totals row is the last, with NULL value in Revenue column.
Here is the code I made to group the revenue according to week.Maybe you can still simplify this.
(sales_agent.name)AS Agent,
(sales_team.name)AS Team,
SUM(IF((WEEK(order_.date) - WEEK(DATE_SUB(order_.date, INTERVAL DAYOFMONTH(order_.date)-1 DAY)) + 1=1), product.price * line_item.quantity,0)) AS Week_1,
SUM(IF((WEEK(order_.date) - WEEK(DATE_SUB(order_.date, INTERVAL DAYOFMONTH(order_.date)-1 DAY)) + 1=2), product.price * line_item.quantity,0)) AS Week_2,
SUM(IF((WEEK(order_.date) - WEEK(DATE_SUB(order_.date, INTERVAL DAYOFMONTH(order_.date)-1 DAY)) + 1=3), product.price * line_item.quantity,0)) AS Week_3,
SUM(IF((WEEK(order_.date) - WEEK(DATE_SUB(order_.date, INTERVAL DAYOFMONTH(order_.date)-1 DAY)) + 1=4), product.price * line_item.quantity,0)) AS Week_4,
SUM(IF((WEEK(order_.date) - WEEK(DATE_SUB(order_.date, INTERVAL DAYOFMONTH(order_.date)-1 DAY)) + 1=5), product.price * line_item.quantity,0)) AS Week_5,
SUM(product.price * line_item.quantity) AS Total
FROM ((((line_item
INNER JOIN order_ ON line_item.order_id = order_.order_id)
INNER JOIN sales_agent ON order_.agent_id = sales_agent.agent_id)
INNER JOIN sales_team ON sales_agent.team_id = sales_team.team_id)
INNER JOIN product ON line_item.product_id = product.product_id)
GROUP BY sales_agent.agent_id;
my query:
SELECT p.idprd,
p.nmprd,
pe.idprd,
pe.stockjual,
pe.stockkeluar,
pe.tothrgjual,
pe.tgljual
FROM tbproduk AS p
INNER JOIN (
SELECT idprd,
Sum(stockjual) AS 'stockjual',
Sum(stockkeluar) AS 'stockkeluar',
Sum(tothrgajual) AS 'tothrgjual',
tgljual
FROM tbpenjualan
WHERE '2019-01-06' >= '2019-01-06' - INTERVAL 7 day
AND '2019-01-06' < '2019-01-06' + INTERVAL 7 day
GROUP BY idprd
) AS pe ON p.idprd = pe.idprd
my result
my data:
date '2019-01-06' not showing. how to my currentdate to showing
GROUP BY has missing column - which would prevent you to even execute SQL statement successfully. But after adjusting SQL statement, results include correct sets of data.
SELECT p.idprd,
p.nmprd,
pe.idprd,
pe.stockjual,
pe.stockkeluar,
pe.tothrgjual,
pe.tgljual
FROM tbproduk AS p
INNER JOIN (
SELECT idprd,
Sum(stockjual) AS 'stockjual',
Sum(stockkeluar) AS 'stockkeluar',
Sum(tothrgajual) AS 'tothrgjual',
tgljual
FROM tbpenjualan
WHERE '2019-01-06' >= '2019-01-06' - INTERVAL 7 day
AND '2019-01-06' < '2019-01-06' + INTERVAL 7 day
GROUP BY idprd, tgljual
) AS pe ON p.idprd = pe.idprd
ORDER BY pe.tgljual desc;
Query Result:
I have a MYSQL table with a TIMESTAMP column 'Start' and a TIMESTAMP column 'End'. I want to return the number of minutes between the start and the end (End is always after than Start). Usually I'd just use 'TIMESTAMPDIFF()' but this time I need to get the minutes from 9am until 22pm, of each day in that date range.
If a row has a Start '2017-01-01 07:15:00' and an End of '2017-01-02 11:30:00' - the elapsed time should be 15.5 hours (930 minutes).
I'm having trouble coming up with a decent way of doing this and my searching online hasn't found quite what I'm looking for. Can someone help me along?
Edit:
CREATE TABLE date_ranges (
Start TIMESTAMP,
End TIMESTAMP
);
INSERT INTO date_ranges VALUES('2017-01-01 07:15:00','2017-01-02 11:30:00');
I came up with this:
SELECT Start, End, TIMESTAMPDIFF(MINUTE, Start, End) AS MinutesElapsed
FROM date_ranges;
I'm missing the part where the time in minutes is calculated only in the specified time range (9am until 22pm). Any ideas?
Here you go:
SELECT t1, t2, (TIMESTAMPDIFF(MINUTE, t1, t2) - TIMESTAMPDIFF(DAY, t1, t2)*660) FROM
(SELECT CASE WHEN t1 < STR_TO_DATE(concat(date_format(t1, '%Y-%m-%d'), ' 09:00:00'), '%Y-%m-%d %h:%i:%s')
THEN STR_TO_DATE(concat(date_format(t1, '%Y-%m-%d'), ' 09:00:00'), '%Y-%m-%d %h:%i:%s')
ELSE t1
END AS t1 FROM test) test1,
(SELECT CASE WHEN t2 > STR_TO_DATE(concat(date_format(t2, '%Y-%m-%d'), ' 22:00:00'), '%Y-%m-%d %h:%i:%s')
THEN STR_TO_DATE(concat(date_format(t2, '%Y-%m-%d'), ' 22:00:00'), '%Y-%m-%d %h:%i:%s')
ELSE t2
END AS t2 FROM test) test2;
660 = number of minutes between 22:00 and 09:00 (11 hours)
Here's the SQL Fiddle.
It's not very concise, but this should give you the results you want:
select started_at,ended_at,
(case
when date(ended_at) = date(started_at)
then
timestampdiff(
minute,
greatest(started_at,concat(date(started_at),' 09:00:00')),
least(ended_at,concat(date(ended_at),' 22:00:00'))
)
else
timestampdiff(
minute,
least(greatest(started_at,concat(date(started_at),' 09:00:00')),concat(date(started_at),' 22:00:00')),
concat(date(started_at),' 22:00:00')
)
+
timestampdiff(
minute,
concat(date(ended_at),' 09:00:00'),
greatest(least(ended_at,concat(date(ended_at),' 22:00:00')),concat(date(ended_at),' 09:00:00'))
)
+ ((datediff(ended_at,started_at)-1)*780)
end) as total_minutes
from your_table;
--Generating all dates in 2017.
CREATE TABLE CALENDAR AS --Use a different table name if CALENDAR already exists
SELECT '2017-12-31 09:00:00' - INTERVAL c.number DAY AS start_datetime,'2017-12-31 22:00:00' - INTERVAL c.number DAY AS end_datetime
FROM (SELECT singles + tens + hundreds number FROM
(SELECT 0 singles
UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) singles JOIN
(SELECT 0 tens
UNION ALL SELECT 10 UNION ALL SELECT 20 UNION ALL SELECT 30
UNION ALL SELECT 40 UNION ALL SELECT 50 UNION ALL SELECT 60
UNION ALL SELECT 70 UNION ALL SELECT 80 UNION ALL SELECT 90
) tens JOIN
(SELECT 0 hundreds
UNION ALL SELECT 100 UNION ALL SELECT 200 UNION ALL SELECT 300
UNION ALL SELECT 400 UNION ALL SELECT 500 UNION ALL SELECT 600
UNION ALL SELECT 700 UNION ALL SELECT 800 UNION ALL SELECT 900
) hundreds
ORDER BY number DESC) c
WHERE c.number BETWEEN 0 and 364
;
--End of table creation
--Actual query begins here
SELECT D.`START`,
D.`END`,
SUM(TIMESTAMPDIFF(MINUTE,GREATEST(D.`START`,C.START_DATETIME), LEAST(D.`END`,C.END_DATETIME))) AS TOTAL_TIME
FROM CALENDAR C
LEFT JOIN DATE_RANGES D ON DATE(C.START_DATETIME) >= DATE(D.`START`)
AND DATE(C.START_DATETIME) <= DATE(D.`END`)
WHERE D.`START` IS NOT NULL
AND D.`END` IS NOT NULL
GROUP BY D.`START`,
D.`END`
;
Construct a calendar table with a dates for a specified number of years. Each date having a start time of 09:00 and an end time of 22:00.
Left join on this table to get one row per date from the date ranges table.
Sum up the differences each day to get the total time worked.
Sample Demo
Day 1 Day 2 Day 3
|--********--|--********--|--********--|
|__________________________|
The question, IMHO is to know how many minutes the first day, and how many minutes the last day, the intermediate days have 780 minutes.
I've used a subquery just to help in the intermediate calculations.
select
if(hour(t1) < 9, date(t1) + interval 9 hour , t1) as tIni1,
date(t1) + interval 22 hour as tFin1,
date(t2) + interval 9 hour as tIni2,
if(hour(t2) > 22, date(t2) + interval 22 hour, t2) as tFin2,
TIMESTAMPDIFF(day, date(t1), date(t2)) numDays
from
tdt
tIni1 and tFin1 is the period of the first day, and tIni2, tFin2 the period of the last day, obviously first and last day can be the same.
Then calculate minutes of first day + minutes of second day + 780 minutes for every intermediate day.
select numDays, tIni1, tFin1, tIni2, tFin2,
if (numDays = 0,
TIMESTAMPDIFF(minute, tIni1, tFin2),
TIMESTAMPDIFF(minute, tIni1, tFin1)
+ TIMESTAMPDIFF(minute, tIni2, tFin2)
+ (numDays - 1) * 780
) as Minutes
from (
select
if(hour(t1) < 9, date(t1) + interval 9 hour , t1) as tIni1,
date(t1) + interval 22 hour as tFin1,
date(t2) + interval 9 hour as tIni2,
if(hour(t2) > 22, date(t2) + interval 22 hour, t2) as tFin2,
TIMESTAMPDIFF(day, date(t1), date(t2)) numDays
from
tdt
) ti
;
Try it here: http://rextester.com/GDHAB78973
Hi i have used the following query for finding date difference ,but i am getting one hour difference between source and target, but when i checked manually for the dates in excel value is matching.
select a.CASE_ID as CASE_ID,a.FST_QUE_TIME_IN_SECStest as
FST_QUE_TIME_IN_SECS from (select
distinct CC.CASE_ID as CASE_ID,
CC.CASE_STS_CD,
CC.CRT_DTTM_PST,
CC.REC_DTTM_PST,
MIN(case when CC.CASE_STS_CD ='Open' then CC.CRT_DTTM_PST end )as
FST_QUE_TIME_IN_SECS1,
MIN(case when CC.CASE_STS_CD ='Open' then CC.REC_DTTM_PST end ) as
FST_QUE_TIME_IN_SECS2,
(FST_QUE_TIME_IN_SECS1-FST_QUE_TIME_IN_SECS2 DAY(4) to SECOND) as
FST_QUE_TIME_IN_SECS,
cast(extract (day from FST_QUE_TIME_IN_SECS) * 86400 + extract(hour from
FST_QUE_TIME_IN_SECS) * 3600 + extract(minute from
FST_QUE_TIME_IN_SECS)
*60 + extract(second from FST_QUE_TIME_IN_SECS) as INTEGER)as
FST_QUE_TIME_IN_SECStest
FROM EDW_KATAMARI_T.CNTCT_CASE CC
where CC.CASE_ID='14424461'
group by 1,2,3,4
)
a
where a.FST_QUE_TIME_IN_SECStest is not NULL
minus
select
CASE_ID,
FST_QUE_TIME_IN_SECS
from EDW_KATAMARI_T.CNTCT_CASE_SUMRY
where CASE_ID='14424461'
I'm having problems with subquery. This query displays a function that I need to perform the query I need
SELECT *,
open_hour_from - ((open_hour_day - 1) * 24 * 60) AS timeFrom,
open_hour_to - ((open_hour_day - 1) * 24 * 60) AS timeTo,
GROUP_CONCAT(open_hour_day) AS days
FROM `open_hours` WHERE open_hour_connect_id = 2
GROUP BY timeFrom, timeTo
ORDER BY days
This are two functions
open_hour_from - ((open_hour_day - 1) * 24 * 60) AS timeFrom,
open_hour_to - ((open_hour_day - 1) * 24 * 60) AS timeTo,
I know that subquery may return only one value. But how can I use timeFrom and timeTo variables? Should I put the in HAVING and how can I do that?
SELECT *,
( SELECT GROUP_CONCAT(open_hour_day)
FROM ` WHERE open_hour_connect_id = 2
GROUP BY timeFrom, timeTo ORDER BY days )
FROM connections
I'm guessing you really want somthing like
SELECT c.*, sub.*
FROM
connections c
INNER JOIN
(
SELECT *,
open_hour_from - ((open_hour_day - 1) * 24 * 60) AS timeFrom,
open_hour_to - ((open_hour_day - 1) * 24 * 60) AS timeTo,
GROUP_CONCAT(open_hour_day) AS days
FROM `open_hours` WHERE open_hour_connect_id = 2
GROUP BY timeFrom, timeTo
ORDER BY days) sub
ON c.days = sub.days