Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query for data count

Author  Topic 

pankaj2910
Starting Member

31 Posts

Posted - 2014-07-03 : 05:53:07
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

30421 Posts

Posted - 2014-07-03 : 06:08:07
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

44 Posts

Posted - 2014-07-03 : 06:15:02
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

31 Posts

Posted - 2014-09-24 : 02:50:36
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

30421 Posts

Posted - 2014-09-24 : 14:59:19
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
   

- Advertisement -