Bigquery SQL - Exclude rows/data for current Financial Year - sql

I have a query in which I want to add a WHERE clause to exclude data for current Financial Year (without hardcoding it)
Note: FinancialYear 1st July to 30 June
SELECT * from TABLE A
WHERE FinancialYear != Current_Financial_Year
Based on a date column I have extracted Financial Year as below but not sure how to check if its current financial year and then exclude it using the WHERE clause above
extract(year from date_add(CalendarDate, interval 6 month)) as FinancialYear

You can compare to the current financial year by using similar logic on current_date:
where extract(year from date_add(CalendarDate, interval 6 month)) <>
extract(year from date_add(Current_Date, interval 6 month))

The best approach would be to use the following query
select * from Table_A where FinancialYear NOT LIKE '%2021%'
All you need to change is the year and you will get the respective data :)
Regards :)

Related

Dynamic scheduled query based on timestamp

I am trying to set up a scheduled query to run on the 1st of each month, and capture one month of data. However it should not be the previous month, but 2 months previous - due to delays in data being loaded in to the source table. The source table is partitioned by day on session_timestamp so refining this as much as possible will be of benefit to reducing query cost.
So far I have this:
WHERE
EXTRACT(YEAR
FROM
session_timestamp) = EXTRACT(YEAR
FROM
DATE_SUB(CURRENT_DATE, INTERVAL 2 MONTH))
AND EXTRACT(MONTH
FROM
session_timestamp) = EXTRACT(MONTH
FROM
DATE_SUB(CURRENT_DATE, INTERVAL 2 MONTH))
This seems a highly inelegant solution but was intended to address cases where a year boundary would be crossed. However I can see from the "This script will process * when run." area that this is going to query everything in 2020 and not just in May 2020.
As you have pointed out, your query doesn't engage partition filter down to the 2 months of data which you want to query.
You don't have to do the year trick because DATE_TRUNC(..., MONTH) has year in it. Please try filter below:
-- Last day of the month
DATE(session_timestamp) <= DATE_SUB(DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), MONTH), INTERVAL 1 DAY)
AND
-- First day of the month
DATE(session_timestamp) >= DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 2 MONTH), MONTH)

Previous year Year To Date with partial current year Year To Date Calculation SQL Server

I have a revenue table with data for last year and current year. I need to calculate the YTD last year and YTD current year, BUT I need to only consider data from min(date) from last year PER branch for current year YTD calculation.
eg: Branch KTM has data from 2018-02-25 not from Jan 1st.
Now I want to get YTD for the current year from the same date on 2019 till today.
I am able to get whole YTD for last year and this year, and also the minimum date/weeknumber for each branch for last year, but unable to calculated partial YTD for the current year.
Here is one drive link to mydata and sql : https://1drv.ms/u/s!Ave_-9o8DQVEgRS7FaJmm48UNsWz?e=lRfOJF
A snippet from my code
I need help with the SQL query to do this.
This returns the number of days between the same day-of-year of a last year's date and today's date:
select current_date - (date'2018-02-25' + interval '1' year); -- PostgreSQL
select datediff(current_date, (date'2018-02-25' + interval '1' year)); -- MySQL
Alternative version:
select extract(doy from current_date) - extract(doy from date'2018-02-25'); -- PostgreSQL
doy stands for day of year. At the time of the answer (2019-09-24) all queries return 211.
To sum values in that date range, use BETWEEN:
SELECT sum(revenue)
FROM your_table
WHERE date BETWEEN date'2018-02-25' + interval '1' year AND current_date

How to take aggregate of a data Grouped by Financial Year in SQL

My Financial Year is between 1st July to 30th June e.g 2018-07-01 and 2019-06-30
I have following data.
i want to aggregate of Rate column by financial year in sql
please help.
You can get the financial year by either subtracting or adding six months.
In ANSI/ISO standard SQL, this looks like:
select extract(year from fromDate + interval '6 month') as fiscal_year
sum(rate)
from t
group by extract(year from fromDate + interval '6 month');
This uses standard SQL -- which ironically does not work in most databases. Date functions are notoriously database-specific, so the exact code might differ in your database (which is unspecified as I write this).
If I correctly understood what you're looking for, this should work for you.
SELECT
SUM(Rate) as Rate,
CASE WHEN MONTH(FromDate) < 7 THEN YEAR(FromDate) - 1 ELSE YEAR(FromDate) END AS Fiscal_Year
FROM #t
GROUP BY CASE WHEN MONTH(FromDate) < 7 THEN YEAR(FromDate) - 1 ELSE YEAR(FromDate) END

Subtract current Year from a SQL table

How to subtract the current year from a table.
I have a table with a column VehicleYear. So I need to subtract only the current year from the VehicleYear to get the VehicleAge.
I tried the following query but it did not work since the SYSDATE seems to reflect the date when the row was added.
SELECT V. VEHICLENAME, (SYSDATE - V.VEHICLEYEAR) AS CURRENTAGE
FROM VEHICLE V
You can run this query to subtract year from two dates
SELECT VEHICLENAME,
EXTRACT(YEAR FROM sysdate) - EXTRACT(YEAR FROM VehicleYear) AS CURRENTAGE
FROM VEHICLE;
SQL Fiddle DEMO

Sql daily comparison by month

I am writing a report for work which requires that I compare the amount of students dropped on a daily basis, what I mean is the report needs to show that on today the 5th of august X amounts of students dropped from the 1st to the 5th compared to X amount which dropped within the 1st to the 5th of July and so on for each Month of the year. Can anyone please help me by providing me with a query which I can use to have that info? thanks.
You want to compare the first number of days from a month. The following query gives you an example:
select yr, mon, count(*)
from (select extract(year from date) as yr, extract(month from date) as mon,
extract(day from date) as day
from t
) t
where day <= extract(day from now())
group by yr, mon
However, the exact syntax may depend on your database. For instance, the current date may be now(), getdate(), system_date or something else. Some databases don't support the extract, but most have a way to get the month and day of month.