Link to home
Start Free TrialLog in
Avatar of rhservan
rhservanFlag for United States of America

asked on

How do I sum a count on nvarchar.

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.
Avatar of dsacker
dsacker
Flag of United States of America image

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.
Avatar of rhservan

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of dsacker
dsacker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial