Solved

How do I sum a count on nvarchar.

Posted on 2013-06-03
3
397 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now