2016-11-03 5 views
0

私はSQLを初めて使用しています。私は今年のユニークな価値を数える必要があるテーブルを持っています。もしそれが何年も前に存在しなければ(基本的に私は2016年の新しいエントリだけを知りたい)この場合実際の年の一意の値をカウントします(前年には存在しません)

BP Name  Year 
A    2013 
B    2016 
A    2014 
A    2016 
C    2012 
C    2016 

だけ1(BP名:B)のようにカウントさ

おかげこれに

Kastriot
+0

使用しているDBMS /サーバーはどれですか? –

+0

Zohoレポートを使用しています(これが役立つかどうかはわかりません)。 – Kastriot

答えて

1

を試してみてください=テーブル名 BPの名前と年=列

select t."BP Name", t."Year" from 
(select "BP Name", "Year" from "unique values this year" where year("Year")=2016) t 
left join (select "BP Name" from "unique values this year" where year("Year")!=2016) t1 on t."BP Name"=t1."BP Name" 
    where t1."BP Name" is null 
+0

タイタスに感謝します。これを必要とするかもしれない他の人のための 私の最後のクエリ(つまりテーブルと列の名前は、私はここに掲載するものとは異なりますので予めご了承ください):。 'DISTINCTトンを選択「BP名」 (\t FROM" \t \t \tを選択BP名」、 \t \t \t "Disbursment日 "(Disbursment日 ")SB_MD \t年は \t"" \t FROM" LEFTトン年(GETDATE()) =)を登録しよう(\t SELECT "BP名前" \t FROM "SB_MD"\t WHERE \t年(配当日)<年(getdate)) )t1 ON t。 "BP名" = t1。 "BP名" WHERE \t t1。 "BP名"がnullです。 – Kastriot

0

チェック。

select count(*) from table1 a 
where a.year=2016 
and not exists (select 1 from table1 b where a.bpname = b.bpname and a.year > b.year) 
0
SELECT COUNT(BPName),BPName,_Year FROM #Temp otr WHERE NOT EXISTS(SELECT 1 FROM #Temp inr WHERE inr.BPName = otr.BPName and inr._Year > otr._Year) GROUP BY BPName,_Year 
1

I集約2つのレベルでこれに近づくことになります。以下のすべての年のカウントん:ちょうど2016の場合

select first_year, count(*) 
from (select pbname, min(year) as first_year 
     from t 
     group by pbname 
    ) t 
group by first_year; 

"一意の値は今年を":

select count(*) 
from (select pbname, min(year) as first_year 
     from t 
     group by pbname 
    ) t 
where first_year = 2016; 
1

これは動作するはずです。この

select count(*) 
from 
(select Distinct bpname,year 
from tmpBP a 
where a.year=2016 
and not exists (select 1 from tmpBP b where a.bpname = b.bpname and a.year > b.year 
) 
)x 
関連する問題