Solved

How do I sum a count on nvarchar.

Posted on 2013-06-03
3
399 Views
Last Modified: 2013-06-03
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
Comment
Question by:rhservan
  • 2
3 Comments
 
LVL 20

Expert Comment

by:dsacker
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

Open in new window

I added the "AS" simply for readability.
0
 

Author Comment

by:rhservan
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

by:
dsacker earned 500 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

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question