Solved

How do I sum a count on nvarchar.

Posted on 2013-06-03
3
409 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
[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
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 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