Note that there are some explanatory texts on larger screens.

plurals
  1. POSafely normalizing data via SQL query
    primarykey
    data
    text
    <p>Suppose I have a table of customers:</p> <pre><code>CREATE TABLE customers ( customer_number INTEGER, customer_name VARCHAR(...), customer_address VARCHAR(...) ) </code></pre> <p>This table does <strong>not</strong> have a primary key. However, <code>customer_name</code> and <code>customer_address</code> <em>should</em> be unique for any given <code>customer_number</code>.</p> <p>It is not uncommon for this table to contain many duplicate customers. To get around this duplication, the following query is used to isolate only the unique customers:</p> <pre><code>SELECT DISTINCT customer_number, customer_name, customer_address FROM customers </code></pre> <p>Fortunately, the table has traditionally contained accurate data. That is, there has never been a conflicting <code>customer_name</code> or <code>customer_address</code> for any <code>customer_number</code>. However, suppose conflicting data did make it into the table. I wish to write a query that will fail, rather than returning multiple rows for the <code>customer_number</code> in question.</p> <p>For example, I tried this query with no success:</p> <pre><code>SELECT customer_number, DISTINCT(customer_name, customer_address) FROM customers GROUP BY customer_number </code></pre> <p>Is there a way to write such a query using standard SQL? If not, is there a solution in Oracle-specific SQL?</p> <p><strong>EDIT: The rationale behind the bizarre query:</strong></p> <p>Truth be told, this customers table does not actually exist (thank goodness). I created it hoping that it would be clear enough to demonstrate the needs of the query. However, people are (fortunately) catching on that the need for such a query is the least of my worries, based on that example. Therefore, I must now peel away some of the abstraction and hopefully restore my reputation for suggesting such an abomination of a table...</p> <p>I receive a flat file containing invoices (one per line) from an external system. I read this file, line-by-line, inserting its fields into this table:</p> <pre><code>CREATE TABLE unprocessed_invoices ( invoice_number INTEGER, invoice_date DATE, ... // other invoice columns ... customer_number INTEGER, customer_name VARCHAR(...), customer_address VARCHAR(...) ) </code></pre> <p>As you can see, the data arriving from the external system is denormalized. That is, the external system includes both the invoice data and its associated customer data on the same line. It is possible that multiple invoices will share the same customer, therefore it is possible to have duplicate customer data.</p> <p>The system cannot begin processing the invoices until all customers are guaranteed to be registered with the system. Therefore, the system must identify the unique customers and register them as necessary. This is why I wanted the query: <strong>because I was working with denormalized data I had no control over</strong>.</p> <pre><code>SELECT customer_number, DISTINCT(customer_name, customer_address) FROM unprocessed_invoices GROUP BY customer_number </code></pre> <p>Hopefully this helps clarify the original intent of the question.</p> <p><strong>EDIT: Examples of good/bad data</strong></p> <p>To clarify: <code>customer_name</code> and <code>customer_address</code> only have to be unique <strong>for a particular <code>customer_number</code></strong>.</p> <pre><code> customer_number | customer_name | customer_address ---------------------------------------------------- 1 | 'Bob' | '123 Street' 1 | 'Bob' | '123 Street' 2 | 'Bob' | '123 Street' 2 | 'Bob' | '123 Street' 3 | 'Fred' | '456 Avenue' 3 | 'Fred' | '789 Crescent' </code></pre> <p>The first two rows are fine because it is the same <code>customer_name</code> and <code>customer_address</code> for <code>customer_number</code> 1.</p> <p>The middle two rows are fine because it is the same <code>customer_name</code> and <code>customer_address</code> for <code>customer_number</code> 2 (even though another <code>customer_number</code> has the same <code>customer_name</code> and <code>customer_address</code>).</p> <p>The last two rows are <strong>not okay</strong> because there are two different <code>customer_address</code>es for <code>customer_number</code> 3.</p> <p>The query I am looking for would fail if run against all six of these rows. However, if only the first four rows actually existed, the view should return:</p> <pre><code> customer_number | customer_name | customer_address ---------------------------------------------------- 1 | 'Bob' | '123 Street' 2 | 'Bob' | '123 Street' </code></pre> <p>I hope this clarifies what I meant by "conflicting <code>customer_name</code> and <code>customer_address</code>". They have to be unique per <code>customer_number</code>.</p> <p><strong>I appreciate those that are explaining how to properly import data from external systems.</strong> In fact, I am already doing most of that already. I <strong>purposely</strong> hid all the details of what I'm doing so that it would be easier to focus on the question at hand. This query is not meant to be the only form of validation. I just thought it would make a nice finishing touch (a last defense, so to speak). This question was simply designed to investigate just what was possible with SQL. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload