473,761 Members | 4,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Select Query help

Hi,

trying to retrieve postal codes from the db but only want the query to look
at the first 3 digits of the code tried using
(LEFT(dbo.booki ng_form.COLL_PO ST_CODE),3) but that doesn't work. I don't
want the query to count individual post codes but instead look at an area
found in the first 3 digits e.g. HA0 3TD is for a particular house but HA)
is for the area Harrow.
"SELECT Count(dbo.booki ng_form.COLL_PO ST_CODE) AS CountOfCOLL_POS T_CODE,
COLL_POST_CODE FROM dbo.booking_for m GROUP BY COLL_POST_CODE ORDER BY
CountOfCOLL_POS T_CODE DESC"

Regards
Simon Gare
The Gare Group Limited

website: www.thegaregroup.co.uk
website: www.privatehiresolutions.co.uk
Jan 5 '07 #1
15 3327
Simon Gare wrote on 05 jan 2007 in
microsoft.publi c.inetserver.as p.general:
Hi,

trying to retrieve postal codes from the db but only want the query to
look at the first 3 digits of the code tried using
(LEFT(dbo.booki ng_form.COLL_PO ST_CODE),3) but that doesn't work. I
don't want the query to count individual post codes but instead look
at an area found in the first 3 digits e.g. HA0 3TD is for a
particular house but HA) is for the area Harrow.
"SELECT Count(dbo.booki ng_form.COLL_PO ST_CODE) AS
CountOfCOLL_POS T_CODE, COLL_POST_CODE FROM dbo.booking_for m GROUP BY
COLL_POST_CODE ORDER BY CountOfCOLL_POS T_CODE DESC"
You should mention the db-engine used for a correct answer.

I use this with the Jet engine:

SQL = "SELECT left(postcode,3 ) as pc,count(pc) as tal" &_
" FROM myTbl GROUP BY left(postcode,3 )"

'''response.wri te SQL &"<hr>"
set mDATA=CONNECT.E xecute(SQL)

Response.Write "<table border=1><tr>" & vbcrlf
Do Until mDATA.Eof
tal = mDATA("tal")
pc = mDATA("pc")
if pc="" then pc="No postcode: " else pc="Postcode: " & pc & ": "
Response.Write "<td>"&pc&"<td> <b>"&tal&"</b><tr>" & vbcrlf
mDATA.MoveNext
Loop
Response.Write "</table>" & vbcrlf

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 5 '07 #2
Thanks Evertjan, having a problem though could you look below and suggest.

<%
Dim AreaColl
Dim AreaColl_numRow s

Set AreaColl = Server.CreateOb ject("ADODB.Rec ordset")
AreaColl.Active Connection = MM_TobiasNET_ST RING
AreaColl.Source = "SELECT (Left(Count(dbo .booking_form.C OLL_POST_CODE), 3))
AS CountOfCOLL_POS T_CODE, COLL_POST_CODE FROM dbo.booking_for m GROUP BY
COLL_POST_CODE ORDER BY CountOfCOLL_POS T_CODE DESC"
AreaColl.Cursor Type = 0
AreaColl.Cursor Location = 2
AreaColl.LockTy pe = 1
AreaColl.Open()

AreaColl_numRow s = 0
%>

and in the body
<td colspan=2>Top 10 collection post codes</td>
</tr>
<%
While ((Repeat1__numR ows <0) AND (NOT AreaColl.EOF))
%>
<tr>
<td width="100"
class="DataSetT ext"><%=(AreaCo ll.Fields.Item( "COLL_POST_CODE ").Value)%> </td>
<td width="790"
class="DataSetT ext"><%=(AreaCo ll.Fields.Item( "CountOfCOLL_PO ST_CODE").Value )
%></td>
</tr>
<%
Repeat1__index= Repeat1__index+ 1
Repeat1__numRow s=Repeat1__numR ows-1
AreaColl.MoveNe xt()
Wend
%>

Thanks in advance
"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******** ************@19 4.109.133.242.. .
Simon Gare wrote on 05 jan 2007 in
microsoft.publi c.inetserver.as p.general:
Hi,

trying to retrieve postal codes from the db but only want the query to
look at the first 3 digits of the code tried using
(LEFT(dbo.booki ng_form.COLL_PO ST_CODE),3) but that doesn't work. I
don't want the query to count individual post codes but instead look
at an area found in the first 3 digits e.g. HA0 3TD is for a
particular house but HA) is for the area Harrow.
"SELECT Count(dbo.booki ng_form.COLL_PO ST_CODE) AS
CountOfCOLL_POS T_CODE, COLL_POST_CODE FROM dbo.booking_for m GROUP BY
COLL_POST_CODE ORDER BY CountOfCOLL_POS T_CODE DESC"

You should mention the db-engine used for a correct answer.

I use this with the Jet engine:

SQL = "SELECT left(postcode,3 ) as pc,count(pc) as tal" &_
" FROM myTbl GROUP BY left(postcode,3 )"

'''response.wri te SQL &"<hr>"
set mDATA=CONNECT.E xecute(SQL)

Response.Write "<table border=1><tr>" & vbcrlf
Do Until mDATA.Eof
tal = mDATA("tal")
pc = mDATA("pc")
if pc="" then pc="No postcode: " else pc="Postcode: " & pc & ": "
Response.Write "<td>"&pc&"<td> <b>"&tal&"</b><tr>" & vbcrlf
mDATA.MoveNext
Loop
Response.Write "</table>" & vbcrlf

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Jan 5 '07 #3
Simon Gare wrote on 05 jan 2007 in
microsoft.publi c.inetserver.as p.general:
"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******** ************@19 4.109.133.242.. .
>Simon Gare wrote on 05 jan 2007 in
microsoft.publ ic.inetserver.a sp.general:

SQL = "SELECT left(postcode,3 ) as pc,count(pc) as tal" &_
" FROM myTbl GROUP BY left(postcode,3 )"
[Please do not toppost on usenet]
Thanks Evertjan, having a problem though could you look below and
suggest.

Set AreaColl = Server.CreateOb ject("ADODB.Rec ordset")
<%=(AreaColl.Fi elds.Item("Coun tOfCOLL_POST_CO DE").Value)%>
I never use a Recordset [you can easily do without it]
and the code you show is much to complex for me to read with all those long
names with multiple _'s and unnecessary ()'s.
.... having a problem though could you look below and
suggest.
If you could test your code yourself, starting with the smallest and most
readable code that gives a problem, perhaps you could even come up with
explaining the kind of problem you have, Simon.

btw, did my code work with you?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 5 '07 #4

"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******** ***********@194 .109.133.242...
Simon Gare wrote on 05 jan 2007 in
microsoft.publi c.inetserver.as p.general:
>"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******* *************@1 94.109.133.242. ..
>>Simon Gare wrote on 05 jan 2007 in
microsoft.pub lic.inetserver. asp.general:

SQL = "SELECT left(postcode,3 ) as pc,count(pc) as tal" &_
" FROM myTbl GROUP BY left(postcode,3 )"

[Please do not toppost on usenet]
>Thanks Evertjan, having a problem though could you look below and
suggest.

Set AreaColl = Server.CreateOb ject("ADODB.Rec ordset")
><%=(AreaColl.F ields.Item("Cou ntOfCOLL_POST_C ODE").Value)%>

I never use a Recordset [you can easily do without it]
and the code you show is much to complex for me to read with all those
long
names with multiple _'s and unnecessary ()'s.
What do you use instead?
-Mark
>.... having a problem though could you look below and
suggest.

If you could test your code yourself, starting with the smallest and most
readable code that gives a problem, perhaps you could even come up with
explaining the kind of problem you have, Simon.

btw, did my code work with you?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Jan 6 '07 #5

"Simon Gare" <sg@simongare.c omwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Thanks Evertjan, having a problem though could you look below and suggest.
What is the problem?

[more comments inline...]

<%
Dim AreaColl
Dim AreaColl_numRow s

Set AreaColl = Server.CreateOb ject("ADODB.Rec ordset")
AreaColl.Active Connection = MM_TobiasNET_ST RING
You should create an explicit connection object, rather than relying on ADO
to create one for you implicitly.
AreaColl.Source = "SELECT (Left(Count(dbo .booking_form.C OLL_POST_CODE), 3))
I doubt this is legal, LEFT expects varchar or text, COUNT returns int. I
think what you want is:

SELECT COUNT(*), LEFT(COLL_POST_ CODE, 3) FROM dbo.booking_for m GROUP BY
LEFT(COLL_POST_ CODE, 3) ORDER BY COUNT(*) DESC

You might want to consider defining a computed column for the left 3 of the
postal code, for both ease of reference and db server efficiency.
-Mark
AS CountOfCOLL_POS T_CODE, COLL_POST_CODE FROM dbo.booking_for m GROUP BY
COLL_POST_CODE ORDER BY CountOfCOLL_POS T_CODE DESC"
AreaColl.Cursor Type = 0
AreaColl.Cursor Location = 2
AreaColl.LockTy pe = 1
AreaColl.Open()

AreaColl_numRow s = 0
%>

and in the body
<td colspan=2>Top 10 collection post codes</td>
</tr>
<%
While ((Repeat1__numR ows <0) AND (NOT AreaColl.EOF))
%>
<tr>
<td width="100"
class="DataSetT ext"><%=(AreaCo ll.Fields.Item( "COLL_POST_CODE ").Value)%> </td>
<td width="790"
class="DataSetT ext"><%=(AreaCo ll.Fields.Item( "CountOfCOLL_PO ST_CODE").Value )
%></td>
</tr>
<%
Repeat1__index= Repeat1__index+ 1
Repeat1__numRow s=Repeat1__numR ows-1
AreaColl.MoveNe xt()
Wend
%>

Thanks in advance
"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******** ************@19 4.109.133.242.. .
>Simon Gare wrote on 05 jan 2007 in
microsoft.publ ic.inetserver.a sp.general:
Hi,

trying to retrieve postal codes from the db but only want the query to
look at the first 3 digits of the code tried using
(LEFT(dbo.booki ng_form.COLL_PO ST_CODE),3) but that doesn't work. I
don't want the query to count individual post codes but instead look
at an area found in the first 3 digits e.g. HA0 3TD is for a
particular house but HA) is for the area Harrow.
"SELECT Count(dbo.booki ng_form.COLL_PO ST_CODE) AS
CountOfCOLL_POS T_CODE, COLL_POST_CODE FROM dbo.booking_for m GROUP BY
COLL_POST_CODE ORDER BY CountOfCOLL_POS T_CODE DESC"

You should mention the db-engine used for a correct answer.

I use this with the Jet engine:

SQL = "SELECT left(postcode,3 ) as pc,count(pc) as tal" &_
" FROM myTbl GROUP BY left(postcode,3 )"

'''response.wr ite SQL &"<hr>"
set mDATA=CONNECT.E xecute(SQL)

Response.Wri te "<table border=1><tr>" & vbcrlf
Do Until mDATA.Eof
tal = mDATA("tal")
pc = mDATA("pc")
if pc="" then pc="No postcode: " else pc="Postcode: " & pc & ": "
Response.Write "<td>"&pc&"<td> <b>"&tal&"</b><tr>" & vbcrlf
mDATA.MoveNext
Loop
Response.Wri te "</table>" & vbcrlf

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Jan 6 '07 #6
Good idea. For a direct marketing app, I created a column which took the
first 2 letters of the postcode, which made more localised selections
easier. For a higher level of granularity, I would suggest all those
characters to the left of the space. The first 3 won't always work. BS2 is
in the centre of Bristol and BS21 is Clevedon - over 20 miles away for
instance, but would both be included in a search for Left(PostCode,3 ) =
"BS2"

--
Mike Brind

"Mark McGinty" <mm******@spamf romyou.comwrote in message
news:ei******** ******@TK2MSFTN GP02.phx.gbl...
>
>
You might want to consider defining a computed column for the left 3 of
the postal code, for both ease of reference and db server efficiency.
-Mark
>AS CountOfCOLL_POS T_CODE, COLL_POST_CODE FROM dbo.booking_for m GROUP BY
COLL_POST_CO DE ORDER BY CountOfCOLL_POS T_CODE DESC"
AreaColl.Curso rType = 0
AreaColl.Curso rLocation = 2
AreaColl.LockT ype = 1
AreaColl.Open( )

AreaColl_numRo ws = 0
%>

and in the body
<td colspan=2>Top 10 collection post codes</td>
</tr>
<%
While ((Repeat1__numR ows <0) AND (NOT AreaColl.EOF))
%>
<tr>
<td width="100"
class="DataSet Text"><%=(AreaC oll.Fields.Item ("COLL_POST_COD E").Value)%> </td>
<td width="790"
class="DataSet Text"><%=(AreaC oll.Fields.Item ("CountOfCOLL_P OST_CODE").Valu e)
%></td>
</tr>
<%
Repeat1__index= Repeat1__index+ 1
Repeat1__numRow s=Repeat1__numR ows-1
AreaColl.MoveNe xt()
Wend
%>

Thanks in advance
"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******* *************@1 94.109.133.242. ..
>>Simon Gare wrote on 05 jan 2007 in
microsoft.pub lic.inetserver. asp.general:

Hi,

trying to retrieve postal codes from the db but only want the query to
look at the first 3 digits of the code tried using
(LEFT(dbo.book ing_form.COLL_P OST_CODE),3) but that doesn't work. I
don't want the query to count individual post codes but instead look
at an area found in the first 3 digits e.g. HA0 3TD is for a
particular house but HA) is for the area Harrow.
"SELECT Count(dbo.booki ng_form.COLL_PO ST_CODE) AS
CountOfCOLL_PO ST_CODE, COLL_POST_CODE FROM dbo.booking_for m GROUP BY
COLL_POST_CO DE ORDER BY CountOfCOLL_POS T_CODE DESC"

You should mention the db-engine used for a correct answer.

I use this with the Jet engine:

SQL = "SELECT left(postcode,3 ) as pc,count(pc) as tal" &_
" FROM myTbl GROUP BY left(postcode,3 )"

'''response.w rite SQL &"<hr>"
set mDATA=CONNECT.E xecute(SQL)

Response.Writ e "<table border=1><tr>" & vbcrlf
Do Until mDATA.Eof
tal = mDATA("tal")
pc = mDATA("pc")
if pc="" then pc="No postcode: " else pc="Postcode: " & pc & ": "
Response.Write "<td>"&pc&"<td> <b>"&tal&"</b><tr>" & vbcrlf
mDATA.MoveNext
Loop
Response.Writ e "</table>" & vbcrlf

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)



Jan 6 '07 #7
Mark McGinty wrote on 06 jan 2007 in
microsoft.publi c.inetserver.as p.general:
>I never use a Recordset [you can easily do without it]
and the code you show is much to complex for me to read with all those
long
names with multiple _'s and unnecessary ()'s.

What do you use instead?
Of what?

The multiple _'s or unnecessary ()'s?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 6 '07 #8

"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******** ************@19 4.109.133.242.. .
Mark McGinty wrote on 06 jan 2007 in
microsoft.publi c.inetserver.as p.general:
>>I never use a Recordset [you can easily do without it]
and the code you show is much to complex for me to read with all those
long
names with multiple _'s and unnecessary ()'s.

What do you use instead?

Of what?

The multiple _'s or unnecessary ()'s?
Instead of Recordset.
-Mark

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Jan 6 '07 #9
Mark McGinty wrote on 06 jan 2007 in
microsoft.publi c.inetserver.as p.general:
>
"Evertjan." <ex************ **@interxnl.net wrote in message
news:Xn******** ************@19 4.109.133.242.. .
>Mark McGinty wrote on 06 jan 2007 in
microsoft.publ ic.inetserver.a sp.general:
>>>I never use a Recordset [you can easily do without it]
and the code you show is much to complex for me to read with all those
long names with multiple _'s and unnecessary ()'s.

What do you use instead?

Of what?

The multiple _'s or unnecessary ()'s?

Instead of Recordset.
Ah, that's what you mean. [I would never have guessed]

Well nothing.

The execute() command already gives me the info I nead when reading with
sql SELECT, and with the UPDATE and INSERT SQL string it does a good job
for writing to the db.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 6 '07 #10

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

Similar topics

5
1943
by: Got2Go | last post by:
Hello Group, I have a table that has 3 columns: ID (int), datetime, Value(varchar) ID = ID for the SNMP device datetime = time record was added value = value added for that device. This table contains sample # values taken from a device (SNMP) every 5
6
17881
by: Rowland | last post by:
Hi, I've got a field that stores numeric values, representing a tracking number. I've also got a stored procedure that will extract this field and return it to a client. However, I would like to return it slightly differently to the way in which it is stored. Basically, I want to return it as TRK000nnn - Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and nnn is the number itself - results would look something like...
1
26574
by: GrungyApe | last post by:
I am new to this type of query and while I've found a lot of help on the forums, I am simply not getting it to work. Here is my attempt currently: SELECT DISTINCT(VADR.SUBSCRIBER_ID), VADR.ACCOUNT_DESC, VADR.COUNTY_NAME, VADR.STREET_NO, VADR.STREET_NO_SUFFIX,
2
2391
by: Iain Adams | last post by:
Hey I want to get some data from some tables but I want every field except the primary key field returned. Is this possible? I.e SELECT * (except primary) FROM table1, table2, table3 Any ideas??
1
2664
by: Jordan M. | last post by:
Hi, Hoping to get some help modifying the following query that I have... TABLE: NAMES ID, FirstName, LastName TABLE: EMAILS ID,LinkID,Email,LastUpdateDate
0
9522
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8770
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.