Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Posts
    1

    Question Unanswered: Joining two table help (it sums EVERYTHING)

    I am a noob at SQL and i'm slowly teaching my self how to get resuslts in my qureys, and I'm stuck. So i'm making a post on here in a cry for help. My problem is thisAs examples)

    I have a view table A with 4 columns dateseq,SalesAmount, customerseq, CostofGoods and customerdescription.
    Fact tabel B that has 4 columns dateseq,SalesAmount, Costofgoods, RebateAmount, and Customerseq.

    my code is like this

    select
    ta.customerdesctiption,
    sum(TA.salesamount) as salesamount,
    sum(TB.RebateAmount) as salesamount

    from
    TableA TA
    left join
    tableB TB
    on TA.dateseq = TB.dateseq

    where
    ta.dateseq like '%2013'

    I would like to get
    Customer A | 10,000
    Customer B | 3,000
    etc

    What I get is:
    Customer A | 20,000
    Customer B | 6,000
    etc

    help, and please for give any small formula typos' Just point me in the right directions and I'll work it out. I'm super stuck.

    Thanks

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Please run:
    Code:
    EXECUTE sp_help 'TableA'
    GO
    EXECUTE sp_help 'TableB'
    GO
    SELECT * FROM TableA
    GO
    SELECT * FROM TableB
    GO
    Post the results from these SQL statements, then we can help you more.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Dec 2013
    Posts
    43
    Provided Answers: 1
    Nemo,

    To get a valid sum, you have to group the data by customer/date.

    If you want just customer totals remove the DateSeq.
    If you want just date totals remove the CustomerSeq.

    More important is the fact that this is just a Select query.
    It won't change any data.

    Feel free to experiment with it.

    Code:
    Select TA.DateSeq, 
           TA.CustomerSeq, 
           Sum(TA.SalesAmount)  As TotalSales, 
           Sum(TB.RebateAmount) As TotalRebate
    From   TA Left Join TB On
             TA.DateSeq = TB.DateSeq And
             TA.CustomerSeq = TB.CustomerSeq
    Group By TA.DateSeq, TA.CustomerSeq
    hth,
    Wayne

Posting Permissions

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