0

4つの異なるテーブルに対して4つのSelectステートメントがあります。各Selectクエリは、例として指定された条件を満たす最新のレコードを提供します。

Select TOP 1 * from table where column_name = 'something' order by col1 DESC;

次に、4つのクエリすべての結果セットを結合し、結合した結果セットからビューを作成する必要があります。

4

2 に答える 2

4
create view some_view as
(
(select TOP 1 * from table1 where ....)
UNION ALL
(select TOP 1 * from table2 where ....)
UNION ALL
(select TOP 1 * from table3 where ....)
UNION ALL
(select TOP 1 * from table4 where ....)
)
于 2012-08-09T04:27:24.853 に答える
0

一部のDBでは、結合されたクエリの内部に「orderby」句を指定できません。

col1 descで注文している場合は、min()またはmax()を適用できる列のタイプである可能性があります。

その場合、以下で問題を解決できます(レコードが多すぎない場合、またはテーブルが大きい場合は、「col1」と「some_column」にインデックスが付けられます)。

create view some_view as
(
select * from table1
  where some_column = 'something'
  and col1 = (select max(col1) from table1 where some_column = 'something')
UNION ALL
select * from table2 
  where some_column = 'something'
  and col1 = (select max(col1) from table2 where some_column = 'something')
UNION ALL
select * from table3 
  where some_column = 'something'
  and col1 = (select max(col1) from table3 where some_column = 'something')
UNION ALL
select * from table4 
  where some_column = 'something'
  and col1 = (select max(col1) from table4 where some_column = 'something')
)
于 2012-08-09T04:53:53.583 に答える