根据条件从不同的表填充

时间:2014-10-29 09:20:45

标签: asp.net sql-server

我试图在视图中加入3个表格;这是情况:     我有3张桌子....     第一个是包含面板信息的[ADM_Panels]     columns:panel_id,panel_number,YearID     第二个是[ADM_PanelsDtl]     列:[panel_id],[pnlmember_id] 我有另一张名为" GEN_Years"有2列     表3:[GEN_Years]     列:[年份ID],[年份]

我试过这样的事情,但我的select语句应该返回2个不同的表

SELECT YearID,count(pnlmember_id) Reviewer FROM [ADM_PanelsDtl] join [ADM_Panels] 
on [ADM_PanelsDtl].[panel_id]=[ADM_Panels].[panel_id]group by YearID

select YearID,count([panel_id])Panel from dbo.ADM_Panels group by YearID

但我想在带有字段

的表格中显示我的结果
[YearID], [Year], count(pnlmember_id), count([panel_id])Panel

加入上述3个表格。关于如何做的任何解决方案?

我不想使用临时表,我只想加入这些表

1 个答案:

答案 0 :(得分:1)

可能是lyk这个

SELECT A.[YearID], c.[Year], a.Reviewer, B.Panel
FROM   (SELECT YearID,
               Count(pnlmember_id) Reviewer
        FROM   [ADM_PanelsDtl]
               JOIN [ADM_Panels]
                 ON [ADM_PanelsDtl].[panel_id] = [ADM_Panels].[panel_id]
        GROUP  BY YearID) A
       JOIN (SELECT YearID,
                    Count([panel_id])Panel
             FROM   dbo.ADM_Panels
             GROUP  BY YearID) B 
         ON a.YearID = b.YearID join GEN_Years c on a.YearID = c.YearID