How to get similar output in SQL? - sql-server

Year
Month
Quarter
2012
1
1
2012
2
1
2012
3
1
2012
4
2
2012
5
2
2012
6
2
2012
7
3
2012
8
3
2012
9
3
2012
10
4
2012
11
4
2012
12
4
2013
1
5
2013
2
5
2013
3
5
2013
4
6
2013
5
6
2013
6
6
2013
7
7
2013
8
7
2013
9
7
2013
10
8
2013
11
8
2013
12
8
Explanation:
Year column increments by 1 after every 12 rows.
Month column goes till 12 and then resets.
Quarter column increments by 1 after every 3 rows but does not reset after year

Try a tally table to get row numbers and use modulo (%) and integer division to derive the numbers
with cteTally as (
SELECT TOP 100 ROW_NUMBER () OVER (ORDER BY NAME) -1 as MonthNumber
FROM sys.objects
)
SELECT
CEILING(MonthNumber / 12) + 2012 as CalYear
, (MonthNumber % 12) + 1 as CalMonth
, 1 + (MonthNumber / 3) as CalQuarter
FROM cteTally as T
CalYear
CalMonth
CalQuarter
2012
1
1
2012
2
1
2012
3
1
2012
4
2
2012
5
2
2012
6
2
2012
7
3
2012
8
3
2012
9
3
2012
10
4
2012
11
4
2012
12
4
2013
1
5
2013
2
5
2013
3
5
2013
4
6
2013
5
6
2013
6
6
2013
7
7
2013
8
7
2013
9
7
2013
10
8
2013
11
8
2013
12
8

Related

Generate Fiscal Calendar from Calendar Year and a specified Fiscal Month using T-SQL

I am answering posts similar to
How do you generate a fiscal calendar from a calendar year and dynamic fiscal start month with week numbers of the month in T-SQL?
I wanted to show my sql table valued function to generate fiscal calendar details from a calendar year and a specified fiscal month start.
Usage:
SELECT *
FROM [dbo].[GenerateFiscalCalendarFromCalendarYear](YEAR(GETDATE()), 8)
WHERE [CMonth] IN ( 7, 8, 9 )
AND [WeekNumOfMonth]IN(1,2);
CMonth
FMonth
FYear
CFirstDay
CLastDay
Day
WeekNumOfMonth
FYearStartMonth
7
12
2022
2022-07-01
2022-07-31
2022-07-01
1
8
7
12
2022
2022-07-01
2022-07-31
2022-07-02
1
8
7
12
2022
2022-07-01
2022-07-31
2022-07-03
2
8
7
12
2022
2022-07-01
2022-07-31
2022-07-04
2
8
7
12
2022
2022-07-01
2022-07-31
2022-07-05
2
8
7
12
2022
2022-07-01
2022-07-31
2022-07-06
2
8
7
12
2022
2022-07-01
2022-07-31
2022-07-07
2
8
7
12
2022
2022-07-01
2022-07-31
2022-07-08
2
8
7
12
2022
2022-07-01
2022-07-31
2022-07-09
2
8
8
1
2023
2022-08-01
2022-08-31
2022-08-01
1
8
8
1
2023
2022-08-01
2022-08-31
2022-08-02
1
8
8
1
2023
2022-08-01
2022-08-31
2022-08-03
1
8
8
1
2023
2022-08-01
2022-08-31
2022-08-04
1
8
8
1
2023
2022-08-01
2022-08-31
2022-08-05
1
8
8
1
2023
2022-08-01
2022-08-31
2022-08-06
1
8
8
1
2023
2022-08-01
2022-08-31
2022-08-07
2
8
8
1
2023
2022-08-01
2022-08-31
2022-08-08
2
8
8
1
2023
2022-08-01
2022-08-31
2022-08-09
2
8
8
1
2023
2022-08-01
2022-08-31
2022-08-10
2
8
8
1
2023
2022-08-01
2022-08-31
2022-08-11
2
8
8
1
2023
2022-08-01
2022-08-31
2022-08-12
2
8
8
1
2023
2022-08-01
2022-08-31
2022-08-13
2
8
9
2
2023
2022-09-01
2022-09-30
2022-09-01
1
8
9
2
2023
2022-09-01
2022-09-30
2022-09-02
1
8
9
2
2023
2022-09-01
2022-09-30
2022-09-03
1
8
9
2
2023
2022-09-01
2022-09-30
2022-09-04
2
8
9
2
2023
2022-09-01
2022-09-30
2022-09-05
2
8
9
2
2023
2022-09-01
2022-09-30
2022-09-06
2
8
9
2
2023
2022-09-01
2022-09-30
2022-09-07
2
8
9
2
2023
2022-09-01
2022-09-30
2022-09-08
2
8
9
2
2023
2022-09-01
2022-09-30
2022-09-09
2
8
9
2
2023
2022-09-01
2022-09-30
2022-09-10
2
8
-- ================================================
-- Template generated from Template Explorer using:
-- Create Multi-Statement Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
-- =============================================
-- Author: Jay Asbury
-- Create date: 8/5/2022
-- Description: Generates Calendar to Fiscal year Table for a year.
-- =============================================
CREATE FUNCTION [dbo].[GenerateFiscalCalendarFromCalendarYear] (
-- Add the parameters for the function here
#CalendarYear INT,
#FicalYearStartMonth INT)
RETURNS TABLE
--(
---- Add the column definitions for the TABLE variable here
--[CMonth] INT,
--[FMonth] INT,
--[FYear] INT,
--[CFirstDay] DATE,
--[CLastDay] DATE,
--[Day] DATE,
--[WeekNumOfMonth] INT,
--[FYearStartMonth] INT)
AS
RETURN ( WITH [months]
AS (SELECT 1 [CMonth]
UNION ALL
SELECT [CMonth] + 1
FROM [months]
WHERE [months].[CMonth] <= 11),
[Fyears]
AS (SELECT *,
CASE
WHEN [months].[CMonth] >= #FicalYearStartMonth THEN #CalendarYear + 1
ELSE #CalendarYear END [FYear],
#CalendarYear [Cyear]
FROM [months]),
[Ranges]
AS (SELECT *,
DATEFROMPARTS([Fyears].[Cyear], [Fyears].[CMonth], 1) [CFirstDay],
EOMONTH(DATEFROMPARTS([Fyears].[Cyear], [Fyears].[CMonth], 1)) [CLastDay],
MONTH(
DATEADD(
MONTH,
MONTH(DATEFROMPARTS([Fyears].[Cyear], [Fyears].[CMonth], 1)) - #FicalYearStartMonth,
DATEFROMPARTS(
DATEPART(YEAR, DATEFROMPARTS([Fyears].[Cyear], [Fyears].[CMonth], 1)), 1, 1))) [FMonth]
FROM [Fyears]),
[Days]
AS (SELECT [Ranges].[CMonth],
[Ranges].[FMonth],
[Ranges].[FYear],
[Ranges].[CFirstDay],
[Ranges].[CLastDay],
[Ranges].[CFirstDay] [Day]
FROM [Ranges]
UNION ALL
SELECT [Days].[CMonth],
[FMonth],
[Days].[FYear],
[Days].[CFirstDay],
[Days].[CLastDay],
DATEADD(DAY, 1, [Days].[Day]) [Day]
FROM [Days]
WHERE DATEADD(DAY, 1, [Days].[Day]) <= [Days].[CLastDay])
SELECT [Days].[CMonth],
[Days].[FMonth],
[Days].[FYear],
[Days].[CFirstDay],
[Days].[CLastDay],
[Days].[Day],
(DATEPART(WEEK, [Days].[Day]) - DATEPART(WEEK, DATEADD(DAY, 1, EOMONTH([Days].[Day], -1)))) + 1 [WeekNumOfMonth],
#FicalYearStartMonth [FYearStartMonth]
FROM [Days]);
GO
SELECT *
FROM [dbo].[GenerateFiscalCalendarFromCalendarYear](YEAR(GETDATE()), 8)
WHERE [CMonth] IN ( 7, 8, 9 )
AND [WeekNumOfMonth] IN ( 1, 2 );

How to query a SQL Server table with Months Range

I have a SQL Server table that have data store in this format, I want to extract data from March 2014 to Feburary 2015, how can I achieve this, please?
ID Months Years fd1 fd2 fd3 fd4
-------------------------------------------
1 january 2014 1 2 3 8
2 february 2014 2 2 2 5
3 march 2014 2 5 3 5
4 april 2014 3 4 2 2
5 may 2014 4 1 3 2
6 june 2014 1 1 2 2
7 july 2014 2 3 3 4
8 august 2014 5 6 2 5
9 september 2014 12 2 5 6
10 october 2014 11 1 5 3
11 november 2014 1 5 5 7
12 december 2014 3 8 5 8
13 january 2015 4 1 8 1
14 february 2015 5 9 8 4
15 march 2015 6 2 2 5
16 april 2015 6 5 20 2
17 may 2015 6 2 2 2
18 june 2015 9 5 2 2
19 july 2015 10 6 2 2
20 august 2015 12 3 2 5
21 september 2015 55 2 2 2
22 october 2015 1 1 2 5
23 november 2015 3 5 5 2
24 december 2015 2 5 5 5
First you should convert Years and Months columns to a DATE field(Here named as Dates).
Then compare the wanted date on Dates column.
WITH cte(ID, Months, Years, fd1, fd2, fd3, fd4, Dates)
AS
(
SELECT ID, Months, Years, fd1, fd2, fd3, fd4, CAST(CAST(Years AS NVARCHAR) + ' ' + CAST(Months AS NVARCHAR) AS DATE)
FROM tblYearMonth
)
SELECT *
FROM cte
WHERE Dates >= '2014 March' AND Dates <= '2015 February'
ORDER BY Dates
SELECT *
FROM YourTableName
WHERE (MONTH(Months+ ' 1 2014') IN (1,2) AND Years = 2015) OR (MONTH(Months+ ' 1 2014') > 2 AND Years = 2014)

Merge two tables while dragging one column from one table to the other

With Sql Server 2014:
I have two tables - Events and Locations, that share a time column and I need to merge them into one table order by time. In the Events table there is an Event column that I need to place in all the Locations row following that event (time wise), here is an example:
Events:
time event
------------
09:00 2
09:10 3
10:15 1
10:17 2
10:30 3
Locations:
time X Y
-------------
09:01 1 3
09:02 2 3
09:05 4 1
09:09 6 4
09:10 7 8
09:11 8 8
09:12 9 7
10:17 1 2
10:19 5 4
10:20 4 3
10:25 5 4
10:28 3 5
Merged Table:
time X Y event
--------------------
09:00 0 0 2
09:01 1 3 2 <
09:02 2 3 2 <
09:05 4 1 2 <
09:09 6 4 2 <
09:10 0 0 3
09:10 7 8 3 <
09:11 8 8 3 <
09:12 9 7 3 <
10:15 0 0 1
10:17 0 0 2
10:17 1 2 2 <
10:19 5 4 2 <
10:20 4 3 2 <
10:25 5 4 2 <
10:28 3 5 2 <
10:30 0 0 3
The elements that mark with '<' are the inserted Events.
Any ideas and help on how to perform this task is welcome.
You can use UNION ALL and APPLY:
SQL Fiddle
SELECT
[Time], X = 0, Y = 0, [Event]
FROM [Events]
UNION ALL
SELECT l.*, x.Event
FROM Locations l
CROSS APPLY(
SELECT TOP 1 *
FROM [Events]
WHERE [Time] <= l.[Time]
ORDER BY [Time] DESC
)x
ORDER BY [Time]

Azure SQL - Calculate stock level based on previous date

I have transactions table that looks like below. I need to create a stored procedure that will calculate the StockLevel column.
The dates are consecutive (always 1 day gap) and the formula is :
StockLevel = yesterdayStockLevel + AIn - AOut
Sample data:
Date | ArticleId | AOut | AIn | StockLevel
Aug 1 1 1 10 10 - 1 = 9
Aug 2 1 2 0 9 - 2 = 7
Aug 3 1 1 0 7 - 1 = 6
Aug 4 1 2 0 6 - 2 = 4
Aug 5 1 3 5 4 - 3 + 5 = 6
Aug 6 1 0 0 6 - 0 = 6
I prefer to run it on SQL (if possible) and not use cursor (if possible).
THANKS!!
Try this :
Select Date, ArticleID, AOut, AIn, AInSum-AOutSum as StockLevel
From (
Select *,
sum(AOut)over (Partition by ArticleId Order by Date) AOutSum,
sum(AIn)over (Partition by ArticleId Order by Date) AInSum
From yourTabble
) X

sql server - help in query

I have a table called employee_salary_master in which we keep salary effective dates and entry dates of each employee in the company.
For processing salary of an employee for a month, we need to fetch most recently entered record. If the effective date is less than
that month then we pick the most recent entry. but if effective date is greater than the first date of the month, then there will be more than
one effective dates for that month
example: the data for an employee is as below.
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
1 5814 Jan 6 2006 Jan 12 2006
2 5814 Jan 10 2006 Jul 17 2006
3 5814 Jan 20 2006 Dec 22 2006
4 5814 May 10 2007 Jul 18 2007
5 5814 Nov 1 2007 Dec 18 2007
6 5814 Aug 1 2008 Aug 20 2008
7 5814 May 1 2008 Sep 2 2008
8 5814 Sep 1 2009 Sep 18 2008
9 5814 Nov 1 2008 Apr 20 2009
10 5814 Nov 10 2009 Nov 25 2009
11 5814 Nov 5 2009 Nov 26 2009
If i need to get the record for Nov 2009, I write the query below
select EMPLOYEE_SALARY_MASTER_ID, EMPLOYEE_ID, EFFECTIVE_DATE, ENTRY_DATE, ARREAR_PROCESS_FLAG from employee_salary_master esm where employee_id = 5814
and (esm.entry_date = (select max(entry_date) from employee_salary_master where employee_id = 5814 and effective_date <= #monthfirstdate)
or (esm.effective_date between #monthfirstdate and #monthlastdate))
which gives the result below..
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
9 5814 Nov 1 2008 Apr 20 2009
10 5814 Nov 10 2009 Nov 25 2009
11 5814 Nov 5 2009 Nov 26 2009
What I need is as follows...
For Nov 1 - Nov 4, salary should be processed as per employee_salary_masterId - 9 and
Nov 5 - Nov 30, salary should be processed as per employee_salary_masterId - 11.
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
9 5814 Nov 1 2008 Apr 20 2009
11 5814 Nov 5 2009 Nov 26 2009
Please help me build this query.
Not quite sure if I understand you completely correct, but have a look at this example
DECLARE #employee_salary_master TABLE(
EMPLOYEE_SALARY_MASTER_ID INT,
EMPLOYEE_ID INT,
EFFECTIVE_DATE DATETIME,
ENTRY_DATE DATETIME
)
INSERT INTO #employee_salary_master SELECT 1,5814,'Jan 6 2006','Jan 12 2006'
INSERT INTO #employee_salary_master SELECT 2,5814,'Jan 10 2006','Jul 17 2006'
INSERT INTO #employee_salary_master SELECT 3,5814,'Jan 20 2006','Dec 22 2006'
INSERT INTO #employee_salary_master SELECT 4,5814,'May 10 2007','Jul 18 2007'
INSERT INTO #employee_salary_master SELECT 5,5814,'Nov 1 2007','Dec 18 2007'
INSERT INTO #employee_salary_master SELECT 6,5814,'Aug 1 2008','Aug 20 2008'
INSERT INTO #employee_salary_master SELECT 7,5814,'May 1 2008','Sep 2 2008'
INSERT INTO #employee_salary_master SELECT 8,5814,'Sep 1 2009','Sep 18 2008'
INSERT INTO #employee_salary_master SELECT 9,5814,'Nov 1 2008','Apr 20 2009'
INSERT INTO #employee_salary_master SELECT 10,5814,'Nov 10 2009','Nov 25 2009'
INSERT INTO #employee_salary_master SELECT 11,5814,'Nov 5 2009','Nov 26 2009'
DECLARE #monthfirstdate DATETIME,
#monthlastdate DATETIME
SELECT #monthfirstdate = '01 Nov 2009',
#monthlastdate = '30 Nov 2009'
SELECt *
FROM (
SELECT TOP 1
*
FROM #employee_salary_master esm
WHERE esm.EFFECTIVE_DATE BETWEEN #monthfirstdate and #monthlastdate
AND esm.EFFECTIVE_DATE < esm.ENTRY_DATE
ORDER BY esm.ENTRY_DATE DESC
) sub
UNION ALL
SELECT *
FROM (
SELECT TOP 1
*
FROM #employee_salary_master esm
WHERE esm.EFFECTIVE_DATE <= #monthfirstdate
AND esm.EFFECTIVE_DATE < esm.ENTRY_DATE
ORDER BY esm.EFFECTIVE_DATE DESC
) sub
ORDER BY EFFECTIVE_DATE
The rule for excluding nov 10 record is that we need to filter out those records for the month of November in which entry_date is greater but effective_date is smaller.
Let me explain you wrt data provided:
As a rule, the latest/last entry will take precedence over the previous entries in a particular month which implies that the entrty date of Nov 26 2009 would take precedence over the entry date of Nov 25 2009. Now since effective date of SAL_MATSER_ID - 10 is greater than that of SAL_MATSER_ID - 11, hence Nov 10 record would be nullified.
Had the data been like the below, all the 3 records would have been used for salary processing.
SAL_MATSER_ID - 9 for salary of Nov 1 - Nov 9
SAL_MATSER_ID - 10 for salary of Nov 10 - Nov 14
SAL_MATSER_ID - 11 for salary of Nov 15 - Nov 30
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
9 5814 Nov 1 2008 Apr 20 2009
10 5814 Nov 10 2009 Nov 25 2009
11 5814 Nov 15 2009 Nov 26 2009
But since SAL_MATSER_ID - 11 is applicable from 5th Nov. onwards, the previous record is nullified. I hope this explains the situation.
Thanks for your support,
Sweta

Resources