Results 1 to 3 of 3
  1. #1
    robsmith is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2020
    Posts
    35

    Combining Two SQL Queries

    Hi guys,



    I have two queries in a database. One was made using the Query Design function and the other using SQL. I'm an SQL novice so please go easy!

    I would like to combine the two queries so that all the fields in the first query are returned and the ranking and sorting in the second query are also returned (although I can live without the sorting if that complicates things).

    I've tried several times to do it but can't get it to work.

    Can anyone point me in the right direction?

    Thanks


    Code:
    SELECT BallByBall.matchId, BallByBall.inningsNo, BallByBall.ballNumber, BallByBall.bowler, BallByBall.batsman, BallByBall.runs, BallByBall.byes, BallByBall.legByes, BallByBall.wides, BallByBall.noBalls, BallByBall.wicket, Lineups.bowlingStyle, Matches.League, Matches.convDate, ScorecardBatting.howDismissedFROM ScorecardBatting RIGHT JOIN (Matches RIGHT JOIN (Lineups RIGHT JOIN BallByBall ON (Lineups.playerId = BallByBall.bowler) AND (Lineups.matchId = BallByBall.matchId)) ON Matches.matchId = BallByBall.matchId) ON (ScorecardBatting.batsmanId = BallByBall.batsman) AND (ScorecardBatting.matchId = BallByBall.matchId);
    Code:
    SELECT BallByBall.matchId, BallByBall.inningsNo, BallByBall.ballNumber, BallByBall.bowler, BallByBall.batsman, BallByBall.runs, BallByBall.byes, BallByBall.legbyes, BallByBall.wides, BallByBall.noBalls, BallByBall.wicket, (   SELECT  COUNT(T2.ballNumber) + 1 
                FROM    BallByBall T2
                WHERE   T2.matchId = BallByBall.matchId
                AND     T2.batsman = BallByBall.batsman
                AND     T2.ballNumber < BallByBall.ballNumber
            ) AS Rank
    FROM BallByBall
    ORDER BY BallByBall.matchId, BallByBall.batsman, BallByBall.ballNumber;

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    If you are a novice, don't use SQL, instead use queries.
    if both queries are the same, put them in a UNION query.

    Union Q3:

    select * from Q1
    Union
    select * from Q2

    then make Q4 to sort Q3.

  3. #3
    robsmith is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2020
    Posts
    35
    Hi ranman,

    Thanks. I had to use SQL for the second query as I understand it's the only way to rank in Access queries.

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

Similar Threads

  1. Combining queries
    By Sheba in forum Queries
    Replies: 8
    Last Post: 11-25-2014, 12:47 PM
  2. Combining queries
    By betmck in forum Queries
    Replies: 1
    Last Post: 09-22-2014, 12:26 PM
  3. Combining two queries.
    By Ray67 in forum Queries
    Replies: 1
    Last Post: 09-07-2012, 12:11 PM
  4. Combining two queries
    By Adele in forum Queries
    Replies: 1
    Last Post: 07-16-2011, 12:17 AM
  5. Combining Two Queries
    By csolomon in forum Queries
    Replies: 1
    Last Post: 09-03-2009, 01:33 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