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 8579967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:43:32+00:00 2026-06-11T20:43:32+00:00

I have to generate a report using 4 tables, I tried a lot of

  • 0

I have to generate a report using 4 tables, I tried a lot of joins, but I get wrong values 🙁

Hope someone will help me.

StaffAssign Table:

----------------------------
| Tid | StaffId | Planned  |
----------------------------
| 123 |   2     | 10:00:00 |
| 123 |   4     | 05:00:00 |
| 124 |   2     | 09:00:00 |
----------------------------

ActualEffort Table:

--------------------------------------------
| Tid | StaffId | ActualEffort  | logdate  |
--------------------------------------------
| 123 |   2     | 05:00:00      |2012-09-01|
| 123 |   4     | 05:00:00      |2012-09-01|
| 123 |   2     | 06:00:00      |2012-09-03|
| 124 |   2     | 09:00:00      |2012-09-04|
--------------------------------------------

ProjectList Table:

-------------------
| ProjectId | Tid |
-------------------
|    1      | 123 |
|    2      | 124 |
-------------------

ProjectName Table:

-------------------
| Id  | name      |
-------------------
| 1   | project1  |
| 2   | project2  |
-------------------

Below is the Query I’m using:

SELECT P.id AS projectid,P.name,
SEC_TO_TIME(SUM(TIME_TO_SEC(A.planned)))planned,SEC_TO_TIME(SUM(TIME_TO_SEC(E.actualeffort)))actual FROM actualeffort E 
INNER JOIN staffassign A ON A.tid = E.tid
INNER JOIN projectlist L ON L.tid = A.tid
INNER JOIN projectname P ON P.id = L.projectid 
WHERE E.logdate BETWEEN FROM_UNIXTIME('1346475600') AND FROM_UNIXTIME('1348290000')
GROUP BY P.id

I’m getting this:

---------------------------------------------
| projectid | name     | planned | actual   |
---------------------------------------------
|     1     | project1 | 15:00:00| 12:00:00 |
|     2     | project2 | 09:00:00| 09:00:00 |
---------------------------------------------

But it should be like:

---------------------------------------------
| projectid | name     | planned | actual   |
---------------------------------------------
|     1     | project1 | 15:00:00| 16:00:00 |
|     2     | project2 | 09:00:00| 09:00:00 |
---------------------------------------------

I’m very confused that, I don’t know where I’m going wrong with the joins.

Someone please help me, I’m struck with this.

  • 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-06-11T20:43:33+00:00Added an answer on June 11, 2026 at 8:43 pm

    The WHERE filter may be wrong. Note that first datetime in the condition starts at ’08:00:00′ –

    SELECT FROM_UNIXTIME('1346475600') dt1, FROM_UNIXTIME('1348290000') dt2;
    +---------------------+---------------------+
    | dt1                 | dt2                 |
    +---------------------+---------------------+
    | 2012-09-01 08:00:00 | 2012-09-22 08:00:00 |
    +---------------------+---------------------+
    

    The type of ActualEffort.logdate field is DATE; the value ‘2012-09-01’ is less then ‘2012-09-01 08:00:00’. So change this condition or use DATE function –

    WHERE
      E.logdate BETWEEN
      DATE(FROM_UNIXTIME('1346475600')) AND DATE(FROM_UNIXTIME('1348290000'))
    

    (edited)

    For one thing, when you join StaffAssign and ActualEffort on the Tid column only, you get a mini-Cartesian product for Tid=123 in particular, because both tables contain more than one row with that Tid and there’s no other specific condition to establish a 1:1 or, at most, 1:N relationship between the rows.

    But in fact, 1:N wouldn’t do either: although it wouldn’t give you the Cartesian product effect, it would result in duplicated values on one side and, as a consequence, in a distorted SUM.

    Therefore, data in StaffAssign and those in ActualEffort must be aggregated separately, then joined, something like this:

    SELECT
      pl.ProjectId,
      pn.name,
      sa.Planned,
      ae.ActualEffort
    FROM ProjectList pl
      JOIN ProjectName pn
        ON pn.Id = pl.ProjectId
      LEFT JOIN (SELECT Tid, SEC_TO_TIME(SUM(TIME_TO_SEC(Planned))) Planned FROM StaffAssign GROUP BY Tid) sa
        ON sa.Tid = pl.Tid
      LEFT JOIN (SELECT Tid, SEC_TO_TIME(SUM(TIME_TO_SEC(ActualEffort))) ActualEffort FROM ActualEffort GROUP BY Tid) ae
        ON ae.Tid = pl.Tid
     GROUP BY ProjectId;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website where users generate an Excel report using a macro, when
I have to generate an html report regarding a few databases. I am using
I'm using nose and coverage to generate coverage reports. I only have one package
for the application I am working on I have to generate consolidated report where
I have been trying to generate report as per age differences of different months
I'm trying to generate a report of the activities that users have done within
I have a database of meetings. I can generate a report that sorts the
I have small web app that generate PDF files as a report. I'm trying
I have an application which needs to generate a report. However, I do not
I currently have the following code to generate a sales report over the last

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.