June 16, 2015 at 12:35 pm
Hello SQL ServerCentral,
I have a quick question. I am trying to index dates to numbers with a large data set.
The first colums is index, the next is FactorsS, the next is value and the next is Date and the last is Lag.
Would it be difficult to write code that would determine the lag values. The lag value is based on the date value.
Index FactorS Value Date Lag
1 XYZ 2.3 12/31/2014 1
2 XYZ 1.4 12/30/2014 2
3 XYZ 3.3 12/29/2014 3
4 ABC 1.8 12/31/2014 1
5 ABC 2.2 12/30/2014 2
6 CBA 1.7 12/31/2014 1
7 CBA 1.8 12/30/2014 2
8 CBA 1.9 12/29/2014 3
9 CBA 2.1 12/28/2014 4
June 16, 2015 at 12:48 pm
you are not very clear in what your requirements are? you want to determine the lag values in reference to what?
read the post at the link in my signature on how to post questions to get better answers.
June 16, 2015 at 12:53 pm
The "Lag" value would be based in the "date" column. "1" would be 1 day back in time, 2 would be 2nd day back in time.
June 16, 2015 at 12:55 pm
pederson1234 (6/16/2015)
Hello SQL ServerCentral,I have a quick question. I am trying to index dates to numbers with a large data set.
The first colums is index, the next is FactorsS, the next is value and the next is Date and the last is Lag.
Would it be difficult to write code that would determine the lag values. The lag value is based on the date value.
Index FactorS Value Date Lag
1 XYZ 2.3 12/31/2014 1
2 XYZ 1.4 12/30/2014 2
3 XYZ 3.3 12/29/2014 3
4 ABC 1.8 12/31/2014 1
5 ABC 2.2 12/30/2014 2
6 CBA 1.7 12/31/2014 1
7 CBA 1.8 12/30/2014 2
8 CBA 1.9 12/29/2014 3
9 CBA 2.1 12/28/2014 4
No problem at all. Use ROW_NUMBER() OVER (PARTITION BY FactorS ORDER BY Date DESC) to create the "Lag" column during a query.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 16, 2015 at 12:57 pm
p.s. You're brand new here. Welcome aboard! 🙂
The people on these forums generally like to test their answers before they post them. Please help those that want to help you by making it easy on them. Please see both articles at the Blue links in my signature line below.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 16, 2015 at 12:58 pm
great thanks...Also what is the maximum number of columns that can be used in a large data set? "Large" meaning 5 columns by 20 million rows?
June 16, 2015 at 1:02 pm
Robert klimes (6/16/2015)
you are not very clear in what your requirements are? you want to determine the lag values in reference to what?read the post at the link in my signature on how to post questions to get better answers.
Will you ever have any holes in your dates...meaning is it possible you will get dates more than 1 day apart?
If so than you could (ironically) make use of the LAG function. Otherwise Jeff's solution will work just fine.
June 16, 2015 at 1:03 pm
Thanks for the blue links..will review and adjust
June 16, 2015 at 1:03 pm
pederson1234 (6/16/2015)
great thanks...Also what is the maximum number of columns that can be used in a large data set? "Large" meaning 5 columns by 20 million rows?
Errr????
The limit on columns in a SELECT statement is 4096.
Not really sure what you're asking.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 16, 2015 at 1:04 pm
yes, weekend dates will be excluded so there will be gaps in the date ranges
June 16, 2015 at 1:05 pm
In that case, can you please post some sample data with the gaps that you expect and show what the results you want are?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 16, 2015 at 1:07 pm
yb751 (6/16/2015)
If so than you could (ironically) make use of the LAG function. Otherwise Jeff's solution will work just fine.
Unless I'm missing something, LAG won't produce the results he wants. LAG just gets the value from a fixed number of rows prior to the current one.
FIRST_VALUE along with a DATEDIFF might, maybe.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 16, 2015 at 1:08 pm
Hi Gail,
My question was referring to converting row data to column data. I am trying to get a sense of how "wide" SQL can go without it crashing. Your answer was very helpful.
Thank you
June 16, 2015 at 1:10 pm
It won't crash. If you exceed the limit, you'll get an error.
That said, you probably want to do that kind of crosstab in the application tier, the built in PIVOT isn't that great.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 16, 2015 at 1:36 pm
GilaMonster (6/16/2015)
yb751 (6/16/2015)
If so than you could (ironically) make use of the LAG function. Otherwise Jeff's solution will work just fine.Unless I'm missing something, LAG won't produce the results he wants. LAG just gets the value from a fixed number of rows prior to the current one.
FIRST_VALUE along with a DATEDIFF might, maybe.
I know I was just rethinking that myself.
Viewing 15 posts - 1 through 15 (of 24 total)
You must be logged in to reply to this topic. Login to reply