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?
 
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
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
 
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 provided the SQL statement required as the complete solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.