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

# Optimize SQL query

## How can I rewrite the following SQL query in an efficient manner?

How can I rewrite the following in an efficient manner?

SELECT DISTINCT(store_num),
(SELECT COUNT(SUBSTR(tran_pd,7,1)) FROM store_sales s WHERE SUBSTR(tran_pd,7,1) = 1 AND store = s.store) AS WEEK1,
(SELECT COUNT(SUBSTR(tran_pd,7,1)) FROM store_sales s WHERE SUBSTR(tran_pd,7,1) = 2 AND store = s.store) AS WEEK2,
(SELECT COUNT(SUBSTR(tran_pd,7,1)) FROM store_sales s WHERE SUBSTR(tran_pd,7,1) = 3 AND store = s.store) AS WEEK3,
(SELECT COUNT(SUBSTR(tran_pd,7,1)) FROM store_sales s WHERE SUBSTR(tran_pd,7,1) = 4 AND store = s.store) AS WEEK4,
(SELECT COUNT(SUBSTR(tran_pd,7,1)) FROM store_sales s WHERE SUBSTR(tran_pd,7,1) = 5 AND store = s.store) AS WEEK5
FROM store_sales GROUP BY store_num

I was hoping there is a SQL function out there that could do this but could not find any.

To begin, we must remove the parentheses from around store_num in the SELECT list. Please see the article DISTINCT is not a function (26 September 2005). DISTINCT always applies to all columns selected.

Next, notice that there is a GROUP BY on store_num. This should prompt us to question whether the DISTINCT is really needed. We must therefore do a quick evaluation of all the columns in the SELECT list. Notice there are five almost identical subqueries in the SELECT. Now, a subquery is allowed to be written in the SELECT only if it returns a single (scalar) value (hence, this type of subquery is called a scalar subquery). But more importantly, these five subqueries are correlated subqueries, and each of them will return its value, a count, based on which store it is. Thus, for the same store, the subquery always returns the same count. Therefore, all the rows produced by the query will be unique, and thus either the DISTINCT or the GROUP BY can be removed.

Hey, wait a second! What are those subqueries actually counting? Answer: the number of transactions for each of five weeks. Does this mean that for every store, five different queries against the entire transaction table are executed? Answer: yes. Can this be simplified? Answer: yes.

```select store_num
, substr(tran_pd,7,1) as weekno
, count(*)            as trans
from store_sales
where substr(tran_pd,7,1)
between 1 and 5
group
by store_num
, substr(tran_pd,7,1)```

This query produces the same counts as your original query. The WHERE clause can be eliminated if 1 through 5 are the only possible weeks. Let's call this our grouping query. The layout of the result set will look like this:

``` store_num  weekno  trans
105        1       9
105        2      37
105        3       4
105        4      42
105        5      11
107        1      21
107        2       5
107        3      16
...```

But your original query produces output that looks like this:

``` store_num  week1 week2 week3 week4 week5
105        9    37     4    42    11
107       21     5    16   ...```

To produce the same layout, we query our grouping query like this:

```select store_num
, sum(case when weekno = 1
then trans end) as week1
, sum(case when weekno = 2
then trans end) as week2
, sum(case when weekno = 3
then trans end) as week3
, sum(case when weekno = 4
then trans end) as week4
, sum(case when weekno = 5
then trans end) as week5
from (
select store_num
, substr(tran_pd,7,1) as weekno
, count(*)            as trans
from store_sales
where substr(tran_pd,7,1)
between 1 and 5
group
by store_num
, substr(tran_pd,7,1)
) as d
group
by store_num```

Here the subquery in the FROM clause is our grouping query. When a subquery is used in the FROM clause, it returns a result set of possibly (usually) many columns, and possibly (almost always) many rows, unlike a scalar subquery. A subquery in the FROM clause is often called an inline view or derived table.

The key to the efficiency of this query is that the counting and grouping of transactions is done in the inner query, the derived table. It is done once, efficiently. Then the outer query simply summarizes the counts in a "crosstab" aggregating query.

#### 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

• ### Where InfluxDB time series database is going

Users need more than SQL for querying databases, according to Paul Dix, co-founder and CTO of InfluxData. That's why the vendor ...

• ### MariaDB X4 brings smart transactions to open source database

Open source database vendor MariaDB updates its flagship platform with new features to enable the convergence of transactional ...

• ### How data lineage tools boost data governance policies

Organizations can bolster data governance efforts by tracking the lineage of data in their systems. Get advice on how to do so ...

• ### Storytelling using data makes information easy to digest

In a Q&A, Nate Nichols and Anna Schena Walsh of AI-based analytics vendor Narrative Science talk about how data storytelling can ...

• ### Data-driven storytelling makes data accessible

In a Q&A, Nate Nichols and Anna Schena Walsh of data storytelling vendor Narrative Science discuss their book on storytelling and...

• ### Startup Uplevel targets software engineering efficiency

Startup vendor Uplevel emerged from stealth on Wednesday touting a platform aimed at improving the efficiency of software ...

## SearchSAP

• ### SAP S/4HANA migration: Critical advice for moving off ECC

With the end of SAP ECC support looming in 2025, organizations must make some tough decisions. Here's a look at your choices.

• ### New SAP leadership faces big challenges in 2020

Industry analysts discuss SAP's biggest issues in 2020, including how the two new CEOs will guide the company deeper into the ...

• ### SAP Data Hub opens predictive possibilities at Paul Hartmann

When medical supply firm Paul Hartmann AG tested a supply chain analysis system built on SAP Data Hub, it found that it could ...

## 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

• ### Don't ever put a non-Java LTS release into production

Development teams should avoid non-long-term support releases at all costs. Pay attention to the Java release cycle to make sure ...

• ### Public API strategy considerations for enterprise adoption

As organizations look for more cost-effective ways to manage data, an evolving landscape with APIs has made the technology more ...

• ### Ideas on how to hold a successful code hackathon

Want to host a hackathon? Here are some ideas on what a company can do to host an event that solves problems and reenergizes the ...

## SearchDataCenter

• ### Organizations try to predict the effect of 5G infrastructure

With more 5G and IoT devices emerging, admins must prepare their data centers to support low-latency apps and edge computing with...

• ### Top infrastructure and operations technology myths of 2019

Admins are consistently evaluating technology to improve I&O efficiency. Cost, integration and business goals are key components ...

• ### Improve efficiency with server energy consumption tools

Deciding what servers are most efficient for your infrastructure requires research. Hardware-level certifications and web-based ...

## SearchContentManagement

• ### 4 popular content collaboration platforms to consider

Companies need to be organized if they want to be efficient. Content collaboration platforms are useful, but first, ensure that ...

• ### AI can enhance content security with a bit of planning

Microsoft and Box both use AI technologies to keep content secure in the cloud. But before using such tools, businesses first ...

• ### Ex-SAP exec steers Episerver CMS toward digital experience market

Alex Atzberger discusses leaving the helm of SAP's CX platform to become Episerver CEO. Now, Episerver looks to reinvent itself ...

## SearchHRSoftware

• ### Why mobile recruiting is the future

Recruiters can use text recruiting to connect with great candidates. Here's a look at how mobile recruiting works, why it's ...

• ### Top 7 HR trends for 2020 and beyond: The change decade

Because HR requires a balance between the needs of the organization with the needs of employees, it's always been a balancing act...

• ### For insider threat programs, HR should provide checks and balances

Insider threat programs may backfire if employees feel they are intrusive and violate privacy, Forrester Research warns. Making ...

Close