私のピボットクエリが生成します。別の複雑なテーブルとピボットに参加
+-----------+----+----+---+---+---+---+---+
| client_id | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+-----------+----+----+---+---+---+---+---+
| 216436 | 9 | 0 | 0 | 0 | 0 | 0 | 0 |
| 110522 | 76 | 3 | 0 | 0 | 0 | 0 | 0 |
| 214981 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
| 216360 | 52 | 1 | 0 | 0 | 0 | 0 | 0 |
| 102574 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 211754 | 97 | 14 | 2 | 0 | 0 | 0 | 0 |
| 210734 | 8 | 4 | 0 | 0 | 0 | 0 | 0 |
| 10| 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 101840 | 2 | 0 | 0 | 0 | 0 | 0 | 0 |
+-----------+----+----+---+---+---+---+---+
ここにクエリです:
select client_id,
[1],[2],[3],[4],[5],[6],[7] -- these are timestested (the amount of times tested)
from
( SELECT DISTINCT CLIENT_ID
, PATIENT_ID
, count(*) over (partition by client_id, patient_id) AS patientcount
from f_accession_daily) as SourceTable
PIVOT
(
count(patient_id)
for patientcount in ([1],[2],[3],[4],[5],[6],[7])
) as pivottable
私は(のために、テストしたすべての時間のための最大/最小の日付を持参する必要がありますが、 [1]、[2]、[3]、など)この表から:
+-----------+-------------+-------+------------+------------+
| client_id | TimesTested | count | maxRecDate | minRecDate |
+-----------+-------------+-------+------------+------------+
| 100034 | 2 | 1 | 6/25/2008 | 6/23/2008 |
| 100034 | 1 | 20 | 6/30/2008 | 6/19/2008 |
| 100038 | 3 | 1 | 7/25/2008 | 7/23/2008 |
| 100038 | 1 | 4 | 7/25/2008 | 7/1/2008 |
| 100050 | 1 | 15 | 8/11/2008 | 7/14/2008 |
| 100060 | 1 | 2 | 8/12/2008 | 7/29/2008 |
| 100070 | 1 | 3 | 8/15/2008 | 8/15/2008 |
| 100049 | 1 | 3 | 8/22/2008 | 7/11/2008 |
| 100029 | 3 | 2 | 8/25/2008 | 6/18/2008 |
+-----------+-------------+-------+------------+------------+
上記テーブルが生成されます:私は取得する必要があり
SELECT a.client_id AS client_id
,a.patientcount TimesTested
, count(a.patientcount)/a.patientcount AS count
, max(f.received_date) AS maxRecDate
, min(f.received_date) AS minRecDate
FROM
(
SELECT DISTINCT CLIENT_ID
, PATIENT_ID
, count(*) over (partition by client_id, patient_id) AS patientcount
from f_accession_daily
) AS a
JOIN F_ACCESSION_DAILY AS f ON a.CLIENT_ID = f.CLIENT_ID
AND a.PATIENT_ID = f.PATIENT_ID
GROUP BY a.CLIENT_ID, a.patientcount
結果のテーブル:
+-----------+----+----------+-----------+----+----------+-----------+---+----------+-----------+---+----------+-----------+-----+
| client_id | 1 | maxdate1 | mindate1 | 2 | maxdate2 | mindate2 | 3 | maxdate3 | mindate3 | 4 | maxdate4 | mindate4 | 5 |
+-----------+----+----------+-----------+----+----------+-----------+---+----------+-----------+---+----------+-----------+-----+
| 216436 | 9 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | etc |
| 110522 | 76 | 1/1/2011 | 1/23/1984 | 3 | 1/1/2011 | 1/23/1984 | 0 | 1/1/2011 | 1/23/1984 | 0 | 2/1/2011 | 1/23/1984 | |
| 214981 | 0 | 1/1/2013 | 1/23/1985 | 1 | 1/1/2013 | 1/23/1985 | 0 | 1/1/2013 | 1/23/1985 | 0 | 1/1/2013 | 1/23/1985 | |
| 216360 | 52 | 1/1/2011 | 1/23/1985 | 1 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | |
| 102574 | 1 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2014 | 1/23/1980 | 0 | 2/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | |
| 211754 | 97 | 1/1/2012 | 1/23/1985 | 14 | 1/1/2012 | 1/23/1985 | 2 | 1/1/2012 | 1/23/1985 | 0 | 1/1/2012 | 1/23/1985 | |
| 210734 | 8 | 1/1/2011 | 1/23/1984 | 4 | 1/1/2011 | 1/23/1984 | 0 | 1/1/2011 | 1/23/1984 | 0 | 1/1/2011 | 1/23/1984 | |
| 10| 1 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1987 | |
| 101840 | 2 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1980 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | |
+-----------+----+----------+-----------+----+----------+-----------+---+----------+-----------+---+----------+-----------+-----+
は、どのように私は2つのテーブルを結合しますか?速度は重要ではありません!あなたの親切な助けをありがとう。
ので、それはclient_idの上の両方に参加しているように見える1、2、3、4 ... –
はい正しいのthats(TimesTestedがある) – user1760020
あなたが彼を必要とするときどこ@JNKがありますか? –