0

我必须在同一个表中选择两个project.project_dates(一个用于开始,一个用于结束)我有两个字段project.start = trueproject.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 回答