GET current month plus last 13 month SQL - sql

I would like to modify the query which will also include the data from this month.
SELECT TOP (14) Entity = 'Total_Group'
, Scenario = 'Actual'
, Date = TRIM(CAST(YEAR(DATEADD(MONTH, ROW_NUMBER() OVER(ORDER BY object_id) * -1, GETDATE())) as CHAR(4))
+ ' P'
+ CAST(MONTH(DATEADD(MONTH, ROW_NUMBER() OVER(ORDER BY object_id) * -1, GETDATE())) as VARCHAR(2)))
FROM [sys].[all_objects]
the result of this table
starts with 2022P12, but would like to include current month too so it should start with 2023P1

Try this:
SELECT TOP (14) Entity = 'Total_Group'
, Scenario = 'Actual'
, Date = TRIM(CAST(YEAR(DATEADD(MONTH, ROW_NUMBER() OVER(ORDER BY object_id) * -1 + 1, GETDATE())) as CHAR(4))
+ ' P'
+ CAST(MONTH(DATEADD(MONTH, ROW_NUMBER() OVER(ORDER BY object_id) * -1 + 1, GETDATE())) as VARCHAR(2)))
FROM [sys].[all_objects]

Related

How do you do a where clause with a select statement as a value

I'm trying to get the total amount from the next month's record.
SELECT TOP (1000)
[DEMAND_POINT_ID],
[DPM_XREF_ID],
[UTIL_TYPE],
[ACCOUNT_ID],
[REV_YR_MNTH],
[TOTAL_AMOUNT],
(SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int)) AS nextmonth,
(SELECT de2.[TOTAL_AMOUNT]
FROM [DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de2
WHERE de2.ACCOUNT_ID = de.ACCOUNT_ID
AND de2.REV_YR_MNTH = (SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int))) AS nextamount
FROM
[DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de
WHERE
ACCOUNT_ID =1
ORDER BY
ACCOUNT_ID, de.REV_YR_MNTH
The value nextmonth creates the next month's rev_yr_mnth correctly however when I try to use this created value in the where clause it brings back nothing in the nextamount field.
It's like it does not recognize the created value as a true value. What do I need to do to have the where clause recognize the value?
Using CTE you can add condition on nextmonth.
With CTE As (
SELECT TOP (1000)
[DEMAND_POINT_ID],
[DPM_XREF_ID],
[UTIL_TYPE],
[ACCOUNT_ID],
[REV_YR_MNTH],
[TOTAL_AMOUNT],
(SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int)) AS nextmonth,
(SELECT de2.[TOTAL_AMOUNT]
FROM [DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de2
WHERE de2.ACCOUNT_ID = de.ACCOUNT_ID
AND de2.REV_YR_MNTH = (SELECT CAST(LEFT((SELECT CONVERT(varchar, DATEADD(month, 1, LEFT(REV_YR_MNTH, 4) + '-' + RIGHT(REV_YR_MNTH, 2) + '-' + '01'), 112)), 6) AS int))) AS nextamount
FROM
[DEQEnergyUsage].[dbo].[DST_ENERGY_USAGE_AGGR] de
WHERE
ACCOUNT_ID =1
ORDER BY
ACCOUNT_ID, de.REV_YR_MNTH
)
select * from CTE
-- Where nextmonth condition

Inconsistent results with CTE (Recursion)

I am pulling my hair with this one!
I have a Stored Procedure that runs everyday:
-- DECLARING DATE VARIABLES
DECLARE #FirstDay date
-- SETTING UP INITIAL VALUES TO DATE VARIABLES
SET #FirstDay = (SELECT MIN(WK_Start) AS FirstDay FROM [TML_RDB].[COMPANY].[COMPANY_CALENDAR]);
-- CREATE DATE RANGE FROM #FIRST DAY
WITH DateRange AS (
SELECT #FirstDay AS [Date]
UNION ALL
SELECT DATEADD("dd",1,[Date]) FROM DateRange
WHERE [Date] < DATEADD("dd",-1,DATEFROMPARTS(YEAR(GETDATE())+2,3,1))
)
SELECT CAST(CONCAT(YEAR(DR.[Date]),FORMAT(MONTH(DR.[Date]),'00'),FORMAT(DAY(DR.[Date]),'00')) AS int) AS [DateKey]
,DR.[Date]
,DATENAME(DW,DR.[Date]) AS [DayOfWeekName]
,CONCAT(RIGHT(YEAR(DR.[Date]),2), FORMAT(DATEPART(WK, DATEADD("dd",-1,DR.[Date])),'00')) AS [CalendarWeek]
,CONCAT('W', RIGHT(YEAR(DR.[Date]),2), FORMAT(DATEPART(WK, DATEADD("dd",-1,DR.[Date])), '00')) AS [CalendarWeekName]
,(DATEPART(DW, DR.[Date])+5) % 7 + 1 AS [DayOfCalendarWeek]
--,DATEPART(WK, DATEADD("dd",-1,DR.[Date])) AS [CalendarWeek]
,MONTH(DR.[Date]) AS [CalendarMonth]
,DATENAME(MM, DR.[Date]) AS [CalendarMonthName]
,DATEPART(DD,DR.[Date]) AS [DayOfCalendarMonth]
,DATEPART(QQ,DR.[Date]) AS [CalendarQuarter]
,CONCAT('Q', DATEPART(QQ,DR.[Date])) AS [CalendarQuarterName]
,ROW_NUMBER() OVER (PARTITION BY YEAR(DR.[Date]), DATEPART(QQ,DR.[Date]) ORDER BY DR.[Date]) AS [DayOfCalendarQuarter]
,CASE
WHEN MONTH(DR.[Date]) < 7 THEN 1
ELSE 2
END AS [CalendarHalf]
,CASE
WHEN MONTH(DR.[Date]) < 7 THEN CONCAT('H',1)
ELSE CONCAT('H',2)
END AS [CalendarHalfName]
,CASE
WHEN YEAR(DR.[Date]) = 2009
THEN ROW_NUMBER() OVER (PARTITION BY YEAR(DR.[Date]), CASE WHEN MONTH(DR.[Date]) < 7 THEN 1 ELSE 2 END ORDER BY YEAR(DR.[Date]), CASE WHEN MONTH(DR.[Date]) < 7 THEN 1 ELSE 2 END) + DATEDIFF(DD,DATEFROMPARTS(YEAR(DR.[Date]),1,1),DATEFROMPARTS(YEAR(DR.[Date]),3,1))
ELSE ROW_NUMBER() OVER (PARTITION BY YEAR(DR.[Date]), CASE WHEN MONTH(DR.[Date]) < 7 THEN 1 ELSE 2 END ORDER BY YEAR(DR.[Date]), CASE WHEN MONTH(DR.[Date]) < 7 THEN 1 ELSE 2 END)
END AS [DayOfCalendarHalf]
,YEAR(DR.[Date]) AS [CalendarYear]
,CONCAT('Y', YEAR(DR.[Date])) AS [CalendarYearName]
,CASE
WHEN YEAR(DR.[Date]) = 2009
THEN ROW_NUMBER() OVER (PARTITION BY YEAR(DR.[Date]) ORDER BY DR.[Date]) + DATEDIFF(DD,DATEFROMPARTS(YEAR(DR.[Date]),1,1),DATEFROMPARTS(YEAR(DR.[Date]),3,1))
ELSE ROW_NUMBER() OVER (PARTITION BY YEAR(DR.[Date]) ORDER BY DR.[Date])
END AS [DayOfCalendarYear]
,CC.[WK_Code] AS [FiscalWeek]
,'W' + CC.[WK_Code] AS [FiscalWeekName]
,DATEPART(DW,DR.[Date]) AS [DayOfFiscalWeek]
,CC.[Sun_Period] AS [FiscalPeriod]
,'P' + CC.[Sun_Period] AS [FiscalPeriodName]
,ROW_NUMBER() OVER (PARTITION BY CC.[Sun_Period] ORDER BY CC.[Sun_Period]) AS [DayOfFiscalPeriod]
,CONCAT(LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/3 ) + 1,'00')) AS [FiscalQuater]
,CONCAT('Q', LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/3 ) + 1,'00')) AS [FiscalQuaterName]
,ROW_NUMBER() OVER (PARTITION BY CONCAT(LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/3 ) + 1,'00')) ORDER BY CONCAT(LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/3 ) + 1,'00'))) AS [DayOfFiscalQuarter]
,CONCAT(LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/6 ) + 1,'00')) AS [FiscalHalf]
,CONCAT('H', LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/6 ) + 1,'00')) AS [FiscalHalfName]
,ROW_NUMBER() OVER (PARTITION BY CONCAT(LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/6 ) + 1,'00')) ORDER BY CONCAT(LEFT(CC.[Sun_Period],2), FORMAT(CEILING(((CAST(Right(CC.[Sun_Period],2) AS int)) - 1)/6 ) + 1,'00'))) AS [DayOfFiscalHalf]
,CONCAT(LEFT(YEAR(DR.[Date]),2), LEFT(CC.[Sun_Period],2)) AS [FiscalYear]
,CONCAT('Y', LEFT(YEAR(DR.[Date]),2), LEFT(CC.[Sun_Period],2)) AS [FiscalYearName]
,ROW_NUMBER() OVER (PARTITION BY CONCAT(LEFT(YEAR(DR.[Date]),2), LEFT(CC.[Sun_Period],2)) ORDER BY CONCAT(LEFT(YEAR(DR.[Date]),2), LEFT(CC.[Sun_Period],2))) AS [DayOfFiscalYear]
FROM DateRange DR
INNER JOIN [TML_RDB].[COMPANY].[COMPANY_CALENDAR] CC
ON DR.[Date] >= CC.[WK_Start] AND DR.[Date] <= CC.[WK_End]
OPTION (MAXRECURSION 10000)
When the above query is run using the Stored Procedure, i get the an output which is incorrect:
This count should start again from 1, not 217.
Now, what is weird is when i run this query manually i get the below result which is correct:
Some important infomration:
This query on some occasions returns the correct results, other times it does not.
The correct result is returned why i reduce the number of rows (Years)
This is to do with the Recursion but cannot undertand it fully.
Any assistance would be much appreciated.
Thanks,

Get Last date of month SQL

I need to find the last day of a month in the american (mm-dd-yyyy) format for a column which has yymm(nvarchar format).
example:- for 1601---> 01-31-2016
Thank you for help!
Using convert to get the date of the 1st of the month, then dateadd to get the next month, and one more dateadd to get one day before:
DECLARE #D char(4) = '1601'
SELECT DATEADD(DAY, -1, DATEADD(MONTH, 1, CONVERT(date, #D + '01', 12)))
Result:
2016-01-31
With a string of 1601, we just need to append a 01 because the century will be assumed. This new string of 160101 can be converted into a date.
Example
select convert(varchar(10),dateadd(day,-1,dateadd(month,1,YourCol+'01')),101)
From YourTable
Returns
01/31/2016
Since you said year 20XX...
declare #oddDate nvarchar(4) = '1601'
select
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, '20' + cast(YY as varchar(2)) + cast(MM as varchar(2)) + '01') + 1, 0)) as LastDayof20Year
,'20' + cast(YY as varchar(2)) + cast(MM as varchar(2)) + '01' as MadeUpDate
from
(select
left(#oddDate,2) as YY
,right(#oddDate,2) as MM) x
Or simply...
select
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, '20' + cast(left(#oddDate,2) as varchar(2)) + cast(right(#oddDate,2) as varchar(2)) + '01') + 1, 0)) as LastDayof20Year
Something like the following should do the trick...
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
DROP TABLE #TestData;
CREATE TABLE #TestData (
CharDate CHAR(4) NOT NULL
);
INSERT #TestData (CharDate) VALUES
('9801'), ('9902'), ('0012'), ('0202'), ('1005'), ('1503');
--============================================================
SELECT
FormattedEOM = CONVERT(CHAR(10), em.EndOfMonth, 101)
FROM
#TestData td
CROSS APPLY ( VALUES (CASE WHEN CAST(LEFT(td.CharDate, 2) AS INT) > 30 THEN '19' ELSE '20' END) ) c (Century)
CROSS APPLY ( VALUES (DATEFROMPARTS(CONCAT(c.Century, LEFT(td.CharDate, 2)), RIGHT(td.CharDate, 2), 1)) ) fm (FirstOfMonth)
CROSS APPLY ( VALUES (EOMONTH(fm.FirstOfMonth)) ) em (EndOfMonth);
HTH,
Jason
Edit: The following should work with 2008....
SELECT
FormattedEOM = CONVERT(CHAR(10), em.EndOfMonth, 101)
FROM
#TestData td
CROSS APPLY ( VALUES (CASE WHEN CAST(LEFT(td.CharDate, 2) AS INT) > 30 THEN '19' ELSE '20' END) ) c (Century)
CROSS APPLY ( VALUES (CAST(c.Century + td.CharDate + '01' AS DATE)) ) fm (FirstOfMonth)
CROSS APPLY ( VALUES (DATEADD(mm, 1, fm.FirstOfMonth)) ) nm (NextMonth)
CROSS APPLY ( VALUES (DATEADD(dd, -1, nm.NextMonth)) ) em (EndOfMonth);

MS SQL distinct function

I have the query below
SELECT distinct
count(patient.id)
CAST(YEAR(completiondate) AS VARCHAR(4)) + '-' + CAST(MONTH(completiondate) AS VARCHAR(2)) AS Mjesec
FROM ....
WHERE
incident.completionDate Between Convert(smalldatetime, '01/10/2007', 103) and Convert(smalldatetime, '01/11/2013', 103)
and servicecharges.serviceid in (47)
and incident.status != 6
and ServiceRequestDescription LIKE 'something'
and chargeDescr = 'test'
group by CAST(YEAR(completiondate) AS VARCHAR(4)) + '-' + CAST(MONTH(completiondate) AS VARCHAR(2))
the result i take, have unique rows of patient.id AND Year-Month. That means that i can have multiple rows with the same patient.id and several year-month.
how can i change that query, in order to take the results, with each patient.id only once, with the first completiondate with the specific elements?
I guess it should be:
SELECT
patient.id
MIN(
CAST(YEAR(completiondate) AS VARCHAR(4)) + '-' + CAST(MONTH(completiondate) AS VARCHAR(2))
)
AS Mjesec
FROM ....
WHERE
incident.completionDate Between Convert(smalldatetime, '01/10/2007', 103) and Convert(smalldatetime, '01/11/2013', 103)
and servicecharges.serviceid in (47)
and incident.status != 6
and ServiceRequestDescription LIKE 'something'
and chargeDescr = 'test'
group by patient.id
Please Try it
WITH CTE AS
(
SELECT *,RN=ROW_NUMBER() OVER (PARTITION BY patient ORDER BY patient DESC) FROM tablename
)
select * from CTE where RN>1

Using DatePart in line of a larger sql clause

This statement will correctly merge 2 columns ('DATE' and 'TIME'):
update AllBW1 set sUserTime =
CAST(
(
STR( YEAR( [DATE] ) ) + '/' +
STR( MONTH( [DATE] ) ) + '/' +
STR( DAY( [DATE] ) ) + ' ' +
(select DATENAME(hour, [TIME]))+ ':' +
(select DATENAME(minute, [TIME])) + ':' +
(select DATENAME(SECOND, [TIME]))
) as DATETIME)
where sUserTime is null
I'd like to refine the above so as to replace the default timezone with my own (GMT-6).
I've tried a few variations:
CAST((select DATEADD(hour, -6, DATENAME(hour, [TIME]))) as smalldatetime) + ':' +
and
(select CAST(DATEADD(hour, -6, DATENAME(hour, [TIME]))) as datetime) + ':' +
and have achieved no joy.
thx
The logfile as parsed into the SQL table by LogParser 2.2 has separate fields for Date and Time but, since both are formatted as datatime fields they end up looking like:
2012-01-04 00:00:00.000 for date (all time fields are zeroed)
2012-01-01 06:04:41.000 for time (all date field = first day of current year)
That's the reason the query is parsing each element the way it is. Thanks to Dems comment I simplified everything down. I've no doubt this can be optimized by for the volumes I'm dealing with this is adaquate:
update myTable set sUserTime =
(
DATENAME(YEAR, [DATE] ) + '/' +
DATENAME(MONTH, [DATE] ) + '/' +
DATENAME(DAY, [DATE] ) + ' ' +
DATENAME(hour, (dateadd(hh, -6, [time])))+ ':' +
DATENAME(minute, [TIME]) + ':' +
DATENAME(SECOND, [TIME])
)
where sUserTime is null