?
Solved

Getting a wrong count in query

Posted on 2012-03-13
3
Medium Priority
?
330 Views
Last Modified: 2012-06-21
select  distinct il.Sale_No, customer_no,count(Il.Invoice_No) NoofInv from  h.dbo.inv_lots il
       INNER JOIN i.dbo.invoice i
         ON il.sale_no = i.sale_no
            AND i.invoice_no = il.invoice_no where Customer_No = 212736
             and il.sale_no IN( 121, 121208, 3516 )
             GROUP BY  il.Sale_No, customer_no

For the above query I get the following result:
Sale_No      customer_no      NoofInv
3516      212736                        358

But the NoofInv - 358 is a wronf answer, becuase when I try this query:
select  distinct il.Sale_No, customer_no,(Il.Invoice_No) NoofInv from  hnai.dbo.inv_lots il
       INNER JOIN hnai.dbo.invoice i
         ON il.sale_no = i.sale_no
            AND i.invoice_no = il.invoice_no where Customer_No = 212736
             and il.sale_no IN( 121, 121208, 3516 )
I get the following result, which tells me there are only 3 invoices, not sure where I am going wrong:
Sale_No      customer_no      NoofInv
3516      212736      10279910
3516      212736      10282521
3516      212736      10282876
0
Comment
Question by:sqlcurious
[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
3 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 37717550
why the distinct?
A group by is already the distinct. And in your other query it could be that the distinct hides rows.

Guess you want to count distinct  Il.Invoice_No's

select  il.Sale_No, customer_no, count(distinct  Il.Invoice_No) NoofInv
 from  h.dbo.inv_lots il 
       INNER JOIN i.dbo.invoice i 
         ON il.sale_no = i.sale_no 
            AND i.invoice_no = il.invoice_no where Customer_No = 212736
             and il.sale_no IN( 121, 121208, 3516 ) 
             GROUP BY  il.Sale_No, customer_no

Open in new window

0
 
LVL 9

Accepted Solution

by:
macarrillo1 earned 2000 total points
ID: 37724923
Your first query is using Il.Invoice_No in a calculation of count.  So distinct is not applied to this column, it is counting all Il.Invoice_No.

Your second query is applying distinct to the column Il.Invoice_No and so it returns fewer rows.

If you add this column (Il.Invoice_No) to the first query you should get the same result as the bottom query.
0
 

Author Closing Comment

by:sqlcurious
ID: 37778414
thanks
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
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.

770 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