Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
COUNT is different that when selecting fields
Message
From
15/02/2014 11:23:55
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
COUNT is different that when selecting fields
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01594404
Message ID:
01594404
Views:
50
I have a select like this:
SELECT COUNT(*)
 FROM
 (SELECT MAX(InvoiceDetail.Numero) AS InvoiceDetailNumero
 FROM Invoice
 INNER JOIN InvoiceDetail ON Invoice.Numero=InvoiceDetail.NoInvoice
 WHERE Invoice.NoCompany=32
 GROUP BY InvoiceDetail.NoInvoice) Invoice2
 INNER JOIN InvoiceDetail ON Invoice2.InvoiceDetailNumero=InvoiceDetail.Numero
 WHERE InvoiceDetail.NoStatus=7 AND InvoiceDetail.AddUser=1
This gives 115063 records.

Now, I would like to collect some field from InvoiceDetail. So, I do something like this:
SELECT InvoiceDetail.NoInvoice
 FROM
 (SELECT MAX(InvoiceDetail.Numero) AS InvoiceDetailNumero
 FROM Invoice
 INNER JOIN InvoiceDetail ON Invoice.Numero=InvoiceDetail.NoInvoice
 WHERE Invoice.NoCompany=32
 GROUP BY InvoiceDetail.NoInvoice) Invoice2
 INNER JOIN InvoiceDetail ON Invoice2.InvoiceDetailNumero=InvoiceDetail.Numero
 WHERE InvoiceDetail.NoStatus=7 AND InvoiceDetail.AddUser=1
This gives 118419.

Basically, if I change the field to something else, it always gives a different number of records.

Obviously, something is missing here. Anyone would know how I can extract some fields for the records related to the COUNT(*)?
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform