SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query for data count
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pankaj2910
Starting Member

India
30 Posts

Posted - 07/03/2014 :  05:53:07  Show Profile  Reply with Quote
Hi,

I have a table having code
code
12345678
21345678
45789612
12345678
21345678
21345678
12347586


I want result on the basis of first 2 digit the repeatation of code is count as 1.
code count
12 2
21 1
45 1


pankajrocks

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 07/03/2014 :  06:08:07  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT LEFT(Code, 2), COUNT(*) FROM dbo.Table1 GROUP BY LEFT(Code, 2);


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

mhorseman
Starting Member

United Kingdom
27 Posts

Posted - 07/03/2014 :  06:15:02  Show Profile  Reply with Quote
The way I read OP's question, perhaps what he wants is something like:

SELECT LEFT(Code, 2), COUNT(*) FROM (select distinct code from dbo.table1) x GROUP BY LEFT(Code, 2);


Mark
Go to Top of Page

pankaj2910
Starting Member

India
30 Posts

Posted - 09/24/2014 :  02:50:36  Show Profile  Reply with Quote
Hi,

I have a table having code
code
12345678
21345678
45789612
12345678
21345678
21345678
12347586


I want result on the basis of first 2 digit the repeatation of code is count as 1.
code1 code2 count
12 34 2
21 34 1
45 78 1

pankajrocks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 09/24/2014 :  14:59:19  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT SUBSTRING(Code, 1, 2) AS Code1, SUBSTRING(Code, 3, 2) AS Code1, COUNT(*) AS [Count] FROM dbo.Table1 GROUP BY SUBSTRING(Code, 1, 2), SUBSTRING(Code, 3, 2);


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000