Results 1 to 11 of 11
  1. #1
    attman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Location
    Sweden
    Posts
    7

    Exclamation join query on same table

    how can i do the query like in the picture



  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    What is the logic? Why 30,00 with 1,50 and Feb in both months?

    What is your decision process when you analyze this data and derive this output?
    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.

  3. #3
    attman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Location
    Sweden
    Posts
    7
    I need to prepare data like this every month, not just once every 2 months.
    according to tax law

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    That doesn't answer question. I don't know tax law. You need to explain the criteria/logic/calculation.
    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.

  5. #5
    attman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Location
    Sweden
    Posts
    7
    I have to declare Simith's a tax this month and b tax next month. tax to be declared in this case: Smith A_pay 30; B_pay 1.5"
    In March, I have to pay and report the taxes in February belonging to Simith.

  6. #6
    attman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Location
    Sweden
    Posts
    7

    file

    database file
    Attached Files Attached Files

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Why do John records show same month in both fields of each record?

    I still don't understand why Feb shows in both output months.

    One way to pull data from another record involves nested query. Example:

    SELECT Tablo1.*, (SELECT TOP 1 B_pay FROM Tablo1 AS Dup WHERE Dup.EmpName=Tablo1.EmpName AND Dup.ID<Tablo1.ID ORDER BY ID DESC) AS BP
    FROM Tablo1 WHERE B_month = 3;

    Or is this what you want:

    SELECT Tablo1.EmpName, Tablo1.A_pay, Tablo1_1.B_pay, Tablo1.A_month, Tablo1_1.B_month
    FROM Tablo1 AS Tablo1_1 INNER JOIN Tablo1 ON (Tablo1_1.B_month = Tablo1.A_month) AND (Tablo1_1.EmpName = Tablo1.EmpName);
    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.

  8. #8
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Hi

    Your 1 table is not normalised

    1 Employee receives Many Payments.

    See the attached example
    Attached Files Attached Files
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  9. #9
    attman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Location
    Sweden
    Posts
    7

    Thanks

    Thanks for your help.
    I solved the problem in a good way.

    Code:
    Private Sub cmdGetTax_Click()
    DoCmd.SetWarnings False
    
    
    DoCmd.RunSQL "SELECT  query1.amonth AS Month,Table1.EmpName, CCur(0) AS A_pay, CCur(0) AS Bpay INTO table_Temp " & vbCrLf & _
    "FROM Table1 INNER JOIN query1 ON Table1.EmpName = query1.EmpName " & vbCrLf & _
    "GROUP BY Table1.EmpName, query1.amonth, 0, 0, 0, """" " & vbCrLf & _
    "ORDER BY Table1.EmpName;"
    
    
    DoCmd.RunSQL "UPDATE table_Temp INNER JOIN Table1 ON (Table1.EmpName = table_Temp.EmpName)" & vbCrLf & _
    "AND (table_Temp.Month = Table1.amonth) SET table_Temp.A_pay = [Table1].[A_pay];"
    
    
    DoCmd.RunSQL "UPDATE Table1 INNER JOIN table_Temp ON (table_Temp.Month = Table1.bMonth)" & vbCrLf & _
    "AND (Table1.EmpName = table_Temp.EmpName) SET table_Temp.Bpay = [Table1].[Bpay];"
    DoCmd.SetWarnings True
    DoCmd.OpenTable "table_Temp"
    End Sub
    and query1

    Code:
    SELECT Table1.EmpName, Table1.amonth FROM Table1 GROUP BY Table1.EmpName, Table1.amonth UNION ALL SELECT Table1.EmpName, Table1.bmonth FROM Table1 GROUP BY Table1.EmpName, Table1.bmonth;

  10. #10
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Hi
    I would recommend NOT doing what you suggest and actually normalise the tables.

    Otherwise, luck with the project.
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  11. #11
    attman is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Location
    Sweden
    Posts
    7

    Thanks

    Quote Originally Posted by mike60smart View Post
    Hi
    I would recommend NOT doing what you suggest and actually normalise the tables.

    Otherwise, luck with the project.
    payment amounts and tables are completely fake. I made a simplification to understand the problem.

    the way the government agency requests the data as in the picture
    There is no problem if there is only one employee group in the enterprise. But when there are employees belonging to 2 payment types, such a query is needed.
    The requested data will not be used elsewhere. xml will be exported and sent once a month.
    will not affect and use another file in the database
    Attached Thumbnails Attached Thumbnails table.png  

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query - Table Join Problem
    By jgs in forum Access
    Replies: 4
    Last Post: 01-26-2021, 05:06 PM
  2. SQL Join and Union query on the same table
    By waterpower in forum Queries
    Replies: 3
    Last Post: 01-23-2021, 11:28 AM
  3. Replies: 8
    Last Post: 10-10-2019, 08:06 AM
  4. Replies: 3
    Last Post: 09-24-2019, 12:22 AM
  5. Replies: 3
    Last Post: 08-27-2016, 10:24 AM

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