BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL query problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Ignacio Vazquez
Guest





PostPosted: Tue Sep 16, 2003 5:01 pm    Post subject: SQL query problem Reply with quote



MS SQL Server 7.0

Assuming the following tables:

lookup
--------
code char(1)
result char(16)
specificity

code result
---- ------
A R1
B R2
% R3


data
--------
rid
amount
code

rid amount code
--- ------ ----
1 4.3 A
2 2.4 B
3 5.4 D
4 3.7 C


I would like to get the following (actual sort order is unimportant):

rid amount result
--- ------ ------
1 4.3 R1
2 2.4 R2
3 5.4 R3
4 3.7 R3

I know I have to do a LIKE between data.code and lookup.code, but that would
result in duplicate records. I also added a specificity field to lookup
whereby the records without the wildcard had higher values and had a HAVING
MAX(specificity), but the SQL parser refused it. Any ideas?

Thanks,
Ignacio

--
No, don't send me e-mail directly. No, just don't.



Back to top
Ignacio Vazquez
Guest





PostPosted: Tue Sep 16, 2003 6:03 pm    Post subject: Re: SQL query problem Reply with quote



"Ping Kam" <pkam (AT) quikcard (DOT) com> wrote in message
[email]3f674edf (AT) newsgroups (DOT) borland.com[/email]...
Quote:
I don't use SQLServer so I can't give you the exact SQL, but I believe a
union all will do it

select d.rid, d.amount, l.result from lookup l, data d
where l.code = d.code
union all
select d.rid, d.amount, R3 as result from data d
where d.code not in (select code from lookup)

This can actually be simplified to

SELECT d.rid, d.amount, ISNULL(l.result, 'R3')
FROM data d
LEFT JOIN lookup l
ON d.code=l.code

However the actual problem I'm working on is far more complex than the one I
presented with multiple joins, and therefore using the LIKE operator is
almost a requirement since I try to follow the 'dumb code, smart data'
paradigm where possible.

Cheers,
Ignacio

--
No, don't send me e-mail directly. No, just don't.




Back to top
Ping Kam
Guest





PostPosted: Tue Sep 16, 2003 6:04 pm    Post subject: Re: SQL query problem Reply with quote



Hi Ignacio,

First, I suggest not to use '%' as code in lookup.

I don't use SQLServer so I can't give you the exact SQL, but I believe a
union all will do it

select d.rid, d.amount, l.result from lookup l, data d
where l.code = d.code
union all
select d.rid, d.amount, R3 as result from data d
where d.code not in (select code from lookup)

Not tested.

HTH,
Ping Kam

"Ignacio Vazquez" <ivazquezATorioncommunications.com> wrote

Quote:
MS SQL Server 7.0

Assuming the following tables:

lookup
--------
code char(1)
result char(16)
specificity

code result
---- ------
A R1
B R2
% R3


data
--------
rid
amount
code

rid amount code
--- ------ ----
1 4.3 A
2 2.4 B
3 5.4 D
4 3.7 C


I would like to get the following (actual sort order is unimportant):

rid amount result
--- ------ ------
1 4.3 R1
2 2.4 R2
3 5.4 R3
4 3.7 R3

I know I have to do a LIKE between data.code and lookup.code, but that
would
result in duplicate records. I also added a specificity field to lookup
whereby the records without the wildcard had higher values and had a
HAVING
MAX(specificity), but the SQL parser refused it. Any ideas?

Thanks,
Ignacio

--
No, don't send me e-mail directly. No, just don't.






Back to top
R. Rogers
Guest





PostPosted: Tue Sep 16, 2003 7:05 pm    Post subject: Re: SQL query problem Reply with quote

Hi Ignacio,

Try this:

Create Table #Lookup (Code Char(1) Not Null, Result Char(16) Not Null)

Insert Into #Lookup (Code, Result) Values ('A', 'R1')
Insert Into #Lookup (Code, Result) Values ('B', 'R2')
Insert Into #Lookup (Code, Result) Values ('%', 'R3')

Create Table #Data (Rid SmallInt Not Null, Amount Decimal(2, 1) Not Null,
Code Char(1) Not Null)

Insert Into #Data (Rid, Amount, Code) Values (1, 4.3, 'A')
Insert Into #Data (Rid, Amount, Code) Values (2, 2.4, 'B')
Insert Into #Data (Rid, Amount, Code) Values (3, 5.4, 'D')
Insert Into #Data (Rid, Amount, Code) Values (4, 3.7, 'C')

Select D.Rid, Min(D.Amount), IsNull(Min(L.Result), 'R3') From #Data D Left
Outer Join #Lookup L On L.Code = D.Code
Group By D.Rid

Richard Rogers


Back to top
Ignacio Vazquez
Guest





PostPosted: Tue Sep 16, 2003 7:11 pm    Post subject: Re: SQL query problem Reply with quote

"R. Rogers" <richardis (AT) rogers (DOT) com> wrote in message
3f675ee7$1 (AT) newsgroups (DOT) borland.com...
Quote:
Try this:

Please read my response to Ping Kam.

Cheers,
Ignacio

--
No, don't send me e-mail directly. No, just don't.



Back to top
Ignacio Vazquez
Guest





PostPosted: Tue Sep 16, 2003 7:33 pm    Post subject: Re: SQL query problem Reply with quote

Found it.

Add a column "specificity" of type int to lookup where the fields with fewer
wildcards have higher values. The query then becomes:

SELECT d.rid, d.amount, l.result
FROM data d
INNER JOIN lookup l
ON d.code LIKE l.code
WHERE EXISTS (
SELECT MAX(l2.specificity)
FROM lookup l2
WHERE d.code LIKE l2.code
HAVING l.specificity=MAX(l2.specificity))

Cheers,
Ignacio

--
No, don't send me e-mail directly. No, just don't.


Back to top
Ignacio Vazquez
Guest





PostPosted: Tue Sep 16, 2003 8:44 pm    Post subject: Re: SQL query problem Reply with quote

"Ping Kam" <pkam (AT) quikcard (DOT) com> wrote in message
[email]3f677472 (AT) newsgroups (DOT) borland.com[/email]...
Quote:
Still don't know why you must use like.

If what I showed was the whole thing then LIKE would've been overkill. The
actual problem, however, is much larger. Imagine between three and five
fields like "code".

Quote:
The query looks a little too complicated. I beleive the following should
yield the same result but looks simpler to me, not sure how it impacts the
performance though.

If I could I'd eliminate the subquery entirely, but the query works as-is
which is fine for now.

Cheers,
Ignacio

--
No, don't send me e-mail directly. No, just don't.



Back to top
Ping Kam
Guest





PostPosted: Tue Sep 16, 2003 8:44 pm    Post subject: Re: SQL query problem Reply with quote

"Ignacio Vazquez" <ivazquezATorioncommunications.com> wrote

Quote:
Add a column "specificity" of type int to lookup where the fields with
fewer
wildcards have higher values. The query then becomes:

SELECT d.rid, d.amount, l.result
FROM data d
INNER JOIN lookup l
ON d.code LIKE l.code
WHERE EXISTS (
SELECT MAX(l2.specificity)
FROM lookup l2
WHERE d.code LIKE l2.code
HAVING l.specificity=MAX(l2.specificity))

Still don't know why you must use like. But anyway, glad you find the

answer.

The query looks a little too complicated. I beleive the following should
yield the same result but looks simpler to me, not sure how it impacts the
performance though.

SELECT d.rid, d.amount, l.result
FROM data d
INNER JOIN lookup l
ON d.code LIKE l.code
WHERE l.specificity = (
SELECT MAX(l2.specificity)
FROM lookup l2
WHERE d.code LIKE l2.code)

Ping Kam



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.