Help with T-SQL query

Homerboy

Lifer
Mar 1, 2000
30,859
4,976
126
I have a query that returns 200 unique numbers from a table.

Code:
Number
--------
4
23
29
62
65
74
89
151
299
...
312

I need to add a column so that the past 10 years is represented for each of those unique numbers (so I will have 2000 rows total: 10x200)


Code:
Year	Number
2013	4
2012	4
2011	4
2010	4
2009	4
2008	4
2007	4
2006	4
2005	4
2004	4
2013	23
2012	23
2011	23
2010	23
2009	23
2008	23
2007	23
2006	23
2005	23
2004	23
...
2013	299
2012	299
2011	299
2010	299
2009	299
2008	299
2007	299
2006	299
2005	299
2004	299

Does that make sense?
I feel like this is really obvious and I've just over thought the problem at this point and can't wrap my brain back around it.

Thanks in advance.
 

Homerboy

Lifer
Mar 1, 2000
30,859
4,976
126
Small addendum to this.

A) it's not really 10 years (it's since 2000, but I can make it a fixed 10 years if need be)
B) I've got my code below. I started down the path of a CTE, but realized that was a dead end.


Code:
DECLARE @startDate DATETIME
DECLARE @endDate DATETIME
SET @startDate = '2000-01-01'
SET @endDate = GETDATE()
    
;with

forwarders (forw_no)
as
(select forw_no from MASFORW where ADDR is not null
)
,

dates(Date) 
as 
(SELECT 
@startdate as Date
UNION ALL
SELECT DATEADD(y,1,[Date])
FROM dates 
WHERE DATE < @enddate
)


select 
forwarders.forw_no
from forwarders
 

Ancalagon44

Diamond Member
Feb 17, 2010
3,274
202
106
Construct a temporary table A with one column - year - stored as an integer. Fill it with your 10 years.

Then, select from A and your query that returns the 200 unique numbers, but do so using a cross join. That will result in 2000 results.
 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
Ancalagon44 Is correct. You're looking for a cross join.
A SQL Server sample since source tables aren't available.

Code:
;WITH TenYears as
(
SELECT top 10 YEAR(DATEADD(YEAR, -ROW_NUMBER() OVER (ORDER BY o.Object_id) + 1, GETDATE())) [Year]
FROM sys.all_objects o
),
TwoHundredNums as
(
SELECT top 200 o.object_id Number
FROM sys.all_objects o
)

SELECT	TenYears.[Year],
	TwoHundredNums.Number
		
FROM TenYears
CROSS JOIN TwoHundredNums
ORDER BY Number, Year DESC
 
Last edited:

KLin

Lifer
Feb 29, 2000
29,556
163
106
1. declare a table variable and include year field
2. insert records into table variable for last 10 years
3. do a select statement and include both tables in from clause just using a comma(should act as a cross join)
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |