Query to show only unique records based on certain criteria in ACCESS - sql

I am working on a database that is something like this.
enter image description here
In the event a job number when both current week and previous week in the "sort" column happens, choose only the current week row.
enter image description here
The first or last function don't work here as the current week or previous week record can either be the first or last row of a particular job number.

You can group on the job and select the max date:
Select [Job Number], Max([Order Date]), YourWeekFunction(Max([Order Date]))
From YourTable
Group By [Job Number]

Related

How to pull next 2 quarter data with a data model that doesn't have fiscal time database and company defined fiscal year

I would like to pull all orders from the current, next and next to next quarter of the time stamp. I am able to pull up current quarter data but I am not able to pull next and next+1 quarter data.
I am currently using MS SQL Server 2013.
The time stamp also appears in a weird format. for eg.20191031_FY20_Q1_Wk1 whereas [Order Close Fiscal Year Quarter Display Code] appears as 2020-Q1. So to pull current quarter data I have used below condition:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code]
What I logically want to do is this:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 1
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 2
Obviously, I came across data type conversion error. I even tried using Dateadd() function:
[Opportunity Close Fiscal Year Quarter Display Code] = DATEADD(quarter,1, cast(left(time_stamp,8) as date) )
but still I keep coming across same error.
The MOST IMPORTANT THING I would like to HIGHLIGHT is my org's Fiscal Year is not same as generic Fiscal Year. In my org, Fiscal Year begins from Oct and ends in Sep. So I am not sure how even DateAdd() function will help. I believe having a Fiscal Time table customized as per org's Fiscal Year could be of great help for me but my manager thinks the BI team won't entertain such a request.
Any help in building this query would be really great!!
You would seem to have time_stamp values whose format is not as you describe.
You should find the column values that don't convert to a date:
select time_stamp
from t
where try_cast(left(time_stamp, 8) as date) is null and time_stamp is not null
With this information, you can debug your code or the data.

multidimensionel cube: sum on filtered fact

I have a fact_dueAnalysis table which contains all my customers outstanding. The table is built with a Date Reporting, so when I pick a specific Date Reporting date, I can see all customers outstanding for that specific day.
Now I want to let my users use a date hierarchy so they can select e.g. week 39 and get all outstanding for the LAST day of that week (I will always use the last date in the hierarchy my users have selected).
I have made the following script:
([Measures].[Due Amount],[Date Reporting].[Year - Week - Date].[Week].members) =
sum(generate(tail(
DESCENDANTS([Date Reporting].[Year - Week - Date].[Week],, leaves),1),1
)
,[Measures].[Customer Due Amount]);
I am here trying to get the latest (tail) date (leave) on my Date Reporting and then sum the Customer Due Amount and get the result in a new measure called Due Amount.
When the users select a specific date, it does work, but when they select a week I get a #VALUE as result.
How should I create this correct?
Here is my end result:
([Measures].[Due Amount],[Date Reporting].[Year - Week - Date].[Week].members) =
SUM(TAIL(DESCENDANTS([Date Reporting].[Year - Week - Date].CURRENTMEMBER,
[Date Reporting].[Year - Week - Date].[Date]),1)
,[Measures].[Customer Due Amount]);
This will do what I want:
Get the latest date in a selected week and sum the Customer Due Amounts and present it as Due Amount.

Tabular Query to Compare Measures Across Dates

I've got a number of rows and I want to calculate the difference per date.
So say I have the following:
[Date] [Transaction Number] [Value]
1 Jan 16 1 1000
2 Jan 16 1 980
I then want a fact that for every row will compare the value with the measure from the previous date.
So If I have a measure on SUM(Value) for the current date, I basically want SUM(CurrentDate) - SUM(PreviousDate) to see the movement.
A couple of things to note:
There will actually be a couple of comparisons: previous date, previous month end, previous year end.
I want this as a calculated measure not column so that I do not need to filter on the transaction number in the previous period.
What I've tried but it just comes up empty:
Previous Value :=CALCULATE(SUM(Table[Value])) - CALCULATE(SUM(Table[Value]), FILTER(Table, Table[Date] = PreviousDay(Table[Date])))
Unfortunately I cannot tell why your measure didn't work, but following should:
Previous Value := CALCULATE(SUM(Table[Value]) - CALCULATE(SUM(Table[Value]), PREVIOUSDAY(Table[date])))

Data for specific date

My report gets data for the 1st of the current month. Let's say the 1st has still not come then how would I make the report show the data for the 1st of the previous month.
Thanks.
Simply use a select top 1 from your table, filtering by extract(day from yourDateColumn) = 1 to get only the rows with the data for the 1st day of any month, and order them in descending order by your date column (order by yourDateColumn desc), so that you always get the 1st day of the last available month in your table.
Docs for Oracle EXTRACT function

Name Change SQL Query - Positive Attrition

Using SQL Server Management Studio and I've got an Employee table which has records for every time something changes with an employee, be it manager, pay-scale, etc. etc. etc. Whenever a change is made, and EffectiveEndDateKey will be marked for the last time that complete field was relevant and the next record will have the next day as and EffectiveBeginDateKey.
What I'm trying to do is extract the last record an employee had BEFORE they changed their job title in the last month. The goal is IF an employee changes their job title within a given month that would count as "Positive Attrition" and we're trying to figure out how much positive attrition we get in a given month. (It's always for the previous month so in the where statement I'm pulling just changes for the previous month).
Take a look below:
In this example, on July 4th, John Doe went from being an Apple Store Clerk to the next day being a manager, so there was a job title change. What I want is to pull the record in the red box (Jon's last day - in July - when her was an Apple Store Clerk before he became a manager) b/c that tells me that an EffectiveEndDateKey had a change that resulted in a job title change.
So the where statement is going to have a Cast in it to convert the EffectiveEndDateKey to a date and then look at last months data, pulling only records that have EffectiveEndDateKeys from last month (July) and what I need help with is the part where those records must ALSO have a different job title.
if say someone changed job titles on July 31st (so their new job title/EffectiveBeginDateKey was 20130801), that would still count as 1 positive attrition and we'd want to pull the last record from July 31st.
Any thoughts?
You can do this with a self-join:
select eprev.*
from Employee e
Employee eprev
on e.EmployeeId = eprev.EmployeeId and
cast(cast(e.EffectiveBeginDateKey as varchar(255)) as datetime) =
cast(cast(eprev.EffectiveEndDateKey as varchar(255)) as datetime) + 1
where cast(cast(e.EffectiveBeginDateKey as varchar(255)) as datetime) >= dateadd(mm, -1, getdate()) and
eprev.JobTitle <> e.JobTitle;
The key here is the conversion of the number to a datetime. The format YYYYMMDD is easily convertible, when it is a string. So, convert the number of a string first, then to a datetime. The rest is just the mechanics of the join.
Since your strings are effectively just dates in ISO format (yyyymmdd), you don't even have to convert it to datetime, you could just get previous row:
select E.EmployeeID, E.JobTitle, ENEXT.JobTitle as NextJobTitle
from Employee as E
outer apply (
select top 1 T.JobTitle
from Employee as T
where
T.EmployeeID = E.EmployeeID and
T.EffectiveEndDateKey > E.EffectiveEndDateKey
order by T.EffectiveEndDateKey asc
) as ENEXT
where
E.JobTitle <> ENEXT.JobTitle and
E.EffectiveEndDateKey >= convert(nvarchar(8), dateadd(mm, datediff(mm, 0, getdate()) - 1, 0), 112)
see SQL FIDDLE example