473,406 Members | 2,620 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,406 software developers and data experts.

sql querry : Union, Exists, IN

I have a querry that works, but it is incredeble slow, so do anyone
have any suggestions how to make this better:

The table looks like this
Gab(ID (key), Gid, Pid, Status, .....), mening there can be serveral
occurences of Gid in the table.

What i want is:

Everyone with staus F2
UNION
Everyone with status F1 or F, but has no occurrence of F2 (grouped on
Gid)
UNION
Everyone with status H, but has no occurrence of F2, F1, F (grouped on
Gid)
My suggestion, that is way to slow:
(SELECT G1.*
FROM Gab AS G1
WHERE G1.Status="F2")

UNION

(SELECT G2.*
FROM Gab as G2
WHERE (G2.Status="F" OR G2.Status = "F1") and NOT EXISTS
(SELECT GID
FROM Gab
WHERE G2.Status="F2" and Gab.Gid = G2.Gid))

UNION

(SELECT G3.*
FROM Gab as G3
WHERE G3.Status="H" and NOT EXISTS
(SELECT GID
FROM Gab
WHERE (G3.Status="F2" OR G3.Status="F" OR G3.Status="F1") and
Gab.Gid = G3.Gid))

so having:

Gab(ID (key), Gid, Pid, Status, .....)
(1, 1, 100, F2, ...)
(2, 1, 200, H, ...)
(3, 2, 300, F1, ...)
(5, 2, 500, F, ...)
(4, 3, 800, H, ...)

I should get the result:

(1, 1, 100, F2, ...)
(3, 2, 300, F1, ...)
(5, 2, 500, F, ...)
(4, 3, 800, H, ...)
Nov 12 '05 #1
0 4735

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

Similar topics

2
by: Eric Kincl | last post by:
Hello, I have an array of data in PHP. I would like to insert each member of the array into it's own row in SQL. The array is of variable length, so it would have to be dynamic code. How would...
0
by: Neculai Macarie | last post by:
Hi! Using Union and Order By gives strange behaviour in the following test-case: drop table if exists gallery; drop table if exists gallery_categ; # create test tables create table gallery...
2
by: David Rasmussen | last post by:
I have some data packed in by bitfields: struct Data { unsigned long a : 8; unsigned long b : 14; unsigned long c : 7; unsigned long d : 3; };
4
by: NickName | last post by:
Hello, Bear with me (not had much sleep last night), pls see following ddl, dml and comments for what is desired, I don't have a problem getting desired result(s), however, I'm wondering if...
4
by: Elroyskimms | last post by:
Using SQL 2000... tblCustomer: CustomerID int CompanyName varchar(20) HasRetailStores bit HasWholesaleStores bit HasOtherStores bit tblInvoiceMessages:
4
by: Akinia | last post by:
Hi every body I've a little problem with my query, I can't figure it out. I've three tables: Table A (EmployeNr, Date, Code) Table B (EmployeNr, Date, Code) Which is an historic of Table A...
3
by: Linn K B | last post by:
I have a querry that works, but it is incredeble slow, so do anyone have any suggestions how to make this better: The table looks like this Gab(ID (key), Gid, Pid, Status, .....), mening there...
1
by: Jamie Pittman via AccessMonster.com | last post by:
Does anyone have any suggestion how to create this form. I want to be able to have form that has two fields and that allow me to select specific parmeters for my Union Query to Look up? I have a...
6
by: virus | last post by:
I want to know wether the visibility mode like private,public & protected exists for union in C++? If exists how memory allocation takes place? Can a union in C++ keeps a member function? Does a this...
0
by: getmeidea | last post by:
I have the following tables, 1> employee_master(emp_id int primary key, emp_name varchar(100)); 2> employee_salary_payment(salary_rid int primary key, emp_id int, sal_date date, paid_amt int); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.