2个SQL查询的交替列

时间:2015-11-26 11:57:03

标签: c# sql asp.net oracle

我有一个oracle查询,它从两个不同的表中获取数据,将它们显示为:

Project | 20151 | 20152 | 20153 | 20154 | Project_1 | 20151_1 | 20152_1 | 20153_1 | 20154_1
-------------------------------------------------------------------------------------------

这确实得到了我需要的正确数据,但是我需要一种方法来交替每个表中的列。 E.g:

Project | Project_1 | 20151 | 20151_1 | ...
---------------------------------------

我的查询如下:

SELECT * FROM(
  SELECT * FROM 
  (
    SELECT planned.YEAR || planned.MONTH as AAA, planned.DAYS as Amount, PROJ.NAME AS Project 
    FROM PLANNED_RESOURCE_TAB planned, USER_LINK_VIEW PROJ
    WHERE ACTUAL.U_P_LINK = PROJ.ID 
    AND PROJ.USER_ID = '1' 
  ) 
  PIVOT
  ( 
    SUM(Amount) FOR AAA in (20151,20152,20153,20154)
  )
) 
PLANNED__ RIGHT OUTER JOIN
(
  SELECT * FROM 
  (
    SELECT ACTUAL.YEAR || ACTUAL.MONTH as BBB, ACTUAL.DAYS as Amount, PROJ.NAME AS Project 
    FROM ACTUAL_RESOURCE_TAB actual, USER_LINK_VIEW PROJ
    WHERE ACTUAL.U_P_LINK = PROJ.ID 
    AND PROJ.USER_ID = '1' 
  )
  PIVOT
  ( 
    SUM(Amount) FOR BBB in (20151,20152,20153,20154)
  ) 
)ACTUAL__
ON PLANNED__.PROJECT = ACTUAL__.PROJECT

数据结果将显示在ASP.NET页面上,因此如果最好操纵页面本身上的列没有问题。我主要想知道是否可以事先用SQL做到这一点,以及随后我怎么做。

无法在第一个select语句中声明列,它们将根据用户输入而变化。数据透视表中的值仅用于我的测试目的,它们将从ASP.NET页面动态生成。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试将*替换为最外部选择的检索列?像这样:

SELECT PLANNED__.Project,
  ACTUAL__.Project,
  PLANNED__."20151",
  ACTUAL__."20151",
  PLANNED__."20152",
  ACTUAL__."20152",
  PLANNED__."20153",
  ACTUAL__."20153",
  PLANNED__."20154",
  ACTUAL__."20154"
FROM
  (SELECT *
  FROM
    (SELECT planned.YEAR
      || planned.MONTH AS AAA,
      planned.DAYS     AS Amount,
      PROJ.NAME        AS Project
    FROM PLANNED_RESOURCE_TAB planned,
      USER_LINK_VIEW PROJ
    WHERE ACTUAL.U_P_LINK          = PROJ.ID
    AND PROJ.USER_ID               = '1'
    ) PIVOT ( SUM(Amount) FOR AAA IN (20151,20152,20153,20154) )
  ) PLANNED__
RIGHT OUTER JOIN
  (SELECT *
  FROM
    (SELECT ACTUAL.YEAR
      || ACTUAL.MONTH AS BBB,
      ACTUAL.DAYS     AS Amount,
      PROJ.NAME       AS Project
    FROM ACTUAL_RESOURCE_TAB actual,
      USER_LINK_VIEW PROJ
    WHERE ACTUAL.U_P_LINK          = PROJ.ID
    AND PROJ.USER_ID               = '1'
    ) PIVOT ( SUM(Amount) FOR BBB IN (20151,20152,20153,20154) )
  )ACTUAL__
ON PLANNED__.PROJECT = ACTUAL__.PROJECT