I want get the Half year or half month using sqlserver - stored-procedures

This is my query
DECLARE #Frequencyvalue DECIMAL(18,2)
DECLARE #Frequencyunits varchar(1)
SET #Frequencyvalue=(SELECT CalibrationFrequencyValue FRO dbo.InstrumentMaster WHERE InstrumentID=#InstrumentID)
SET #Frequencyunits=(SELECT CalibrationFrequencyUnits FROM dbo.InstrumentMaster WHERE InstrumentID=#InstrumentID)
DECLARE #HalfFrequencyvalue DECIMAL(18,2)
SET #HalfFrequencyvalue=#Frequencyvalue/2
Get HALF(Calibration Frequency and calibration Period) from the InstrumentMaster. [If Calibration Frequency is 1 Year, then due date should be calculated for 6 months)
Calculate next calibration date".
Just I need How to get the half Year or half month query

Assuming you want the due date calculated from today, and #Frequencyunits is 'y','m':
RETURN SELECT DATEADD(CASE WHEN #Frequencyunits = 'y' THEN 'yy' ELSE 'm' END,#HalfFrequencyvalue) AS DueDate

Related

daterunc missing values tableau

I don't think this problem is solvable but I figured I would ask.
Data:
DateDim table with 1 row for every date. Columns = date
Sales table with 1 row for every sale. Important columns = id, sales_date
tables relate on date = sales_date
Plot:
I am plotting count of sales for each date, with date on the x-axis and count([id]) on the y-axis.
Problem:
I need a dynamic x-axis. If I filter date for less than a month I need days on the X-axis, but if I filter greater than a month I need month-year on the x-axis.
I accomplished this with daterunc function.
If endDate - startDate < 30
then daterunc('day', [date])
else if endDate - startDate >= 30
then daterunc('month', [date])
end
This works nice except for one issue: missing values. If I use the date as the axis, I can right click the date and select "show missing values" which will correctly plot 0 on days with no sales.
But the calculation above does not have a 'show missing values' option. So the plot does not show zeros when I use the calculation.
Is it possible to modify the above calculation to show missing values?

How To Calculate Time Left For The Day

I have used formula "=TEXT( NOW() , "HH:mm" )" for the current time of the day.
What formula can I use to calculate the time that is left till the end of the day, 12am?
Say the current time now is 7:12pm. I would like to have a formula that would be able to calculate that I only have 4:48 hours left till 12am.
Try =TEXT(1 - NOW()), "HH:mm").
Use TIMEVALUE
https://support.google.com/docs/answer/3267350
Which returns the fraction of a day that a time represents. So you can chain it with NOW.
=TIMEVALUE(NOW())
Would return 0.5 if it was midday.
So then you can
= 1 - TIMEVALUE(NOW())
To get the amount of time left in the day expressed as a fraction.
Then you wrap it in your TEXT function to get it in hours and minutes.
=TEXT(1-TIMEVALUE(NOW()), "HH:ss")

Informix - Need to create date time parameters for Where clause

Informix is not my normal environment and the way it handles datetime values is throwing me for a loop. I can't imagine this is difficult, but for the life of me I'm not yet able to figure it out.
This is the SQL:
SELECT agentid,
extension As Ext,
resourcefirstname As FirstNm,
resourcelastname As LastNm,
Min(eventdatetime) As FirstIn
FROM agentstatedetail AS asdr Join
resource As r On asdr.agentid = r.resourceid
WHERE asdr.eventdatetime BETWEEN '2016-10-20 04:00:00' AND '2016-10-21 03:59:59'
AND eventtype = 3
AND assignedteamid = 14
Group By agentid, extension, resourcefirstname, resourcelastname
Order By Min(eventdatetime)
Everything works as is, but the dates in the Between clause are currently entered manually- not optimal. I just need some way to describe "yesterday at 4:00 AM" and "Today at 4:00 AM" Will somebody please clue me in?
Using Informix version 12.10.FC6DE, I can do this:
SELECT
TODAY::DATETIME YEAR TO SECOND AS today_zerohour
, TODAY::DATETIME YEAR TO SECOND - '20:00:00'::INTERVAL HOUR TO SECOND AS yesterday_dawn
, TODAY::DATETIME YEAR TO SECOND + '04:00:00'::INTERVAL HOUR TO SECOND AS today_dawn
FROM
systables
WHERE
tabid = 1;
And it returns:
today_zerohour yesterday_dawn today_dawn
2016-10-21 00:00:00 2016-10-20 04:00:00 2016-10-21 04:00:00
So, what is happening here:
The operator TODAY returns the system date as a DATE type. The DATE type does not have the precision I want (it only has year, month and day), so I cast the value (cast operator is ::) to a DATETIME with precision from year to second (the hour, minutes and seconds are set to zero):
TODAY::DATETIME YEAR TO SECOND
In Informix, for an addition or subtraction with a DATETIME value to return another DATETIME value, I need to add or subtract an INTERVAL value. So I created 2 INTERVAL values.
One INTERVAL of 20 hours to subtract from the today value (again the cast operator :: is used, this time to cast from a string to an INTERVAL):
'20:00:00'::INTERVAL HOUR TO SECOND
One INTERVAL of 4 hours to add to the today value:
'04:00:00'::INTERVAL HOUR TO SECOND

TABLEAU: calc field to get the last value available

I'm using Tableau Desktop, my data are like this:
KPI,date,monthValue
coffee break,01/06/2015,10.50
coffee break,01/07/2015,8.30
and I want to build a table like this
KPI, year(date), last value
coffee time, 2015, 8.30
How can I set a calculated field in order to show me the last value available in that year? I tried to do:
LOOKUP([MonthValue], LAST())
But it didn't work and tells me 'cannot mix aggregate and non-aggregate', so I did:
LOOKUP(sum([MonthValue]), LAST())
But it didn't work too. How should I proceed?
If you are using Tableau 9 then you can do this with an LOD calc that looks for the max value in your date field and then checks if the current date value is the same as the max date value.
[Date] == {fixed: max([Date])}
As you can see in the example below when you use the calc as a filter you will only get the last row from your example above.
UPDATE: to get the values per year you can do something like:
Here I am using a table calculation to find the max date per year and then ranking those dates and filtering down to the latest date in each year (which will be the one that has a rank equal to 1).
!max date is WINDOW_MAX(ATTR(Date))
!rank is RANK(Date)
You need to make sure that the table calculations are computer in the correct way (in this case across the values of each year).

Calculate elapsed time using NOW() with conditional formatting

I'm trying to set up a Google Sheet for volunteers at the local dog shelter. The idea is to have an auto-updating spreadsheet that shows at a glance which dogs really need to be walked, in two ways:
conditional formatting the rows with color based on time elapsed:
red if it's been >6 hours since their last walk,
yellow for 3-6 hours, or
green if they were walked <3 hrs ago.
auto-sorting the rows so that the dogs that have gone the longest are always at the top of the list, and when they get walked, they go to the bottom of the list.
Here's where I'm at.
Problem 1: I'm trying to calculate time elapsed by using the NOW() function (which returns the current date and time), minus the time of the last walk. The problem is that if you only enter a time, Google apparently assumes the date is 12/30/1899. So if I put in 8:00 am, and the current time based on NOW() is 4:00 pm, instead of returning 8:00 hours, it calculates the the duration as 10,000,000+ hours that have elapsed since 8:00 am on December 30, 1899 up to the current date and time. (Similar problem discussed here).
If I keep the output cells in the HH:MM format, it'll initially look OK, because it'll just return the HH:MM as 8:00, BUT I can't use that because then the conditional formatting won't work - it's still actually calculating the 10,000,000 hours since 1899. So I can't set duration values for red/green/yellow because all the output values will be slowly increasing every day we get further away from 1899, meaning I would have to reset the ranges daily.
Obviously I could work around this by always including the time and date, but the idea is to have a spreadsheet that is idiot-proof so that any volunteer can use it by just adding the time they walked the dog. It won't work if they have to input the date too.
Problem 2: Assuming I can get the above to work, how can I set it up so the table automatically sorts itself after any change (i.e. when a dog gets walked and the entry gets updated)?
Help?
Please try:
Problem 1
Green is easy, just format all your data that way with standard fill (CF will override this where applicable).
Select A1 and apply a Custom formula is of:
=and($D1<>"",timevalue(now())>$D1+6/24)
with colour Red and Range A:E
Repeat (the order of these two is important):
=and($D1<>"",timevalue(now())>$D1+3/24)
with colour Yellow and Range A:E.
Save rules and close window.
Problem 2
Create a pivot table by selecting ColumnA:E (may have to get rid of some content present low down in the sheet first) and Data > Pivot Table Report..., to Rows Add field Dog name (do not Show totals), to Values Add field Time since last walk and Summarise by:SUM. Name the sheet PT.
In say J2 of your other sheet (not PT) enter:
=query(PT!A:B, "Select * order by B desc ")
May be worth noting that without the day part there might be problems where times span midnight - if you have walkers with insomnia?!
For the autosorting, I found a script elsewhere on stackoverflow and modified it slightly for my purposes:
function onEdit(event){
var sheet = SpreadsheetApp.getActiveSheet();
if(sheet.getName()=='Sheet1'){
var editedCell = sheet.getActiveCell();
var columnToSortBy = 4;
var tableRange = "a2:f91";
if(editedCell.getColumn() == columnToSortBy){
var range = sheet.getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}
}
}

Resources