2

これを行う簡単な方法はありますか?6 年のうち少なくとも 2 年分の料金コードを持つ世帯のみが必要です - (cy= 今年 ly = 昨年など) ??

Exists (SELECT * FROM ac_ViewMultiHouseholdChargeCodeYearCode AS VMH 
WHERE 
aa_Households.HouseholdID = VMH.HouseholdiD and 
VMH.ChargeCode = 'Dinner' and 
(VMH.ChargeCount_CY > 0 and
 (VMH.ChargeCount_LY > 0 or
  VMH.ChargeCount_2Y > 0 or 
  VMH.ChargeCount_3Y > 0 or 
  VMH.ChargeCount_4Y > 0 or 
  VMH.ChargeCount_5Y > 0)) or 
(VMH.ChargeCount_LY > 0 and 
 (VMH.ChargeCount_CY > 0 or
  VMH.ChargeCount_2Y > 0 or
  VMH.ChargeCount_3Y > 0 or 
  VMH.ChargeCount_4Y > 0 or
  VMH.ChargeCount_5Y > 0)) or
(VMH.ChargeCount_2Y > 0 and 
 (VMH.ChargeCount_LY > 0 or
  VMH.ChargeCount_CY > 0 or 
  VMH.ChargeCount_3Y > 0 or
  VMH.ChargeCount_4Y > 0 or
  VMH.ChargeCount_5Y > 0)) or
(VMH.ChargeCount_3Y > 0 and
 (VMH.ChargeCount_LY > 0 or 
  VMH.ChargeCount_2Y > 0 or
  VMH.ChargeCount_CY > 0 or
  VMH.ChargeCount_4Y > 0 or
  VMH.ChargeCount_5Y > 0)) or
(VMH.ChargeCount_4Y > 0 and
 (VMH.ChargeCount_LY > 0 or
  ChargeCount_2Y > 0 or
  VMH.ChargeCount_3Y > 0 or
  VMH.ChargeCount_CY > 0))) 
4

1 に答える 1

5

あなたはそれを次のようなものに単純化することができます:

where (
  (case when VMH.ChargeCount_CY > 0 then 1 else 0 end) +
  (case when VMH.ChargeCount_LY > 0 then 1 else 0 end) +
  (case when VMH.ChargeCount_2Y > 0 then 1 else 0 end) +
  (case when VMH.ChargeCount_3Y > 0 then 1 else 0 end) +
  (case when VMH.ChargeCount_4Y > 0 then 1 else 0 end) +
  (case when VMH.ChargeCount_5Y > 0 then 1 else 0 end)
) >= 2
于 2012-09-25T00:10:07.527 に答える