0

同じテーブルで 2 つproject.project_dates(開始用と終了用に 1 つ) を選択する必要があります。テーブルには 2 つのフィールドがproject.start = trueありproject.end = true、それを識別するために

SQL クエリはどのようになりますか?

私が試してみました:

SELECT 
    pz.cognome AS Cognome,
    (select s.presa_in_carico_data from tbl_progetto s where s.is_progetto = 1 AND                    
        s.cc_id_fk = cc.cc_id) as Start,
    (select e.presa_in_carico_data from tbl_progetto e where e.is_dimissione = 1 AND 
        e.cc_id_fk = cc.cc_id) as End, 
    trf.trf_note AS "Tipo trattam.",
    trf.trf_prezzou as Retta,
    COUNT(tra.trt_id) AS "N.GG." /*<-- this is normal count from date range select from user(#) */
FROM
    tbl_progetto p 
    JOIN tbl_cartellaclinica cc ON cc.cc_id = p.cc_id_fk
    JOIN tbl_paziente pz ON pz.id = cc.pz_fk_id
    JOIN tbl_distretti_sanitari di ON pz.distretto_appartenenza = di.dss_id
    JOIN tbl_trattamenti tra ON p.pr_id = tra.pr_fk_id    
WHERE
    tra.trt_data BETWEEN '2012-08-01' AND '2012-08-31' AND
    p.pr_faseriab_fk = 4
GROUP BY cc_id
4

1 に答える 1

1
select p.id as ProjectId,
(select s.date from project_dates s where s.start = true and s.project_id = p.id) as StartDate,
(select e.date from project_dates e where e.end = true and e.project_id = p.id) as EndDate
into #temp
from Project p 

アップデート:

あなたのコメントに答えるには、あなたのデータベースで物事がどのように見えるかについて仮定する必要がありますが、私は次のようにします:

上記のクエリの結果を格納するための一時テーブルを作成します (変更を参照してください)。

select p.id
       (select count(1) from Meeting m where m.date between '2012-01-01' and '2012-08-31' and m.project_id = p.id) as Count1
       (select count(1) from Meeting m where m.date between (select StartDate from #temp where ProjectId = p.id) and (select EndDate from #temp where ProjectId = p.id) and m.project_id = p.id) as count2
from Project p 
[where p.id = <something>]
于 2012-09-20T18:43:07.890 に答える