Question

So the title may be misleading, however I can't really think how to word it.

In theory, lets say I have a table of 7000 records with the column Year.

Year data ranges from 1990 to 2011 but their are hundreds of repeat occurences of the same year.

Using my current SQL statement:

SELECT MonthlySales.Year FROM MonthlySales

simply would return 7000 records.

I need to get a count of each individual occurence of each year ONCE in a table (so there should be 21 records in my table). I am using PHP and SQL server 2008.

I originally just filled in the options form like this:

<option>1990</option>
<option>1991</option>

However I have to allow the user to input more other Year's if need's be in to my data so it has to be dynamic. With the data I hope to use it in a HTML form that gives the option to select a year:

<option>.$year.</option> 

Could someone clarify whether this would also work.

I just cannot think how to do it! Even though it appears simple.

Thanks for looking.

Was it helpful?

Solution

To get the number of rows that each year appears

select count(*), year
from MonthlySales
group by year;

To simply remove duplicates

SELECT DISTINCT Year FROM MonthlySales;

OTHER TIPS

the below SQL will give you the number of times the year appears in the table and the year

select count(Year), year from MonthlySales group by year

if you want the amount a specific year appears, just add a where cause like

select count(Year), year from MonthlySales where year = ? group by year
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top