Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello!
I am using Microsoft SQL Server 2005
I have a table like this:
PHP
QueueNo
 3B001
 3B002  
 3B003 
 3G004
 3G005  
 2P001
 2G002
 2P003  
 1P001


I want to make a new table that counts all the ones that start with '3','2', and '1'?
Maybe a table like this:
PHP
QueueInitial    Count
     3            5
     2            3
     1            1


Super thanks in advance!
Posted
Updated 14-May-14 22:57pm
v4

Try:
SQL
SELECT LEFT(QueueNo,1) AS QueueInitial, COUNT (*) AS [Count]
FROM MyTable
GROUP BY LEFT(QueueNo,1)
 
Share this answer
 
Comments
charliedev 15-May-14 20:26pm    
Thanks, and can you help me with this question:

http://www.codeproject.com/Questions/773673/Count-items-in-column-by-date-SQL

i seem to be having some problems
Try like this :

SQL
select substring(QueueNo,0,2) as QueueInitial    ,
            count(QueueInitial) as Count
 from Your_Table_NAME
group by substring(QueueNo,0,2)


//You can create new table or temp table and insert the result in that
 
Share this answer
 
Try this.

select substring(QueueNo,1,1) as QueueInitial,count(QueueInitial) as Count

from Table_NAME

group by substring(QueueNo,1,1)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900