1

オフサイトで 3 人の別々のユーザーが使用する Access データベースがあります。このオフサイトの場所にはネットワーク リンクがありません (それもできません)。

3 つの同一のデータベースがあるため、3 つの同一のテーブルがあります。各ユーザーは、同じ主キーを使用して情報を入力します。この「食品評価」の例では:

Item       |      Color       | Timestamp

PERSON 1 (first database)
Carrot     |     Orange       | 2012-12-21 13:00:00
Watermelon |     Red          | 2012-12-21 19:00:00 <--
Blueberry  |     Blue         | 2012-12-21 17:00:00 <--

PERSON 2 (second database)
Carrot     |     Yellow       | 2012-12-21 15:00:00 <--
Apple      |     Green        | 2012-12-21 15:00:00 <--

PERSON 3 (third database)
Watermelon |     Green        | 2012-12-21 11:00:00 
Apple      |     Red          | 2012-12-21 14:00:00
Orange     |     Orange       | 2012-12-21 15:00:00 <--

テーブルを出力する必要があります:

Blueberry  |     Blue         | 2012-12-21 17:00:00
Watermelon |     Red          | 2012-12-21 19:00:00
Carrot     |     Yellow       | 2012-12-21 15:00:00
Apple      |     Green        | 2012-12-21 15:00:00
Orange     |     Orange       | 2012-12-21 15:00:00

したがって、タイムスタンプに基づいて、重複の 1 つだけを選択するだけでなく、重複していないものもすべて選択する必要があります (アイテムはプライマリの一意のキーです)。私は一生、これのSQLを取得することはできません..

SELECT Item, Color, MAX(timestamp) 
FROM (SELECT ... FROM first 
UNION SELECT ... FROM second
UNION SELECT ... FROM third)
GROUP BY Item, Color

ただし、MAX 関数を取得するには Color でグループ化する必要があるため、依然としてインデックス違反が発生します。

では、どうすればこの出力を取得できますか?

4

3 に答える 3

2

各アイテムの最新のタイムスタンプを見つけようとしているだけで、最初は Color を無視しています。したがって、それのみを行うクエリを作成する必要があり、その後、テーブル全体と結合できます。

select a.Item, a.Color, a.Timestamp

from
(SELECT ... FROM first 
UNION SELECT ... FROM second
UNION SELECT ... FROM third) a

inner join

(SELECT Item, MAX(timestamp) as MaxTime
FROM (SELECT ... FROM first 
UNION SELECT ... FROM second
UNION SELECT ... FROM third) z
GROUP BY Item) b

on a.Item = b.Item and a.Timestamp = b.MaxTime
于 2012-12-22T01:36:15.397 に答える
1

これにより、推定結果が得られます。

create table t1 ( food nvarchar(20), color nvarchar(10), timestamp datetime );
create table t2 ( food nvarchar(20), color nvarchar(10), timestamp datetime );
create table t3 ( food nvarchar(20), color nvarchar(10), timestamp datetime );

insert into t1 values 
( 'Carrot', 'Orange', '2012-12-21T13:00:00' ), 
( 'Watermelon', 'Red', '2012-12-21T19:00:00' ), 
( 'Blueberry', 'Blue', '2012-12-21T17:00:00' );

insert into t2 values 
( 'Carrot', 'Yellow', '2012-12-21T15:00:00' ), 
( 'Apple', 'Green', '2012-12-21T15:00:00' );

insert into t3 values 
( 'Watermelon', 'Green', '2012-12-21T11:00:00' ), 
( 'Apple', 'Red', '2012-12-21T14:00:00' ), 
( 'Orange', 'Orange', '2012-12-21T15:00:00' );

with "data"
as 
(
    select * from t1 
    union all select * from t2 
    union all select * from t3
)
, "maxdata"
as
(
    select
        *,
        latest = MAX( "timestamp" ) over ( partition by "food" )
    from
        data
)
select 
    "food", "color", "timestamp"
from 
    maxdata
where
    "timestamp" = "latest"

@edit: "ms access" を上書き - これは tsql 用です。そのために残念。それでも、クエリを使用して、アクセス用の有効な構文に変換できる可能性があります。

于 2012-12-22T01:40:15.267 に答える
0

EDITED

One great thing about MS Access is the query on a query. You may save the union into a query. treat it as your main table. Another query that is grouped by item and max date. The join them.

Or make second query to return only max dates and use distinct item with IN operator ;-)


The answer is in ANSI SQL , so you may incorporate into MS ACCESS. All you need is a union and then group it out :)

Query:

select x.*, max(x.TimeStamp) 
from (
select * from person1
union all
select * from person2
union all
select * from person3) as x
group by x.Item
order by x.TimeStamp asc
;

Results:

ITEM        COLOR   MAX(X.TIMESTAMP)
Carrot      Orange  December, 21 2012 15:00:00+0000
Apple       Green   December, 21 2012 15:00:00+0000
Orange      Orange  December, 21 2012 15:00:00+0000
Blueberry   Blue    December, 21 2012 17:00:00+0000
Watermelon  Red     December, 21 2012 19:00:00+0000
于 2012-12-22T01:50:50.287 に答える