Hello. I want to know if it's possible to have a query that returns (cross table) the sales of a month but in two...
different years, e.g.
jan-00 feb-00 mar-00 2890 2389 4444
jan-01 feb-01 mar-01 3000 3444 3222
Yes, but it's ugly.
In Microsoft Access it's a piece of cake, because CROSSTAB is one of the supported query options. If you do not have a similar feature on your application or front-end program, you may have to do it the hard way --
create temptable ( year integer , jansales integer , febsales integer ... , decsales integer )
insert into temptable select salesyear, sum(sales), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 from yourTable where salesmonth=1 group by salesyear
insert into temptable select salesyear, 0, sum(sales), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 from yourTable where salesmonth=2 group by salesyear
insert into temptable select salesyear, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sum(sales) from yourTable where salesmonth=12 group by salesyear
select salesyear, sum(jansales), sum(febsales), ... , sum(decsales) from temptable group by salesyear
For More Information
- What do you think about this answer? E-mail the edtiors at editor@searchDatabase.com with your feedback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig Deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.