table1
、table2
およびtable3
異なる列がありますが、すべてに列がありOrderDate
ます。3 つのテーブルすべてから行の結果セットを取得し、最終的な結果セットを で並べ替えたいと考えていOrderDate
ます。
( select * from table1 LEFT join table2 on 0=1 LEFT join table3 on 0=1
where somedate <= table1.orderdate )
union all
( select * from table1 RIGHT join table2 on 0=1 LEFT join table3 on 0=1
where somedate <= table2.orderdate )
union all
( select * from table1 RIGHT join table2 on 0=1 RIGHT join table3 on 0=1
where somedate <= table3.orderdate )
これは機能しますが、この結果セットを次の順序で並べたいorderdate
ので、次を追加します。
order by case when table1.orderdate is not null then table1.orderdate
when table2.orderdate is not null then table2.orderdate
else table3.orderdate end
SQL Server は、「ステートメントに UNION、INTERSECT、または EXCEPT 演算子が含まれている場合、ORDER BY 項目を選択リストに含める必要があります」というエラーを返します。
交換したら
select *
に
select *, table1.orderdate, table2.orderdate, table3.orderdate
同じエラーが発生します。
は?ありがとう。