Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:38:56+00:00 2026-05-27T06:38:56+00:00

Not sure why I am getting just one row with wrong count! Following is

  • 0

Not sure why I am getting just one row with wrong count!

Following is the working code:

SELECT project_name, sub_project_name
FROM projects, sub_projects
WHERE projects.project_id = sub_projects.projects_project_id

Result:

project_name         sub_project_name
Bakken               Ghost fracture Study
Bakken               Bakken Mylo QAQC
Bossier              Doyle Boles K No.1
Eagleford            Kennedy Unit#1H
Eagleford            Wehmeyer Unit #1
Niobrara             Crow Valley 7-62-32-1M
Poland               Poland
Woodford             Ridenour Phase 2
Woodford             Teague 1-14H

Each sub_project has multiple tables and I am basically trying to see if a column in one of the tables has any row containing non-Null values and then show that count beside the sub_project in another column for each sub_project.

Here is what I get when I query the following statement:

SELECT projects.project_name, sub_projects.sub_project_name, COUNT( bl.bl_por ) AS porosity
FROM projects, sub_projects
LEFT JOIN bl ON sub_projects.sub_project_id = bl.sub_project_id
WHERE projects.project_id = sub_projects.projects_project_id

Result:

project_name         sub_project_name          porosity
Bakken               Ghost fracture Study      99

All the rows from every sub_project in the first row and nothing else.

Whats wrong here?


Edit:

Erwin’s solution nailed it. But I do not have sub_project_id in my tables. I just added it in one table to make it simpler for testing.

So I used Erwin’s GROUP BY suggestion to edit my statement and I get the right matrix shape but not the right counts. Counts are way off.

SELECT p.project_name, sp.sub_project_name, COUNT( bl.bl_por ) AS porosity
FROM projects p, sub_projects sp, wells w, cores c, samples s, inputs i
LEFT JOIN bl ON i.inputs_id = bl.inputs_inputs_id
WHERE p.project_id = sp.projects_project_id
AND s.sample_id = i.samples_sample_id
AND c.core_id = s.cores_core_id
AND sp.sub_project_id = c.sub_projects_has_wells_sub_projects_sub_project_id
GROUP BY p.project_name, sp.sub_project_name

Result:

project_name    sub_project_name        porosity
Bakken          Bakken Mylo QAQC        147
Bakken          Ghost fracture Study    252
Bossier         Doyle Boles K No.1      189
Eagleford       Kennedy Unit#1H         294
Eagleford       Wehmeyer Unit #1        0
Niobrara        Crow Valley 7-62-32-1M  0
Poland          Poland                  714
Woodford        Ridenour Phase 2        483
Woodford        Teague 1-14H            0

The correct result should be:

Bakken          Bakken Mylo QAQC        7
Bakken          Ghost fracture Study    12
Bossier         Doyle Boles K No.1      9
Eagleford       Kennedy Unit#1H         14
Eagleford       Wehmeyer Unit #1        0
Niobrara        Crow Valley 7-62-32-1M  0
Poland          Poland                  34
Woodford        Ridenour Phase 2        23
Woodford        Teague 1-14H            0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:38:56+00:00Added an answer on May 27, 2026 at 6:38 am

    Try an explicit JOIN condition and explicit GROUP BY (although mysql allows to skip on the later).

    SELECT p.project_name, sp.sub_project_name, COUNT(bl.bl_por) AS porosity
    FROM   projects p
    JOIN   sub_projects sp ON p.project_id = sp.projects_project_id
    LEFT   JOIN bl ON sp.sub_project_id = bl.sub_project_id
    GROUP  BY p.project_name, sp.sub_project_name;
    

    Answer to additional question:

    Again, try proper SQL syntax whit explicit JOIN and join-conditions:

    SELECT p.project_name, sp.sub_project_name, COUNT(bl.bl_por) AS porosity
    FROM   projects p
    JOIN   sub_projects sp ON p.project_id = sp.projects_project_id
    JOIN   wells w -- no JOIN condition? Results in cross join.
    JOIN   cores c ON sp.sub_project_id = c.sub_projects_has_wells_sub_projects_sub_project_id
    JOIN   samples s ON c.core_id = s.cores_core_id
    JOIN   inputs i ON s.sample_id = i.samples_sample_id
    LEFT   JOIN bl ON i.inputs_id = bl.inputs_inputs_id
    GROUP  BY p.project_name, sp.sub_project_name
    

    This way you notice immediately that the table wells is joined in without unconditionally. This results in a cross join: every row of the left side is extended with every row on the right side, which produces a lot of rows. Probably the source of your excessive counts.

    Also be aware that count only counts non-null values. Any row where bl.bl_por IS NULL is not counted. If you actually want to count the number of rows, you can use count(bl.*).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure why I'm getting this error, but shouldn't this code compile, since
Ok I'm working on getting better with python, so I'm not sure this is
Just starting getting into Entity Framework and Linq for EF. I'm not sure which
We are getting a weird issue on which we are not sure what exactly
I'm not sure if I'm missing something really obvious, but I keep getting a
I am getting unexpected T_TRY, expecting T_FUNCTION error message and am not sure as
I'm reading about connection pooling in .net and i'm not sure if i'm getting
I'm having trouble getting a JTextArea to scroll. I'm not sure how you can
I am getting an error message, and I not quite sure where the issue
SELECT * from `employees` a LEFT JOIN (SELECT phone1 p1, count(*) c, FROM `employees`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.