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

# GROUP BY with two tables, part 2

I want the sum of two columns from two different tables -- one column from each table. When I write a simple query like...

```SELECT    p.ddate
,         SUM (p.ddblock)
,         SUM (l.gtblock)
FROM      permit      p
,         localpwblk  l
GROUP BY  p.ddate;
```

... I received a wrong answer. (The sum from table p is multiplied by the number of records in table l and vice versa.)

Solutions:
Solution 1: Uncorrelated GROUP BY
Solution 2: Correlated GROUP BY

### Uncorrelated GROUP BY

This assumes the desired results consist of one row for every permit.ddate; each row also shows the grand total from localpwblk. In this solution, the relevant totals from each table are computed in in-line views.

```SELECT    pg.ddate
,         pg.sum_ddblock
,         lg.sum_gtblock
FROM      (  -- begin in-line view pg on permit
SELECT    ddate
,         SUM (ddblock) AS sum_ddblock
FROM      permit
GROUP BY  ddate
) pg
,         (  -- begin in-line view lg on localpwblk
SELECT  SUM (gtblock)  AS sum_gtblock
FROM    localpwblk
) lg
ORDER BY  pg.ddate;

DDATE       SUM_DDBLOCK SUM_GTBLOCK
----------- ----------- -----------
21-Mar-2004          10         700
22-Mar-2004          90         700
```

Notice that there is no join condition, so the result is a cross-join. Since there is only one row in one of the "tables" (the in-line view "lg") this does not result in an abnormal number of rows in the final result set.

Starting with version 9.0 of Oracle, you can get exactly the same results with a scalar sub-query on "localpwblk".

```SELECT    pg.ddate
,         pg.sum_ddblock
,         (  -- Begin scalar sub-query
SELECT  SUM (gtblock)  AS sum_gtblock
FROM    localpwblk
) AS sum_gtblock
FROM      (  -- begin in-line view pg on permit
SELECT    ddate
,         SUM (ddblock) AS sum_ddblock
FROM      permit
GROUP BY  ddate
) pg
ORDER BY  pg.ddate;
```

### Correlated GROUP BY

Here, the desired results are one row for each ddate. The total of localpwblk.gtblock for the same day, if any, is included.

```SELECT    pg.ddate
,         pg.sum_ddblock
,         lg.sum_gtblock
FROM      (  -- begin in-line view pg on permit
SELECT    ddate
,         SUM (ddblock) AS sum_ddblock
FROM      permit
GROUP BY  ddate
) pg
LEFT OUTER JOIN
(  -- begin in-line view lg on localpwblk
SELECT    TRUNC (ldate)  AS tldate
,         SUM (gtblock)  AS sum_gtblock
FROM      localpwblk
GROUP BY  TRUNC (ldate)
) lg
ON        pg.ddate = lg.tldate
ORDER BY  pg.ddate;

DDATE       SUM_DDBLOCK SUM_GTBLOCK
----------- ----------- -----------
21-Mar-2004          10
22-Mar-2004          90         700
```

In Oracle 9.0 (and later) you can get exactly the same results with a scalar sub-query.

```SELECT    pg.ddate
,         pg.sum_ddblock
,         (  -- Begin scalar sub-query
SELECT  SUM (gtblock)  AS sum_gtblock
FROM    localpwblk
WHERE   TRUNC (ldate) = ddate
) AS sum_gtblock
FROM      (  -- begin in-line view pg on permit
SELECT    ddate
,         SUM (ddblock) AS sum_ddblock
FROM      permit
GROUP BY  ddate
) pg
ORDER BY  pg.ddate;
```

Click to continue in part 3.

## SearchDataManagement

• ### Oracle brings GoldenGate data integration service to cloud

Oracle is making its GoldenGate real time data technology available on its second-generation Oracle Cloud Infrastructure platform...

• ### Era Software raises \$15.25M for enterprise data management

The startup that began as the EraDB time series database is advancing its efforts with new funding and a cloud service for its ...

• ### Dgraph GraphQL database users detail graph use cases

Graph DB vendor Dgraph Labs is expanding its AWS cloud footprint with new regions and adding change data capture capabilities in ...

• ### TigerGraph unveils support for GCP, adds new connectors

Graph database vendor TigerGraph unveiled support for Google Cloud and new connectors to Snowflake and Tableau on April 21 during...

• ### Startup Veezoo emerges from stealth with NLQ-based platform

Aiming to be 'Siri for enterprises,' an analytics startup emerged from stealth with a platform that enables users to interact ...

• ### 15 data science tools to consider using in 2021

Numerous tools are available for data science applications. Read about 15, including their features, capabilities and uses, to ...

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

• ### Nvidia SDK simulates quantum computing circuits on GPU systems

Nvidia edged its way into the quantum computing market with an SDK that simulates quantum circuits by adding horsepower to ...

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

## SearchContentManagement

• ### Hyland gets digital asset management tech with Nuxeo buy

By acquiring a smaller competitor's digital asset management platform, Hyland looks to build on its 2020 purchase of Alfresco, ...

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

## SearchHRSoftware

• ### Shift to HR shared services could save Connecticut millions

By consolidating 17 separate HR operations into one shared service, Connecticut expects to save significantly on costs, most of ...

• ### 10 steps to support your HCM system post go-live

To ensure a new system's success, HR leaders need to develop a plan for how they will support their new HCM system post go-live. ...

• ### Face mask detection a newcomer to employee surveillance

Face mask detection has emerged as another form of employee surveillance technology, and its adoption may be helped indirectly by...

Close