SQL simple case statement with formula - sql

I tried adding in a THEN after the WHEN but don't know how to just make it show up as the result of the formula
CASE
WHEN (PLANAVAILDATE - CURRENT_DATE) =>280
ELSE ''
END AS Days Overdue

You don't need a case statement for this you can directly count the days of overdue
by using this
DATEDIFF(hour, PLANAVAILDATE, CURRENT_DATE)/24 AS 'Days Overdue'
if you can provide the full question I will give you exact solution

Related

I want to put 0 instead of negative values from DATEDIFF

The table with the following structure is considered:
MEDICINE: (name, price, quantity, expiration_date );
I did
SELECT Name,
DATEDIFF(expiration_date, CURRENT_DATE)
AS days
FROM MEDICINE;
I got negative results from datediff, but I want to put 0 instead of the negative values. How can I make this? Any help?
Try this:
SELECT Name
, CASE
WHEN DATEDIFF(expiration_date, CURRENT_DATE) < 0 THEN 0
ELSE DATEDIFF(expiration_date, CURRENT_DATE)
END AS days
FROM MEDICINE;
Unfortunately you have to repeat the expression, but the engine should be smart enough to only calculate it once.
You can use GREATEST() in combination with a - because DATEDIFF() doesn't exist in PostgreSQL:
SELECT Name,
GREATEST(expiration_date - CURRENT_DATE, 0)
AS days
FROM MEDICINE;
This should work for you.
GREATEST(0, DATEDIFF(expiration_date, CURRENT_DATE))
Read about these conditional operators here. Spend some time reading about the suite of functions and operators in your SQL language: it's time well spent.
You can do it using SQL case.
This should work:
SELECT Name,
CASE WHEN DATEDIFF(expiration_date, CURRENT_DATE) > 0 THEN DATEDIFF(expiration_date, CURRENT_DATE)
ELSE 0
END
AS days
FROM MEDICINE;
You can read more about it here: https://www.w3schools.com/sql/sql_case.asp

AWS Athena [Presto] How to receive data from the past 7 days only?

I've gone through multiple similar threads but still can't get this to work for some reason. I have this query that is currently pulling all data from stage_date in January, but I want to be able to get the data from only the past week (7 days). When I've done some of the other answers for questions similar to this I get an error like > cannot be applied to varchar, date or something along those lines. (When I put the > currentdate formula in my query)
select reason_code, service_category, stage_date,
case when reason_code LIKE 'UAR'
then 'Unauthorized Return'
else 'FALSE'
end as "reason_long",
case when service_category like 'CRED'
then 'Credit Approved'
else 'FALSE'
end as "service_long"
from "table.name"
where
stage_date like '01%'
order by customer_name,
stage,
reason_code
If stage_date is of type string, cast it to date:
WHERE date(stage_date) >= current_date() - INTERVAL 7 DAY

Can functions such as 'CONCAT' or 'DATANAME' be utilized in Alias in SQL?

I am trying to create a report that will forecast future gains. I am trying to find a way for my column names to update themselves over time from current month. My approach was to create the same case statement bellow for my future months by inclemently adding 1 to the month. However, it seems that I cannot use functions as ALIAS. What would you recommend I do?
CASE
WHEN DATEDIFF(month, GETDATE(), j.Estimated_Start_Date) <= 0 AND
DATEDIFF(month, GETDATE(),j.Estimated_Comp_Date) >= 0
THEN 1
ELSE 0
END AS **CONCAT(DATENAME(MONTH,j.Estimated_Start_Date),'-', DATENAME(YEAR,j.Estimated_Start_Date))**

How can I express a SUM condition using SWITCH or nested IIF?

I have a request with a SQL code which calculates working days of each of these activities: Interstaff, Mission, ``Congés` using a defined VBA Function.
When there are two activities on the same day, it counts 1 day for each activity, I want the Sum function to ignore this day, and go to the next one.
I would like to modify my SQL code to add this condition:
"When calculating mission, if there is a Congés within the same period, ignore this day (and give priority to only count it in congés)"
I read that there is a SWITCH or a nested IIF conditions, but I couldn't translate that within my actual code...
Please do consider that I am still a beginner, on his way to learning!
SELECT
Z.Planning_Consultants.ID_Consultant,
Sum(IIf([Activité]="(2) Interstaff",WorkingDaysInDateRange([maxBegin],[minEnd])*Planning_Consultants.Time_Allocated,0)) AS NonBillable,
Sum(IIf([Activité]="(1) Mission",WorkingDaysInDateRange([maxBegin],[minEnd])*Planning_Consultants.Time_Allocated,0)) AS Billable,
Sum(IIf([Activité]="(3) Congés/Arrêt",WorkingDaysInDateRange([maxBegin],[minEnd])*Planning_Consultants.Time_Allocated,0)) AS Absent,
For example Mr A got a mission from 03/06/2019 to 07/06/2019. With a day off (congé) on 06/06/2019. I expect the output to be Mission 4 days and congé 1 day, in my case I will have mission 5 days, and congé 1 day,
Here's an example of a Dataset :
Activity BegDate EndDate Time_Allocated
(1)Mission 01/01/2019 31/12/2019 100%
(3)congé 02/04/2019 05/04/2019 100%
For April for example, I would like to have 18 working days and 4 congé, instead of 22 working days and 4 congé
This is the sort of query that should work for you. I've built this in sql server rather than access so you might have to make some mods, but it will give you the idea. It will give you the actual days for the activity, plus the total leave days that cover the activity. The rest I will leave up to you.
select t1.Activity, t1.Time_Allocated,
WorkingDaysInDateRange(t1.BegDate, t1.EndDate) as days,
sum(WorkingDaysInDateRange(
case when t2.BegDate<T1.BegDate then T1.BegDate else t2.BegDate end,
case when t2.EndDate>T1.EndDate then T1.EndDate else t2.EndDate end))
as LeaveDays
from times t1
left join times t2 on t2.BegDate<=t1.Enddate and t2.EndDate>=t1.BegDate
and t2.Activity='(3)congé'
and t1.Activity!='(3)congé'
group by t1.Activity, t1.Time_Allocated, t1.BegDate, t1.EndDate

Not getting actual minutes in SQL DateDiff

I searched in different places and found below queries. I am using the following queries to get the actual minutes difference in SQL. The dates I provide are the same day. I need difference in minutes only but SQL is returning 35 instead of 5 minutes in the first query. And the second query return milliseconds.
SELECT DATEDIFF(MINUTE,GETDATE(), CONVERT(datetime,'2016-08-11 16:04:24'))%3600/60 AS MinuteDiff
SELECT datediff(minute,GETDATE(), CONVERT(datetime,'2016-08-11 16:04:24')) as MinutesDiff
What is missing. Please help.
I need to put a condition that if time is less than 20 minutes then
do this
else
do this
Updated:
The issue occurs when i use GetDate(). When I use a fix date it works fine
You need to place the GETDATE() after your datetime value, other wise in your case you will get the minutes in negative value.
SELECT DATEDIFF(MINUTE,CONVERT(datetime,'2016-08-11 16:04:24'), GETDATE()) AS MinuteDiff
The current GETDATE() is 2016-08-11 17:05:39.053, so it returns 61.
Then based on the value, using IF ... ELSE ... you can do your expected operation:
IF DATEDIFF(MINUTE,CONVERT(datetime,'2016-08-11 16:04:24'), GETDATE()) < 20
PRINT 'With in 20 mins'
ELSE
PRINT 'More than 20 mins'
Here is a working example of what your after...although you probably need to switch out the date components as appropriate for your usage.
select
case
when
(SELECT datediff(minute,GETDATE(), CONVERT(datetime,'2016-08-11 06:00:00'))) < 20 then
(select 'do this')
else
(select 'do something else')
end as answer
If you want minute span between two datetime, then your second one is enough.
SELECT datediff(n, CONVERT(datetime,'2016-08-11 16:04:24'),GETDATE()) as MinutesDiff
you can use CASE for your further
select
case when
datediff(n, CONVERT(datetime,'2016-08-11 16:04:24'),GETDATE()) < 20 then
`your code`
else
`your else code`
end minte
Hey sorry for the initial poor explanation.
I use something like the following frequently this will return a INT and decide if it's then you can do the logic on it, equal to, not equal less than greater than etc.
If it is true it will return a 1 or it is false a 0. You can get it to return columns or set it to a string.
Hope it helps
select
Case
When DateDiff(minute,[column],Getdate()) > 20 then 1 else 0
end as [alias]