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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:31:46+00:00 2026-05-15T17:31:46+00:00

Am interested to know how correct is my query the question is posted here

  • 0

Am interested to know how correct is my query the question is posted here., I doubt If I have written a perfect one, and I wish to ask you all where can I improve my code.

Note: I did not want to fill this place up with lot of code, was very brief. Thank you.

SELECT a.BookingId                         AS BookingId,
       CAST(b.TransactionDateTime AS DATE) AS TransactionDate
FROM   TC33_AuditTrial A
       JOIN TC33_AuditTrial b
       ON     a.AuditId                    = b.AuditId
WHERE  a.TransactionType                   = 'S'
AND    CAST(a.TransactionDateTime AS DATE) =
       (SELECT CAST(b.TransactionDateTime AS DATE)
       FROM    TC33_AuditTrial b
       WHERE   b.BookingId = a.BookingId
       AND     b.AuditId   =
               (SELECT MAX(b.AuditId)
               FROM    TC33_AuditTrial b
               WHERE   a.BookingId       = b.BookingId
               AND     b.TransactionType = 'R'
               )
       )
AND    a.TransactionValue =
       (SELECT SUM(b.TransactionValue)
       FROM    TC33_AuditTrial b
       WHERE   a.BookingId       = b.BookingId
       AND     b.TransactionType = 'R'
       )
  • 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-05-15T17:31:47+00:00Added an answer on May 15, 2026 at 5:31 pm

    Undeleted

    My original answer was

    SELECT BookingId, CONVERT(VARCHAR(8), TransactionDateTime, 112) AS TransactionDate
    FROM TC33_AuditTrial
    GROUP BY BookingId, CONVERT(VARCHAR(8), TransactionDateTime, 112)
    HAVING 
    SUM(CASE TransactionType WHEN 'S' THEN TransactionValue END) <= 
    SUM(CASE TransactionType WHEN 'R' THEN TransactionValue END)
    ORDER BY BookingID
    

    Which I was quite pleased with as it just took one pass through the data.

    Then I noticed the updated bit of the spec that it has to be restricted to the first date that contains an entry for that booking. The only way I could think to do this was to add a sub query that increased the cost of the query quite dramatically.

    The frustrating aspect though is that I want to restrict the results to the first day for each BookingId and I am already grouping by BookingId, Day

    It feels like there might be some clever way of getting this restriction without the sub query but if so it has eluded me so far!

    I just managed to get the ‘OVER’ working. I wouldn’t have posted this answer until after the competition was closed except it has exactly the same query plan as the one posted by 8kb

    WITH X AS
    (
    SELECT   BookingId,
             RANK() OVER(PARTITION BY BookingId ORDER BY CONVERT(VARCHAR(8), TransactionDateTime, 112)) AS Rnk,
             CONVERT(VARCHAR(8), TransactionDateTime, 112) AS TransactionDate,
             SUM(CASE TransactionType WHEN 'S' THEN TransactionValue END) AS S,
             SUM(CASE TransactionType WHEN 'R' THEN TransactionValue END) AS R
    FROM     TC33_AuditTrial t1
    GROUP BY BookingId,
             CONVERT(VARCHAR(8), TransactionDateTime, 112)
    )
    
    SELECT BookingId, TransactionDate
    FROM X
    WHERE Rnk = 1 AND S<=R
    ORDER BY BookingID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My last question was somewhat narrower than this one. I am interested to know
Question: I'm interested to know the best practice for killing a long standing operation
This is a two-part question: First, I am interested to know what the best
I am interested to know what you guys feel should be deemed correct behaviour
I have written a Sign function and am wondering whether it is correct or
I am interested to know the best way to develop en JS. If I
I am interested to know what a proper x (vector matrix or data frame)
I am interested to know is there a script or otherway available to collect
I am interested to know how can I do the same thing that the
I am interested to know how the DNS requests to political sites differ in

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.