SQL - Counting an Expression

m0atz

Board Regular
Joined
Jul 17, 2008
Messages
247
Hi Guys, I have two columns in a "Customers" table: "Cust" and "Cust_Adr" which are the Customers Name and Customers Address. The postcode is included in the Address column and I want to extract the Postcode and then return results where the same postcode appears more than 5 times.

So I tried:

Code:
[COLOR=#333333][FONT=Segoe UI]SELECT c.Cust, c.Cust_ADR, [/FONT][/COLOR][COLOR=#333333][FONT=Segoe UI]Mid(Cust_adr,InStrRev(cust_adr,",")+1) AS Postcode[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]FROM tbl_Customers AS c[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]GROUP BY c.Cust, c.Cust_ADR, [/FONT][/COLOR][COLOR=#333333][FONT=Segoe UI]Mid(Cust_adr,InStrRev(cust_adr,",")+1)[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]HAVING COUNT([/FONT][/COLOR][COLOR=#333333][FONT=Segoe UI]Mid(Cust_adr,InStrRev(cust_adr,",")+1)[/FONT][/COLOR][COLOR=#333333][FONT=Segoe UI]) >=5[/FONT][/COLOR]

But no joy. I'm getting "Data Type Mismatch is Criteria Expression" Error.

Can anyone help with where I'm going wrong? Cheers
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You don't want to return the individual Cust and Cust_ADR because if records have the same PostCode but different Cust or Cust_ADR fields, they won't be grouped!

I think you code should look something like this:
Code:
SELECT Mid([Cust_adr],InStrRev([cust_adr],",")+1) AS Postcode, Count(Mid([Cust_adr],InStrRev([cust_adr],",")+1)) AS PostcodeCount
FROM Customers
GROUP BY Mid([Cust_adr],InStrRev([cust_adr],",")+1)
HAVING Count(Mid([Cust_adr],InStrRev([cust_adr],",")+1))>=5;
 
Upvote 0
Thanks Joe, appreciate the reply. I'm still getting the "Data Type Mismatch is Criteria Expression" Error. It must be something this end within the data I assume, I will recheck. Appreciate the code.
 
Upvote 0
Do you have any null or blank values in the Cust_ADR field anywhere in your table?
Or any really short entries that would cause errors for your formula?
 
Upvote 0
Hi Joe, could well be the case. I've ran your code on some sample data and it works a treat, so must be something within the live data. Appreciate the reply.
 
Upvote 0
I entered a NULL value in for one address, and got the error you were talking about.
I made this change, and it seemed to handle the NULLs and worked:
Code:
SELECT IIf(Len([Cust_adr])>5,Mid([Cust_adr],InStrRev([cust_adr],",")+1),"") AS Postcode, Count(IIf(Len([Cust_adr])>5,Mid([Cust_adr],InStrRev([cust_adr],",")+1),"")) AS PostcodeCount
FROM Customers
GROUP BY IIf(Len([Cust_adr])>5,Mid([Cust_adr],InStrRev([cust_adr],",")+1),"")
HAVING Count(IIf(Len([Cust_adr])>5,Mid([Cust_adr],InStrRev([cust_adr],",")+1),""))>=5;
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top