Sunday, March 25, 2012

Count by matching first two letters

Say I have a table of names
How\Can I provide a count for names that have matching first two letters
For instance if my table was this
Name
DA234
DA333
ebEEE
EBddd
EEddd
I would want my output to be
DA 2
EB 2
EE 1
I hope I am communicating my question correctly and thanks for your
consideration.
ScottTry:
select
LEFT (MyCol, 2)
, count (*)
from
MyTable
group by
LEFT (MyCol, 2)
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"scott" <scott@.discussions.microsoft.com> wrote in message
news:3ED42509-B63D-4376-8C81-981ED42C9D2D@.microsoft.com...
Say I have a table of names
How\Can I provide a count for names that have matching first two letters
For instance if my table was this
Name
DA234
DA333
ebEEE
EBddd
EEddd
I would want my output to be
DA 2
EB 2
EE 1
I hope I am communicating my question correctly and thanks for your
consideration.
Scott|||Try,
select left(name, 2) as l2_name, count(*) as cnt
from t1
group by left(name, 2)
AMB
"scott" wrote:

> Say I have a table of names
> How\Can I provide a count for names that have matching first two letters
> For instance if my table was this
> Name
> DA234
> DA333
> ebEEE
> EBddd
> EEddd
> I would want my output to be
> DA 2
> EB 2
> EE 1
> I hope I am communicating my question correctly and thanks for your
> consideration.
> Scott|||scott wrote:
> Say I have a table of names
> How\Can I provide a count for names that have matching first two
> letters
> For instance if my table was this
> Name
> DA234
> DA333
> ebEEE
> EBddd
> EEddd
> I would want my output to be
> DA 2
> EB 2
> EE 1
> I hope I am communicating my question correctly and thanks for your
> consideration.
> Scott
SELECT UPPER(LEFT(COL_NAME, 2)), COUNT(*)
FROM TABLE_NAME
GROUP BY UPPER(LEFT(COL_NAME, 2))
David Gugick
Imceda Software
www.imceda.com

No comments:

Post a Comment