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'
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)
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 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
Hi all I am trying to aggregate the number of searches that clients are doing. I currently have this working for 1 day. I would also like to put in a column for searches for that week, month, year and total
USE live_travelcoglog;
SELECT lu.Name, lu.UID, IFNULL(l.AgentId, 'CP Total') AS "CP", COUNT(*) AS "DateTotal", MAX(l.Submitted) AS "LastSearchTime"
FROM logs l INNER JOIN live_travelcog.users lu ON l.ChannelPartnerId = lu.CustId
WHERE Submitted BETWEEN '2014-04-23 00:00:00' AND '2014-04-23 23:59:59'
AND l.MessageType = 'COG_HotelAvail_RS'
GROUP BY lu.Name, l.AgentId ASC WITH ROLLUP;
Now I can run the queries for the different values that I am after but I am sure there is a nicer way that they can all be grouped together. If someone could kindly point me in the right direction it would be greatly appreciated.
Thanks
Daz
Is this the sort of thing you were looking for?
USE live_travelcoglog;
SELECT
lu.Name,
lu.UID,
IFNULL(l.AgentId, 'CP Total') AS "CP",
SUM(Submitted BETWEEN '2014-04-23 00:00:00' AND '2014-04-23 23:59:59') AS DateTotal,
SUM(Submitted BETWEEN '2014-04-17 00:00:00' AND '2014-04-23 23:59:59') AS WeekTotal,
SUM(Submitted BETWEEN '2014-04-01 00:00:00' AND '2014-04-23 23:59:59') AS MonthTotal,
MAX(l.Submitted) AS "LastSearchTime"
FROM logs l
INNER JOIN live_travelcog.users lu
ON l.ChannelPartnerId = lu.CustId
WHERE
l.MessageType = 'COG_HotelAvail_RS'
GROUP BY
lu.Name,
l.AgentId ASC
WITH ROLLUP;
I'm new to tsql and use mysql db. Seen an example code(at below) to get data from database every 2 hour:
SELECT date(`dateTime`) dateDay, 2*floor(date_format(`dateTime`,'%H')/2) dateHour,
avg(channel1), avg(channel2), avg(channel3)
FROM `Table`
WHERE `id` =1
AND `dateTime` >= '2011-10-15 00:00:01'
AND `dateTime` <= '2011-10-17 23:59:59'
Then I did few changes based on the above code to get data every 15 minutes:
SELECT date(`dateTime`) dateDay, 15*floor(date_format(`dateTime`,'%i')/15) dateHour,
avg(channel1), avg(channel2), avg(channel3)
FROM `Table`
WHERE `id` =1
AND `dateTime` >= '2011-10-15 00:00:01'
AND `dateTime` <= '2011-10-17 23:59:59'
group by date(`dateTime`), 15*floor(date_format(`dateTime`,'%i')/15)
However the query is not correct.
My questions are:
How to amend the query to get data every 15 min?
How to write sql query to get all data as well? and dateDay and dateHour columns need to be here.
1: every 15 minutes:
SELECT date(dateTime) dateDay, 2*floor(date_format(dateTime,'%H')/2) dateHour,
avg(channel1), avg(channel2), avg(channel3)
-->
SELECT date(`dateTime`) dateDay
,CONCAT( 2*floor(date_format(`dateTime`,'%H')/2), ':', 15*FLOOR(DATE_FORMAT(`dateTime`, '%i')/15)) dateQuarter
,avg(channel1), avg(channel2), avg(channel3)
FROM `Table`
WHERE `id` =1
AND `dateTime` >= '2011-10-15 00:00:01'
AND `dateTime` <= '2011-10-17 23:59:59'
GROUP BY 1,2;
2: all data. I wasn't really sure what you wanted. but here's every row with hour:minute in dateHour field.
SELECT date(`dateTime`) dateDay,
,DATE_FORMAT(`dateTime`, '%H:%i') dateHour,
FROM `Table`
WHERE `id` =1
AND `dateTime` >= '2011-10-15 00:00:01'
AND `dateTime` <= '2011-10-17 23:59:59'