Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count in subquery #152

Open
GoogleCodeExporter opened this issue Jun 24, 2015 · 7 comments
Open

Count in subquery #152

GoogleCodeExporter opened this issue Jun 24, 2015 · 7 comments

Comments

@GoogleCodeExporter
Copy link

In Silverlight project query:

SELECT
    [n].[NewsId],
    (
        SELECT
            Count(*)
        FROM
            [NewsComment] [c]
        WHERE
            [n].[NewsId] = [c].[NewsFeedId]
    ) as [c1]
FROM
    [NewsFeed] [n]

return wrong count values on cloumn c1.

But Query

SELECT
    [n].[NewsId],
    (
        SELECT
            Sum([c].[NewsFeedId])
        FROM
            [NewsComment] [c]
        WHERE
            [n].[NewsId] = [c].[NewsFeedId]
    ) as [c2]
FROM
    [NewsFeed] [n]

return correct Sum.

Original issue reported on code.google.com by [email protected] on 13 Apr 2012 at 1:13

@GoogleCodeExporter
Copy link
Author

May be a SQLite issue, some of the aggregate functions were changed after 3.7

Can you supply your schema and some sample data please

Original comment by [email protected] on 29 Jun 2012 at 10:20

  • Changed state: Reviewed

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

DB without data:
http://sdrv.ms/N3O5qV

I don't have any tools to generate sample data.

Original comment by [email protected] on 2 Jul 2012 at 10:00

@GoogleCodeExporter
Copy link
Author

I am having the same issue. COUNT is not working properly if it is inside a Sub 
query and the sub query uses outer table column to filter data

select (select count(*) from Cars where Cars.TypeID = CarTypes.TypeID) AS 
CarTypeCount
from CarTypes


Original comment by [email protected] on 27 Nov 2014 at 11:00

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

I found a Solutions for this issue,

just use  SUM(1)  instead of  COUNT(*) in your query

Because sum(1) is exactly the same as count(*) 

to work this properly Use following complete query,

IFNULL(SUM(1),0) AS MY_COUNT


Original comment by [email protected] on 28 Nov 2014 at 12:34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant