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
Related
I have the following MySQL table:
My requirements are,At a 30-minute time interval query data,with no need for group by,This concept,For the first time encounter this kind of question,How can I achieve that effect,The interval between the last one and the next is 30 minutes
I think this does what you want:
select t.*
from t
where t.time = (select min(t2.time)
from t t2
where floor(to_seconds(t2.time) / (30 * 60)) = floor(to_seconds(t.time) / (30 * 60))
);
the other day think of answer:
SELECT FROM_
UNIXTIME(UNIX_TIMESTAMP(t.Time)- UNIX_TIMESTAMP(t.Time)%(15*60))
AS timekey,DB33,Stream,`View`,Coil
FROM (select DB33,Stream,`View`,Time,Coil from running_check where
DB33=#{DB33} ORDER BY id desc limit 10000) as t
GROUP BY timekey
i want make sql query that will search database to find multiple closest value
I have following query to find closest value.
SELECT * FROM table
WHERE price >= (50 * .9) and price <= (50 * 1.1)
order by abs(price - 50) LIMIT 1;
Its working fine, but i want to make that it will search more than one values something like:
SELECT * FROM table
WHERE price >= (50 * .9) and price <= (50 * 1.1) //here i want one result (limit 1)
or price >= (50 * 1.9) and price <= (50 * 2.1) //here i want one result (limit 1)
order by abs(price - 50)
I want for each price limit 1 not find all values.
How i can do this?
//edit
just found answer.
(select *
from table
WHERE price >= (50 * .9) and price <= (50 * 1.1)
order by abs(price - 50)
limit 1
) union all
(select *
from table
WHERE price >= (50 * 1.9) and price <= (50 * 2.1)
order by abs(price - 50)
limit 1
)
do you want this
SELECT * FROM table
WHERE price >= (50 * .9) and price <= (50 * 1.1) //here i want one result (limit 1)
union
SELECT * FROM table
WHERE price >= (50 * 1.9) and price <= (50 * 2.1) //here i want one result (limit 1)
order by abs(price - 50)
How about using union all?
(SELECT *
FROM table
WHERE price >= (50 * 0.9) and price <= (50 * 1.1)
ORDER BY ABS(price - 50)
LIMIT 1
) UNION ALL
(SELECT *
FROM table
WHERE price >= (50 * 1.9) and price <= (50 * 2.1)
ORDER BY ABS(price - 2*50)
LIMIT 1
) ;
Get 7 days data from multiple tables in mysql.
This is my select statement:
$sql="SELECT * FROM chain_management UNION
SELECT * FROM adagio_accounting_suite UNION
SELECT * FROM billquick UNION
SELECT * FROM budgetingdb UNION
SELECT * FROM chain_management UNION
SELECT * FROM accountpayable
WHERE DATE >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) ORDER BY curtime DESC";
You can try to use this:
$sql="SELECT * FROM (SELECT * FROM chain_management UNION
SELECT * FROM adagio_accounting_suite UNION
SELECT * FROM billquick UNION
SELECT * FROM budgetingdb UNION
SELECT * FROM chain_management UNION
SELECT * FROM accountpayable) as XYZ
WHERE DATE >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) ORDER BY curtime DESC";
I'm trying to group my timestamp every 30 minutes.
I want my result to be like this:
2016-03-09 00:00:00
2016-03-09 00:30:00
2016-03-09 01:00:00
Instead, my results are this:
2016-03-09 00:00:23
2016-03-09 00:35:02
2016-03-09 01:00:03
The query that I'm using is this
SELECT timestamp
FROM a.tablename
WHERE name = 'example' AND timestamp LIKE '2016-03-09%'
GROUP BY ROUND(((60/30) * HOUR(timestamp) + FLOOR( MINUTE(timestamp) / 30)));
How can I have my desired results? I've researched other answers on SO and non of the answers helped
Here's the basic query to group by 30 minutes interval.
SELECT
FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(timestamp)/(30* 60)) * (30*60)) thirtyHourInterval
FROM tablename
GROUP BY ROUND(UNIX_TIMESTAMP(timestamp)/(30* 60));
Note: ROUND() may lead you towards wrong output. Use the following query instead. Look at the following example:
SELECT ROUND(3.7), ROUND(4.2);
Result: 4 4.
Both lies in the same segment. The same holds for the above query while rounding the timestamp of different segments might fall in the same segment thus leading towards wrong output
[The following query is Recommended]
SELECT
FROM_UNIXTIME((UNIX_TIMESTAMP(`timestamp`) DIV (30* 60) ) * (30*60)) thirtyHourInterval
FROM tablename
GROUP BY UNIX_TIMESTAMP(`timestamp`) DIV (30* 60)
SQL FIDDLE DEMO
Alternatively you can adopt the following query.
SELECT
FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(timestamp)/(30* 60)) * (30*60)) thirtyHourInterval
FROM tablename
GROUP BY ( 4 * HOUR( `timestamp` ) + FLOOR( MINUTE( `timestamp` ) / 30 ));
Relatedpost
One method is to use to_seconds(), truncate the value, and then re-create the datetime value:
select date_add(0, interval floor(to_seconds(timestamp) / (30 * 60)) second) as timestamp
from a.tablename
where name = 'example' and timestamp >= '2016-03-09' and timestamp < '2016-03-10'
group by date_add(0, interval floor(to_seconds(timestamp) / (30 * 60)) second)
order by 1;
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