Default prompt for date using month names from month numbers in OBIEE 11G analysis - sql

Our database has a field for months as periods 1,2,3, . . . and a field for years. I would like the prompt to have month names instead of digits and I would like to have a prompt for my analysis that defaults the prior month from the time the report was run. I have year and month working in digit format(works for every month except if run in January) but I can't seem to get a solution that works with month names defaulting to the prior month.
The months need to be Year to Date, so <= the prior month.
I am in accounting so it is to get the ledger for the last day of the prior month. Each period month has just the activity for that month, so Prior year is 0, Current year activity is <>0 and the balance as of the month is 0 through the prior month.
I want users to be able to change the selections if they need but have defaults selected that they will use 99% of the time.
Right now, with the period numbers I was using
SELECT "Date"."Fiscal Period Nbr"
FROM "General Ledger"
WHERE "Date"."Fiscal Period Nbr" = (VALUEOF(Current_Month) - 1)
And for the years
SELECT "Date"."Fiscal Yr Nbr" FROM "General Ledger"
WHERE "Date"."Fiscal Yr Nbr" = (VALUEOF(Current_Year))
I created a group for each month selecting the periods, so January is 0,1 and February is 0,1,2 and so on. In the prompt I select those 12 groups and was trying to do some sort of SQL for the Default selection.

Related

I am trying to get a month and year to date column in SQL

I am trying to create a month and year to date column but the FY starts from October and I need it to roll into the next year so this can automatically update in Power Bi I have the Date ID for our year now maybe need flags to show the current month and current year then I can create a measure in Power Bi, also need it to sum up the months for year to date etc if I click january it shows me sales from October to January.

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.

Ms ACCESS: calculating past annual averages over varying date ranges

In a form on Ms ACCESS, a user can select a commodity (such as copper, nickel, etc.) from a list and a commodity price date from a list. A trailing 12 month average commodity price should then be calculated.
For example: the user selects Copper as commodity and February 1st 2010, 02/01/2010. I then want the average price to be calculated over the time period: [02/01/2009 - 02/01/2010].
I'm not sure how to write this in query form. This is the current incomplete code;
SELECT Avg(CommPrices.Price) AS Expr1,
FROM CommPrices
WHERE (((CommPrices.Commodity)=[Forms]![Tool Should Cost]![List243]))
AND CommPrices.DateComm = [Forms]![Tool Should Cost]![List55];
List243 is the list of commodities the user can select from, list55 is the list of dates the user can select. All data is obtained from the table CommPrices.
Note: the earliest dates in the column DateComm is 01/01/2008. So if the user selects a date for example 02/01/2008, then calculating the average over the past 12 months before 02/01/2008 won't be possible. I do want the code to still calculate the average using the dates available. (in the example it would just be the average over the past month)
Second Note: the column DateComm only has monthly dates for the first day of every month (e.g 01/01/2008, 02/01/2008, 03/01/2008). The dates listed in list55 can refer to different days in the month (e.g 03/16/2009), in that case I want the code to still calculate the past 12 month average using the closest commodity dates possible. So if the user selects date 03/16/2009, I want the code to calculate the 12 month average for 03/01/2008 - 03/01/2009.
For "integer" months it would be:
SELECT
Avg(CommPrices.Price) AS AveragePrice,
FROM
CommPrices
WHERE
CommPrices.Commodity=[Forms]![Tool Should Cost]![List243]
AND
CommPrices.DateComm = BETWEEN
DateSerial(Year([Forms]![Tool Should Cost]![List55]) - 1, Month([Forms]![Tool Should Cost]![List55]), 1)
AND
DateSerial(Year([Forms]![Tool Should Cost]![List55]), Month([Forms]![Tool Should Cost]![List55]), 1)

Oracle Week Number from a Date

I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;

SQL Statement to return previous quarter balance

I'm trying to create a query that can return the previous quarter balance for a series of records.
I have financial data for accountid (acctid), fiscal year (fyear), fiscal quarter (fquarter) and fiscal period (fperiod) that I'm summing and tracking through a series of other queries that I'm dropping in to a temporary table, that data includes the net change for the period (nperiod), net change for the quarter (nquarter) and net change for the year (nyear). Net change for the period is only the account transactions in the period, net change for the quarter is the cumulative total of the transactions that appear in periods 1-3, 4-6, 7-9 and 10-12 respectively (amount from previous periods are not calculated in proceeding quarters, ie the net change for the quarter resets to $0 for periods 4, 7 and 9) and the net change for the year is the total cumulative sum. I'm now trying to create a sql statement that returns the previous quarter end balance.
So for periods 1-3 I need to write a separate select statements to return the ending balance for the previous year, for periods 4-6 I want them ALL to return the net change for the quarter from period 3, for 7-9 I want to return the net change for the quarter from period 6 for all records and for period 10-12 I want to return net change for the quarter from period 9.
Can I get some assistance because I have a gigantic query that returns the max period per quarter, then the nquarter amount associated with that period and then trying to do a where exists, but something tells me there's a better way to do it.
Thanks!