1

次のような Excel テーブルを生成する ColdFusion ページを作成しています。ここに画像の説明を入力

データベース内の 1 つのテーブルから複数の列の情報を生成したいと考えています。結果を得るには、列ごとに SELECT COUNT が効果的に必要です。このような;

Select count (yT) as pageOneView from tTable where userGUID=#Query.guid# AND id='00101'

Select count (yT) as pageTwoView from tTable where userGUID=#Query.guid# AND id='00201'

Select count (yT) as pageThreeView from tTable where userGUID=#Query.guid# AND id='00301'

変更される詳細は、カウント名と ID のみであることに注意してください。for eachを使用する代わりに、これらを 1 つに結合するにはどうすればよいですか?<cfquery><cfquery>

4

2 に答える 2

4

コメントに応じて編集

「ブール値」のみを表示したい場合は、すべての行をカウントするのは非効率的です。

select
    case when exists
        (select * from tTable
            where guid = #Query.guid# 
                and yT is not null  
                and id = '00101')
        then 'True' else 'False' end pageOneView,
    case when exists
        (select * from tTable
            where guid = #Query.guid# 
                and yT is not null  
                and id = '00201')
        then 'True' else 'False' end pageTwoView,
    case when exists
        (select * from tTable
            where guid = #Query.guid# 
                and yT is not null  
                and id = '00301')
        then 'True' else 'False' end pageThreeView

このような、

select
        count(case id when '00101' then yT end) pageOneView,
        count(case id when '00201' then yT end) pageTwoView,
        count(case id when '00301' then yT end) pageThreeView
    from
        tTable
    where
        userGUID = #Query.guid#;

Andomar の回答では使用するコードが少なくなり、SQL エンジンによってより効率的に処理される可能性がありますが、必要なスキーマを含む結果セットが返されないことに注意してください。

于 2013-04-29T09:20:38.213 に答える
1
select  id
,       count (itn)
from    tTable 
where   userGUID = #Query.guid#
        and id in ('00101', '00201', '00301')
group by
        id
于 2013-04-29T09:14:15.180 に答える