如何从视图中插入表中的值

时间:2015-12-04 06:13:17

标签: sql-server view insert sql-update case

我有一个视图,我必须在表中插入值。视图如下:

Author_ID   Research_Area   Category    Paper_Count Paper_Year  Rank
--------------------------------------------------------------------
677         feature         8           1           2005        1
677         image           11          1           2005        2
677         retrieval       12          1           2005        3
677         semantic        19          1           2007        1
677         feature         8           1           2009        1
677         video           5           2           2013        1
1359        image           11          2           2005        1
1359        adversary       1           1           2005        2
1359        archiving       12          1           2005        3
1359        linear          1           3           2006        1
1359        real time       17          3           2006        2
...  
...  

表格如下:

id   Author_ID   Category_2005   Category_2006   Category_2007.....Category_2014   Rank
---------------------------------------------------------------------------------------  

我必须插入每个Author_ID,每个Author_ID根据每个Category的排名,Paper_Year列有三个值,有些Author_ID可能没有某些Paper_Year的值,因此必须为没有0值的Paper_Year插入Category,而每个Author_ID只应显示3结果表中的时间。

我仅针对Paper_Year尝试此操作,例如2005

UPDATE A
SET A.aid       =   (SELECT vA.Author_ID 
                     FROM Author_Area vA 
                     WHERE vA.Paper_Year = 2005 AND vA.Rank = 1),
A.Category_2005 =   (SELECT vA.Category 
                     FROM Author_Area vA 
                     WHERE vA.Paper_Year = 2005 AND vA.Rank = 1)
FROM Author_Areas A  

但无法更新表格中的任何行。我应该在这种情况下使用INSERTUPDATE,并且可以使用CASE语句,如果是,那么如何?

此外,我在结果表中的输出应该是这样的:

id   Category_2005   Category_2006   Category_2007 ...   Category_2014   Rank
-----------------------------------------------------------------------------
677  8               0               19                  0               1
677  11              0               0                   0               2
677  12              0               0                   0               3
1359 11              1               0                   1               1
1359 1               17              0                   0               2
1359 12              0               10                  0               3
...
...

请帮忙!
感谢

1 个答案:

答案 0 :(得分:0)

我假设您要在表格中创建新行? 另外,我认为id列是主键IDENTITY列?

您只想找到一种方法来转动(actually a language feature)。在这种情况下,在不使用PIVOT语法的情况下编写相对容易,只需使用过滤的聚合(MAX内置CASE)。

INSERT INTO Author_Areas (
  ID,
  Category_2005,
  Category_2006,
  Category_2007,
  Category_2008,
  Category_2009,
  Category_2010,
  Category_2011,
  Category_2012,
  Category_2013,
  Category_2014,
  Rank
)
SELECT Author_ID,
  MAX(CASE WHEN Paper_Year = 2005 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2006 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2007 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2008 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2009 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2010 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2011 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2012 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2013 THEN category ELSE 0 END),
  MAX(CASE WHEN Paper_Year = 2014 THEN category ELSE 0 END),
  Rank
FROM
  Author_Area
GROUP BY
  Author_ID, Rank