1

私はSQLに比較的慣れていないので、これについて助けてください。基本的に、関数が少し遅いため、クエリでいくつかの計算を実行しようとしています。これを改善できますか?

-- Query to retrive ADA from every school
Select  Distinct DY, 
        DATENAME(MM,DT) as 'Month',
        CONVERT(nvarchar,DT,101) as 'Date',
        (
            Select ((@M_stu - (Select Count(SC) from dbo.ATT where sc=1 AND al!='' AND DY=T.DY))/@M_stu)*100
        ) as 'Merced ADA',
        (
            Select ((@A_stu - (Select Count(SC) from dbo.ATT where sc=2 AND al!='' AND DY=T.DY))/@A_stu)*100
        ) as 'Atwater ADA',
        (
            Select ((@L_stu - (Select Count(SC) from dbo.ATT where sc=3 AND al!='' AND DY=T.DY))/@L_stu)*100
        ) as 'Livingston ADA',
        (
            Select ((@B_stu - (Select Count(SC) from dbo.ATT where sc=4 AND al!='' AND DY=T.DY))/@B_stu)*100
        ) as 'Buhach ADA',
        (
            Select ((@Y_stu - (Select Count(SC) from dbo.ATT where sc=5 AND al!='' AND DY=T.DY))/@Y_stu)*100
        ) as 'Yosemite ADA',
        (
            Select ((@I_stu - (Select Count(SC) from dbo.ATT where sc=6 AND al!='' AND DY=T.DY))/@I_stu)*100
        ) as 'Independence ADA',
        (
            Select ((@G_stu - (Select Count(SC) from dbo.ATT where sc=10 AND al!='' AND DY=T.DY))/@G_stu)*100
        ) as 'Golden Valley ADA',
        (
            Select ((@S_stu - (Select Count(SC) from dbo.ATT where sc=92 AND al!='' AND DY=T.DY))/@S_stu)*100
        ) as 'Sequoia ADA'
From dbo.ATT as T
Order by DY ASC
4

3 に答える 3

3

この解決策を試してください:

SELECT  DISTINCT 
        t.DY                            AS DY, 
        DATENAME(MM,t.DT)               AS [Month],
        CONVERT(NVARCHAR,t.DT,101)      AS [Date],
        ((@M_stu - d.[1])/@M_stu)*100   AS [Merced ADA],
        ((@A_stu - d.[2])/@A_stu)*100   AS [Atwater ADA],
        ((@L_stu - d.[3])/@L_stu)*100   AS [Livingston ADA],
        ((@B_stu - d.[4])/@B_stu)*100   AS [Buhach ADA],
        ((@Y_stu - d.[5])/@Y_stu)*100   AS [Yosemite ADA],
        ((@I_stu - d.[6])/@I_stu)*100   AS [Independence ADA],
        ((@G_stu - d.[10])/@G_stu)*100  AS [Golden Valley ADA],
        ((@S_stu - d.[92])/@S_stu)*100  AS [Sequoia ADA]
FROM    dbo.ATT AS t
OUTER APPLY (
    SELECT  c.*
    FROM (
        SELECT  a.sc
        FROM    dbo.ATT AS a
        WHERE   a.sc IN (1,2,3,4,5,6,10,92) 
        AND     a.al!='' 
        AND     a.DY=t.DY
    ) b
    PIVOT( COUNT(b.sc) FOR a.sc IN ([1],[2],[3],[4],[5],[6],[10],[92]) ) c
) d
ORDER BY t.DY ASC;

次のインデックスのいずれかがあるかどうかも確認する必要があります:CREATE [UNIQUE] INDEX index_name ON dbo.ATT (DY, sc, al)またはCREATE [UNIQUE] INDEX index_name ON dbo.ATT (DY, sc) INCLUDE (al)

于 2013-05-31T15:39:42.833 に答える
3

これを試して

Select  DY, 
        DATENAME(MM,DT) as 'Month',
        CONVERT(nvarchar,DT,101) as 'Date',
        @M_stu - (Count(case when sc=1 AND al!='' AND DY=T.DY then 1 else null end)/@M_stu)*100 as 'Merced ADA',
        @A_stu - (Count(case when sc=2 AND al!='' AND DY=T.DY then 1 else null end)/@A_stu)*100 as 'Atwater ADA',
        ...
From dbo.ATT as T
GROUP BY DY, DT
Order by DY ASC
于 2013-05-31T14:50:26.760 に答える
1

結果を現在のクエリと比較する必要がありますが、これはおそらくこれを達成する方法です。

Select  Distinct DY, 
        DATENAME(MM,DT) as Month,
        CONVERT(char(10),DT,101) as Date,
        @M_stu - sum(case when sc=1 then 1.0 else 0.0 end)/@M_stu)*100 as [Merced ADA],
        @A_stu - sum(case when sc=2 then 1.0 else 0.0 end)/@A_stu)*100 as [Atwater ADA],
        @L_stu - sum(case when sc=3 then 1.0 else 0.0 end)/@L_stu)*100 as [Livingston ADA],
        @B_stu - sum(case when sc=4 then 1.0 else 0.0 end)/@B_stu)*100 as [Buhach ADA],
        @Y_stu - sum(case when sc=5 then 1.0 else 0.0 end)/@Y_stu)*100 as [Yosemite ADA],
        @I_stu - sum(case when sc=6 then 1.0 else 0.0 end)/@I_stu)*100 as [Independence ADA],
        @G_stu - sum(case when sc=10 then 1.0 else 0.0 end)/@G_stu)*100 as [Golden Valley ADA],
        @S_stu - sum(case when sc=92 then 1.0 else 0.0 end)/@S_stu)*100 as [Sequoia ADA]
From    dbo.ATT as T
Where   al!=''
Order by DY ASC

テストするサンプル データがなければ、これが最も簡単な方法だと思います。

于 2013-05-31T20:17:14.463 に答える