0

ここに画像の説明を入力

列の横にある各性別の合計数を示す別の列を探していeduCountsます。残りのクエリは正常に機能します。この場合、異なる性別の数を見つけるには、次のようにする必要があります。select count(distinct patid) from...

初挑戦:

declare @sexcounts int
set @sexcounts = case when members.sex 'm' then (select COUNT(distinct patid) from members where sex='m')
                 when members.sex 'f' then (select COUNT(distinct patid) from members where sex='f')

select sex, edutext, COUNT(*) as eduCounts from 
(
select distinct m.patid, m.sex, e.eduText
    from members as m
    inner join claims as c on c.patid=m.PATID
    inner join icdClm as ic on ic.clmid=c.clmid
    inner join tblICD as t on t.ICD=ic.icd
    inner join EducationTable as e on e.eduID=m.education
    inner join IncomeTable as i on i.incomeID=m.income
    where ISNUMERIC(ic.icd)=1 and SUBSTRING(ic.icd,1,3)='707'
)t
group by sex, eduText
order by sex

2回目の試行:

declare @sexcounts int
set @sexcounts = (select COUNT(distinct patid),case when sex 'm' then (select COUNT(distinct PATID) from members where sex='m') else (select COUNT(distinct patid) from members where sex='f') 
                    from members)

    select sex, edutext, COUNT(*) as eduCounts from 
    (
    select distinct m.patid, m.sex, e.eduText
        from members as m
        inner join claims as c on c.patid=m.PATID
        inner join icdClm as ic on ic.clmid=c.clmid
        inner join tblICD as t on t.ICD=ic.icd
        inner join EducationTable as e on e.eduID=m.education
        inner join IncomeTable as i on i.incomeID=m.income
        where ISNUMERIC(ic.icd)=1 and SUBSTRING(ic.icd,1,3)='707'
    )t
    group by sex, eduText
    order by sex

派生テーブルを作成して性別列に結合できることは知っていますが、可能であれば変数を使用してそれを行う方法を知りたいです。

4

1 に答える 1

1

これを意味しますか:

select Sex, eduText, eduCounts, sum(eduCounts) over (Partition by Sex) from(
select Sex, eduText, count(eduText) eduCounts
From TableName group by Sex, eduText)x
于 2012-11-07T13:32:59.107 に答える