473,412 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Set operations (EXCEPT/EXCEPT ALL)

Hi all,

I just start using SQL Server for my project. I have some questions
related to set operations. Suppose I have two tables, Table A and
Table B, as following.

TableA TableB
======= =======
--------------- ---------------
| ID | DATA | | ID | DATA |
--------------- ---------------
| 1 | Val-01 | | 1 | Val-01 |
| 2 | Val-01 | | 2 | Val-02 |
| 3 | Val-02 | | 3 | Val-02 |
| 4 | Val-03 | | 4 | Val-03 |
| 5 | Val-04 | ---------------
| 6 | Val-05 |
---------------

In DB2, I can write SQL statements as following

SQL 1:
===========================
SELECT DATA FROM TableA
EXCEPT
SELECT DATA FROM TableB

And the result will be

Val-04
Val-05
===========================

SQL 2:
===========================
SELECT DATA FROM TableA
EXCEPT ALL
SELECT DATA FROM TableB

And the result will be

Val-01
Val-04
Val-05
===========================

1. How can I handle the EXCEPT (ALL) operator in SQL Server?
2. Are there equivalent SQL queries for the above SQL queries?

Thank you

Erwin Leonardi
Jul 20 '05 #1
4 22662

"Erwin Leonardi" <er***@theleonardi.com> wrote in message
news:32**************************@posting.google.c om...
Hi all,

I just start using SQL Server for my project. I have some questions
related to set operations. Suppose I have two tables, Table A and
Table B, as following.

Unfortunately, MS SQL Server doesn't support EXCEPT.

However, recent (this month or lasts) SQL Server Magazine has an article on
how to do this.
Jul 20 '05 #2
Erwin Leonardi (er***@theleonardi.com) writes:
--------------- ---------------
| ID | DATA | | ID | DATA |
--------------- ---------------
| 1 | Val-01 | | 1 | Val-01 |
| 2 | Val-01 | | 2 | Val-02 |
| 3 | Val-02 | | 3 | Val-02 |
| 4 | Val-03 | | 4 | Val-03 |
| 5 | Val-04 | ---------------
| 6 | Val-05 |
---------------

In DB2, I can write SQL statements as following

SQL 1:
===========================
SELECT DATA FROM TableA
EXCEPT
SELECT DATA FROM TableB

And the result will be

Val-04
Val-05


In SQL Server what comes closest at hand is
SELECT Data FROM TableA a
WHERE NOT EXISTS (SELECT *
FROM TableB b
WHERE a.Data = b.Data)

Of course, with many columns in the result set, this becomes somewhat
tedious to write.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
"Erwin Leonardi" <er***@theleonardi.com> wrote in message
news:32**************************@posting.google.c om...
Hi all,

I just start using SQL Server for my project. I have some questions
related to set operations. Suppose I have two tables, Table A and
Table B, as following.

TableA TableB
======= =======
--------------- ---------------
| ID | DATA | | ID | DATA |
--------------- ---------------
| 1 | Val-01 | | 1 | Val-01 |
| 2 | Val-01 | | 2 | Val-02 |
| 3 | Val-02 | | 3 | Val-02 |
| 4 | Val-03 | | 4 | Val-03 |
| 5 | Val-04 | ---------------
| 6 | Val-05 |
---------------

In DB2, I can write SQL statements as following

SQL 1:
===========================
SELECT DATA FROM TableA
EXCEPT
SELECT DATA FROM TableB

And the result will be

Val-04
Val-05
===========================

SQL 2:
===========================
SELECT DATA FROM TableA
EXCEPT ALL
SELECT DATA FROM TableB

And the result will be

Val-01
Val-04
Val-05
===========================

1. How can I handle the EXCEPT (ALL) operator in SQL Server?
2. Are there equivalent SQL queries for the above SQL queries?

Thank you

Erwin Leonardi


EXCEPT can be handled, as Erland pointed out, with a NOT EXISTS subquery.
EXCEPT ALL can be handled by creating a table of natural numbers.

CREATE VIEW Digits (d)
AS
SELECT 0
UNION ALL
SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
UNION ALL
SELECT 7
UNION ALL
SELECT 8
UNION ALL
SELECT 9

CREATE VIEW NaturalNumbers (n)
AS
SELECT Ones.d + 10 * Tens.d + 100 * Hundreds.d + 1
FROM Digits AS Ones
CROSS JOIN
Digits AS Tens
CROSS JOIN
Digits AS Hundreds

CREATE TABLE A
(
col1 INT NOT NULL
)

INSERT INTO A (col1)
VALUES (1)
INSERT INTO A (col1)
VALUES (2)
INSERT INTO A (col1)
VALUES (2)
INSERT INTO A (col1)
VALUES (2)
INSERT INTO A (col1)
VALUES (2)

CREATE TABLE B
(
col1 INT NOT NULL
)

INSERT INTO B (col1)
VALUES (2)
INSERT INTO B (col1)
VALUES (3)

-- SELECT col1 FROM A EXCEPT ALL SELECT col1 FROM B
SELECT A.col1
FROM (SELECT A.col1, A.tally - COALESCE(B.tally, 0) AS tally
FROM (SELECT col1, COUNT(*) AS tally
FROM A
GROUP BY col1) AS A
LEFT OUTER JOIN
(SELECT col1, COUNT(*) AS tally
FROM B
GROUP BY col1) AS B
ON A.col1 = B.col1
WHERE B.tally IS NULL OR B.tally < A.tally) AS A
INNER JOIN
NaturalNumbers AS N
ON N.n <= A.tally
ORDER BY A.col1

col1
1
2
2
2

--
JAG
Jul 20 '05 #4
> ===========================
SELECT DATA FROM TableA
EXCEPT
SELECT DATA FROM TableB

Another method.

Select a.*
from TableA a
left outer join TableB b
on a.data=b.data
where b.data is null
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
36
by: mrby | last post by:
Hi, Does anyone know of any link which describes the (relative) performance of all kinds of C operations? e.g: how fast is "add" comparing with "multiplication" on a typical machine. Thanks!...
3
by: Hallvard B Furuseth | last post by:
I'm wondering how to design this: An API to let a request/response LDAP server be configured so a user-defined Python module can handle and/or modify some or all incoming operations, and later...
6
by: carsonbj | last post by:
I have an issue where the below operation works on a little-endian architecture but not on a big-endian architecture. I was under the impression that pointer arithmetic is architecture independant...
6
by: jm.suresh | last post by:
Hi, Frequently I get to do like this: a = (1, 2, 3, 4) # some dummy values b = (4, 3, 2, 1) import operator c = map(operator.add, a, b) I am finding the last line not very readable especially...
27
by: David Marsh | last post by:
I understand that C99 supports a complex type and complex arithmetic. There is nothing about it in the FAQ and online searches turned up very little except synopses. Can anyone point me toward...
9
by: j_depp_99 | last post by:
My program reads in hex values from a file and then performs arithmetic operations on them. All works well except when I use large hex values and my answers are incorrect. For example...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.