Counting days at the place - sql

I am working on the crystal report Xi and my problem is in my report I have to show the count of the days patient spend on one place, they can get transfer any number of times or they don't get transfer even one time. So every time they transferred from their admission to discharge I want to count of days on that place the patient spend.
PatID Admission Discharge Place TransferPlace TransferDate
121 05\06\2013 06\01\2013 102 105 05\10\2013
121 105 101 05\20\2013
121 101 108 05\25\2013
Here I just created a data the patient first transfer on 05\10\2013 from 102 to 105 that means he was at 102 since he got admitted (4 days) and in 105 he was for 10 days.
I hope it should be clear.**

Try DATEDIFF() function it will return the date difference

To get the difference between dates you use the DateDiff function. In your case you want to get the amount of days between the dates, you would use:
DateDiff(day, {Admission.Date}, {Transfer.Date})
{Admission.Date} and {Transfer.Date} are the date fields you would be using in your report.
EDIT:
So I think you can make this work using the PREVIOUS function in Crystal. Try something like:
if isNull(previous({Transfer.Date}) then
DateDiff(day, {Admission.Date}, {Transfer.Date}) else
DateDiff(day, previous({Transfer.Date}), {Transfer.Date})
You could also go the other way by using the NEXT function.

Related

How can I handle a Julian Date rollover in SQL?

I have a list of julian dates that I need to keep in order ex. 362, 363, 364, 365, 001, 002, 003. My query starts with getting the last julian date processed and each date after that. Right now it will max my lowest date out at 365 and I can't get the records that follow it. The same set of data also has a date field with the year attached but it doesn't seem to be helpful since those records won't be gathered until the rollover is corrected. Here is my simplified query:
select JulianDate, RecordDate
from table
where JulianField > #LowestJulianDate
and RecordDate between GetDate() and DateAdd(day, 6, GetDate())
Sample date:
JulianDate
RecordDate
362
2020-12-28
363
2020-12-29
364
2020-12-30
365
2020-12-31
001
2021-01-01
002
2021-01-02
003
2021-01-03
Desired output:
JulianDate
362
363
364
365
001
002
003
So if you'll imagine we start on day 362, our #LowestJulianDate is 362, and our record date range is today and the next 6 days, completing that list of julian dates.
How can I get the dates to go in order and resolve in a rollover?
You cannot by just using the "JulianDate" which is actually the DayOfYear value. You would need to also store the year that it refers to either separately or as part of the "JulianDate" value. For example, instead of "362" you need "2021362".
well why not sorting by year column and Julian date column ?
select JulianDate, RecordDate
from table
order by yearcolumn,JulianDate
What we are doing in the case of not having a year and wanting to sort a list on the year rollover for a 7 day rolling window is looking at the left 1 of the Julian day. If it's less than 3 roll it's rolled over. We sort into 2 baskets (old year and new year), order them, then recombine them with the new year's data being the "greatest" in the list.
We look at the left 1 because in our application, the last day of data we get may be 357 and the rollover may be 003 for example.

notification expiry product in vb.net with ms access

I just wanted to ask on how to notify a product on or before its expiry date in vb.net. I tried a lot of codes about it but still i am not able to solved this problem. I will really appreciate your help. Thank you
Since you didnt provide the table structure, I will try to help you understand the logic, which you can easily apply to your table.
SELECT Count(*)
FROM tbproducts
WHERE Datediff(d, now, tbproducts.expiarydate) > 0
Here you are simply saying, "please count for me how many products (rows) that passed their expiration day, when compared with right now"
Datediff(d, now, tbproducts.expiarydate) Will subtract today's date Now from the product's expiry date, and return the difference (in days d). This will return a long number.
So the difference between 12/7/2017 (now) and 12/5/2017 (expired 2 days ago), will be 7 (7th) - (minus) 5 (5th) = 2
And the difference between 12/7/2017 (now) and 12/10/2017 (not yet expired), will be 7 (7th) - (minus) 10 (10th) = -3
So as long as Datediff(d, now, tbproducts.expiarydate) returns a number over 0 then the product is expired

How to use Dense Rank for Date Field in Business Objects

I am trying to filter for only latest 4 weeks of data in Business objects, In the below example I want to retrieve are filter for only data for below dates(Latest Weeks). Week Date is a field which is every Monday date of the week.
10/9/2017
10/2/2017
9/25/2017
9/18/2017
Week Date Sales
8/28/2017 100
8/28/2017 101
9/4/2017 102
9/4/2017 103
9/11/2017 104
9/11/2017 105
9/18/2017 106
9/18/2017 107
9/25/2017 108
9/25/2017 109
10/2/2017 110
10/2/2017 111
10/9/2017 112
I tried with Rank Function and spent hours finding solution but had no luck. I hope Dense Rank would help, later I can filter for 1 to 4.
You could do this in one variable and a filter on that variable, but I like to break things down to understand exactly what is happening. You can always combine variables later.
First, create a variable to represent the cutoff date...
[CutOff Date] = RelativeDate(CurrentDate(); -4; WeekPeriod)
Second, create another variable to determine if a particular week should be included...
[Last Four Weeks] = If([Week Date] > [CutOff Date]; 1; 0)
Finally, create filter on to only show the last four weeks...
[Last Four Weeks] = 1
You may need to adjust the values depending on the timing of when you are running your report, but that is the general idea.
As I mentioned earlier, you could combine [CutOff Date] and [Last Four Weeks] into one variable...
[Last Four Weeks] = If([Week Date] > RelativeDate(CurrentDate(); -4; WeekPeriod)
; 1; 0)
Noel

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?

Need Excel Sheet to Calculate Complex Vacation Accrual and Use of Vacation Time

I've been working on this for a while and trying different styles... I need to develop a tracking spreadsheet (for 50 employees) that will calculate accruing vacation time and vacation time used based on the following parameters:
*Employees who have worked less than 3 years but past the 90 day anniversary with the company (at which they start accruing) (there is one more caveat on this... the accrual day is the 1st of the month of the month past the 90 days)[I figured this 90 day item out on my excel sheet] it will accrue at:
- 4 hours a month, at the end of the month, with a MAX cap of 72 hours that they can have stored (but if they use vacation time and fall below the 72 hours they can continue to accrue again up to 72 hours...)
*Employees who have worked more than 3 years but less than 6 years with the company carry over their prior vacation and begin to accrue from here onward at:
- 6.8 hours a month, at the end of the month, with a MAX cap of 122.4 hours that they can have stored (but if they use vacation time and fall below the 122.4 hours they can continue to accrue again up to 122.4 hours...)
*Employees 6 years and over with the company carry over their prior vacation and begin to accrue from here onward at:
- 10 hours a month, at the end of the month, with a MAX cap of 180 hours that they can have stored (but if they use vacation time and fall below the 180 hours they can continue to accrue again up to 180 hours....)
& yes, I need to be able to deduct vacation time used.
Does anyone have any suggestions for layout or for a formula that can do part of these functions? I appreciate any advice or suggestion on what else I can use!
I have created a test sheet, and was accruing based on these conditions for the first and started on the second set of rules (for years 3+).
However when I accrue to the max of 72 hours on the first policy, it no longer accrues correctly if they used vacation and fall under the 72 hours cap again.
I know this is a overcomplicated policy, but that is what the company wants and they will not budge.... Any help or advice is appreciated. I know my sheet isn't great.. but I'm trying options.
Below is the equation I used for the 90 day:
=IF(F2<TODAY()-90, F2+90, "90 Day Period")
Then to get the first day of the month after I used:
=IF(G2="90 Day Period","N/A",DATE(YEAR(G2),MONTH(G2)+1,1))
I tried using for the accrual of the first rule (but it has issues...):
=IF(N2="N/A","N/A",IF(N2<=36,MIN(72,((N2*4)-P2),72),(72-P2)))
For second rule:
=IF(AND(N2>36,N2<=72),MIN(122.4,((N2-36)*6.8)+Q2-S2),0)
Let
column A EmpName
column B EntryDate
Column C DaysSpent for the current month
Column D capped Accruel buffer at end of month
repeat Columns C and D month after month
example
01-Sep-2013 01-Oct-2013
EmpName EntryDate spent buffer spent buffer ... etc ...
.-------.-----------.-----------.------.-----------.------.
me 01-Jan-2010 0 12 0 18.8
you 01-Jun-2013 0 4 0 4
In order not to get spaghetti formulas I recommend to create some user defined functions in VBA, like
Function GetCap(EntryDate, ThisDate) As Single
Function GetMonthly(EntryDate, ThisDate) As Single
By experience it's easier to debug/maintain 2-3 nested If's or Select Case's in VBA than 92 character long formulas with no blanks, no comments etc. in the sheet. Should the business logic change, there's one code block to review - instead of dozens/hundreds of formula in a sheet that has grown for 3x12 months x 50 users.
The above functions may want the help of e.g.
Function EndOfMonth(MyDate) as Date
Function BeginOfNextMonth(MyDate) as Date
so that in the sheet you just
manually enter hours spent month after month
calculate new buffer as = MIN([oldbuffer] - [Spent] + GetMonthly(...), GetCap(...))
carefully use relative/absolute addressing to make the formula "copyable" across columns/rows, e.g.
row-absolute on ThisDate got from the header when copying downwards
column-absolute on EntryDate for each Emp when copying rightwards
You can of course use =GetCap(...) and =GetMonthly(...) directly in cells of your sheet to display intermediate results and for debugging purposes.
Be carefull when you compare dates
Tips:
3 years later is not always 365x3 days later
check what the VBA DateSerial() functions does for months > 12 and months < 0
the end of next month always is the first days of 2 months ahead minus 1 ... even in February of a leap year ggg
and post more questions if you get stuck on these functions.