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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query to look for differences 1

Status
Not open for further replies.

dmkennard2

Technical User
Jun 11, 2004
101
GB
Hi,
I have a stock database in access. In the database is flat table with locations, codes Qty's etc....

Each location is split into Back and Front with a maximum capacity to hold 2 codes.
I cant seem to figure out how to create a query that will display locations that have different codes in the Front from the Back or Vice Versa.

Any help much appreciated.

Dazz
 
Have you looked at a Find Unmatched Query (query wizard). Is it what you want?
 
No i dont think so.

If i have:
1 - Loc1Back = Code1 & Loc1Front = Code1
2 - Loc2Back = Code1 & Loc2Front = Code2
3 - Loc3Back = Code2 & Loc3Front = Code1

Then the query will return records 2 and 3 because these are the records with 2 codes in.

I have tried to count codes for a location and only display the ones that show 2 or more, but this is counting the amount of pallets in each location and this can 2 in each!

Thanks
Dazz
 
How about:
[tt]SELECT tblQ.Field1, Mid([Field1],InStr([field1],"Back")+7,5) AS Chk1, Mid([Field1],InStr([field1],"Front")+8) AS Chk2
FROM tblQ
WHERE (((Mid([Field1],InStr([field1],"Back")+7,5))<>Mid([Field1],InStr([field1],"Front")+8)));[/tt]

Have you considered a redesign to split this field into two?
 
Hi Remou, i tried your code and it takes the B ad F and puts it into a seperate field

Sample Data as requested
Code Location
123 H01L-001-3B
123 H01L-001-3B
1234 H01L-001-3F
1234 H01L-001-3F
----------------------------------
10000 H01L-001-4B
10000 H01L-001-4B
10000 H01L-001-4F
10000 H01L-001-4F

With the above data the query will return location H01L-001-3B as this is the only location wiht more than one Code.
If it helps i also have the Location split down like this:
Aisle - H01
Side - L
Location - 001
Level - 4
Back or Front - B

Thanks
Dazz
 

try this (replace table1 with your table name)

Code:
SELECT 
DISTINCT Loc
FROM (
SELECT 
Left(location,Len(location)-1) AS Loc, code, Count(code)
FROM 
table1
GROUP BY 
Left([location],Len(location)-1), code
HAVING Count(code)=1
)

N
 
Excellent that works a treat.
Thank you very much.

One more question if i can be so bold?
If i wanted to see the codes associated with these locations in a report, how can i add these?
I have a list of codes in a seperate table as well as the table i am using in the above query?

Thanks again.
Dazz
 
unless you give us details such as tables' structure (fields,datatypes), sample data and expected results can only guess. so my guess is

use my query in the where clause:

Code:
SELECT 
code 
FROM 
table 
WHERE 
location in (
myQuery
)
 
Hi,
Going back to my original question, this is the code that i end up with.

Code:
SELECT DISTINCT [%$##@_Alias].Location
FROM [SELECT Left(WCS_Location,Len(WCS_Location)-1) AS Location, Material, Count(Material)
FROM 
tblMainData
GROUP BY 
Left([WCS_Location],Len(WCS_Location)-1), Material
HAVING Count(Material)=1]. AS [%$##@_Alias];

Not sure where the "[%$##@_Alias]."comes from!!

Every day i delete all data in the Material table and the MainData table. After i have done this i get an error when running the query again. The error is "Invalid Procedure Call"

Any ideas?

Dazz
 

this is a better solution

Code:
select location from
(SELECT 
Left(WCS_Location,Len(WCS_Location)-1) as location
FROM 
tblMainData
GROUP BY 
Left(WCS_Location,Len(WCS_Location)-1), material)
group by location
having count(location)>1

Every day i delete all data in the Material table and the MainData table.

I am not sure what you mean. First of all we are only getting data from one table in this query, tblMainData, and second this query will get nothing if the table is empty. Also, it can't handle null values for WCS_Location.

N
 

the alias "[%$##@_Alias]." is added by access. do not worry about it.

N

 
Sorry i should have explained a bit better.
Each day i delete all the data in the MainData table and re-import fresh SAP data back into the table. We use Access to manipulate the data easily and produce some reports.

After i have re-imported the data i get the error "Invalid Procedure Call"

I have just tried your new code and am getting the error even though its in a new query.

Dazz
 
Update - I took out the records where the WCS_Location are Null and it works. Is there a way of getting it to run even if WCS_Location is Null?

Dazz
 

thought so. try this

Code:
SELECT loc
FROM (SELECT 
iif(location is not null, Left(location,Len(location)-1),'') as loc
FROM 
table1
GROUP BY 
iif(location is not null,Left(location,Len(location)-1),''), code
)
GROUP BY loc
HAVING count(loc)>1;

N
 
Nope,Still getting the error, this is my code:
Code:
SELECT Location
FROM [SELECT 
iif(WCS_Location is not null, Left(WCS_Location,Len(WCS_Location)-1),'') as Location
FROM 
tblMainData
GROUP BY 
iif(WCS_location is not null,Left(WCS_Location,Len(WCS_Location)-1),''), Material
]. AS [%$##@_Alias]
GROUP BY Location
HAVING count(Location)>1;

I can work around this by using an update query to enter "No Location" into the Nulls when i import the data. ;)

Dazz
 

that's one solution i suppose... how do you run the query? inside access, from a vb app, etc?

N
 
I have a spreadsheet with VB i have written that formulates the extract from SAP. This then saves to a specific area.
In Access i have a Control Panel (Switchboard) that has a button that runs a Macro to import that data from the spreadsheet and then run the update query to insert the "No Location" into Null locations.

Probably not the best way to to things, but it works.

Dazz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top