Returning the following season and year - sql

For a particular season and year when it started I have to get the following season and year when it starts, e.g. for Summer 2021 I should get Autumn 2021, and for Winter 2021 I should get Spring 2022.
"Seasons" table has the "Order" column which indicates the order of seasons in a year, so it looks like this:
SeasonID
Order
Name
1
1
Spring
2
2
Summer
3
3
Autumn
4
4
Winter
Remark: "Order" column may seem redundant, but this is actually simplified/adapted version of the problem I have, where "Order" column is neccesary.
I have a stored procedure that has #Year and #SeasonID as input parameters. I have to get following season/year in #FollowingSeasonID and #FollowingYear parameters, which I use later in the stored procedure. I'm not sure if I've used the best technique for that:
(...)
DECLARE #FollowingSeasonID int;
DECLARE #FollowingYear int;
if object_id('tempdb..#Years') is not null drop table #Years
CREATE TABLE #Years ([Year] int)
INSERT INTO #Years ([Year]) VALUES (#Year), (#Year + 1)
SELECT #FollowingSeasonID = FollowingSeasonID, #FollowingYear = FollowingYear
FROM
(SELECT SeasonID,
[Year],
LEAD(SeasonID) OVER (ORDER BY [Year], Order) AS FollowingSeasonID,
LEAD([Year]) OVER (ORDER BY [Year], Order) AS FollowingYear
FROM Seasons
CROSS JOIN #Years) t
WHERE SeasonID = #SeasonID AND [Year] = #Year
(...)
Is there a better way to do that? Could it be achieved in just one query?
I need those values in multiple stored procedures/views/... so I wanted to extract that part of code if a function. Scalar valued function can't return two values, so I have to create a table valued function (and I have to use table variable instead of temp table #Years). However, is there a better way to do that instead of having a table valued function that always returns just one row?

You can wrap this code in UDF and you should be able to get seasonId, year
DECLARE #season table(SeasonId int, SeasonOrder int, Name varchar(10))
INSERT INTo #season values
(1, 1 ,'Spring')
,(2, 2 ,'Summer')
,(3, 3 ,'Autumn')
,(4, 4 ,'Winter');
DECLARE #FollowingSeasonID int;
DECLARE #FollowingYear int;
DECLARE #year int = 2021, #SeasonId int = 2;
;WITH CTE_YearSeason AS
(
SELECT y,s.* FROM #season as s
CROSS APPLY
(VALUES (#year), (#year+1)
) as t(y)
), cte_ranking as
(
SELECT *, row_number() over (order by y,SeasonOrder) as rnk
FROM CTE_YearSeason)
SELECT SeasonId, Y as year
FROM cte_ranking as c
where c.rnk = (SELECT c1.rnk
from cte_ranking as c1
where c1.SeasonId = #SeasonId and c1.y = #year) +1
SeasonId
year
3
2021

Unless I've misunderstood, given a single seasonId you just need the following Id and reset it to 1 and increment the year if necessary?
declare #seasonid int=1, #Year int=2021
select FollowingSeasonID, #Year + yr FollowingYear
from (
select *,
IsNull(Lead(SeasonId) over(order by [order]),1) FollowingSeasonID,
case when Lead(SeasonId) over(order by [order]) is null then 1 else 0 end yr
from seasons
)s
where SeasonId=#SeasonId
If you don't want to use a TVF you could just use this as a view/cte, joining on currentId and return the followingId and yr value to add to your current year.

Related

SQL search in two columns with one combined value

I will try to demonstrate what I am trying to achieve. This is an oversimplified example for my case.
Suppose I have a table contains two columns
ID YEAR
--- ----
1 2017
2 2018
and I have a search term 2017 / 1
What I want to do is something like this
select * from table where 'YEAR / ID' LIKE '%searchterm%'
Is this possible ?
Thanks in advance.
In my opinion the most effective way is:
Firstly divide String x = "2017 / 1" to two int values int year = 2017, int id = 1. I don't know what kind of programing language you are using but all of programing languages have special functions to make it easily (between all values you have '/').
Then use this query:
Select *
from table
where year = 2017
and id = 1
Use Below query, I have considered your search text format as 2017 / 1.
DECLARE #tblTest AS Table
(
Id INT,
YearNo INT
)
INSERT INTO #tblTest values (1,2017)
INSERT INTO #tblTest values (2,2018)
INSERT INTO #tblTest values (3,2017)
INSERT INTO #tblTest values (4,2018)
DECLARE #searchterm VARCHAR(50)='2017 / 1'
LEFT will give you string starting from left position to applied length.
RIGHT will give you string starting from right position to applied length
SELECT
*
FROM #tblTest
WHERE YearNo=LEFT(#searchterm,4)
AND Id = REPLACE(RIGHT(#searchterm,LEN(#searchterm)-(CHARINDEX('/',(REPLACE(#searchterm, ' ', ''))))),'/','')
If your database compatibility could be 130 then You can Try String_Split ref https://learn.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql
Sql most long awaited function (as msdn says)
Declare #tbl table (id int Identity(1,1), value nvarchar(5))
Insert into #tbl ([value]) SELECT value from STRING_SPLIT(#searchstring,'/')
Declare #id int
Select #id = cast(value as int) from #tbl where id=2 --will give 1
Declare #value int
Select #id = cast(value as int) from #tbl where id=1 --ill give 2017
-- —now use them in sql
select * from table where YEAR=#value and ID = #id
You are going to screw up the performance if you do anything like below
select * from table where 'YEAR / ID' LIKE '%searchterm%'
Best way is you can split your search and supply to respective col
Declare #Search varchar(15)='2017/1'
Declare #Year int = (select LEFT(#Search,CHARINDEX('/',#search)-1))
Declare #month int = (select Right(#Search,(len(#search) -CHARINDEX('/',#search))))
select * from #temp where id=#month and year=#Year
Try this code :
select * from table where YEAR + ' / ' + ID LIKE '%searchterm%'
this query will run, but it will perform very poor.

select records that one column are numbers close to 10

I have a table with 3 columns.
one of them is [Code]. I have many records on this table.
I want to select records that their [Code] are numbers close to 10 regularly
for example if select records that has [Code]=9 then select records that has [Code] = 8 etc...
This is what I implement based on your though.
If you wish near record or record-id, not value, then you can change only condition a.data to a.rid.
declare #t table (data int)
insert into #t values(1), (2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(50),(51),(52)
declare #value int = 11 , #getDatToValue int = 2
select * from
(
select * , ROW_NUMBER( ) over(order by data) rid
from #t
)
a
where
a.data between (#value - #getDatToValue) and (#value + #getDatToValue)

Group data without changing query flow

For me it's hard to explait what do I want so article's name may be unclear, but I hope I can describe it with code.
I have some data with two most important value, so let it be time t and value f(t). It's stored in the table, for example
1 - 1000
2 - 1200
3 - 1100
4 - 1500
...
I want to plot a graph using it, and this graph should contain N points. If table has rows less than this N, then we just return this table. But if it hasn't, we should group this points, for example, N = Count/2, then for an example above:
1 - (1000+1200)/2 = 1100
2 - (1100+1500)/2 = 1300
...
I wrote an SQL script (it works fine for N >> Count) (MonitoringDateTime - is t, and ResultCount if f(t))
ALTER PROCEDURE [dbo].[usp_GetRequestStatisticsData]
#ResourceTypeID bigint,
#DateFrom datetime,
#DateTo datetime,
#EstimatedPointCount int
AS
BEGIN
SET NOCOUNT ON;
SET ARITHABORT ON;
declare #groupSize int;
declare #resourceCount int;
select #resourceCount = Count(*)
from ResourceType
where ID & #ResourceTypeID > 0
SELECT d.ResultCount
,MonitoringDateTime = d.GeneratedOnUtc
,ResourceType = a.ResourceTypeID,
ROW_NUMBER() OVER(ORDER BY d.GeneratedOnUtc asc) AS Row
into #t
FROM dbo.AgentData d
INNER JOIN dbo.Agent a ON a.CheckID = d.CheckID
WHERE d.EventType = 'Result' AND
a.ResourceTypeID & #ResourceTypeID > 0 AND
d.GeneratedOnUtc between #DateFrom AND #DateTo AND
d.Result = 1
select #groupSize = Count(*) / (#EstimatedPointCount * #resourceCount)
from #t
if #groupSize = 0 -- return all points
select ResourceType, MonitoringDateTime, ResultCount
from #t
else
select ResourceType, CAST(AVG(CAST(#t.MonitoringDateTime AS DECIMAL( 18, 6))) AS DATETIME) MonitoringDateTime, AVG(ResultCount) ResultCount
from #t
where [Row] % #groupSize = 0
group by ResourceType, [Row]
order by MonitoringDateTime
END
, but it's doesn't work for N ~= Count, and spend a lot of time for inserts.
This is why I wanted to use CTE's, but it doesn't work with if else statement.
So i calculated a formula for a group number (for use it in GroupBy clause), because we have
GroupNumber = Count < N ? Row : Row*NumberOfGroups
where Count - numer of rows in the table, and NumberOfGroups = Count/EstimatedPointCount
using some trivial mathematics we get a formula
GroupNumber = Row + (Row*Count/EstimatedPointCount - Row)*MAX(Count - Count/EstimatedPointCount,0)/(Count - Count/EstimatedPointCount)
but it doesn't work because of Count aggregate function:
Column 'dbo.AgentData.ResultCount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
My english is very bad and I know it (and i'm trying to improve it), but hope dies last, so please advice.
results of query
SELECT d.ResultCount
, MonitoringDateTime = d.GeneratedOnUtc
, ResourceType = a.ResourceTypeID
FROM dbo.AgentData d
INNER JOIN dbo.Agent a ON a.CheckID = d.CheckID
WHERE d.GeneratedOnUtc between '2015-01-28' AND '2015-01-30' AND
a.ResourceTypeID & 1376256 > 0 AND
d.EventType = 'Result' AND
d.Result = 1
https://onedrive.live.com/redir?resid=58A31FC352FC3D1A!6118&authkey=!AATDebemNJIgHoo&ithint=file%2ccsv
Here's an example using NTILE and your simple sample data at the top of your question:
declare #samples table (ID int, sample int)
insert into #samples (ID,sample) values
(1,1000),
(2,1200),
(3,1100),
(4,1500)
declare #results int
set #results = 2
;With grouped as (
select *,NTILE(#results) OVER (order by ID) as nt
from #samples
)
select nt,AVG(sample) from grouped
group by nt
Which produces:
nt
-------------------- -----------
1 1100
2 1300
If #results is changed to 4 (or any higher number) then you just get back your original result set.
Unfortunately, I don't have your full data nor can I fully understand what you're trying to do with the full stored procedure, so the above would probably need to be adapted somewhat.
I haven't tried it, but how about instead of
select ResourceType, CAST(AVG(CAST(#t.MonitoringDateTime AS DECIMAL( 18, 6))) AS DATETIME) MonitoringDateTime, AVG(ResultCount) ResultCount
from #t
where [Row] % #groupSize = 0
group by ResourceType, [Row]
order by MonitoringDateTime
perhaps something like
select ResourceType, CAST(AVG(CAST(#t.MonitoringDateTime AS DECIMAL( 18, 6))) AS DATETIME) MonitoringDateTime, AVG(ResultCount) ResultCount
from #t
group by ResourceType, convert(int,[Row]/#groupSize)
order by MonitoringDateTime
Maybe that points you in some new direction? by converting to int we are truncating everything after the decimal so Im hoping that will give you a better grouping? you might need to put your row-number over resource type for this to work?

Pivot on two aggregate fields

Hi thanks for any help
Gerry
How do I pivot on LAVG and Patient for the months. Example
Facility Jan Feb Mar ect
Downey 30 25 28
Downey 10 9 8
I know this doesn't work but this is what I what to do
PIVOT (SUM(LAVG), Count(Patient) FOR months IN (jan, feb, mar ect
ALTER PROCEDURE [dbo].[Spoclos]
-- Add the parameters for the stored procedure here
#Year INT,
#PayerCode VARCHAR(3),
#ID INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT *
FROM (SELECT LEFT(Months, 3) AS Months,
FacilityName,
LAVG,
Patient,
Region,
ID,
payercode,
facilityCode
FROM dbo.LOSGroup
WHERE PayerCode = #PayerCode
AND years = #year
AND ID = #id) AS s
PIVOT (Sum(LAVG)
FOR months IN (jan,feb,mar,apr,
may,jun,jul,aug,
sep,oct,nov,dec)) AS piv
ORDER BY ID
END
As you mentioned you cannot have two Aggregate functions inside pivot.
Have two separate Pivot queries one to find Sum and another to find Count. Then use Union All to Combine the result.
SELECT 'Sum' [Aggregate],*
FROM (SELECT LEFT(Months, 3) AS Months,
FacilityName,LAVG,Patient,
Region,ID,payercode,
facilityCode
FROM dbo.LOSGroup
WHERE PayerCode = #PayerCode
AND years = #year
AND ID = #id) AS s
PIVOT (Sum(LAVG)
FOR months IN (jan,feb,mar,apr,
may,jun,jul,aug,
sep,oct,nov,dec)) AS piv
UNION ALL
SELECT 'Count' [Aggregate],*
FROM (SELECT LEFT(Months, 3) AS Months,
FacilityName,LAVG,Patient,
Region,ID,payercode,
facilityCode
FROM dbo.LOSGroup
WHERE PayerCode = #PayerCode
AND years = #year
AND ID = #id) AS s
PIVOT (Count(LAVG)
FOR months IN (jan,feb,mar,apr,
may,jun,jul,aug,
sep,oct,nov,dec)) AS piv
ORDER BY ID
Note: To differentiate the Sum and Count I have used a extra column Aggregate

5 new entries for every existing row

date, product, new_col,
2013/05/16, A,
2013/05/18, A,
2013/06/15, A,
2013/05/16, B,
2013/06/13, B,
2013/06/20, B
I am using SQL Server 2008.
For every existing entry, in new_col I need to store into new_col 5 previous calendar dates up to and including the date in the date column. E.g. for the 1st row in the above table, I need 5/16,5/15,5/14,5/13,5/12 in the new_col.
I have a lot of products in the table and a lot of dates for every product.
I have tried to partition the table based on these two columns but can't figure out a way to enter 5 days in the new_col.
Please help with any suggestions to do this.
WITH cal AS (
SELECT CAST('20130101' AS DATE) as cal_date
UNION ALL
SELECT DATEADD(day, 1, cal_date)
FROm cal
WHERE cal_date < CAST('20131231' AS DATE )
)
SELECT
[date],[product],
new_col = STUFF((SELECT ',' + CONVERT(VARCHAR(10),cal.cal_date,111)
FROM cal
WHERE cal.cal_date BETWEEN DATEADD(day, -4, tbl.[date]) AND tbl.[date]
ORDER BY cal.cal_date DESC
FOR XML PATH('')
),1,1,'')
FROM tbl
OPTION (maxrecursion 0)
sql fiddle demo
I don't know if this is the best solution but it's the first that popped into my head. Also as people have been mentioning, you do not need to store this... you can call this function directly from whatever reporting solution you've deployed. Was lazy about removing the last comma but that's pretty simple. Also feel free to adjust the datetime formatting as you see fit.
CREATE TABLE dbo.Sam ([Date] DATE, Product VARCHAR(10), NewCol VARCHAR(1000))
INSERT INTO dbo.Sam
SELECT '2013/05/16','A',NULL
UNION ALL SELECT '2013/05/18','A',NULL
UNION ALL SELECT '2013/06/15','A',NULL
UNION ALL SELECT '2013/05/16','B',NULL
UNION ALL SELECT '2013/06/13','B',NULL
UNION ALL SELECT '2013/06/20','B',NULL
GO
CREATE FUNCTION dbo.fnGet4PreviousDates(#Date DATE, #Product VARCHAR(10))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE #Out VARCHAR(1000) = ''
SELECT #Out = #Out + CONVERT(VARCHAR,[Date],112) + ','
FROM (
SELECT TOP 5 [Date]
FROM dbo.Sam
WHERE Product = #Product
AND [Date] < #Date
ORDER BY [Date] DESC
) t
RETURN #Out
END
GO
UPDATE dbo.Sam
SET NewCol = dbo.fnGet4PreviousDates([Date],Product)
SELECT * FROM dbo.Sam