Join sum sql statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seralasu
    New Member
    • Oct 2006
    • 34

    #1

    Join sum sql statement

    Hi,
    I have two tables.One of them is used only in run time( like temporary table). While I 'm passing from page to another, I'm using two table.
    Temporary table have fields:
    stok_no miktar dusulen_id
    Real Table have fields;
    id stok_no kalan

    we assume that user inserted some data
    Real table
    [code=text]
    id stok_no kalan
    393 1375 10
    394 1375 8
    [/code]

    Temporary table
    [code=text]
    stok_no miktar dusulen_id
    1375 3 393
    1375 4 393
    [/code]

    Finally I want to show this
    [code=text]
    id stok_no kalan miktar toplam fark
    393 1375 10 6 4
    394 1375 8 0 8
    [/code]

    I have sql statement but it doesn't work as I want

    [CODE=sql]SELECT (D.kalan-G.miktari) kalan
    FROM ayniyat_fis_det ay D,
    (SELECT dusulen_id, SUM(miktari) as miktari
    FROM ayniyat_fis_det ay_gecici
    GROUP BY dusulen_id) G
    where D.id= G.dusulen_id[/code]

    It works only id=393 because temporary table has it.
    Last edited by Atli; May 27 '07, 04:33 PM. Reason: Added code tags
  • pradeep kaltari
    Recognized Expert New Member
    • May 2007
    • 102

    #2
    Hi,
    I guess using LEFT OUTER JOIN will solve your problem.

    Regards,
    Pradeep.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Are you using the temporary table to store calculated values, or is your temporary table more like a 'view' of other existing tables?

      If it's the latter (and in a couple of cases, even if it's the former), you might want to consider using a view instead.

      Comment

      Working...