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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:50:31+00:00 2026-06-08T01:50:31+00:00

Alright, so I know this is a large Procedure, but my question is very

  • 0

Alright, so I know this is a large Procedure, but my question is very simple. I am trying to get a single instance of a Bond from this, but it is giving me each instance times the number of users in the system related to the agency since I am an administrator. This happens at the bottom where the OR EXISTS statement is. I just need help figuring out how to limit the results down to 1 of each bond while still getting the correct results for the amounts needed, which is why the individual join statements are necessary.

ALTER PROCEDURE dbo.GetBondAmounts
    (
    @Username varchar(20)
    )
AS
    SELECT Bond.ID BondID, (ISNULL(Powers.Amount,0) + ISNULL(Charges.Amount,0)) BondAmount,
    (ISNULL(BondFee.Amount,0) + ISNULL(Powers.Premium,0) + ISNULL(Charges.Premium,0)
    + ISNULL(Forfeiture.CostOfApprehension,0) + ISNULL(Forfeiture.AmountPaid,0) + Bond.StateTax) BondTotal,
    (ISNULL(BondFee.Amount,0) + ISNULL(Powers.Premium,0) + ISNULL(Charges.Premium,0)
    + ISNULL(Forfeiture.CostOfApprehension,0) + ISNULL(Forfeiture.AmountPaid,0) + Bond.StateTax
    - ISNULL(BalanceForgiveness.Amount,0) - ISNULL(Payment.Amount,0)) BondBalance
    FROM Bond
    LEFT OUTER JOIN UserAgency ON Bond.Agency = UserAgency.Agency
    LEFT OUTER JOIN
    (
        SELECT BondID, SUM(AmountForgiven) Amount
        FROM BalanceForgiveness
        GROUP BY BondID
    ) AS BalanceForgiveness ON Bond.ID = BalanceForgiveness.BondID
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(Amount) Amount
        FROM BondFee
        GROUP BY Bond
    ) AS BondFee ON Bond.ID = BondFee.Bond
    LEFT OUTER JOIN
    (
        SELECT Powers.Bond, SUM(Charge.BondAmount) Amount,
        ISNULL(SUM(Charge.BondPremium), 0) Premium
        FROM Powers INNER JOIN Charge ON Powers.Surety = Charge.PowerSurety
        AND Powers.PowerPrefix = Charge.PowerPrefix AND Powers.PowerNumber = Charge.PowerNumber
        GROUP BY Bond
    ) AS Powers ON Bond.ID = Powers.Bond
    LEFT OUTER JOIN
    (
        SELECT BondID, SUM(BondAmount) Amount, SUM(BondPremium) Premium
        FROM ChargeWithoutPower
        GROUP BY BondID
    ) AS Charges ON Bond.ID = Charges.BondID
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(CostOfApprehension) CostOfApprehension, SUM(AmountPaid) AmountPaid
        FROM Forfeiture
        GROUP BY Bond
    ) AS Forfeiture ON Bond.ID = Forfeiture.Bond
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(Amount) Amount
        FROM Payment
        GROUP BY Bond
    ) AS Payment ON Bond.ID = Payment.Bond
    WHERE UserAgency.Username = @Username
    OR EXISTS (SELECT * FROM Users WHERE Username = @Username AND Admin = 1)
  • 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-06-08T01:50:33+00:00Added an answer on June 8, 2026 at 1:50 am

    Alright, so I found my own answer to my question. I just had to make the UserAgency the same as the other joins. Here is how it looks:

    LEFT OUTER JOIN
    (
        SELECT Agency, Username FROM UserAgency
        WHERE Username = @Username
    ) AS UserAgency ON Bond.Agency = UserAgency.Agency
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I know this is a simple question, but I can't seem to get
Alright I know this is more than likely a amateur question but I have
Alright, I know this question has been asked before, but mine has a different
Alright, so I know about the Settings.Properties.Default.* Stuff, but thats not what im trying
Alright, I know questions like this have probably been asked dozens of times, but
Alright, nub question. I know. Basic response might be Convert.ToInt32(string); But naturally, C# does
I know this question might sound quite stupid, but I've tried my best and
Alright, I know I've asked similar questions, but I feel this is hopefully a
Alright, I've gone nuts looking for an answer. This is simple, I know it.
Alright I know there is a lot on here about this, but I haven't

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.