×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SQL Help

SQL Help

(OP)
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

RE: SQL Help

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

RE: SQL Help

“ValueThisYear” is the result of a report.

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

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: SQL Help

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

CODE --> 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close