I have rows in a table in SQL Server 2008
Tell me please how select only unique years from table?
P.S.: in this table unique year is 2013
Use the YEAR function, with DISTINCT like this:
SELECT DISTINCT YEAR([date])
FROM Tablename;
SQL Fiddle Demo
This will give you:
| YEAR |
--------
| 2013 |
To use the order by clause, give it an alias and order by this alias not the original name like this:
SELECT DISTINCT YEAR([date]) AS Year
FROM Tablename
ORDER By Year;
Related
I have a table which has a numeric column named 'capacity'. I want to select first rows which the total sum of their capacity is no greater than X, Sth like this query
select * from table where sum(capacity )<X
But I know I can not use aggregation functions in where part.So what other ways exists for this problem?
Here is some sample data
id| capacity
1 | 12
2 | 13.5
3 | 15
I want to list rows which their sum is less than 26 with the order of id, so a query like this
select * from table where sum(capacity )<26 order by id
and it must give me
id| capacity
1 | 12
2 | 13.5
because 12+13.5<26
A bit late to the party, but for future reference, the following should work for a similar problem as the OP's:
SELECT id, sum(capacity)
FROM table
GROUP BY id
HAVING sum(capacity) < 26
ORDER by id ASC;
Use the PostgreSQL docs for reference to aggregate functions: https://www.postgresql.org/docs/9.1/tutorial-agg.html
Use Having clause
select * from table order by id having sum(capacity)<X
You can use the window variant of sum to produce a cumulative sum, and then use it in the where clause. Note that window functions can't be placed directly in the where clause, so you'd need a subquery:
SELECT id, capacity
FROM (SELECT id, capacity, SUM(capacity) OVER (ORDER BY id ASC) AS cum_sum
FROM mytable) t
WHERE cum_sum < 26
ORDER BY id ASC;
I haven't had much experience with SQL and it strikes me as a simple question, but after an hour of searching I still can't find an answer...
I have a table that I want to add up the totals for based on ID - e.g:
-------------
ID Quantity
1 30
2 11
1 4
1 3
2 17
3 16
.............
After summing the table should look something like this:
-------------
ID Quantity
1 37
2 28
3 16
I'm sure that I need to use the DISTINCT keyword and the SUM(..) function, but I can only get one total value for all unique value combinations in the table, and not separate ones like above. Help please :)
Select ID, Sum(Quantity) from YourTable
Group by ID
You can find here some resources to learn more about "Group by": http://www.w3schools.com/sql/sql_groupby.asp
SELECT ID, SUM(QUANTITY) FROM TABLE1 GROUP BY ID ORDER BY ID;
Select ID, Sum(Quantity) AS Quantity
from table1
Group by ID
Replace table1 with name of the table.
Just posting a complete answer that aliases the column and orders the results:
SELECT ID, SUM(Quantity) as [Quantity]
FROM TableName
GROUP BY ID
ORDER BY ID
Say my start year is 2000 and I would like to have a one column select return every year from 2000 to the current year, example:
2000
2001
...
2012
2013
This is to populate a parameter in Reporting Services.
The easiest thing for you to do would be to create a numbers table that you would use for these types of queries.
You could also use a recursive Common Table Expression to generate the list of years:
;with cte (yr) as
(
select 2000
union all
select yr + 1
from cte
where yr+1 <=2013
)
select yr
from cte;
See SQL Fiddle with Demo
I have a table in which the first two rows are Company, Year. Each company will have some years, but not necessarily all of them:
ABC | 2010
ABC | 2011
ABC | 2012
BBC | 2011 //does not have all the years, don't want to select it
I'd like to select a list of companies which have ALL the years (not just some of them), but I'm having trouble writing a select query to do that. I imagine this is really easy but I can't figure it out for some reason.
try
select company
from your_table
group by company
having count(distinct year) = (select count(distinct year) from your_table)
Select * FROM Company Where CompanyId In(
select CompanyId From Company
group by CompanyId
having count(*) = (select count(distinct Year) from Company)
)
http://www.sqlfiddle.com/#!3/c2f81/11
Note that if you alread know how many years there should be, then obviously you would just say that number instead of doing a select distinct year.
SELECT Company
FROM Table
GROUP BY Company
HAVING COUNT(Distinct YEAR) = 3
I have table named usertable and structure is
id Name Year
==========================
1 a 2010
____________________________
2 b 2008
____________________________
3 c 2010
____________________________
4 d 2007
____________________________
5 e 2008
Now I want the Output result like this
Year
==========
2010
____________
2008
____________
2007
I don't know the SQL query .
So please help me.
Every Ideas and suggestions are welcome.
Not exactly sure what you're looking for, but if you're looking for the years that are in the table in descending order, then you could use this:
SELECT DISTINCT year FROM usertable ORDER BY year DESC;
SELECT DISTINCT [Year]
FROM myTable
ORDER BY [Year] DESC
SELECT DISTINCT Year
FROM MyTable
ORDER BY Year DESC
SELECT DISTINCT Year
FROM TABLE
ORDER BY Year DESC
if you wan't to be more than tape monkey here's a brilliant hands on source http://sqlzoo.net/0.htm