Kastriot Kastriot - 9 months ago 32
SQL Question

Count unique values of actual year (not present in previous years)

I am new to SQL and I was hoping you can help me with a question. I have a table from which I need to count unique values of this year, if they do not exist in years before (Basically I want to know only new entries for 2016).

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

In this case it would count only as 1 (BP Name: B)


Answer Source

This should work:

"unique values this year" =Table Name BP Name and Year = Columns

 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