Sometime, we are asked to write a query where we have to get a result set based on current financial year. Let's say my financial year begin 1st of November ever year and end 31st October.
And we have to do a select between these two date ranges.
Here's how we do it.
Select * from mytable
where myfinancialdate >= CASE WHEN month(getdate()) > 11
THEN convert(datetime, cast(year(getdate()) as varchar) + '-11-1')
ELSE convert(datetime, cast(year(getdate()) - 1 as varchar) + '-11-1') end
AND (myfinancialdate <= CASE WHEN month(getdate()) > 11
THEN convert(datetime, cast(year(getdate()) + 1 as varchar) + '-10-31')
ELSE convert(datetime, cast(year(getdate()) as varchar) + '-10-31') end);
This will result set only following between current financial year. For example in 2014 year, the result will bring value from 11-1-2013 to 10-31-2014.
Let say that we want to retrieve all the data from last fiscal year and current fiscal year. To do this, we can use this approach:
Let's declare some variable.
--Fiscal year variables
, @Now datetime = getdate()
, @CurrentFiscalYear_BeginDate date
, @PastFiscalYear_BeginDate date
, @CurrentFiscalYear_EndDate date
--Set Fiscal year values
select @CurrentFiscalYear_BeginDate = '11/1/' + cast(case when month(@Now) > 11 then year(@Now) else year(@Now)-1 end as varchar(4))
select
@PastFiscalYear_BeginDate = dateadd(year, -1, @CurrentFiscalYear_BeginDate) --decrement year of current FY begin date to get past FY begin date
, @CurrentFiscalYear_EndDate = dateadd(day, -1, dateadd(year, 1, @CurrentFiscalYear_BeginDate)) --increment year of current FY begin date to get next FY begin date and then subtract one day to get end date of current FY
Now we can use these variable to get data.
Select * from mytable
Where myfinancialdate
between @PastFiscalYear_BeginDate AND @CurrentFiscalYear_EndDate
This will retrieve all the row between last fiscal year and current fiscal year.
Showing posts with label SQL query. Show all posts
Showing posts with label SQL query. Show all posts
Wednesday, February 26, 2014
SQL: Get Current Financial Year dynamically in Select Statement.
Labels:
dynamic sql to get financial year,
financial year,
fiscal year in select,
multiple fiscal year where condition,
SQL query
Friday, April 19, 2013
SQL: Intersect, Except and Union
Let's say we have Table A with ID Column and its values are (1,2,3,4,5) and Table B with its ID Column and values (3,4,5,6,7).
Let's say we want to write different queries giving us something like this:
Find me all the value which common in both table? (3,4,5)
Find me all unique value in both table? (1,2,3,4,5,6,7)
Find me all values which are unique in table A and that are also not in Table B (1,2)
Find me all values which are unique in both Table (1,2,6,7)
Fire your SQL Server and let's create these table and insert data in them and see how it work.
CREATE Table TableA
(ID int);
GO
INSERT INTO TableA VALUES (1),(2),(3),(4),(5);
GO
CREATE Table TableB
(ID int);
GO
INSERT INTO TableB VALUES (3),(4),(5),(6),(7);
Find me all the value which common in both table? (3,4,5)
To find values in both table, take a look at the picture above. The question ask: what are the values which are common to both table? What word comes to your mind? Intersect? right.. there is keyword "INTERSECT" in sql language.
Try this
SELECT * FROM TableA
INTERSECT
SELECT * FROM TableB;
Let's say we want to write different queries giving us something like this:
Find me all the value which common in both table? (3,4,5)
Find me all unique value in both table? (1,2,3,4,5,6,7)
Find me all values which are unique in table A and that are also not in Table B (1,2)
Find me all values which are unique in both Table (1,2,6,7)
Fire your SQL Server and let's create these table and insert data in them and see how it work.
CREATE Table TableA
(ID int);
GO
INSERT INTO TableA VALUES (1),(2),(3),(4),(5);
GO
CREATE Table TableB
(ID int);
GO
INSERT INTO TableB VALUES (3),(4),(5),(6),(7);
Find me all the value which common in both table? (3,4,5)
To find values in both table, take a look at the picture above. The question ask: what are the values which are common to both table? What word comes to your mind? Intersect? right.. there is keyword "INTERSECT" in sql language.
Try this
SELECT * FROM TableA
INTERSECT
SELECT * FROM TableB;
Result
ID
3
4
5
Find me all unique value in both table? (1,2,3,4,5,6,7)
To find value which are unique in both table, take a look at picture above again. think about it. programming and database is all about logic.
Select * from Table A
Union
Select * from Table B
Result
ID
1
2
3
4
5
6
7
If you do UNION ALL, we will get repeat value of common number in both table like this.
ID
1
2
3
4
5
3
4
5
6
7
Find me all values which are unique in table A and that are also not in Table B (1,2)
In this case we have to use EXCEPT keyword.
SELECT * FROM TableA
EXCEPT
SELECT * FROM TableB;
Result
ID
1
2
Find me all values which are unique in both Table (1,2,6,7)
For this query we have to really think hard.. think about UNION and EXCEPT combining somehow?
SELECT * FROM TableA
Union
Select * FROM TableB
EXCEPT
SELECT * FROM TableA
INTERSECT
SELECT * FROM TableB;
Result
ID
1
2
6
7
Hope this help you.
Few things to remember about these queries are:
The basic rules for combining the result sets of two queries that use EXCEPT or INTERSECT are the following:
1. The number and the order of the columns must be the same in all queries.
2. The data types must be compatible.
IF you don't follow this rule, will will get error message.
Cheers!!!
Subscribe to:
Posts (Atom)
