从两个表中选择数据SQL

时间:2012-04-27 17:08:37

标签: sql sql-server sql-server-2008

我有一个表(SeekerInfo)喜欢:

--------------------------------------------------
SeekerID -  SeekerName - SeekerSex - SeekerMobile
12121    -  mmmm       - Male      - 067676767
13223    -  ssss       - Female    - 078876767
--------------------------------------------------

和另一个表(SeekerCources)一样(每个导引头最多都有3个cources):

--------------------------------------------------
SeekerID - CourceName - Duration
12121    - MCSA       - 1 year
12121    - MCPD       - 6 months
13223    - CCNA       - 1 year
13223    - CCNP       - 1 year
13223    - MCTS       - 4 months

我想制作一个select语句,预览来自两个表的数据如下所示:

SeekerID -  SeekerName - SeekerSex - SeekerMobile - Cource1 - Cource2 - Cource3
12121    -  mmmm       - Male      - 067676767    - MCSA    - MCPD    - *NULL*
13223    -  ssss       - Female    - 078876767    - CCNA    - CCNP    - MCTS 

3 个答案:

答案 0 :(得分:1)

这是一种方法,您可以为每个课程分配数字1-3,然后进行三个连接

WITH cte 
     AS (SELECT Row_number() OVER (partition BY SeekerID ORDER BY CourceName) rn 
                , 
                SeekerID, 
                CourceName, 
                Duration 
         FROM   SeekerCources) 
SELECT si.SeekerID, 
       si.SeekerName, 
       si.SeekerSex, 
       si.SeekerMobile, 
       c1.CourceName AS Cource1, 
       c2.CourceName AS Cource2, 
       c3.CourceName AS Cource3 
FROM   SeekerInfo si 
       LEFT JOIN cte c1 
         ON si.SeekerID = c1.SeekerID 
            AND c1.rn = 1 
       LEFT JOIN cte c2 
         ON si.SeekerID = c2.SeekerID 
            AND c2.rn = 2 
       LEFT JOIN cte c3 
         ON si.SeekerID = c3.SeekerID 
            AND c3.rn = 3 

DEMO

一个尚未解决的问题是,我们没有很好的方法来决定1,2或3中的哪个课程,如果你有一个类似日期的字段你可以用那个替换CourceName

e.g。
Row_number() OVER (partition BY SeekerID ORDER BY DateTaken)

答案 1 :(得分:1)

从长远来看,你似乎会有很多NULL值,这意味着你迟早会遇到问题。 此外,您尝试在结果中引入n个属性,n是最大数量的课程结果,这不会使客户端更容易。

我可以建议一种不同的方法,你可以将所有结果作为行,这意味着你的结果中会有重复的数据(这并不重要,因为你没有存储它)。

SELECT i.*, c.CourseName 
FROM SeekerInfo i JOIN SeekerCourses c 
ON i.SeekerID = c.SeekerID;

这将返回所有带有额外搜索者信息的课程;)

编辑:对不起,我没有正确阅读您的帖子 - 您将始终最多有3门课程。不管怎样,也许这会有所帮助。

答案 2 :(得分:0)

在这里,我试图分三步显示你的答案。

CREATE TABLE [SeekerCources](
    [SeekerID] [nvarchar](50) NULL,
    [CourceName] [nvarchar](50) NULL,
    [Duration] [nvarchar](50) NULL
) 


CREATE TABLE [SeekerInfo](
    [SeekerID] [nvarchar](50) NULL,
    [SeekerName] [nvarchar](50) NULL,
    [SeekerSex] [nvarchar](50) NULL,
    [SeekerMobile] [nvarchar](50) NULL
)

INSERT [SeekerCources] ([SeekerID], [CourceName], [Duration]) VALUES (N'12121', N'MCSA', N'1 year')
INSERT [SeekerCources] ([SeekerID], [CourceName], [Duration]) VALUES (N'12121', N'MCPD', N'6 months')
INSERT [SeekerCources] ([SeekerID], [CourceName], [Duration]) VALUES (N'13223', N'CCNA', N'1 year')
INSERT [SeekerCources] ([SeekerID], [CourceName], [Duration]) VALUES (N'13223', N'CCNP', N'1 year')
INSERT [SeekerCources] ([SeekerID], [CourceName], [Duration]) VALUES (N'13223', N'MCTS', N'4 months')
INSERT [SeekerInfo] ([SeekerID], [SeekerName], [SeekerSex], [SeekerMobile]) VALUES (N'12121', N'mmmm', N'Male', N'067676767')
INSERT [SeekerInfo] ([SeekerID], [SeekerName], [SeekerSex], [SeekerMobile]) VALUES (N'13223', N'ssss', N'Female', N'078876767')

第1步

Select SeekerID, CourceName, 
'Course' + CAST(ROW_NUMBER() over (partition by SeekerID order by CourceName) as varchar(10)) as CourseSquence
from SeekerCources  

步骤1结果应如下所示。

Step 1 Result

演示:link

第2步

Select SeekerID, Course1, Course2, Course3
from    
(
    select SeekerID, CourceName, 
    'Course' + CAST(ROW_NUMBER() over (partition by SeekerID order by CourceName) as varchar(10)) as CourseSquence
    from SeekerCources  
) Temp
Pivot
(
    Max(CourceName)
    For CourseSquence in (Course1, Course2, Course3)
) PIV

步骤2结果应如下所示。

Step 2 Result

演示:link

第3步

Select SeekerInfo.*, PIV.SeekerID, Course1, Course2, Course3
from    
(
    select SeekerID, CourceName, 
    'Course' + CAST(ROW_NUMBER() over (partition by SeekerID order by CourceName) as varchar(10)) as CourseSquence
    from SeekerCources  
) Temp
Pivot
(
    Max(CourceName)
    For CourseSquence in (Course1, Course2, Course3)
) PIV
inner join SeekerInfo on SeekerInfo.SeekerID = PIV.SeekerID

步骤3结果应如下所示。

Step 3 Result

演示:link

<强>解释
视频1:SQL Server中的数据透视 - https://youtu.be/h3BtudZehuo
视频2:将行转换为列 - https://youtu.be/C0mQqDnF7wQ