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

# Exactly one row? At least one row? (Part 1)

I am trying to retrieve data from one table, but I want specific data from two different columns. My example table is:

``` ID LOC_NUMB LOC_PREC 000000000 TIRE01 P 000000001 TIRE02 P 000000002 TIRE03 P 000000003 TIRE04 P 000000004 TIRE05 P 000000001 TIRE06 S 000000002 TIRE07 S
```

The results I am looking for:

``` ID LOC_NUMB LOC_PREC 000000001 TIRE02 P 000000002 TIRE03 P 000000001 TIRE06 S 000000002 TIRE07 S
```

I need ID with the LOC_PREC P and S.

``` SELECT item_loc.id, item_loc.loc_numb, item_loc.loc_prec FROM item_loc WHERE item_loc.id contains **** this is where I am having a problem (item_loc.loc_prec 'S and P') GROUP BY item_loc.id ORDER BY item_loc.id asc
```

Is there any way I can get the data from column ID that contains only S and P for that specific ID. I don't need AND, OR; I need both? Thank you.

To answer your question, yes, you can do it. You didn't say whether you wanted exactly one S and and P, or at least one S and one P, so I'll show you how to do it either way. Let's start with this query:

```select id , loc_numb , loc_prec from item_loc where id in ( select id from item_loc where loc_prec in ('S','P') group by id having count(*) = 2 )
```

Let's look first at the subselect. The subselect returns all S and P rows in the table. Then it groups them by ID. If a particular ID has exactly 2 rows, then it is selected, and both of its rows are included in the result set of the outer query.

But is this correct? We are assuming that there is one S and one P, but there's a "gotcha" in that logic. The subquery also chooses every ID that has two S's and no P's, or two P's and no S's. And if there's a particular ID that happens to have one S and two P's? It would not be chosen! We could fix this by using

``` having count(*) >= 2
```

But this still lets an ID with only three P's or only three S's through.

This is familiar OR behaviour, the result of "mingling" the selected rows in the IN?('S','P') condition. Let's see what happens when we split the S and P conditions into separate subselects, so that the selected rows are not mingled.

```select id , loc_numb , loc_prec from item_loc where id in ( select id from item_loc where loc_prec = 'S' group by id having count(*) = 1 ) and id in ( select id from item_loc where loc_prec = 'P' group by id having count(*) = 1 )
```

This query lists ID's which have exactly one S and one P. Use an equality condition in the HAVING clause to specify an exact number of rows.

To find one or more rows, use an inequality. For example, to find every ID with one S and at least one P, the query is:

```select id , loc_numb , loc_prec from item_loc where id in ( select id from item_loc where loc_prec = 'S' group by id having count(*) = 1 ) and id in ( select id from item_loc where loc_prec = 'P' group by id having count(*) >= 1 )
```

If you think about it for a moment, a HAVING clause that says COUNT(*)?>=?1 is redundant. It's easy to see why, if you consider how a query with a GROUP BY clause is processed. First, rows are retrieved based on WHERE conditions. Then the selected rows are assembled into groups, typically by sorting. Then the groups are processed. There will not be a group with no rows, since during selection, no rows were selected for that group. Therefore each group that comes out of row selection and goes into group processing has to have one or more rows.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Lessons learned from Credit Karma GraphQL architecture

Credit Karma's vice president of engineering explains why and how the personal finance service is using the GraphQL data query ...

• ### EnterpriseDB looks to grow market for PostgreSQL

Enterprises are increasingly using the open source PostgreSQL database. Read in this Q&A where the growth is coming from and how ...

• ### 3 of the top use cases for graph databases

Graph databases establish many unique relationships between data points. These unusual relationships are beneficial in many use ...

• ### Embedded BI software creates common ground for diverse analytics

Learn how embedding separate business intelligence capabilities into one application empowers users to drill down, access and ...

• ### Embedded BI and analytics apps speed workflows, insight access

Embedded BI is fast becoming a focal point for analytics uses as data analysts, developers and business users enjoy faster ...

• ### What Salesforce means for Tableau in the cloud

After the Salesforce acquisition, users have wondered about the future of Tableau in the cloud. Experts weigh in on how the ...

## SearchSAP

• ### Understand SAP ECC vs. HANA vs. S/4HANA vs. R/3

SAP ECC, HANA, S/4HANA and R/3 are all valuable tools in the IT world, but it's easy to get them confused. Learn about their ...

• ### 5 tips for a successful S/4HANA Cloud implementation

Moving to S/4HANA public cloud can help you save on IT maintenance and simplify real-time analysis. Here's a look at what you can...

• ### Take care of data before SAP S/4HANA migration

In this Q&A, Rajesh Rengarethinam of ERP security vendor Appsian discusses why reviewing data security and business processes are...

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

• ### CSS performance optimization in 5 easy steps

Here are five CSS performance tips to help you avoid website performance problems.

• ### Why the 8 Java primitive data types are not objects

What's the difference between Java primitive types and objects? To start, don't classify primitive types as objects. Let's ...

• ### A brief history of Java: How it forever changed programming

The Java programming language is an important tool in an application developer's quiver. But Java has also undergone many changes...

## SearchDataCenter

• ### Plan for a successful ITIL implementation

Corporate buy-in and training are essential for organizations that decide to adopt the ITIL framework. Admins should also help ...

• ### Learn the main Linux OS components

Linux is one operating choice to run on your infrastructure. Get started with these terms to discover how the OS works and how it...

• ### New Dell EMC PowerEdge servers are built for the rugged edge

New Dell Streaming Media Platform includes two small form factor PowerEdge servers and a Modular Data Center. Dell aims the ...

## SearchContentManagement

With new Google Drive integration, Adobe embeds free features for commenting and annotating PDFs inside Google Docs. Acrobat ...

• ### 4 benefits of business process automation

Companies are automating business processes to improve workflows and use technology effectively. Some benefits of BPA include ...

• ### Test yourself on the differences between SharePoint and OneDrive

Businesses use Microsoft SharePoint and OneDrive capabilities for a number of reasons. Test your knowledge on the differences ...

## SearchHRSoftware

• ### Trump's H-1B work visa actions disappoint ex-Disney workers

President Donald Trump arrived in office promising to reform the H-1B visa program. But his actions have fallen short of what ...

• ### How real-time analytics can benefit HR

For analytics to be truly useful, it must be embedded into user workflows. Learn how HR tools are incorporating such approaches ...

• ### Human vs. AI in recruiting: Why both matter

Recruiters may consider using both the human element and the data brought in by intelligent tools if it can help them make better...

Close