I am trying to identify specific weeks that are missing in my data. I have multiple states with different date ranges and would like to output all of the missing weeks for the various states that I do have. Not even sure where to begin with a SQL code that would begin to identify missing weeks. Any help on this would be greatly appreciated. BTW I'm using SQL Server 2016.
Thanks!
Sample Data:
State WeekEndingDate Sales
ID 7/5/2015 125000
ID 12/13/2015 127263
IN 8/20/2016 126589
IN 8/27/2016 124568
IN 10/15/2016 119654
MI 01/02/2017 105687
MI 02/05/2017 145962
An example of my desired output would be:
MI 01/09/2017 136589
MI 01/16/2017 125641
MI 01/23/2017 145769
MI 01/30/2017 135697
IN 09/03/2016 145693 and so on....
This is what's commonly known as a "gaps and islands" problem.
To solve, I strongly suggest you create a date table. Then, assuming that you have established that your week ending day is always Saturday, you can include that date table in the query.
select MD.state,DT.weekendingdate,MD.sales
from DateTable DT
left outer join MyData MD on DT.Weekendingdate = MD.Weekendingdate
where dt.weekendingdate >= '2016-08-01' and dt.weekendingdate <= '2016-08-31'
I am using CakePHP 3.x. I have converted and stored all my dates to string using the strtotime(date('Y/m/d h:i', time())); .And in DB it will store like this 1479466560 But Right now i need to select rows from exactly 14 days ago. I tried like below conditions,
$this->loadModel('Users');
$userList = $this->Users->find('all')
->select(['created'])
->where(['status' => 1,'created' => 'created + INTERVAL 14 DAY > NOW()']);
It is returning empty rows only. How to do this ?. Can any one help me !
I know this does not tell you how to use interval but it does solve the problem.
What I've done in the past is a simple >= < combination together with php variables:
$cur_date = date_create();
$date = clone $cur_date;
$date->modify("-12 hours");
$this->Model->find()
->where(['created >= ' => $date])
->andWhere(['created < '=> $cur_date]);
This effectively uses an interval. It is also nicely dynamically editable.
Your query doesn't match with any created date from your users table.
Your CakePHP3 query generated below SQL:
SELECT created FROM users WHERE (status = 1 AND created = created + INTERVAL 14 DAY > NOW())
Note: At First try and adjust above query from your phpmyadmin.
You can try below SQL for INTERVAL test
SELECT '2016-01-17 10:57:21' + INTERVAL 14 DAY > NOW();
OR
SELECT '2016-01-17 10:57:21' + INTERVAL 14 DAY;
Note: Adjust date string as you need. Now you can see what is the wrong you have done
Please, I need help for the correct sql syntax and how to do the following:
SELECT count(CreateDateTime)
FROM tblINFO
WHERE
(CreateDateTime BETWEEN '2016-03-22 06:59:00' AND '2016-03-22 14:59:00')
OR
(CreateDateTime BETWEEN '2016-04-14 06:59:00' AND '2016-04-14 14:59:00')
I tried this Code but It calculated the count of (CreatedDateTime) only for those two days between the given times, But also I need to Calculate the count of the days that are between those two days between the same given times.
Thank you.
I could interpret your question two ways. First one is that you want created date to be on either of those days, in which case you would do this;
SELECT count(CreatedDateTime)
FROM tblINFO
WHERE
(createdDateTime BETWEEN '2015-03-22 06:59:00' AND '2015-03-22 14:59:00')
OR
(createdDateTime BETWEEN '2015-04-14 06:59:00' AND '2015-04-14 14:59:00')
The alternative would be that you want the created date to be between the start and end of those two dates, in which case you'd have this;
SELECT count(CreatedDateTime)
FROM tblINFO
WHERE createdDateTime BETWEEN '2015-03-22 06:59:00' AND '2015-04-14 14:59:00'
If you're after everything between those times on any day between 2015-03-22 and 2015-04-14 then you'll want something like this;
SELECT count(CreatedDateTime)
FROM tblINFO
WHERE
(CONVERT(DATE,createdDateTime) BETWEEN '2015-03-22' AND '2015-04-14')
AND
(CONVERT(TIME,createdDateTime) BETWEEN '06:59:00' AND '14:59:00')
And if you want this to show you the count for all days but split by day, you'll want this;
SELECT
CONVERT(Date,CreatedDateTime) Date
,count(CreatedDateTime) Volume
FROM tblINFO
WHERE
(CONVERT(DATE,createdDateTime) BETWEEN '2015-03-22' AND '2015-04-14')
AND
(CONVERT(TIME,createdDateTime) BETWEEN '06:59:00' AND '14:59:00')
GROUP BY CONVERT(Date,CreatedDateTime)
If I Understood correctly your requirement, then this may help you
SELECT count(CreatedDateTime)
FROM tblINFO
WHERE (createdDateTime BETWEEN '2015/03/22 6:59:00' AND '2015/03/22 14:59:00' )
OR(createdDateTime BETWEEN '2015/04/14 6:59:00' AND '2015/04/14 14:59:00' )
OR(createdDateTime BETWEEN '2015/03/22 6:59:00' AND '2015/04/14 14:59:00' )
I have a select statement That I need to turn into an Update.
I need to update a particular unix time field to January 1, 2016.
I have to select the records using a compound select statement.
Update archive_queue set archive_time = 1451606400
FROM
select recordings.(star), archive_queue.(star)
from
recordings, archive_queue
where
recordings.device_alias = '70285' and recordings.keepdays = 120
and recordings.ident = archive_queue.rec_ident
The above gives me a syntax error. The select gives me the records that I need to update. I looked at some examples here, but can't figure out the proper syntax based on my needs. Thanks everyone !
You should remove the select and only have one FROM statement:
Update archive_queue set archive_time = 1451606400
from recordings, archive_queue
where recordings.device_alias = '70285' and recordings.keepdays = 120
and recordings.ident = archive_queue.rec_ident
I have this big database table that contains 12 medical ceritifcations, expiration dates, links to files and what companies they're from. I need to generate a report via email within 90,60,30 and 15 days of the the certification expiring date.
Here's what the datebase looks like:
certID,
profileID,
cprAdultExp,
cprAdultcompany,
cprAdultImage,
cprAdultOnFile,
cprInfantChildExp,
cprInfantChildcompany,
cprInfantChildImage,
cprInfantChildOnFile,
cprFPRExp,
cprFPRcompany,
cprFPRImage,
cprFPROnFile,
aedExp,
aedcompany,
aedImage,
aedOnFile,
firstAidExp,
firstAidcompany,
firstAidImage,
firstAidOnFile,
emtExp,
emtcompany,
emtImage,
emtOnFile,
waterSafetyInstructionExp,
waterSafetyInstructioncompany,
waterSafetyInstructionImage,
waterSafetyInstructionOnFile,
bloodPathogensExp,
bloodPathogenscompany,
bloodPathogensImage,
bloodPathogensOnFile,
oxygenAdminExp,
oxygenAdmincompany,
oxygenAdminImage,
oxygenAdminOnFile,
lifegaurdingExp,
lifegaurdingcompany,
lifegaurdingImage,
lifegaurdingOnFile,
wildernessResponderExp,
wildernessResponderCompany,
wildernessResponderImage,
wildernessResponderOnFile,
notes
How do I write some sort of loop to check all the dates (anything with EXP is a date) then store which ones are expiring, and email all those details to a person?
Since you have to send the email through CF (I presume) then the way I'd approach this is to run a scheduled task once a day that checks which rows have a 15, 30, 60 and 90 day expiry anniversary. So the scheduled task would run a few queries and then send the emails.
The first thing is to actually find the rows in question (all my SQL presumes MS SQL Server - other RDBMSs will have similar syntax):
<cfquery name="qExpiring">
select
certID,
dateDiff(day, cprAdultExp, getDate()) as cprAdultExpDaysSince
dateDiff(day, cprInfantChildExp, getDate()) as cprInfantChildExpDaysSince
from yourTable
where
dateDiff(day, cprAdultExp, getDate()) in (15, 30, 60, 90)
or
dateDiff(day, cprInfantChildExp, getDate()) in (15, 30, 60, 90)
</cfquery>
This should give you a result set like so:
certID|cprAdultExpDaysSince|cprInfantChildExpDaysSince|etc.
___________________________________________________________
xxxxxx|30 |5 |etc.
xxxxxx|16 |60 |etc.
xxxxxx|2 |90 |etc.
Any that have matches on 15, 30, 60, 90 you are interested in processing futher. You could use a query of queries to do this:
<cfquery name="qAdultExpRenewal" dbtype="query">
select * from qExpiring
where cprAdultExpDaysSince in (15, 30, 60, 90)
</cfquery>
You cn then loop over these records and send the appropriate email based on the value of cprAdultExpDaysSince.
You're missing a fair bit of info to give you a comprehensive answer but I hope that this will point you in the right direction.
If you have access to the database, you should probably create a view or views which give you only those who are expiring or adds a field for days until expiration for each cert and query that. Alternatively, you could write a single query which grabs all info where any cert is expiring (WHERE cprAdultExp [comparison] date OR cprInfantExp [comparison] date....), then loop through the records to filter or group by cert expiring.
You proably want some sort of stupid-huge query which looks like
select "CPR Adult", cprAdultExp, cprAdultcompany, cprAdultImage, cprAdultOnFile from thebigtable where cprAdultExp [comparison] interestingdate
union
select "CPR Child", cprChild....
union ...
Then iterate over the rows that you get back, using the first column to indicate what ceritification is expiring.