Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# How do I sum a count on nvarchar.

Posted on 2013-06-03
Medium Priority
423 Views
Current:
Select  Company_Id ci, count (distinct Order_Number) cnt
From TableA
Having  Count(Order_Number) >1

Order Number is a alpha numeric, example PQR0725102115, and is nvarchar.

Yields:

Ci            cnt
1               10
2                 2
3                 6
4               15

Desired result:

I need the sum of the cnt column:

cnt
33

The problem is in the nvarchar.
0
Question by:rhservan
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 20

Expert Comment

ID: 39217199
Don't think the problem really is the nvarchar. You're definitely on the right track, but simply may need to sum what you just queried.

Try this:

``````Select  ci,
SUM(cnt) AS cntSum
From  ( Select  Company_Id AS ci,
count (distinct Order_Number) AS cnt
From    TableA
Having  Count(Order_Number) >1 ) t1
Group By ci
``````
0

Author Comment

ID: 39217272
Hey dsacker thanks for the help.

After implementing your solution I still get

Ci            cnt
1               10
2                 2
3                 6
4               15

I need just one row sum result on cnt:

cnt
33
0

LVL 20

Accepted Solution

dsacker earned 2000 total points
ID: 39217322
This should work. :)

``````Select  SUM(cnt) AS cntSum
From  ( Select  Company_Id AS ci,
count (distinct Order_Number) AS cnt
From    TableA
Having  Count(Order_Number) >1 ) t1
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that witâ€¦
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
###### Suggested Courses
Course of the Month8 days, 9 hours left to enroll