2

私はこれに近づいていますか?
「Ape」を含むすべてのレコードと、「Ape」レコードの x 日前に発生するすべてのレコードをリストしようとしています。自己結合テーブルが必要だと思いますか?

-- Doesn't work:
SELECT tblA.MyDate, tblA.MyPet  FROM TestTable As tblA                      WHERE tblA.MyPet='Ape'
UNION
SELECT tblA.MyDate, tblA.MyPet  FROM TestTable AS tblA, TestTable AS tblB   WHERE tblA.MyPet='Ape' AND tblA.MyDate>tblB.MyDate-0.5 AND tblA.MyDate<tblB.MyDate
ORDER BY tblA.MyDate ASC

-- Doesn't work:
SELECT * FROM TestTable As tblA     INNER JOIN TestTable As tblB ON tblA.MyPet =         'Ape'     AND tblA.MyDate>tblB.MyDate-1  AND tblA.MyKey>tblB.MyKey

-- Doesn't work:
SELECT * FROM TestTable WHERE MyDate IN (SELECT MyDate-1 FROM TestTable WHERE MyPet='Ape')

-- Doesn't work
SELECT id, uid, date FROM orders current
WHERE EXISTS 
(
   SELECT * from orders future 
   WHERE future.date < DateAdd(DAYS, 1, current.date)
   AND future.date > getdate()
   AND future.uid = current.uid
)

-- Doesn't work
SELECT * FROM TestTable AS tblA
WHERE EXISTS
( 
   SELECT * FROM TestTable AS tblB
   WHERE tblB < DateAdd(DAYS, 1, tblA.MyDate)
   AND tblB.MYDate > GetDate()
   AND tblA.MyKey = tblB.MyKey
)

=========================== 表は次のとおりです。

CREATE TABLE TestTable   
(
MyKey       Int             IDENTITY(1,1)   PRIMARY KEY,     

MyDate      DateTime        NOT NULL    DEFAULT GetDate(),
MyPet       VarChar(22)     NOT NULL    DEFAULT ''
)

INSERT INTO TestTable(MyDate, MyPet)   VALUES('01-Dec-2012 06:12:10', 'Cat'   )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('01-Dec-2012 10:11:10', 'Dog'   )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('01-Dec-2012 14:13:10', 'Fish'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('01-Dec-2012 16:14:10', 'Duck'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('01-Dec-2012 17:15:10', 'Bird'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('01-Dec-2012 20:16:10', 'Kitten')
INSERT INTO TestTable(MyDate, MyPet)   VALUES('02-Dec-2012 01:17:10', 'Dog'   )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('02-Dec-2012 12:19:10', 'Fish'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('02-Dec-2012 13:20:10', 'Duck'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('02-Dec-2012 14:21:10', 'Bird'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('02-Dec-2012 16:18:10', 'Cat'   )     -- These are within 1 day before any "Ape" record

INSERT INTO TestTable(MyDate, MyPet)   VALUES('03-Dec-2012 05:26:10', 'Dog'   )     --
INSERT INTO TestTable(MyDate, MyPet)   VALUES('03-Dec-2012 11:22:10', 'Kitten')     --
INSERT INTO TestTable(MyDate, MyPet)   VALUES('03-Dec-2012 12:23:10', 'Duck'  )     --
INSERT INTO TestTable(MyDate, MyPet)   VALUES('03-Dec-2012 13:24:10', 'Bird'  )     --
INSERT INTO TestTable(MyDate, MyPet)   VALUES('03-Dec-2012 14:25:10', 'Ape'   )     -- An "Ape" record
INSERT INTO TestTable(MyDate, MyPet)   VALUES('03-Dec-2012 16:27:10', 'Cat'   )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('04-Dec-2012 01:32:10', 'Dog'   )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('04-Dec-2012 04:28:10', 'Fish'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('04-Dec-2012 07:30:10', 'Bird'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('04-Dec-2012 10:31:10', 'Kitten')
INSERT INTO TestTable(MyDate, MyPet)   VALUES('04-Dec-2012 16:29:10', 'Duck'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('05-Dec-2012 11:35:10', 'Kitten')
INSERT INTO TestTable(MyDate, MyPet)   VALUES('05-Dec-2012 12:36:10', 'Duck'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('05-Dec-2012 13:33:10', 'Duck'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('05-Dec-2012 13:37:10', 'Bird'  )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('05-Dec-2012 14:34:10', 'Dog'   )
INSERT INTO TestTable(MyDate, MyPet)   VALUES('06-Dec-2012 04:41:10', 'Fish'  )     -- These are within 1 day before any "Ape" record
INSERT INTO TestTable(MyDate, MyPet)   VALUES('06-Dec-2012 05:39:10', 'Dog'   )     --
INSERT INTO TestTable(MyDate, MyPet)   VALUES('06-Dec-2012 14:38:10', 'Kitten')     --
INSERT INTO TestTable(MyDate, MyPet)   VALUES('06-Dec-2012 16:40:10', 'Ape'   )     -- An "Ape" record
INSERT INTO TestTable(MyDate, MyPet)   VALUES('06-Dec-2012 16:42:10', 'Duck'  )
4

4 に答える 4

2

これはあなたが望むものです:

SELECT DISTINCT
    tblA.*
FROM TestTable AS tblA
    INNER JOIN TestTable AS tblB
        ON (tblA.MyDate >= DATEADD(day, -1, tblB.MyDate)) AND (tblA.MyDate <= tblB.Mydate) AND (tblB.MyPet = 'Ape');

これが動作するSQLFiddleです。

アップデート

DISTINCT以下のゴードンのコメントごとに追加されました。

于 2013-02-28T20:35:06.500 に答える
1

あなたが探している処方は次のとおりだと思います:

SELECT *
FROM TestTable tblA
WHERE EXISTS (SELECT *
              FROM TestTable tblB
              WHERE tblB.mydate between tblA.MyDate - X and tblA.MyDate and
                    tblB.MyPet = 'Ape'
             )

2 つのメモ。まず、これは構文「date - x」を使用します。これは、日付が であるdatetimeが ではない場合に機能しますdate。と同等dateadd(day, -x, tbla.MyDate)です。

次に、これは「Ape」を含むすべてのレコードを返します。これbetweenは、同等性を処理するためです。

Jesse のクエリは、外部結合と個別を使用して本質的に同じことを行います。

于 2013-02-28T21:13:41.163 に答える
0

SQLフィドル

クエリ1

-- Doesn't work:
SELECT tblA.MyDate, tblA.MyPet  
FROM TestTable As tblA
WHERE tblA.MyPet='Ape'
UNION all
SELECT tblA.MyDate, tblA.MyPet  
FROM TestTable AS tblA
WHERE dateadd(day, 1, tblA.MyDate) < (select min(MyDate) from TestTable where MyPet = 'Ape')
ORDER BY tblA.MyDate ASC

結果

|                          MYDATE |  MYPET |
--------------------------------------------
| December, 01 2012 06:12:10+0000 |    Cat |
| December, 01 2012 10:11:10+0000 |    Dog |
| December, 01 2012 14:13:10+0000 |   Fish |
| December, 01 2012 16:14:10+0000 |   Duck |
| December, 01 2012 17:15:10+0000 |   Bird |
| December, 01 2012 20:16:10+0000 | Kitten |
| December, 02 2012 01:17:10+0000 |    Dog |
| December, 02 2012 12:19:10+0000 |   Fish |
| December, 02 2012 13:20:10+0000 |   Duck |
| December, 02 2012 14:21:10+0000 |   Bird |
| December, 03 2012 14:25:10+0000 |    Ape |
| December, 06 2012 16:40:10+0000 |    Ape |

編集

最初のApeレコードの前のx日間にレコードが発生する必要がある場合、クエリは次のようになります(たとえば、x = 1

SELECT tblA.MyDate, tblA.MyPet  
FROM TestTable As tblA
WHERE tblA.MyPet='Ape'
UNION
SELECT tblA.MyDate, tblA.MyPet  
FROM TestTable AS tblA
WHERE (select min(MyDate) from TestTable where MyPet = 'Ape') between tblA.MyDate and dateadd(day, 1, tblA.MyDate)
ORDER BY tblA.MyDate ASC
于 2013-02-28T20:40:14.230 に答える
0
  1. プロセス: クエリの各半分を個別に記述し、テストして機能することを確認します。次にそれらを UNION でまとめます。
  2. 重複を削除する場合は、UNION の代わりに UNION ALL を使用してください。
于 2013-02-28T20:27:14.940 に答える