Difficulty filtering by Select Column - sql

I'm going to apologize in advance. I basically stumble through SQL as projects need done but my knowledge is rather lacking, so I apologize for any incorrect terminology or poor syntax.
I would appreciate it if anyone would be able to help me out.
I have the following query.
WITH BusinessDayCalc
AS
(
SELECT
EstimatedClosingDate AS EstimatedClosingDate
from SampleDB
)
SELECT
a.*,
(DATEDIFF(dd, GETDATE(), EstimatedClosingDate) + 1)
-(DATEDIFF(wk,GETDATE(), EstimatedClosingDate) * 2)
-(CASE WHEN DATENAME(dw, GETDATE()) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, EstimatedClosingDate) = 'Saturday' THEN 1 ELSE 0 END)
-(Select Count(*) FROM Holiday Where Date >= GETDATE() AND Date < EstimatedClosingDate)
AS BusinessDaysUntil
FROM BusinessDayCalc a
Where EstimatedClosingDate > GetDate() AND EstimatedClosingDate < (GetDate()+17)
I have also attached pics of the current Output and the Holiday Table that is being referenced.
My issue is that I would like to be able to filter my data to show any data that is 8 or 12 business days out, however, I am unable to pull through the column name or have SQL recognize the BusinessDaysUntil column.
Would someone be able to help me out? Once I get this squared away, the rest of the project should go smoothly.

You can't use a derived column in the WHERE clause.
Also you are using a rather useless CTE which returns only 1 column of the table SampleDB.
Instead create a CTE with the query and then select from it and filter:
WITH cte AS (
SELECT
EstimatedClosingDate,
(DATEDIFF(dd, GETDATE(), EstimatedClosingDate) + 1)
-(DATEDIFF(wk,GETDATE(), EstimatedClosingDate) * 2)
-(CASE WHEN DATENAME(dw, GETDATE()) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, EstimatedClosingDate) = 'Saturday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, EstimatedClosingDate) = 'Saturday' THEN 1 ELSE 0 END)
-(Select Count(*) FROM Holiday Where Date >= GETDATE() AND Date < EstimatedClosingDate) AS BusinessDaysUntil
FROM SampleDB
WHERE EstimatedClosingDate > GetDate() AND EstimatedClosingDate < (GetDate()+17)
)
SELECT * FROM cte
WHERE BusinessDaysUntil > ?
Replace BusinessDaysUntil > ? with the filter that you want to apply.

Related

Select Distinct Case Query

I have the below query
SELECT Distinct dbo.WSUS_ComputerUpdateStatusWithApprovals.fulldomainname AS [Computer Name]
,dbo.WSUS_ComputerUpdateStatusWithApprovals.title AS [Update Title]
,count((case when (DATEDIFF(day, [ArrivalDate], GetDate())) > '0' and (DATEDIFF(day, [ArrivalDate], GetDate())) < '31' Then '' End)) AS 'Green'
,count((case when (DATEDIFF(day, [ArrivalDate], GetDate())) > '31' and (DATEDIFF(day, [ArrivalDate], GetDate())) < '91' then '' End)) AS 'Yellow'
,count((case when (DATEDIFF(day, [ArrivalDate], GetDate())) > '90' then '' End)) AS 'Red'
,DATEDIFF(day, [ApprovalCreationDate], GetDate()) AS 'DaysOldSinceApproval'
FROM dbo.WSUS_ComputerUpdateStatusWithApprovals
WHERE dbo.WSUS_ComputerUpdateStatusWithApprovals.IsApproved = 1
Group by dbo.WSUS_ComputerUpdateStatusWithApprovals.fulldomainname
,[ArrivalDate]
,dbo.WSUS_ComputerUpdateStatusWithApprovals.ApprovalCreationDate
,dbo.WSUS_ComputerUpdateStatusWithApprovals.title
ORDER BY dbo.WSUS_ComputerUpdateStatusWithApprovals.fulldomainname ASC
The result set comes out like this:
Where I would want it to say Computer Name and a Total count for each Green, Yellow, Red. So in this case, Computer Name, Green = 3, Yellow = 1 and Red = 1. I included the title and days old to show additional information.
If I followed you correctly, you just need to change the group by clause so it generates one row per fulldomainname:
select
fulldomainname AS [Computer Name],
sum(case when datediff(day, [ArrivalDate], GetDate()) between 1 and 30 then 1 else 0 end) AS Green,
sum(case when datediff(day, [ArrivalDate], GetDate()) between 30 and 90 then 1 else 0 end) AS Yellow,
sum(case when datediff(day, [ArrivalDate], GetDate()) > 90 then 1 else 0 end) AS Red
from dbo.WSUS_ComputerUpdateStatusWithApprovals
where IsApproved = 1
group by fulldomainname
order by fulldomainname ASC
Note that I changed the conditional aggregation logic to use sum() rather than count(), because it seems easier to follow.

Combine queries to show one set of results

I have two separate queries where I need to match up and combine together based on a date value.
select convert(varchar,Delivery_Date,101) as 'Date',
sum(case when billing_group = '3' and delivery_date >= dateadd(day,datediff(day, 0, GetDate()) - 30, 0) then 1 else 0 end) as 'OR to WA'
from orders
where delivery_Date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0) and billing_group in ('3')
group by delivery_date
order by date desc
select convert(varchar,Origin_Date,101) as 'Date',
sum(case when billing_group = '4' and origin_date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0)then 1 else 0 end) as 'WA to OR'
from orders
where origin_Date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0) and billing_group in ('4')
group by origin_Date
order by date desc
Please note that I am using a different date value (delivery_date) in the first query and a different for the second (origin_date)
Thanks for the help!!
I think you just want conditional aggregation, but your query offers other room for improvement:
select convert(varchar(255), v.thedate, 101) as [Date],
sum(case when o.billing_group = 3 then 1 else 0 end) as [OR to WA],
sum(case when o.billing_group = 4 then 1 else 0 end) as [WA to OR]
from orders o cross apply
(values (case when o.billing_group = 3 then delivery_date else o.origin_date end)
) v(the_date)
where v.thedate >= dateadd(day, -30, cast(getdate() as date)) and
o.billing_group in (3, 4)
group by convert(varchar(255), v.thedate, 101)
order by v.thedate desc
Notes:
Never use varchar() without a length parameter. The length varies by context and it might not do what you expect.
I am guessing that billing_group is a number. If so, don't use single quotes. Do use single quotes if I'm wrong.
I cannot tell what the data type of delivery_date is. It is safer to aggregate by the full expression, rather than just the column.
Do not use single quotes for column aliases. Only use single quotes for string and date constants.
There is no reason to use the "trick" of adding days to 0 to remove a time component. Instead, just convert to date.
The condition on delivery_date does not have to appear in both the case expression and the where clause.
Try using UNION and adding the missing columns
then do group by and ordering
select * from (
convert(varchar,Delivery_Date,101) as 'Date',
sum(case when billing_group = '3' and delivery_date >= dateadd(day,datediff(day, 0, GetDate()) - 30, 0) then 1 else 0 end) as 'OR to WA',
0 as 'WA to OR'
from orders
where delivery_Date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0) and billing_group in ('3')
-- group by delivery_date
--- order by date desc
UNION
select convert(varchar,Origin_Date,101) as 'Date',
0 as 'OR to WA'
sum(case when billing_group = '4' and origin_date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0)then 1 else 0 end) as 'WA to OR'
from orders
where origin_Date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0) and billing_group in ('4')
--group by origin_Date
--order by date desc
) group by [Date]
order by [Date] desc

sql difference in two dates

In sql server, I have two tables:
Tran_Ex
Transactions
They both have customer_id which is the key to join the tables.
I want to find the difference in WORKING DAYS of the Date_Reported (from transactions) from the Date_Received (from the Tran_ex). I would like an extra column with these figures:
eg
Date Reported | Date Received | Difference in days
Thanks in advance
Use DATEDIFF() function()
You can get Working Day (Monday to Friday) difference from this query, for bank holidays you need seperate logic.
Select Date_Reported,
Date_Received ,
(DATEDIFF(dd, Date_Reported, Date_Received) + 1)
-(DATEDIFF(wk, Date_Reported, Date_Received) * 2)
-(CASE WHEN DATENAME(dw, Date_Reported) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, Date_Received) = 'Saturday' THEN 1 ELSE 0 END)
AS Working_days_Difference
from Tran_Ex as tx
inner join
Transactions as tr
on(tx.customer_id = tr.customer_id)
Modified the Query on suggestions based on #scsimon for not using shorthands.
SELECT Date_Reported,
Date_Received ,
datediff(day,((CASE WHEN Datename(weekday, Date_Reported) = 'Sunday' THEN 1 ELSE 0 END ) - (CASE WHEN Datename(weekday, Date_Received ) = 'Saturday' THEN 1 ELSE 0 END )),Datediff(day,(Datediff(week, Date_Reported, Date_Received ) * 2 ),
(Datediff(day, Date_Reported, Date_Received ) + 1 )))
AS Working_days_Difference
from Tran_Ex as tx
inner join
Transactions as tr
on(tx.customer_id = tr.customer_id)
Use DATEDIFF() function :
select t.Date_Reported, t1.Date_Received,
datediff(day, t.Date_Reported, t1.Date_Received) [Difference in days]
from Tran_Ex tx
inner join Transactions t on t.customer_id = tx.customer_id;

T-SQL Get 3 Month Interval : Trying to count Property_IDs over a close_dt Interval

Trying to count Property_IDs over a close_dt Interval
select distinct pd.state AS StateName, zw.CountyName AS [County Name]
,sum(case when pc.close_dt >= dateName(MM,dateadd(MM,-3,GetDate()))then 1
else 0 end) AS [0-3 Months]
,sum(case when pc.close_dt >= dateName(MM,dateadd(MM,-6,GetDate()))
and pc.close_dt < dateName(MM,dateadd(MM,-3,GetDate())) then
1 else 0 end) AS [3-6 Months]
from resnet_mysql.dbo.property_details pd (Nolock)
join resnet.dbo.ZipCodesView zw (nolock)
on CAST(LEFT(pd.zip, 5) AS varchar) = CAST(zw.ZipCodeID AS varchar)
join resnet_mysql.dbo.property_closings pc (nolock)
on pd.property_id = pc.property_id
group by pd.state, zw.countyName, pc.close_dt
How can I get the 3 month interval from the previous 3 month interval value? So it will be 3-6 months?
I want it to look like this.
But I get this error.
I am thinking you want something like this:
select pd.state AS StateName, zw.CountyName AS [County Name],
sum(case when pc.close_dt >= dateadd(month, -3, GetDate()) then 1
else 0
end) AS [0-3 Months]
sum(case when pc.close_dt >= dateadd(month, -6, GetDate()) and
pc.close_dt < dateadd(month, -3, GetDate())
then 1
else 0
end) AS [3-6 Months]
from resnet_mysql.dbo.property_details pd join
resnet.dbo.ZipCodesView zw
on LEFT(pd.zip, 5) = CAST(zw.ZipCodeID as VARCHAR(5)) join
resnet_mysql.dbo.property_closings pc
on pd.property_id = pc.property_id
group by pd.state, zw.countyName;
Your original code has so many errors, it is hard to list them:
DATENAME() returns a string. Why would you want to compare that to a date?
You are aggregating based on date ranges. You don't want to include the date in the GROUP BY.
LEFT() already returns a string; there is no need to convert it.
You probably don't want to compare a string version of zip code to a numeric id. But if you do, the conversion should specify the length.
WITH (NOLOCK) is not recommended unless you actually know what you are doing.

SQL Server query, remove date dimension

I need help in removing the date dimension from the query below. In other words make the query independent of the date / time interval
My goal is to load the table into SSAS so that i would not have to change the date every time i run reports.
the query is huge (months, quarters, years, and aggregated date CR12,PR12 ...), i just gave a short example below
I sincerly appreciate any help
drop table #tmptmp
SELECT *, (DATEDIFF(day, enrollmentsDate, ShipmentDate))
- ((DATEDIFF(WEEK, enrollmentsenttDate, InitialShipmentDate) * 2)
+(CASE WHEN DATENAME(DW, enrollmentsentDate) = 'Sunday' THEN 1 ELSE 0 END)
+(CASE WHEN DATENAME(DW, ShipmentDate) = 'Saturday' THEN 1 ELSE 0 END)
- (select count(*) from tblFactoryHolidayDates where Date >= enrollmentsentDate
and Date < InitialShipmentDate)) as countdays into #tmptmp from
#tmpTouchpointsEnrollments
where EnrollmentSentDate is not null
----------------------------
drop table #tmp
select * into #tmp
from #tmptmp
where countdays < 20
drop table #tmpMetric
Select 'GrandTotal' as Dummy,'Avg days' as Metrics,'1' as MetricOrder,
Sum(case when Year(EnrollmentReceiveddate) ='2010' then (countdays) end) *1.0/
count(case when Year(EnrollmentReceiveddate) ='2010' then (patientID) end) *1.0 as Y2010,
into #tmpMetric
from #tmp
Thank you very much