Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query problem

Status
Not open for further replies.

dfwcharles

Technical User
Apr 10, 2002
36
US
I'm trying to query a table to find certain Glaccount codes. The datalength of the code varies, the maximum length of the code is 15, but I only want to find the codes that have a datalength of 10. Here's the catch; Some of the codes have only 10 char, but there will be a code that matches the frist 10 with an additional 5 chars at the end. I only want the codes that have 10 with no matching 15. Here's an small example of the data.

glaccount
---------------------
1106195001
110619500181155
1106400000
110640000082167
1106400001
110640000100000
1106400002
1106400003
110640000381155
110640000381156
1106400004
110640000482141
110640000482142
1106400005
1106400006
110640000682723

In this example I would only what to see 1106400002 and 1106400005. I was thinking about joining the table to itself, with some kind of where clause, but I'm stumped.

Thanks in advance
Charles
 
Code:
select a.code
from tbl a
where len(a.code) = 10 and a.code not in (select left(code,10) from tbl where len(code) > 10)

what's this give yOU?

dlc
 
I think this is what you are looking for:
Code:
Select Max(Glaccount) from MyTable Group By Left(Glaccount,10) Having Count(*)=1
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
I would agree with DonutMan

however, do you need the MAX?

Code:
Select Glaccount from MyTable Group By Left(Glaccount,10) Having Count(Glaccount )=1
 
Checkai, I get codes with a length of 10, but they still have matching codes with 15.

Donutman, you query gets me close enough. Your code also returns glaccounts that are 15 in length but have no corresponding code of 10. Does make sense? I.e, returns 110640400000000 which does not have a match like this 1106404000. Close enough for what i need to do

Thanks for y'alls help
Charles
 
Good point. Missed that part.
Code:
Select Glaccount from MyTable Group By Left(Glaccount,10) Having Count(Glaccount )=1 and Len(Glaccount)=10
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Can the 10-letter code ever be repeated?

1106400004
1106400004

Donutman's query is great, otherwise. If there can be duplicate 10-letter codes, try:

Code:
SELECT G1.Glaccount
   FROM TheTable G1
      LEFT JOIN TheTable G2 ON Len(G2.Glaccount)=15 AND G1.GlAccount = Left(G2.Glaccount,10)
   WHERE G2.Glaccount IS NULL
 
The glaccounts will always be distinct. I think your query returned everthing but what i needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top