Consulting

Results 1 to 8 of 8

Thread: TSQL Select Third Largest Value

  1. #1
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location

    TSQL Select Third Largest Value

    Hi Everyone,
    I didn't know we have SQL forum on VBAX so I just posted this thread in another forum too.
    I am totally a Newbie to T-SQL and need to pass a technical test for my interview. I have done the most of them but stuck with this one below:

    Question: Select account name, contact last name, case number, quote number, quote date and quote value for the third largest quote ever created for each of the accounts in the BR2 area.
    There are 4 tables: account, contact, case, quote
    the query I wrote:

    SELECT ac.AccountName, cn.LastName, cs.CaseNumber, q.QuoteNumber, q.QuoteDate, q.Value 
    FROM Tbl_Account ac LEFT JOIN Tbl_Contact cn
    ON ac.AccountID=cn.AccountID
    JOIN Tbl_Case cs
    ON cn.ContactID=cs.ContactID
    JOIN Tbl_Quote q
    ON cs.CaseID=q.CaseID
    WHERE ac.Postcode LIKE 'BR2%';
    I don't know how to select third largest quote ever created for each of the accounts in BR2 area. Can anyone help me with this please?
    Cheers
    B.
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I know this thread is a bit stale, and you have probably had the interview, but if it wants the top Nth entry on a table, both the correlated sub-query approach and the more obscure OFFSET FETCH approach are beyond SQL tyro in my view, so if they need that level of skill, as a T-SQL newbie you shouldn't be applying, you will struggle.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    Quote Originally Posted by xld View Post
    I know this thread is a bit stale, and you have probably had the interview, but if it wants the top Nth entry on a table, both the correlated sub-query approach and the more obscure OFFSET FETCH approach are beyond SQL tyro in my view, so if they need that level of skill, as a T-SQL newbie you shouldn't be applying, you will struggle.
    Hi xld,

    there was no time limit for the test and I was told they were testing the approach instead right exact answers as they have developers for that this is an analyst role. I just wanted to do my best even I struggled I learned a lot during this test. My final answer was like below. It was just before Christmas so I haven't had any feedback yet.

    SELECT a.AccountName, co.LastName, ca.CaseNumber, q.QuoteDate, q.Value
    ROW NUMBER() OVER(ORDER BY q.Value) RowID
    FROM Tbl_Account a
    INNER JOIN Tbl_Contact co ON a.AccountID=co.AccountID
    INNER JOIN Tbl_Case ca ON co.ContactID=ca.ContactID
    INNER JOIN Tbl_Quote q ON ca.CaseID=q.CaseID
    WHERE RowID=3
    GROUP BY a.Postcode LIKE ‘BR2%’;
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Beatrix View Post
    Hi xld,

    there was no time limit for the test and I was told they were testing the approach instead right exact answers as they have developers for that this is an analyst role. I just wanted to do my best even I struggled I learned a lot during this test. My final answer was like below. It was just before Christmas so I haven't had any feedback yet.

    SELECT a.AccountName, co.LastName, ca.CaseNumber, q.QuoteDate, q.Value
    ROW NUMBER() OVER(ORDER BY q.Value) RowID
    FROM Tbl_Account a
    INNER JOIN Tbl_Contact co ON a.AccountID=co.AccountID
    INNER JOIN Tbl_Case ca ON co.ContactID=ca.ContactID
    INNER JOIN Tbl_Quote q ON ca.CaseID=q.CaseID
    WHERE RowID=3
    GROUP BY a.Postcode LIKE ‘BR2%’;

    All I can say is well done, that looks pretty good to me.

    I did have to change it a bit to get it to work with my data, like so

    SELECT * FROM 
      (SELECT a.AccountName
             ,co.LastName
    		 ,ca.CaseNumber
    		 ,q.QuoteDate
    		 ,q.Value 
             ,ROW NUMBER() OVER(ORDER BY q.Value) RowID 
       FROM Tbl_Account a 
       INNER JOIN Tbl_Contact AS co ON a.AccountID=co.AccountID 
       INNER JOIN Tbl_Case AS ca ON co.ContactID=ca.ContactID 
       INNER JOIN Tbl_Quote AS q ON ca.CaseID=q.CaseID) AS ac
    WHERE RowID=3
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    xld you made my day! It's an honor being told well done by you.. I don't know I'll get this job or not but I am so happy to have this experience.

    thanks very much for sharing your knowledge.

    Cheers
    B.
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I'll keep my fingers crossed for you on the job Beatrix. Maybe I will see you one day at one of the SQL Saturdays, SQLBIts, or other community events like those in the UK (I am in the UK now, disregard the flag).
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    Many Thanks xld. It would be my pleasure to meet you.
    I'll have to work really hard If I get this job and I would definitely attend one of those events!
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •