I am looking to find difference between my two query results. Following are my queries:
Query 1:
SELECT
COUNT(*) AS Total
FROM
transaction
WHERE
last_local_call_time >= '2022-03-04 00:00:00'
AND last_local_call_time < '2022-03-05 00:00:00';
Result 1:
Total
--------
213966
Query 2:
SELECT
COUNT(*) AS Total
FROM
transaction
WHERE
modify_date >= '2022-03-04 00:00:00'
AND modify_date < '2022-03-05 00:00:00';
Result 2:
Total
--------
877349
I want to find Query1-Quer2 results (not row count but content). Following is one of my many failed attempts:
SELECT *
FROM transaction
WHERE VALUE IN (SELECT * FROM transaction WHERE modify_date >= '2022-03-04 00:00:00' AND modify_date < '2022-03-05 00:00:00';)
AND NOT IN (SELECT * FROM transaction WHERE last_local_call_time >= '2022-03-04 00:00:00' AND last_local_call_time < '2022-03-05 00:00:00';)
Any assistance would be appericiated.
Use a where clause which includes the first query but excludes the second:
SELECT *
FROM `transaction`
WHERE last_local_call_time >= '2022-03-04 00:00:00' AND
last_local_call_time < '2022-03-05 00:00:00' AND
(modify_date < '2022-03-04 00:00:00' OR
modify_date >= '2022-03-05 00:00:00');
Related
The query is this to find renewed orders
A renewal is any school that PAID (i.e. total_line_price >0) for an order in the previous school year , but that same school has purchased (paid for – total line price>0) another order.
next year '08-01-2016 00:00:00' and '07-31-2017 00:00:00' -2016
current year '08-01-2015 00:00:00' and '07-31-2016 00:00:00' -2015
previous year '08-01-2014 00:00:00' and '07-31-2015 00:00:00' - 2014
Below is query that i have written and its not right. Need some help
select
(school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where school_ucn not in ((select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where (((start_date between '08-01-2014 00:00:00' and '07-31-2015 00:00:00')
and (total_line_price >0) ))
and in
(select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where ((start_date between '08-01-2015 00:00:00' and '07-31-2016 00:00:00') and ( total_line_price >0))
)))
You are using and in inside your query. It should be and school_ucn in.
Always format your query properly to analyse easily.
select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where school_ucn not in (
select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where start_date between '08-01-2014 00:00:00' and '07-31-2015 00:00:00'
and total_line_price >0)
and school_ucn in (select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where start_date between '08-01-2015 00:00:00' and '07-31-2016 00:00:00'
and total_line_price >0)
Using Not Exists, you can rewrite your query as follows.
select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order as a
where start_date between '08-01-2015 00:00:00' and '07-31-2016 00:00:00'
and total_line_price >0
and not exists (select 1
from storiacloud_staging.schl_royl_vw_edw_oms_order as b
where b. school_ucn = a. school_ucn
and start_date between '08-01-2014 00:00:00' and '07-31-2015 00:00:00’
and total_line_price >0)
I am using the following query.
select DATE_FORMAT(created_at, '%Y-%m') AS month, sum(points) from table_name group by month;
I am getting the sum of points but the result is not consistent with the result I get when I try by a date range . Eg,
select sum(points) from table_name where '2015-01-01 00:00:00' <= date(created_at) <= '2015-01-31 23:59:59';
The result of Jan, 2015 by 1st and 2nd query above are not same. Where is the problem in the first query?
The way you're writing your second query is not valid. You can only use binary expressions, so your query should be like this:
select sum(points)
from table_name
where '2015-01-01 00:00:00' <= date(created_at)
and date(created_at) <= '2015-01-31 23:59:59';
In your original form, the where is being evaluated like this (I think):
First: '2015-01-01 00:00:00' <= date(created_at)
Assuming this is true, this would be evaluated as 1
Second: '2015-01-01 00:00:00' <= date(created_at) <= '2015-01-31 23:59:59',
but this is equivalent to 1 <= '2015-01-31 23:59:59', so... it's of course not what you want
I am trying to build a query that will load records from and to specific date comparing 2 fields - the start_time and the end_date.
SELECT start_time
,end_time
,DATEDIFF(end_time, start_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
AND DiffDate < 100
LIMIT 1000;
Unfortunately the DiffDate returns always 0.
The ideal scenario was to calculate the difference between start_time and end_time when inserting the end_time but the I cant make any changes on the database.
What am I doing wrong here? Even if the DiffDate was working will it considered as a good solution?
From the condition in the where clause it appears that you are trying to get data for the same date, however using the datediff for the same day always would result 0
mysql> select datediff('2015-04-27 12:00:00','2015-04-27 00:00:00') as diff ;
+------+
| diff |
+------+
| 0 |
+------+
1 row in set (0.03 sec)
You may need other means of calculation perhaps using the timestampdiff
mysql> select timestampdiff(minute ,'2015-04-27 00:00:00','2015-04-27 12:00:00') as diff ;
+------+
| diff |
+------+
| 720 |
+------+
1 row in set (0.00 sec)
Also you are using alias in the where clause which is not allowed you have to change that to having clause
SELECT start_time
,end_time
,timestampdiff(minute,start_time,end_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
having DiffDate < 100
LIMIT 1000;
Coming from MS SQL I was using DATEDIFF but the solution is:
SELECT start_time
,end_time
,TIMESTAMPDIFF(SECOND,start_time,end_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
AND DiffDate < 100
LIMIT 1000;
I would like to know if there is a better solution from that.
Start_time and end_time are datetime column. So use TimeDIFF..
SELECT start_time, end_time, TIMEDIFF(end_time, start_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
AND DiffDate < 100
LIMIT 1000;
SELECT start_time,end_time,DATEDIFF(end_time, start_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time between '2015-04-27 00:00:00' AND '2015-04-28 00:00:00'
AND for date diff < 100
LIMIT 1000;
I have a query that uses multiple UNION ALL. I'm getting a syntax error and I can't find it. I think it has to do with using multiple unions. I'm only using MySQL. Any help is appreciated.
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALL(SELECT a.' at line 41
SELECT Sum(counter) AS counter,
DATE_FORMAT(dtime,'%Y-%m-%d 00:00:00') AS dtime
FROM (
SELECT counter,
dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='980371'
AND counter != 0
UNION ALL
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='979661')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='979661' ))
ORDER BY dtime
AND counter != 0
UNION ALL (this is line 41)
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='984121')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible where dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='984121' ))
ORDER BY dtime
AND counter != 0) AS MainTable
GROUP BY WEEKOFYEAR(dtime)
HAVING SUM(counter) > 0
If I change the 'id=' to id in and include the other id number there and drop the second union all it works. I'm pretty sure it has something to do with the second union all. This statement is built dynamically so I cannot easily change to using in instead of multiple union alls.
Let me know if I'm not explaining this clearly and I'll provide more detail.
check your statement just above the line where you've written (this IS line 41)
Try to remove/update this statement and execute your query.
ORDER BY
dtime
AND counter != 0
we can't use order in between the UNION & UNION ALL. Order by should be at the very last, after all UNION / UNION ALL statements
I currently have the following query:
select min(nw_lpdTEMP) AS Min_Lkpmp_Temp_C,
max(nw_lpdPH) AS Max_Lkpmp_PH,
(select format(nw_uvttlflw,7) from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 limit 1) as Min_GPM,
format(min(nw_bpdcl2),7) as Min_Chlorine_Residual,
(select format(nw_uvttlflw,7) from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 desc limit 1) as Max_GPM,
format(max(nw_bpdcl2),7) as Max_Chlorine_Residual from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59'
and am looking to see if I can add the following query which returns the sum of a gallons per minute flow (nw_uvttlflw) between 24 hours. I can run the (sum query) query that I am trying to add to the above query by itself and it returns exactly what I want. My current problem is trying to nest it with the above query so it returns the sum (nw_uvttlflw) as the last column in the original larger query. (Sorry for my lack of quality formatting for the nested SQL query as I am not the most versed with lengthy queries)
The query I am trying to append is here:
select format(sum(nw_uvttlflw),7) as Total_flow from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59';
Your time and effort is greatly appreciated. I have been trying to piece this together for some time an can't get my syntax just right. Thanks
-Mark
Is this what you're looking for?
select min(nw_lpdTEMP) AS Min_Lkpmp_Temp_C,
max(nw_lpdPH) AS Max_Lkpmp_PH,
(select format(nw_uvttlflw,7) from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 limit 1) as Min_GPM,
format(min(nw_bpdcl2),7) as Min_Chlorine_Residual,
(select format(nw_uvttlflw,7) from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59' order by nw_bpdcl2 desc limit 1) as Max_GPM,
format(max(nw_bpdcl2),7) as Max_Chlorine_Residual,
(select format(sum(nw_uvttlflw),7) from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59') as Total_flow
from tag_history
where from_unixtime(floor(unix_timestamp(t_stamp)/60)*60)
Between '2012-07-01 00:00:00' AND '2012-07-01 23:59:59'