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

# Totals by yearly quarters

I have a table that contains sales data with the following columns that are important to me: Customer#, SalesDate,...

SalesAmount. I would like to be able to total the SalesAmount into yearly quarters (i.e. Q1, Q2,...), for each customer. Is it possible to do this in one query? I'm using a Progress database, so any functions available in Access/SQLServer/Oracle are not available to me, I'm afraid. Thank you!

Allow me to offer my condolences. A quick Web search turns up two references: Progress SQL-89 Guide and Reference and Progress SQL-92 Guide and Reference. Sure enough, the SQL-92 version supports a substantial list of scalar functions including DAYOFYEAR, so you could write:

```select Customer#
, case dayofyear(SalesDate)
when <  92 then 1
when < 183 then 2
when < 274 then 3
else 4
end           as Quarter
, sum(SalesAmount)
from SalesData
group
by Customer#
, case dayofyear(SalesDate)
when <  92 then 1
when < 183 then 2
when < 274 then 3
else 4
end ```

However, if you are using the SQL-89 version, it looks like you need another way to find the day number. There is no indication that you could do something even as simple as adding an integer to a date (see The integers table for an example of generating a series of dates). The only obvious solution that requires no date calculations or functions is a calendar table which will allow you to associate every date with its quarter:

```create table Calendar
( CalDate  date not null
, Q    smallint not null
).
insert into Calendar values ('01/01/04',1).
insert into Calendar values ('01/02/04',1).
insert into Calendar values ('01/03/04',1).
insert into Calendar values ('01/04/04',1).
...
insert into Calendar values ('12/29/04',4).
insert into Calendar values ('12/30/04',4).
insert into Calendar values ('12/31/04',4). ```

Two quirks that I spotted in the SQL-89 manual: each statement must end in a period, and date strings must be specified as mm/dd/yy. Now you can say:

```select Customer#
, Q
, sum(SalesAmount)
from Calendar
, SalesData
where CalDate = SalesDate
group
by Customer#
, Q.```

One final note: in the first solution above, the assumption is that the first 91 days of the calendar year are in Q1, the second 91 in Q2, and the last 92 or 93, as the case may be, are in Q4. Realistically, this just doesn't happen.

In most organizations that use quarters or similar types of fiscal period, everything is measured in whole weeks. In 2004, January 1 through 3 would most likely be part of Q4 of 2003, since there are more 2003 days in that week than 2004 days. Likewise, January 1st of 2005 would likely be part of Q4 in 2004. When your quarters are actually fiscal quarters, you really do need to use a calendar table, and it should look like this:

```create table Calendar
( CalDate  date not null
, FY   smallint not null
, FQ   smallint not null
).
insert into Calendar values ('01/01/04',2003,4).
insert into Calendar values ('01/02/04',2003,4).
insert into Calendar values ('01/03/04',2003,4).
insert into Calendar values ('01/04/04',2004,1).
...
insert into Calendar values ('12/29/04',2004,4).
insert into Calendar values ('12/30/04',2004,4).
insert into Calendar values ('12/31/04',2004,4).
insert into Calendar values ('01/01/05',2004,4).
insert into Calendar values ('01/02/05',2005,1).```

Now just join on the date and group by the fiscal year and fiscal quarter.

This was last published in February 2004

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Hyperledger Fabric offers path to enterprise blockchain future

Blockchain arose from bitcoin, but it's looking to find a place in the enterprise. Frameworks like Hyperledger Fabric could ...

• ### MongoDB 4.0 takes ACID transactions to multi-document level

MongoDB is taking a deeper step into SQL-style processing waters with a 4.0 update that brings increased support for ...

• ### Data lake concept needs firm hand to pay big data dividends

Data lakes pose technology deployment and data management challenges that can leave analytics users high and dry if the ...

• ### AI functionality limited today but could be a game-changer

Limited AI capabilities could soon give way to technology that is truly transformative for enterprises, surpassing the overhyped ...

## SearchSAP

• ### ControlPanelGRC app eases Steelcase's compliance pain

When Steelcase's SAP environment grew in size and complexity, it turned to Symmetry ControlPanelGRC to save time, have more ...

• ### Translytical data platforms emerge with SAP HANA as a leader

SAP HANA is a leading translytical platform, according to Forrester, and consulting firm Convergent IS says the combination of ...

• ### SAP HANA and Esri combine for geospatial database platform

SAP and Esri are combining SAP HANA's in-memory database capabilities with Esri geospatial applications, and utilities are taking...

## SearchSQLServer

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

• ### Five SQL Server maintenance steps you should take -- ASAP

Putting off SQL Server administration tasks can lead to database problems. Enact these often-neglected maintenance items to help ...

## TheServerSide.com

• ### Pluralsight IQ, Stack Overflow boost developer street cred

Tying the Pluralsight IQ skills test to the Stack Overflow Developer Story helps developers measure their technical skills and ...

• ### Why this quantum computing breakthrough is a security risk

Quantum computing will void pretty much all security encryption techniques and open the door to hackers. Here's how to protect ...

• ### Database automation drives DevOps into the persistence layer

A new breed of software tools is driving DevOps processes deep into the persistence layer, as database automation and continuous ...

## SearchDataCenter

• ### Evaluate read-intensive and write-intensive SSD use cases

Consider write wear, performance and other factors when choosing between read-intensive, write-intensive and mixed-use ...

• ### Some hyper-converged infrastructure use cases pose pitfalls

Hyper-converged infrastructure adoption is skyrocketing, but that doesn't mean that the technology is the best choice for every ...

• ### Dell hyper-converged reorg streamlines products, ups CI odds

Market pressures and manufacturing synergies drove Dell to integrate its HCI and CI products with its core business units, but ...

## SearchContentManagement

• ### Content management in the cloud a main theme in 2018

The future of content management resides in the cloud and with AI, as several 2018 conferences will assure you.

• ### Six things to know about today's SharePoint implementations

As companies migrate their on-premises Microsoft SharePoint sites to the cloud, here are some things they should know about the ...

• ### Upgrades for the SharePoint Online portal

As more organizations migrate SharePoint sites to the cloud, Microsoft has increased at-a-glance dashboard data and analytics to ...

## SearchHRSoftware

• ### Don't overlook the many benefits of Microsoft Excel for HR

The maligned spreadsheet tool is no substitute for enterprise apps like HRMS and people analytics, but it will do in a pinch and ...

• ### HR is failing to use people analytics tools, new report says

Human resource departments fail to use people analytics tools effectively, a new global study concludes. The findings were called...

• ### Does your company need new human resource management tools?

Finding the best human resource management tools starts with identifying your company's present and future needs, before ...

Close