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

# Complex query with DECODE

## I have the following query but it's not working. Can we use nested queries with decode?

I have the following query but it's not working. I want to know where I have gone wrong and please provide me with...

the correct query. Can we use nested queries with decode?

```SELECT A.TYPE,
(SELECT SUM(DECODE(TYPE,'HOT',1,0)) HOT_CAKE,
SUM(DECODE(TYPE,'SWEET',1,0)) SWEET_CAKE
FROM PRODUCT_DETAIL WHERE PRODUCT_CODE = 1 GROUP BY TYPE) A,
(SELECT SUM(DECODE(TYPE,'HOT',1,0)) HOT_BURGER,
SUM(DECODE(TYPE,'SWEET',1,0)) SWEET_BURGER
FROM PRODUCT_DETAIL WHERE PRODUCT_CODE = 2 GROUP BY TYPE) B
WHERE A.TYPE = B.TYPE;
```

I want the output as

```HOT_CAKE   SWEET_CAKE   HOT_BURGER   SWEET_BURGER
50          30         26             45
```

Where you've gone wrong (since you did ask) is to place the derived tables into the SELECT clause instead of the FROM clause.

This is probably what you intended --

```select A.TYPE
, HOT_CAKE
, SWEET_CAKE
, HOT_BURGER
, SWEET_BURGER
from ( SELECT TYPE
, SUM(DECODE(TYPE,'HOT',1,0))
HOT_CAKE
, SUM(DECODE(TYPE,'SWEET',1,0))
SWEET_CAKE
FROM PRODUCT_DETAIL
WHERE PRODUCT_CODE = 1
GROUP BY TYPE ) A
inner
join ( SELECT TYPE
, SUM(DECODE(TYPE,'HOT',1,0))
HOT_BURGER
, SUM(DECODE(TYPE,'SWEET',1,0))
SWEET_BURGER
FROM PRODUCT_DETAIL
WHERE PRODUCT_CODE = 2
GROUP BY TYPE ) B
on A.TYPE
= B.TYPE
```

Here's another way to obtain the same result, which might be a little easier to write --

```select TYPE
, sum(case when PRODUCT_CODE = 1
and TYPE = 'HOT'
then 1 else 0 end
) as HOT_CAKE
, sum(case when PRODUCT_CODE = 1
and TYPE = 'SWEET'
then 1 else 0 end
) as SWEET_CAKE
, sum(case when PRODUCT_CODE = 2
and TYPE = 'HOT'
then 1 else 0 end
) as HOT_BURGER
, sum(case when PRODUCT_CODE = 2
and TYPE = 'SWEET'
then 1 else 0 end
) as SWEET_BURGER
from PRODUCT_DETAIL
group
by TYPE
```

This form of the query also makes it easier to add other products. In addition, it's likely more efficient, since it makes only one pass of the table instead of two passes with a join.

This was last published in April 2006

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

• ### Ingesting data into a data lake may give you data indigestion

Big data vendors routinely push the notion of ingesting all of your data into a data lake. But in many cases, doing so is an ...

• ### Deft preprocessing marks deep learning techniques for data preparation

Deep learning techniques for data preparation include exploration of data sets and algorithms. This calls for more than a bit of ...

• ### DMBOK2 eyes evolving data management best practices, challenges

In a Q&A, DAMA president Sue Geuens talks about the second edition of the group's DMBOK reference book and how data management ...

• ### Examining the KNIME Analytics Platform for big data analytics

KNIME offers open source data analytics, reporting and integration tools, as well as commercial software that can help build more...

• ### Machine learning platforms comparison: Amazon, Azure, Google, IBM

The platform war over machine learning tools is heating up. Use our features comparison chart to see how four top vendors stack ...

• ### Exploring Oracle Advanced Analytics

Oracle Advanced Analytics and other data analytics tools, including Oracle R Enterprise and Oracle Data Miner, enable business ...

## SearchSAP

• ### SAP BW/4HANA application could streamline finance and planning

SAP Business Planning and Consolidation for BW/4HANA aims to simplify and consolidate enterprise financial planning; Business ...

• ### Choosing SAP HR module complicated by S/4HANA, SuccessFactors options

A SuccessFactors consultant explains the benefits and challenges of adding to or replacing on-premises SAP HCM with the newer ERP...

• ### Move to SAP S/4HANA Cloud improves sales app's security and performance

Emplay, an SAP Startup Focus partner, moved its Sales GPS app to SAP S/4HANA Cloud and saw improvements in performance, ...

## SearchSQLServer

• ### A closer look at Python-SQL Server 2017 integration

Do you know everything you need to take advantage of SQL Server 2017's support of Python?

• ### How much do you really know about SQL Server 2016 licensing?

Should you use Server + CAL licensing for SQL Server 2016 or does Per Core licensing offer more bang for your buck? Read our ...

## TheServerSide

• ### How Pokemon Go needed a Kubernetes powered Java cloud

How did Pokemon Go scale so well? A big part of it's scalability was the Java cloud platform that leveraged containers and ...

• ### How microservices patterns made Uber's architecture perform better

How did Uber manage to deal with the inevitable Halloween rush? Applying microservices patterns to their architecture played a ...

• ### Effective DevOps hinges on automating a continuous delivery pipeline

Many organizations struggle to bring development and operations together, only to realize that effective DevOps means ...

## SearchDataCenter

• ### Distributed data centers boost resiliency, but IT hurdles remain

Distributed data center architectures increase IT resiliency compared to traditional single-site models, with networking, data ...

• ### Server firmware security threat, real or not, gets renewed attention

The latest threat to server security could be the firmware. That's the emphasis of at least one server-maker, hoping to help ...

• ### Assess and compare DCIM software options in the market

DCIM software can improve the management and operation of your data center. Choose the right vendor for your company to control ...

## SearchContentManagement

• ### M-Files puts content first by adding AI to content management tools

M-Files is adding AI functionality to its platform to better assist in document search, focusing on what a document is, rather ...

• ### Combining DAM and DX for digital experience management

Digital asset management systems predate the web. Cutting-edge digital experiences, however, depend on keeping DAM in order, with...

• ### Three BI dashboard best practices you need to know

As companies struggle with BI integration, Microsoft has figured out the magic formula with its Power BI dashboard. Here are the ...

## SearchFinancialApplications

• ### Bundled payments healthcare tool finds better deals for employers

Trying to rein in healthcare costs, large self-insured employers are using HR tech tools, such as analytics-based bundled payment...

• ### Ceridian uses employee engagement tool from its own joint venture

After forming a new joint venture, LifeWorks, HCM vendor Ceridian uses LifeWorks' employee recognition, engagement and perks ...

• ### Evaluate HR technology to fit your talent management processes

Choose your talent management system wisely or risk irking your employees. Learn the criteria for evaluating software that ...

Close