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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query Help Count
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ann
Posting Yak Master

220 Posts

Posted - 08/08/2014 :  14:22:43  Show Profile  Reply with Quote
CREATE TABLE [dbo].[Codes](
[CodeID] [int] IDENTITY(1,1) NOT NULL,
[Code1] [int] NULL,
[Code2] [varchar](10) NULL,
[Code3] [varchar](10) NULL,
CONSTRAINT [PK_Codes] PRIMARY KEY CLUSTERED
(
[CodeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
insert into Codes values(1,null,null)
insert into Codes values(1,null,null)
insert into Codes values(2,null,null)
insert into Codes values(3,'a','i')
insert into Codes values(3,'a','i')
insert into Codes values(3,'a','ii')
insert into Codes values(3,'b','i')
insert into Codes values(3,'b','ii')
insert into Codes values(3,'b','ii')
insert into Codes values(3,'c','')

Expected Results:
Code codeCount
1 2
2 1
3ai 2
3aii 1
3bi 1
3bii 2
3c 1

I need help figuring this out. I can do a count if there's only a code1, but I can't figure out how to combine when there are code2 & code3.

Can anyone help me figure this out?

Thanks

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1754 Posts

Posted - 08/08/2014 :  18:13:46  Show Profile  Reply with Quote
select cast(Code1 as varchar(10)) + coalesce(Code2, '') + coalesce(Code3, '') as Code, count(*) as CodeCount
from dbo.Codes 
group by Code1, Code2, Code3




Too often we enjoy the comfort of opinion without the discomfort of thought. - John F. Kennedy

Edited by - Bustaz Kool on 08/08/2014 18:14:35
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 08/08/2014 :  20:19:24  Show Profile  Reply with Quote
wow - you made that look so easy.

Thanks - worked great
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.06 seconds. Powered By: Snitz Forums 2000