Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Help

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
GB
Hi All,
I need help with a bit of SQL...

I have a table which shows the below:

season | ValueThisYear
AW17-1 | 100
AW17-2 | 205
AW18-1 | 199
AW18-2 | 300

I need to create a view based on similar columns which will give me the data as below. ie. ValueThisYear and ValueLastYear. where season is AW18-1 the value this year is 199 and the value Last year is taken from AW17-1 being 100.

I need the view result showing as below:

season | ValueThisYear | ValueLastYear
AW17-1 | 100 |
AW17-2 | 205 |
AW18-1 | 199 | 100
AW18-2 | 300 | 205


Thanks in advance
Nims
 
And how do you know which record is for this year and which is for the last one?
Don't tell me "by the name" please.

Borislav Borissov
VFP9 SP2, SQL Server
 
“ValueThisYear” is the result of a report.

You need to get to the data from which that report was generated. Must have DATES!

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
I assume the 17 and 18 are years but it would have been appropriate for neemi to clarify in the original post. Also use of TGML is a must for members who have started over 200 threads in 18 years.

I expect something like this would work. At least it gives the same results as requested

SQL:
WITH 
cteA AS
(
 SELECT 'AW17-1' Season, 100 ValueThisYear UNION 
 SELECT 'AW17-2', 205 UNION
 SELECT 'AW18-1', 199 UNION
 SELECT 'AW18-2', 300
 )
 SELECT A.SEASON, A.ValueTHisYear, B.ValueTHisYear ValueLastYear
 FROM cteA A LEFT JOIN cteA B ON
  LEFT(A.Season,2) = Left(B.Season,2) AND 
  RIGHT(A.Season,1) = Right(B.Season,1) AND 
  Convert(int,SUBSTRING(A.Season,3,2)) = Convert(int,SUBSTRING(B.Season,3,2))+1

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top