473,385 Members | 1,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Query problem: sales volume comparision with previous year

Dear collegues,

small query problem.

A table (simplified example)...

customer as char(5)
inv_date as date
amount as double

stores the gross amount per invoice-date per customer.

select customer, dec(sum(amount) as Gross from orders
where inv_date between "2005-01-01" and "2005-12-31"
order by Gross desc

gives a sales volume list by custimer with the "biggest" customer at
top. (Customer, Gross)

Now I like to create a list with a comparision of the the sales volume
"current year - previous year" order by sales volume current year.

Customer / Sales 2005 / Sales 2004

Any idea how to sqeeze this into a single query?

Bernd
Dec 29 '05 #1
2 3542
Bernd Hohmann wrote:
Dear collegues,

small query problem.

A table (simplified example)...

customer as char(5)
inv_date as date
amount as double

stores the gross amount per invoice-date per customer.

select customer, dec(sum(amount) as Gross from orders
where inv_date between "2005-01-01" and "2005-12-31"
order by Gross desc

gives a sales volume list by custimer with the "biggest" customer at
top. (Customer, Gross)

Now I like to create a list with a comparision of the the sales volume
"current year - previous year" order by sales volume current year.

Customer / Sales 2005 / Sales 2004

Any idea how to sqeeze this into a single query?


Apart from the fact that your query is syntactically not correct, one idea
might be this:

SELECT o.customer,
( SELECT DEC(SUM(i.amount)) AS gross
FROM orders AS i
WHERE YEAR(i.inv_date) = YEAR(CURRENT DATE) AND
i.customer = o.customer ) AS this_year,
( SELECT DEC(SUM(i.amount)) AS gross
FROM orders AS i
WHERE YEAR(i.inv_date) = YEAR(CURRENT DATE) - 1 AND
i.customer = o.customer ) AS prev_year
FROM orders AS o
or this:

SELECT t1.customer, t1.amount, t2.amount
FROM ( SELECT customer, DEC(SUM(amount)) AS amount
FROM orders
WHERE YEAR(i.inv_date) = YEAR(CURRENT DATE)
GROUP BY customer ) AS t1 JOIN
( SELECT customer, DEC(SUM(amount)) AS amount
FROM orders
WHERE YEAR(i.inv_date) = YEAR(CURRENT DATE) - 1
GROUP BY customer ) AS t2 ON
( t1.customer = t2.customer )

Or via a single scan:

SELECT customer,
DEC(SUM(CASE
WHEN YEAR(i.inv_date) = YEAR(CURRENT DATE)
THEN amount
ELSE 0
END)),
DEC(SUM(CASE
WHEN YEAR(i.inv_date) = YEAR(CURRENT DATE) - 1
THEN amount
ELSE 0
END))
FROM orders
GROUP BY customer

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 29 '05 #2
Knut Stolze wrote:
Any idea how to sqeeze this into a single query?


Apart from the fact that your query is syntactically not correct, one idea
might be this:


Well, SQL isn't my mother tongue :-)

Thanks for your help, I'll try to transform your examples into the final
query and query visual explain how the're performing.

Bernd
Dec 29 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per...
3
by: UnixSlaxer | last post by:
Hello, I am trying to validate all the 22-Queries in the TPC-H Benchmark on DB2 UDB 8.2, I am having a little bit of trouble with Query #8 The result should be: YEAR MKT_SHARE 1995 .03...
2
by: Marius Kaizerman | last post by:
Hi, I have a report that is a based on a query. One of the fields in the query is "time of sale", which is a date field that holds the sales dates (day,month,year). I want to use that field as...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
4
by: Tess2819 | last post by:
Hi Everyone, This is my first topic so I hope you can help. I have searched but can't seem to find what I am looking for, so here it is. I want to create a query using design view in...
3
by: santoni | last post by:
I really need help me with this. I'm sure it's really easy for most. I really appreciate any help. Thanks! I have tables Employee and Sales. In Employees the yes/no field IsManager denotes...
9
by: JJM0926 | last post by:
I'm trying to create a running totals query in access 97. I have followed the directions on how to do it from Microsofts website article id 138911. I took their code they had and replaced it with...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
5
by: stateemk | last post by:
I am trying to run two queries. One query is doing a count to figure out how many total sales there were each year based on the assessor. That query is working fine. On the other query, I'm trying...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.