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.

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