Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    GHawk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    13

    Query from 2 DB is retrieving bad information

    Hi I have this query request from 2 tables: Tbl1 and Tbl2.

    I'm trying to get total working hours per day from Tbl1 (SO) and used hours from Tbl2 (Hours) on the same day. And I want to display totals per day

    SELECT (Count(SO)*8.5) as Working, Round(Sum(Hours),2) as Used, INT(Datein) as Day FROM Tbl1,
    Tbl2 WHERE (INT(Datein) = Int(Fecha)) GROUP BY INT(Datein),INT(Fecha)

    I run this and is getting thru but results I'm getting are bad on both Working and Used.

    One more thing I'm not doing a join because I used to get the "Join Expression Not Supported" error.

    Thanks in advance for your support.
    Last edited by GHawk; 09-30-2014 at 02:39 PM. Reason: SOLVED

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    This is called mixing and matching, who knows what results will be displayed. Always fix problems, error messages are given to you for a reason.

    I would recommend doing this in multiple queries - one for each table and then one to JOIN the two resulting queries, with a join that works. That will simplify what your results are showing and will be much easier to troubleshoot.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    SELECT (Count(SO)*8.5) as Working, Round(Sum(Hours),2) as Used, INT(Datein) as Day 
    FROM Tbl1, Tbl2 
    WHERE (INT(Datein) = Int(Fecha)) GROUP BY INT(Datein),INT(Fecha)
    This results in a Cartesian product or Cartesian join.
    A Cartesian join is when you join every row of one table to every row of another table. Most of the time, not very useful.

    Can't you link the tables on "Fecha"?

  4. #4
    GHawk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    13
    Hi, thanks for the quick answer
    aytee111: I know errors are there to be cleaned but that particular error has no clear answer and I spent too much time on it. I'm not an expert so if you can explain in detail how the multiple queries would look like I will appreciate it.

    ssanfuL: I tried this:
    "SELECT (Count(SO)) as Working, Round(Sum(Hours),2) as Used, INT(Datein) as Day FROM Tbl1 LEFT JOIN Tbl2 ON (INT(Datein) = Int(Fecha)) GROUP BY INT(Datein),INT(Fecha)"

    But I got constantly a JOIN Expression Not Supported. I will appreciate if you can look there is something wrong here....

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What is Fecha field?

    Why are you using Int() function?

    If these tables do not have a relationship then can't join in query.

    aytee might be suggesting you do two aggregate queries, one for each table with grouping on the common linking field. Then build a query that joins the two aggregate queries on the common field. This will work only if both tables have the same values in the common field. Otherwise need a dataset of all possible common values and link the two aggregate queries to that dataset.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    <snip>..LEFT JOIN Tbl2 ON (INT(Datein) = Int(Fecha))..<snip>
    Why are you using the INT() function on what I am guessing are date fields?

  7. #7
    GHawk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    13
    June7 , Fecha is a Date/Time field. I'm using the INT to get the Date part only (like short date) that way I can join both tables. On regards of Aytee's suggestion I agree but I'm just not savvy enough to make that query, I appreciate any help I can get.

    ssanfu, as I just said I'm taking the Date part only of a long date field (Or at least that is what I'm trying to do) maybe that the key part of my problem... Is there any other way to get the Date part only?...

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I prefer to use the DateValue() function.
    If the date argument includes time information, DateValue doesn't return it. However, if date includes invalid time information (such as "89:98"), an error occurs

    The Int() function rounds numbers..... while Int() does remove the decimal part (the time), it does round.

    There are also the conversions functions like Clng(). It would look like:

    CDate(CDbl(CLng(CDbl([datetimefield]))))

    It takes the date/time field, converts it to a double, converts the result to a Long Int, converts that result back to a double, finally converts the result back to a date/time.

  9. #9
    GHawk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    13
    ssanfu I change that INT to DateValue but I'm still having the cartesian issue, I really need some help here to get only the information I need as I'm clueless on the suggestions about how to make the other query...

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What specifically do you not understand about suggestion described in post 5?

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    GHawk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    13
    June7, I mean I can make the individual query for each table grouped by the Date field. But I don't know how to create the extra query to join them. (I think I tried by using the Join query I put on post #4 which used to give me an error)

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    The query should be simple join on the common field the queries are grouped by. The query in post 4 is joining tables, not 2 aggregate queries.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    GHawk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    13
    June7, thanks for clarifying that (as I mentioned before I'm not very good at this) I change that but on the JOIN I'm still getting the original JOIN Expression Not Supported Error :-\

    Here is the query I came with:
    "SELECT * FROM (SELECT (Count(SO)) as Working, DateValue(DateIn) as Day FROM Checadas GROUP BY DateValue(DateIn)) LEFT JOIN
    (SELECT Round(Sum(Horas_Usadas),2) as Used, DateValue(Fecha) as Day2 FROM SeguimientoOrdenes GROUP BY DateValue(Fecha)) ON
    (Dia = Dia2)"

    As an extra information: here is where I'm getting the error:
    Dim DAx As New OleDb.OleDbDataAdapter(MySQLQuery, frm_kit.CNEff)
    Dim DSx As New DataSet
    ' DAx.Fill(DSx, MyTable)
    DAx.Fill(DSx) <---- Here is the error

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    There are no fields in the subqueries named Dia and Dia2. There are Day and Day2.

    The subqueries need alias names.

    Did you use query designer to build and save query objects? Use query designer to help get correct SQL syntax if you want to build an all-in-one statement. The final query should be like:

    SELECT Q1.*, Q2.* FROM (SELECT Count(SO) AS Working, DateValue(DateIn) AS Q1Day FROM Checadas GROUP BY DateValue(DateIn)) AS Q1 INNER JOIN
    (SELECT Round(Sum(Horas_Usadas),2) as Used, DateValue(Fecha) AS Q2Day FROM SeguimientoOrdenes GROUP BY DateValue(Fecha)) AS Q2 ON
    Q1.Q1Day=Q2.Q2Day;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    GHawk is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    13
    June7, about Day and dia it was a typo as I changed the names for the forum but forgot to change those ones. About the suggestion it was really helpful and I actually got the results that I need, thank you very much

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2014, 09:27 AM
  2. Replies: 7
    Last Post: 05-23-2012, 02:19 PM
  3. Retrieving PDF's Efficiently
    By tcheck in forum Access
    Replies: 3
    Last Post: 08-05-2011, 07:30 AM
  4. Replies: 2
    Last Post: 03-31-2009, 11:15 AM
  5. SQL- retrieving info
    By jmarti57 in forum Programming
    Replies: 0
    Last Post: 12-10-2008, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums