Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Sum of sales for two years, by month

## I need a query that will give me the following result for a particular region. I need to get the sum of sales value for two years for one particular region, and group by months.

I have a sales table which consists of the following columns:

```id (autogenerated)
Region
SalesDate
SalesAmount ```

I need a query that will give me the following result for a particular region. I need to get the sum of sales value for two years for one particular region, and group by months as follows:

```
month   2000    2001
-----  ------  -----
Jan    111.0   332.00
Feb    222.0   225.00
March  232.0   234.00```

I am able to get it for one year.

The query for one year would be:

```select monthno(SalesDate) as mm
, monthname(SalesDate) as mth
, sum(SalesAmount) as amt
from SalesTable
where Region = 937
and year(SalesDate) = 2001
group
by monthno(SalesDate)
, monthname(SalesDate) ```

Please overlook the fact that `monthno` and `monthname` may not actually be functions in SQL. Just choose the equivalent functions in your particular database system.

Now combine the results for two years like this:

```select coalesce(y1.mm,y2.mm) as "mm"
, coalesce(y1.mth,y2.mth) as "month"
, sum(y1.amt) as "2001"
, sum(y2.amt) as "2002"
from (
select monthno(SalesDate) as mm
, monthname(SalesDate) as mth
, sum(SalesAmount) as amt
from SalesTable
where Region = 937
and year(SalesDate) = 2001
group
by monthno(SalesDate)
, monthname(SalesDate)
) as y1
full outer
join (
select monthno(SalesDate) as mm
, monthname(SalesDate) as mth
, sum(SalesAmount) as amt
from SalesTable
where Region = 937
and year(SalesDate) = 2002
group
by monthno(SalesDate)
, monthname(SalesDate)
) as y2
on y2.mth = y1.mth
order
by coalesce(y1.mm,y2.mm)```

Now let me to put to all of you good readers the following questions:

1. What happens if there are sales in every month except for February 2001?
2. What happens if there are sales in every month except for February 2001 and February 2002?
3. Why is it a FULL OUTER JOIN?
4. What are those COALESCE functions doing?
5. Optional Is there an easier way to do this using the analytic functions in SQL-99?

Please submit your brief explanation and we'll publish the best one. Remember, keep it brief. You may prefer (or not) to have your name mentioned, so please make sure you give your preference. We would never publish your email in any case.

## SearchDataManagement

• ### Who belongs on a high-performance data governance team?

Putting together a high-quality data governance team can be a challenge. Explore the necessary team members and best practices ...

• ### Soda launches cloud service to improve data observability

Data quality vendor Soda has had a busy 2021, building out new services and raising funding to help organizations identify and ...

• ### Bigeye raises \$17M Series A funding to boost data quality

Former Uber exec, CEO and co-founder of a startup, outlines the challenges and opportunities of enabling a new data trust ...

• ### Insightsoftware acquisition of Izenda targets embedded BI

ERP vendor Insightsoftware acquired embedded analytics specialist Izenda on April 15, marking its second purchase of an embedded ...

• ### Data scientist shortage leaves organizations uncertain

Despite their desire to use data science in their decision-making process, some organizations can't find qualified data ...

• ### Air Force's data overhaul makes analytics a priority

With its data locked in legacy systems that didn't communicate with one another, in 2017 the Air Force started to overhaul of its...

## SearchSAP

• ### S/4HANA Cloud SaaS ERP: Buying team overview

SAP's multi-tenant SaaS ERP, S/4HANA Cloud, is a viable choice for companies that need ease in their infrastructure management. ...

• ### SAP forms financial services partnership with Dediq

SAP and financial industry investment firm Dediq are forming a new business unit to develop applications that help banks and ...

• ### Unpatched applications threaten SAP security

Cyberattacks are a significant threat to unpatched, unprotected SAP applications, according to a new threat intelligence report ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Incorporate diversity and inclusion in technology design

DEI in technology is about more than creating a diverse workplace. We talked to a few DEI professionals about how teams build ...

• ### Microsoft previews OpenJDK distro to the delight of devs

In a move meant to attract more Java developers to its Azure cloud and further support the Java community, Microsoft launched a ...

• ### Supreme Court ruling on Java APIs eases developer worries

Now that the Supreme Court has ruled for Google over Oracle in their high-stakes copyright battle over Java APIs, developers can ...

## SearchDataCenter

• ### Programmable processor technology for next-gen data centers

The right processing technology can benefit your data center. Learn about advancements in CPU technologies, recent vendor ...

• ### Data processing units accelerate infrastructure performance

DPUs often run on networking packets to move information in the data center, instead of supporting processing workflows. Get an ...

• ### New Intel Ice Lake processors boost performance, security

Intel launches third-generation Xeon Scalable processors that bolster security, accelerate common data center workloads by 46% on...

## SearchContentManagement

• ### OpenText releases Cloud Editions content services updates

OpenText CE 21.2 includes federated document compliance that extends to Microsoft Office 365, along with a revamped content ...

As the pandemic disrupts paper workflows, Adobe courts small business users with simple webforms, digital signatures and payments...

• ### Know when to use a headless CMS and when it's not worth it

Headless CMSes aren't a silver bullet for every circumstance. Evaluate three solid use cases for a headless CMS and three ...

## SearchHRSoftware

• ### DEI tools arrive at critical time for HR

Employers are being held accountable for meeting diversity, equity and inclusion goals, especially executives. HR vendors are ...

• ### Oracle Cloud HCM introduces low-code/no-code template tools

Oracle Cloud HCM has been upgraded to ease development of HR processes and improve employee experience, the firm said.

• ### Hybrid workplace may give employers hiring edge

The post-pandemic office is leaning strongly in the direction of hybrid workplaces. Job candidates view working from home as a ...

Close