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

  • Home
  • SEARCH
  • 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 929855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:14:19+00:00 2026-05-15T20:14:19+00:00

The question is: Two tables ( t1 , t2 ) Table t1 : SELLER

  • 0

The question is:

Two tables (t1, t2)

Table t1:

SELLER | NON_SELLER
    A     B
    A     C
    A     D
    B     A
    B     C
    B     D
    C     A
    C     B
    C     D
    D     A
    D     B
    D     C

Table t2:

SELLER | COUPON | BAL
A        9        100
B        9        200
C        9        300
D        9        400
A        9.5      100
B        9.5       20
A       10         80

Using SELECT Statement to get this result:

SELLER| COUPON | SUM(BAL)
A       9        900
B       9        800
C       9        700
D       9        600
A       9.5       20
B       9.5      100
C       9.5      120
D       9.5      120
A      10          0  # !!!
B      10         80
C      10         80
D      10         80

For seller A SUM(BAL) means sum( B.BAL,C.BAL,D.BAL), for B, SUM(BAL)=SUM(A.BAL,C.BAL,D.BAL)…

Please find a way with good performance and don’t use temporary table.

My solution:
Running this query will get the result but without the row “A 10 0”:

  select t1.seller, t2.coupon, sum(bal)
  from t1, t2
  where t1.non_seller = t2.seller
  group by t1.seller, t2.coupon
  order by t2.coupon

Please help ~~~~~~

  • 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-15T20:14:19+00:00Added an answer on May 15, 2026 at 8:14 pm

    If I understand you correctly, you’re looking for data on all sellers and all coupons. So let’s start with a cross join that generates a list of coupons and sellers:

    select  sellers.seller
    ,       coupons.coupon
    from    (
            select  distinct seller
            from    Table2
            ) as sellers
    cross join
            (
            select  distinct coupon
            from    Table2
            ) as coupons
    

    For each seller-coupon combination, you’re looking for the sum they can buy from other sellers. This can be accomplished by a left join:

    select  sellers.seller
    ,       coupons.coupon
    ,       case when sum(t2.bal) is null then 0 else sum(t2.bal) end
    from    (
            select  distinct seller
            from    Table2
            ) as sellers
    cross join
            (
            select  distinct coupon
            from    Table2
            ) as coupons
    left join
            Table2 t2
    on      t2.seller <> sellers.seller
            and t2.coupon = coupons.coupon
    group by
            sellers.seller
    ,       coupons.coupon
    

    The only function of the case statement is to replace a null sum with a 0.

    The output matches the one in your answer. Note that this solution doesn’t use Table1: the list of other sellers is produced by the t2.seller <> sellers.seller condition in the left join.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables (well, two relevant for this question) : Bets (holds the
i have two tables: CREATE TABLE public.auctions ( id VARCHAR(255) NOT NULL, auction_value_key VARCHAR(255)
I have two tables Orders table and customers table, both have customerid as common
I have two tables: a schedule table that contains information about how an employee
I have two tables that are related, which, for the purpose of this question,
ok here is it. i have two tables: products and warehouse product table consist
I am new to sql. i have a small question. i have two tables
I'm using Lua inside a C application, and I have two tables. I want
Background This question is in two parts. I have a one-way WCF operation hosted
This is actually a two part question. First,does the HttpContext.Current correspond to the current

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.