sql help to group the result set and add a new label to the columns

select [CaseOriginCode], COUNT (*) CaseOriginCode
 from IncidentBase
 where CustomerIdName = '[Unknown Account]' and StateCode=0
 group by CaseOriginCode

The above SQL statement returns
 
<table>
<tr><td>CaseOriginCode</td><td>CaseOriginCode</td></tr>
<tr><td>1</td><td>12</td></tr>
<tr><td>27732000</td><td>1</td></tr>
<tr><td>2</td><td>3</td></tr>
<tr><td>277320001</td><td>269</td></tr>
<tr><td>3986</td><td>50</td></tr>
<tr><td>3</td><td>24</td></tr>
<tr><td>2483</td><td>500</td></tr>
</table>

Open in new window



I would like something like this....
<table>
<tr><td>Origin</td><td>Total</td></tr>
<tr><td>Phone</td><td>12</td></tr>
<tr><td>Mail</td><td>1</td></tr>
<tr><td>Email</td><td>3</td></tr>
<tr><td>277320001</td><td>269</td></tr>
<tr><td>Twitter</td><td>50</td></tr>
<tr><td>Web</td><td>24</td></tr>
<tr><td>Facebook</td><td>500</td></tr>
</table>

Open in new window

LVL 8
a0k0a7Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Then just group by the Origin instead of the OriginCode and select that value.  Am i missing something?
0
a0k0a7Author Commented:
my bad teh HTML formatting display not good.
0
a0k0a7Author Commented:
@Brian Crowe, yes, you're missing the sql statement.  :)

How I can do the Group By and set custom labels for the columns on the caseorigincode
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>select [CaseOriginCode], COUNT (*) CaseOriginCode
Unless I'm mistaken, this is two columns with the same exact name, which is usually not allowed.
Either that or the first one is a typo, and should be a name column like CaseOriginName.
0
Brian CroweDatabase AdministratorCommented:
You didn't provide schema information so I'm assuming there is an "Origin" column that contains the label.

SELECT Origin, COUNT(Origin) AS [Total]
from IncidentBase
 where CustomerIdName = '[Unknown Account]' and StateCode=0
 group by Origin
0
a0k0a7Author Commented:
select [CaseOriginCode] as Origin <- this ok

how I can do
 COUNT (*) CaseOriginCode
 as Total

and set the values i.e. 1 to Phone,  277320000 to Web
when the results I receive look like Capture.JPG
0
a0k0a7Author Commented:
Thanks @BrianCrowe, all I need now is to map the origin numeric value  to a label
0
Brian CroweDatabase AdministratorCommented:
You can see an example of that in my earlier post.  FYI it is called an alias.  It can be encased in single quotes or square brackets if it is a keyword or if it contains spaces but otherwise they are not needed.  The COUNT function counts NON-NULL values of the parameter column so you can use just about any column as the parameter or even just the number one if you want in this case.

SELECT CaseOriginCode AS Origin, COUNT(CaseOriginCode) AS [Total]
from IncidentBase
 where CustomerIdName = '[Unknown Account]' and StateCode=0
 group by CaseOriginCode
0
a0k0a7Author Commented:
I am novice at SQL,  I'm thinking may be a left inner join or something may help me resolve the mapping

I can get the data from the following SQL

SELECT AttributeValue, Value
  FROM StringMapBase
  where ObjectTypeCode=112 and AttributeName='caseorigincode'
0
a0k0a7Author Commented:
the SQL returns the following Capture.JPG
0
a0k0a7Author Commented:
I did it...

select StringMapBase.Value, COUNT (CaseOriginCode) AS Total
 from IncidentBase
  left join StringMapBase
    ON StringMapBase.AttributeValue=IncidentBase.CaseOriginCode
 where CustomerIdName = '[Unknown Account]'
        and StateCode=0
        and.StringMapBase.AttributeName='caseorigincode'
 group by Value
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
a0k0a7Author Commented:
I provided the SQL statement required as the complete solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SSRS

From novice to tech pro — start learning today.