如何在sql中建立同一个表的联合

时间:2017-04-04 12:17:31

标签: mysql

我的sql中有一个表格,显示以下数据 我必须按照dpr日期,出价日期作为类别将数据保持在彼此之下并显示其日期。基本上是同一个表的联合。

Region  Market  Project PS ID   Site Name       dpr date    bid date
East    GAAL    DTS_AWS 134027  ROWLAND_SPRINGS 2/14/2017   2/28/2017
East    GAAL    DTS_AWS 165455  EGG AND BUTTER  2/22/2017   2/28/2017
East    GAAL    DTS_AWS 134359  DOLDSONVILLE    2/22/2017   2/28/2017
East    GAAL    DTS_AWS 121528  EASTABOGA       2/22/2017   2/28/2017
East    GAAL    DTS_AWS 133398  PATRICK_MILL    2/22/2017   3/6/2017

我必须编写一个查询,以下面的格式显示数据

Region  Market  Project PS ID   Site Name       Categoty     date
East    GAAL    DTS_AWS 134027  ROWLAND_SPRINGS dpr date    2/14/2017
East    GAAL    DTS_AWS 165455  EGG AND BUTTER  dpr date    2/22/2017
East    GAAL    DTS_AWS 134359  DOLDSONVILLE    dpr date    2/22/2017
East    GAAL    DTS_AWS 121528  EASTABOGA       dpr date    2/22/2017
East    GAAL    DTS_AWS 133398  PATRICK_MILL    dpr date    2/22/2017
East    GAAL    DTS_AWS 134027  ROWLAND_SPRINGS Bid date    2/28/2017
East    GAAL    DTS_AWS 165455  EGG AND BUTTER  Bid date    2/28/2017
East    GAAL    DTS_AWS 134359  DOLDSONVILLE    Bid date    2/28/2017
East    GAAL    DTS_AWS 121528  EASTABOGA       Bid datn    2/28/2017
East    GAAL    DTS_AWS 133398  PATRICK_MILL    Bid date    3/6/2017

1 个答案:

答案 0 :(得分:0)

您的目标绝对不清楚,表格架构看起来很奇怪或错误。 但只是猜测如何达到预期的结果:

http://sqlfiddle.com/#!9/dc6b2/3

SELECT 
  Region,  
  Market,  
  Project, 
  PS_ID,   
  Site_Name, 
  'dpr date' category,
  dpr
FROM t1
UNION
SELECT 
  Region,  
  Market,  
  Project, 
  PS_ID,   
  Site_Name, 
  'Bid date' category,
  bid
FROM t1;