有点难度的SQL,谁可以回答下!有重赏!
CREATE TABLE dbo.analysts(
ID smallint PRIMARY KEY,
Name varchar(100) NOT NULL
)
GO
INSERT INTO analysts(ID,Name) VALUES(1, 'Morgan Keegan ')
INSERT INTO analysts(ID,Name) VALUES(4, 'Oppenheimer & Company ')
INSERT INTO analysts(ID,Name) VALUES(8, 'Hilliard Lyons ')
================
CREATE TABLE dbo.calc_periods(
PeriodID smallint PRIMARY KEY,
Label varchar(100) NOT NULL
)
GO
INSERT INTO calc_periods(PeriodID,Label) VALUES(1, '1 Year ')
INSERT INTO calc_periods(PeriodID,Label) VALUES(2, '2 Years ')
INSERT INTO calc_periods(PeriodID,Label) VALUES(3, '3 Years ')
=============================
生成结果如下:
AnalystID PeriodID label
----------- ----------- -----------
1 1 1 Year
1 2 2 Year
1 3 3 Year
4 1 1 Year
4 2 2 Year
4 3 3 Year
8 1 1 Year
8 2 2 Year
8 3 3 Year
------解决方案--------------------select a.ID,b.PeriodID,b.Label from analysts a , calc_periods b
order by a.ID
ID PeriodID Label
------ -------- -------
1 1 1 Year
1 2 2 Years
1 3 3 Years
4 1 1 Year
4 2 2 Years
4 3 3 Years
8 1 1 Year
8 2 2 Years
8 3 3 Years
(所影响的行数为 9 行)