SELECT a.delivery_date,
a.delivery_hour,
a.price as EX-ANTE,
FROM mms_realtime_dispatch_prices_report a
UNION ALL
SELECT b.delivery_date,
b.delivery_hour,
b.price as EX-POST,
FROM mms_realtime_dispatch_prices_report b
UNION ALL
SELECT c.region,
c.dem_rtdel,
c.date,
FROM pub_demand_lwap c;
UNION ALL
SELECT region,
report,
hour,
SUM(q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11)
FROM pub_markets_bids_and_offers
WHERE delivery date=03/16/2011
GROUP BY hour
帮助!需要将这四个表合并为一个新表没有重复数据 你能帮我把这四个表合并成一个表吗?这是我第一次遇到这个。我真的需要帮助:(
答案 0 :(得分:0)
在SQL server union中,只有在查询返回相同数量和类型的列时才能工作。
答案 1 :(得分:0)
你需要获得你的联盟查询1我可以看到查询有很多错误,目前
SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name1 UNION ALL SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name2 UNION ALL SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name3
等......
一旦满足所有这些条件,那么您可以执行类似的操作以消除结果集中的重复行
;with CTE
AS
(
SELECT ID_Column, rn = ROW_NUMBER() OVER (PARTITION BY Column1, Column2, Column3... ORDER BY ID ASC)
FROM ( -- All of your UNION ALL Statements Can go here --)q
)
DELETE FROM CTE
WHERE rn = 1