473,548 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

T-SQL Problem - should I use the "ALL" clause?

Hi

Hope someone can help me with this - have been staring at this problem
all day, and with the cold I've got, just don't seem to be able to
figure it out!

I've got two tables, here with some test data - sorry, can't create DDL
as I'm at home & can't access the server :-(

TABLE 1: Application_Int ermediary

CaseID Intermediary_Ty pe
---------------- ----------------------------
101010101 73
101010101 123
101010101 90
202020202 75
202020202 90
303030303 90
303030303 73

TABLE 2: Intermediary_gr ouping

Relationship_ty pe Intermediary_Ty pe
-------------------------- ---------------------------
1 73
1 123
1 90
2 75
2 90

Here's what they do, in english. We get applications which can come
under a number of different sourcing relationships. One record is
posted to Application_Int ermediary per company involved in the deal, so
you get a record telling you the application number (CaseID) and the
type of intermediary. You are likely, therefore, get more than one
record per application.

What I need to be able to do is classify these apps into a relationship
type, so have set up the second table, Intermediary_gr ouping. This
would let me know that, should an application have introducers of type
73 AND 90 AND 123 attached to it, it was introduced under relationship
type 1.

So in the examples above, case 101010101 would fall into
relationship_ty pe 1, 202020202 into relationship_ty pe 2 - and 303030303
would not be classified.

Have tried a variety of subqueries, using ANY/SOME/ALL/IN all with no
success. The only way I can think of doing this is to create a
temporary table with one record per application & a concatination of
the intermediary types (e.g. '101010101', '73-123-90'. I have written a
function to do this, but it takes a LONG time to execute.

There has to be a simple way of doing this, that I'm overlooking.

Any ideas would be gratefully received - I'm stuck!

Thanks

Lee

Jun 20 '06 #1
4 4132
go****@the-gallowgate.com wrote:
Hi

Hope someone can help me with this - have been staring at this problem
all day, and with the cold I've got, just don't seem to be able to
figure it out!

I've got two tables, here with some test data - sorry, can't create DDL
as I'm at home & can't access the server :-(

TABLE 1: Application_Int ermediary

CaseID Intermediary_Ty pe
---------------- ----------------------------
101010101 73
101010101 123
101010101 90
202020202 75
202020202 90
303030303 90
303030303 73

TABLE 2: Intermediary_gr ouping

Relationship_ty pe Intermediary_Ty pe
-------------------------- ---------------------------
1 73
1 123
1 90
2 75
2 90

Here's what they do, in english. We get applications which can come
under a number of different sourcing relationships. One record is
posted to Application_Int ermediary per company involved in the deal, so
you get a record telling you the application number (CaseID) and the
type of intermediary. You are likely, therefore, get more than one
record per application.

What I need to be able to do is classify these apps into a relationship
type, so have set up the second table, Intermediary_gr ouping. This
would let me know that, should an application have introducers of type
73 AND 90 AND 123 attached to it, it was introduced under relationship
type 1.

So in the examples above, case 101010101 would fall into
relationship_ty pe 1, 202020202 into relationship_ty pe 2 - and 303030303
would not be classified.

Have tried a variety of subqueries, using ANY/SOME/ALL/IN all with no
success. The only way I can think of doing this is to create a
temporary table with one record per application & a concatination of
the intermediary types (e.g. '101010101', '73-123-90'. I have written a
function to do this, but it takes a LONG time to execute.

There has to be a simple way of doing this, that I'm overlooking.

Any ideas would be gratefully received - I'm stuck!

Thanks

Lee


Try this. Note my assumptions about the keys.

CREATE TABLE Application_Int ermediary (CaseID INT NOT NULL,
Intermediary_Ty pe INT NOT NULL, PRIMARY KEY
(CaseID,Interme diary_Type));

CREATE TABLE Intermediary_gr ouping (Relationship_t ype INT NOT NULL,
Intermediary_Ty pe INT NOT NULL, PRIMARY KEY
(Relationship_t ype,Intermediar y_Type));

INSERT INTO Application_Int ermediary (CaseID, Intermediary_Ty pe)
SELECT 101010101, 73 UNION ALL
SELECT 101010101, 123 UNION ALL
SELECT 101010101, 90 UNION ALL
SELECT 202020202, 75 UNION ALL
SELECT 202020202, 90 UNION ALL
SELECT 303030303, 90 UNION ALL
SELECT 303030303, 73 ;

INSERT INTO Intermediary_gr ouping (Relationship_t ype,
Intermediary_Ty pe)
SELECT 1, 73 UNION ALL
SELECT 1, 123 UNION ALL
SELECT 1, 90 UNION ALL
SELECT 2, 75 UNION ALL
SELECT 2, 90 ;

SELECT A.CaseID, I.Relationship_ type
FROM Application_Int ermediary AS A
JOIN Intermediary_gr ouping AS I
ON A.Intermediary_ Type = I.Intermediary_ Type
GROUP BY A.CaseID, I.Relationship_ type
HAVING COUNT(*) =
(SELECT COUNT(*)
FROM Intermediary_gr ouping
WHERE Relationship_ty pe = I.Relationship_ type);

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Jun 20 '06 #2
That's brilliant, solves my first question, and has taught me something
I didn't know yesterday.

But here's another one to test you with :-)

What could I do to identify the third case, which doesn't match the
criteria? I figured I'd put the results of the query you've helped me
with into a table, then do an unmatched between that and the original
case list.... which will work, but seems a bit "belt and braces". I'm
sure there must be a more elegant solution out there.

Cheers

Lee

Jun 21 '06 #3
On 20 Jun 2006 23:58:29 -0700, go****@the-gallowgate.com wrote:
That's brilliant, solves my first question, and has taught me something
I didn't know yesterday.

But here's another one to test you with :-)

What could I do to identify the third case, which doesn't match the
criteria? I figured I'd put the results of the query you've helped me
with into a table, then do an unmatched between that and the original
case list.... which will work, but seems a bit "belt and braces". I'm
sure there must be a more elegant solution out there.


Hi Lee,

Same idea, but all combined into a single query:

SELECT A.CaseID, D.Relationship_ type
FROM (SELECT DISTINCT CaseID
FROM Application_Int ermediary) AS A
LEFT JOIN (SELECT A.CaseID, I.Relationship_ type
FROM Application_Int ermediary AS A
INNER JOIN Intermediary_gr ouping AS I
ON A.Intermediary_ Type = I.Intermediary_ Type
GROUP BY A.CaseID, I.Relationship_ type
HAVING COUNT(*) =
(SELECT COUNT(*)
FROM Intermediary_gr ouping AS I2
WHERE I2.Relationship _type = I.Relationship_ type)) AS D
ON D.CaseID = A.CaseID;

Note that unmatched grooups will appear with relationship_ty pe NULL. You
can use a COALESCE function in the first SELECT to change that, if you
wish.

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP
Jun 25 '06 #4
>> What I need to be able to do is classify these apps into a relationship type, so have set up the second table, Intermediary_gr ouping. This would let me know that, should an application have introducers of type 73 AND 90 AND 123 attached to it, it was introduced under relationship type 1.<<

Look up "Relationsl Division"; this is one of Codd's original
operators.

Jun 25 '06 #5

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

Similar topics

2
2128
by: steve | last post by:
To gain performance, do I need to index ALL the fields in the where clause. Say we have a query like: select stuff from table where field1=.. and field2=... If field1 selection substantially reduces the number of records selected, do we really need to index field2 as well. In other words, does the query first subselect from the first...
11
2825
by: Summa | last post by:
Hi NG, I have the following problem that I hope you can help me with (MS-SQL server 2000) Imagine a statement like this: "select id, firstname, (select top 1 id from testdata) as testid, lastname from nametable order by firstname"
27
4684
by: Chris, Master of All Things Insignificant | last post by:
I have come to greatly respect both Herfried & Cor's reponses and since the two conflicted, I wanted to get some clarification. My orginal post: Herfried, maybe your example here can get you to answer a question I've wondered about for a while. With Me.Label1 .Text = ... .Refresh()
3
1846
by: Sean Shanny | last post by:
To all, We are running postgresql 7.4.1 on an G5 with dual procs, OSX 10.3.3 server, 8GB mem, attached to a fully configured 3.5TB XRaid box via fibre channel. I think we have run into this issue before but I thought the code was fixed. :-( I have the following SQL:
25
1850
by: metaperl.etc | last post by:
A very old thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e discusses the optional "else:" clause of the for statement. I'm wondering if anyone has ever found a practical use for the else branch?
5
2496
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Hoping to find a logical explanation for a certain behavior. Consider this script: USE pubs GO IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = 'CA') PRINT 'TRUE'
5
10143
by: No bother | last post by:
I am using 5.0.26-NT on Windows 2000. I have need to use a reference in the outer from clause in a subquery in the select clause. Consider the following example: Select (select b.baitID from b where b.entrydate curdate()) as wantedBaitIDs from bait_tbl b; My actual need is more complex than this as part of it is a rough cross tab. If...
8
3468
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 , etc. I want to go to the first row, do a WHERE statement, return the
2
1391
by: AWasilenko | last post by:
I can't figure out this problem Im having, I just can't understand why it is ignoring the call I put in. First the code (This is a cherrypy website): import sys, cherrypy, html class Root: @cherrypy.expose def index(self, pageid = "Index"): selection = html.Page()
1
2044
by: mtnbikur | last post by:
Lots of reading out there says you should only place “join criteria” in the join clause. That makes perfect sense. What about other search criteria? I’ve got other criteria, ie. cst_delete_flag = 0 that predecessors have put in the where clause and sometimes it’s programmed into the join clause. Ie. Select cst_id, cst_name, from...
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.