Solved

How do I sum a count on nvarchar.

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

746 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

12 Experts available now in Live!

Get 1:1 Help Now