How to get a count of occurrences in a timeframe of 7 and 30 days in SQL? - sql

I have a table Emp and I want to get the count of records starting from a particular date to next seven days for each unique ID.
For instance, there are two occurrences of ID1 each day from 2020-11-01 to 2020-11-07, then the count for this ID would be 14.
Next, the count would start from 2020-11-08 to 2020-11-14 and so on for each given ID. The same question holds for a range of 30 days.
I have tried DATEADD(), GETDATE() and DATEDIFF(), but none of them return the correct output.
It's easy to get the count for a given date but seems a bit tricky to get the same for a range. Any help would be appreciated.

Related

How to insert a record on next selected date if count of a date is full

I have a table where I maintain working days in a week like 2nd and 4th day and number of records it can accept is 10 records per working day
usercode DaysofWeek NumberOfRecords
0623PO54 2 10
0623PO54 4 10
On insertion I have application date example 01-09-2017(dd/mm/yyyy) which is Friday.
Now I have to insert this record in closest working day from 01-09-2017 which is 05-09-2017 as it is 2nd working day. After inserting 10 records next records should be insert on 4th working day which is 07-09-2017.
I don't know how to get closest date from application date and insert record on it.
If you also want to exclude holidays than use a master table for Ex. CalenderMastr of dates which have holidays flag and day of week like Monday=2. and as you mansion that you are maintaining working days in a table for ex. workdaymaster. Now make a select query to get next date from CalenderMastr from current date and day of week stored in workdaymaster. now on output date check if in your transaction table count is smaller or not if count is small insert new record or if not than move to next date using while loop in your query. hope you can understand what i am trying to say.

Cumulative count in SQL

I am working on SQL and came across one scenario that needs to build in SQL.
Below is scenario:
PatientID AdmitDate DischargeDate
12 7/24/2017 09:45 7/24/2017 11:01
13 7/21/2016 08:37 7/22/2017 00:15
I want result set as below:
For patientID 13, count is calculated in first 2 rows and
For patientid 12, count is calculated in last row.
Well, that looks like whatever you do will be slow. I think I'd use a tally table. The table, instead of just containing the usual n years worth of dates / days / day of week etc. would also contain one record for each hour in the day. The Primary Key would represent one of those segments.
Then you could pass the admission date and discharge date for a patient to a function that would return a list, or range, of the hours that the patient is in for. So, Patient 13 might get a return value of (for example) 1500,1517 (i.e the patient was in for 17 hours and you will know the date and time because 1500 will be the Primary Key of a record that gives you the date and hour of the day he was admitted). Patient 12 would (to continue the example) return a value of 1544,1546
You could then build the dataset from Date A to Date B by returning all the data between those dates from the tally table and then check whether each hour is a yes or no for a particular patient.
The display of the data - your desired result set - I would do in somewhere else. I'd probably return two datasets. One would be used to build your table structure. The other would be used to decide whether to put a '1' in the box or not. You could do your summing on the fly.
I imagine this would get interesting with patients on the same dates ... you'd have to show each patient separately?

SQL- Last 4 weeks with date column

USERS
ID TIMEMODIFIED
1 1400481271
2 1400481489
3 1400486453
4 1400486525
5 1401777484
I have timemodified field, From timemodified, I need to get the rows of last 4 weeks by taking from today's date.
SELECT id FROM USERS
WHERE FROM_UNIXTIME(timemodified,'%d-%m-%Y') >= curdate()
AND FROM_UNIXTIME(timemodified,'%d-%m-%Y') < curdate()-1
Your times are already in Unix timestamp format. Bear in mind that it'll be far more efficient to compare [TIMEMODIFIED] against the current date converted to a Unix timestamp. In addition, you don't need to check any upper bound unless [TIMEMODIFIED] can be in the future.
Try:
-- 60x60x24x7x4 = 2419200 seconds in four weeks
SET #unix_four_weeks_ago = UNIX_TIMESTAMP(curdate()) - 2419200;
SELECT id FROM USERS
WHERE timemodified >= #unix_four_weeks_ago;
NB. Four weeks ago (i.e. today – 28 days) was 1,437,696,000 (24th July) at the time of this answer. The latest record in the sample you provided has a timestamp going back to the 3rd June 2014, and so none of these records will be returned by the query.

Select records from after last 6 AM

Have table: item (int) and timestamp (datetime).
Need to know if there are any records with timestamp after last 6 AM.
Example:
At 5 AM it should check if there are any records from after 6 AM
yesterday. AT 7 AM it should check if there are any records from
after 6 AM today
This could be done of course by making a variable with datepart as:
if time now is < 6 AM datepart should be yesterday if time now
is >= 6 AM datepart should be today
but there must be a simpler way ?
This expression should always return the most recent 06:00 in the past:
select DATEADD(hour,
(DATEDIFF(hour,'2014-01-01T06:00:00',CURRENT_TIMESTAMP)/24)*24,
'2014-01-01T06:00:00')
It works by asking how many hours have happened since some arbitrary, known, 6AM. We then round this number down to the closest multiple of 24 (by dividing and multiplying with integer maths), and add this number back onto the same, arbitrary, 6AM

Query - Trying to SUM one field based on content of another field

Table:
DayOfWeek Enrollments
Monday 35
Monday 12
Saturday 25
Tuesday 15
Monday 9
Tuesday 15
Basically I'm trying to sum the total enrolments for each day.
so the Output will look like:
DayOfWeek Enrollments
Monday 56
Saturday 25
Tuesday 30
I've spent around 4 hours trying to work this out trying many many different ways but no luck.
The problem I'm having is i can count how many enrollments for each day but can't have it aligned with the correct day when i run the query e.g. I want The total to be on the same line as the day it was calculated from. (I hope that is clear enough)
Group by DayOfWeek, and ask for the sum of Enrollments within each group. The SQL will look like this.
SELECT DayOfWeek, Sum(Enrollments) AS SumOfEnrollments
FROM YourTable
GROUP BY DayOfWeek;
If you're using the Access query designer to create this, select your fields, then click the symbol for "Totals" query (Greek character sigma). In the "Total:" row of the design grid, select Group By and Sum for the appropriate fields.