私はvfpでSQLを使用していますので、私のコマンドの中にはいくつかの違いがあります。 case when
の代わりにiif([condition],[if true],[if false])
が使用されます。行う必要があり、このコードは、各ギフトカテゴリ内寄贈しましたどのように多くのドナーを見つけることは何フィールドが一意である場合にcount()を選択してください
SELECT
giftsource,
COUNT(donor) AS total_gifts, /*this is not the one that need fixed*/
SUM(amt) AS total_amt,
SUM(iif(unique = 1 AND language != 'F' AND renew = '0' AND type = 'IN',1,0)) as d_Count_E_New_Indiv,
SUM(iif(unique = 1 AND language = 'F' AND renew = '0' AND type = 'IN',1,0)) as d_Count_F_New_Indiv,
SUM(iif(unique = 1 AND language != 'F' AND renew != '0' AND type = 'IN',1,0)) as d_Count_E_Re_Indiv,
SUM(iif(unique = 1 AND language = 'F' AND renew != '0' AND type = 'IN',1,0)) as d_Count_F_Re_Indiv,
SUM(iif(unique = 1 AND language != 'F' AND renew = '0' AND type != 'IN',1,0)) as d_Count_E_New_Org,
SUM(iif(unique = 1 AND language = 'F' AND renew = '0' AND type != 'IN',1,0)) as d_Count_F_New_Org,
SUM(iif(unique = 1 AND language != 'F' AND renew != '0' AND type != 'IN',1,0)) as d_Count_E_Re_Org,
SUM(iif(unique = 1 AND language = 'F' AND renew != '0' AND type != 'IN',1,0)) as d_Count_F_Re_Org,
FROM (select *,
cast(/* equivalent to a bunch of if elses*/
iif( list_code="WEB","1",
iif( list_code="GRO","2",
iif( list_code="CHO","3",
iif( list_code="TEL","4",
iif( list_code="TES","5",
iif( list_code="POS" AND amt < 10000,"6",
iif((LIKE(list_code,"4%") OR list_code = "4") AND amt < 10000,"7",
iif((LIKE(list_code,"4%") OR list_code = "4" OR list_code = "POS") AND amt >= 10000,"8",
iif( LIKE(list_code,"9%") OR list_code = "9","9",
"10"))))))))) as c(1))
as giftsource
from cGift) gift
LEFT JOIN
(select didnumb, language, type from dp) d
on cast(gift.donor as i) = cast(d.didnumb as i)
LEFT JOIN /*this does not do what i want it to*/
(select min(gidnumb) gid, 1 as unique from cGift group by donor) uGift
on gift.gidnumb = uGift.gid
GROUP BY giftsource
ORDER BY giftsource
:
は、ここに私のコードです。同じフィールド(つまり、同じlist_code/lang/renew/etc)内の重複したドナーを数えるべきではありませんが、別のフィールドの場合はドナーを2回カウントする必要があります。
例:ドナー#3はd_Count_E_New_Indivで1回しかカウントできませんが、d_Count_E_New_Orgでカウントすることもできます。
gidnumbがこのテーブルの主キーです。
私の2回目の参加で、私はテーブルの最初のドナーに(ユニークという名前の)フィールドを付けました。これは、1つのフィールドでドナーをカウントするだけなので、機能しません。
誰かがそれを行う正しい方法は何か教えていただけますか?また、私はもっとたくさんのSUM(...)を持っていますので、私の選択をあまりにも精選しない方が好きです。
編集:私は、私は視覚的なキツネはプロ持っていない私はあなたが離れてtotal_gifts AS COUNT(個別のドナー)とを得ることができると思い、次のselect count(distinct IIF(renew = '0' AND lang != 'F',donor,0)) FROM dpgift
ありがとう、私はこれに非常に似た何かを実装しました。 – slicedtoad