0

与えられたデータは次のようになります

PLI_ID   OWN_ID   DCSF_ID   SCH_NAME          PREMIUM1 PREMIUM2  DESCRIPTION  
901      2        1000     Dfe-School 1        86.40     7.20    Heads, Deps
902      2        1000     Dfe-School 2        403.30    8.40    Relief Bursar
903      2        1000     Dfe-School 3        327.00    8.40    £10.00 
904      2        1000     Dfe-School 4        381.50    8.40    £11.00 
905      2        1000     Dfe-School 5        152.60    8.40    Teaching staff

期待されるデータは次のようになります

School          £10.00  £11.00  Heads, Deps  Relief Bursar  Teaching staff  Total
Dfe-School 1                            
Dfe-School 2                            
Dfe-School 3                            
Dfe-School 4                            

Total                                                                  [Pre-Total]

提案されているように、クロスタブクエリとクロスオーバークエリのどちらを作成するかわかりません。さらに説明が必要な場合は、お知らせください。

私のクエリは約6つのテーブルを使用しており、次のようになります。

SELECT PolicyLine.PLI_POS_Id、Policy.POL_OWN_Id、Policy.POL_DCSF、School.SCH_Name、PolicyLine.PLI_Premium、CoverPremium.CPR_Premium、StaffCategory.SCA_Description FROM
学校
School.SCH_OWN_Id=Policy.POL_OWN_IdおよびSchool.SCH_DCSF=Policy.POL_DCSFの内部結合ポリシー
INNER JOIN PolicyLine ON Policy.POL_Id = PolicyLine.PLI_POL_Id
PolicyLine.PLI_COP_Id=CoverOption.COP_Idの内部結合CoverOption
INNER JOIN CoverPremium ON CoverOption.COP_Id = CoverPremium.CPR_COP_Id AND Policy.POL_OWN_Id = CoverPremium.CPR_OWN_Id
CoverOption.COP_SCA_Id=StaffCategory.SCA_のRIGHTOUTERJOIN StaffCategory
4

2 に答える 2

1

既存のクエリに基づいて、次のようなものを使用します。

SELECT School.SCH_Name school,
  [£10.00], [£11.00], 
  [Heads, Deps], [Relief Bursar], [Teaching staff]
FROM
(
  SELECT PolicyLine.PLI_POS_Id, 
    Policy.POL_OWN_Id, 
    Policy.POL_DCSF, 
    School.SCH_Name, 
    PolicyLine.PLI_Premium, 
    CoverPremium.CPR_Premium, 
    StaffCategory.SCA_Description 
  FROM School 
  INNER JOIN Policy 
    ON School.SCH_OWN_Id = Policy.POL_OWN_Id 
    AND School.SCH_DCSF = Policy.POL_DCSF 
  INNER JOIN PolicyLine 
    ON Policy.POL_Id = PolicyLine.PLI_POL_Id 
  INNER JOIN CoverOption 
    ON PolicyLine.PLI_COP_Id = CoverOption.COP_Id 
  INNER JOIN  CoverPremium 
    ON CoverOption.COP_Id = CoverPremium.CPR_COP_Id 
    AND Policy.POL_OWN_Id = CoverPremium.CPR_OWN_Id 
  RIGHT OUTER JOIN StaffCategory 
    ON CoverOption.COP_SCA_Id = StaffCategory.SCA_
) x
PIVOT
(
  sum(PLI_Premium)
  for SCA_Description in([£10.00], [£11.00], 
                     [Heads, Deps], [Relief Bursar], [Teaching staff])
) p

デモで SQL Fiddle を参照してください

于 2012-08-15T10:15:55.847 に答える
0

おそらくPIVOTクエリが必要ですが、あなたの例から、それがどのように見えるべきかを伝えるのは難しいです.

SELECT *
FROM yourtable 
PIVOT 
(SUM(Premium) FOR sch_name in ([£10.00],[£11.00],[Heads, Deps],[Relief Bursar],[Teaching staff])) 
于 2012-08-15T08:27:16.133 に答える