?
Solved

Problem with dublicates and SUM Sql Query

Posted on 2011-09-06
9
Medium Priority
?
212 Views
Last Modified: 2012-05-12
I have 2 tables:

Customer table:
customer(nvarchar(255))

Certificates
customer(nvarchar(255))
cert(int)


Customer

570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266
570715000000038266

Certificate
    Customer                     cert
570715000000038266      1383
570715000000038266      1656
570715000000038266      1944
570715000000038266      -1383
570715000000038266      -1350
570715000000038266      1350
570715000000038266      -1944
570715000000038266      850
570715000000038266      -1532
570715000000038266      1532
570715000000038266      774
570715000000038266      -1656
570715000000038266      -774


When I execute this query:

SELECT     [Customer].[Customer], SUM(Certificate.Cert)
FROM         [Customer]
inner JOIN Certificate ON [Customer].[Customer] = Certificate.[Customer]
WHERE [Customer].[Customer] = '570715000000038266'
GROUP BY [Customer].[Customer]

I get this result:
 Customer                           Cert
570715000000038266    12750

It multiplies the customer records with all the certificates lines.
15 customers records X all the matching cert records = 12750

The result i am looking for is:

 Customer                           Cert
570715000000038266    850    (12750/15=850)

There are more than one customer in the customer table with multiple lines…


Hope you can help!?
0
Comment
Question by:Madsing
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 36488101
Hi,

Try


SELECT     [Customer].[Customer], SUM(Certificate.Cert), AVG(Certificate.Cert)
FROM         [Customer]
inner JOIN Certificate ON [Customer].[Customer] = Certificate.[Customer]
WHERE [Customer].[Customer] = '570715000000038266'
GROUP BY [Customer].[Customer]

/peter
0
 

Author Comment

by:Madsing
ID: 36488121
Then I get:

Customer                          (Cert)             (AVG cert)
570715000000038266      12750      65,3846153846154

So that is no good.
0
 
LVL 5

Expert Comment

by:DavidMorrison
ID: 36488206
I think what you need is:

SELECT     [Customer].[Customer],
sum(Certificate.Cert) over (partition by [Customer].[Customer]) /
count(*) over (partition by [Customer].[Customer]) as Cert
FROM         [Customer]
inner JOIN Certificate ON [Customer].[Customer] = Certificate.[Customer]
WHERE [Customer].[Customer] = '570715000000038266'
GROUP BY [Customer].[Customer]
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 22

Expert Comment

by:pivar
ID: 36488207
How about


SELECT     [Customer].[Customer], SUM(Certificates.Cert),
SUM(Certificates.Cert)/(SELECT COUNT(Customer) FROM Customer x WHERE x.Customer  = [Customer].[Customer])
FROM         [Customer]
inner JOIN Certificates ON [Customer].[Customer] = Certificates.[Customer]
WHERE [Customer].[Customer] = '570715000000038266'
GROUP BY [Customer].[Customer]
0
 
LVL 22

Expert Comment

by:pivar
ID: 36488213
Or why not


SELECT     [Customer], SUM(Certificates.Cert)
FROM Certificates
WHERE [Customer] = '570715000000038266'
GROUP BY [Customer]
0
 
LVL 5

Expert Comment

by:DavidMorrison
ID: 36488221
Pivar, I hate to be critical of another professional but are you even reading what OP has asked for?


0
 
LVL 22

Expert Comment

by:pivar
ID: 36488259
Please be critical, but please also be clear of what you referring to. I may have misunderstood the question. But right now I don't understand what you getting at.  
0
 
LVL 10

Accepted Solution

by:
dwe761 earned 2000 total points
ID: 36488495
Madsing,
It might help us to know what question you are actually trying to answer with your query?

I'm not sure where you got 12750 unless it is the absolute value of all of the certs.  From the data you've shown us, the 850 you want is actually the only cert that does not have a matching negative cert.

So if you just want to find the sum of certs for a single Customer, it seems you'd want something like this:


SELECT     c.[Customer], SUM(Certificate.Cert)
FROM      ( SELECT DISTINCT Customer FROM [Customer]) c
inner JOIN Certificate ON c.[Customer] = Certificate.[Customer]
WHERE c.[Customer] = '570715000000038266'
GROUP BY c.[Customer]

Open in new window

0
 
LVL 10

Expert Comment

by:dwe761
ID: 36488529
Oh, I see where you got 12750.  Since you have 15 rows in your Customer table and only one row in your Certs table that does not have a matching negative cert, your query found those 15 matches on 850 and you asked it to sum them which it gave you 12750.  All other rows cancelled themselves out because they all had matching positive and negative numbers on the same cert.

So even though my query will give you what you want for a single customer, I'm still not sure that's what your original intent was.  If you remove the WHERE clause on my query, you'll just get the sum of all positive certs without matching negative certs which may be kind of meaningless.  You'll have to answer that.  Are you looking for all customer's certs that do not have a matching negative cert?

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

862 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