hrm

Well-known member
  • Mar 19, 2008
    5,977
    3,435
    113
    Battaramulla, Sri Lanka
    machan me approach try karapan.

    habay math meke output gaththe ne. gedara DB ekak install karala ne.
    result set eka adjust karaganin group by clause eka one widiyata change karagene.
    meka ubata nikam idea ekak ganna witharai danne mata test karaganne widiyak nethi nisa.
    Oracle wala nam sysntax tikak wenas wenewa. ehema unoth DECODE function eka use karapan.

    SELECT t1.id, t1.name, t1.team,
    t2.month, MAX(t2.year) ,
    'OT' = case t2.type
    when 'OT' then t2.type,
    'Incent'= case t2.type
    when 'Incent' then t2.type
    FROM Table1 t1 INNER JOIN Table2 t2
    ON t1.id = t2.id
    GROUP BY t1.id, t1.name,t1.team, t2.month
     

    twisted

    Well-known member
  • Feb 21, 2008
    34,398
    818
    113
    upon purple clouds
    try this, I've no sql at home, so couldn't check. and this doesn't check for max month.

    select table1.id,table1.name,table1.team,table2.month,table2.year,table3.amount as ot,
    table4.amount as incent
    from table1 join table2 on table1.id = table2.id join
    (select id,max(year),amount from table2 where type = 'ot' group by id,year,amount) table3
    on table2.id = table3.id
    and table2.year = table3.year join
    (select id,max(year),amount from table2 where type = 'incent' group by id,year,amount) table4
    on table2.id = table4.id and table2.year = table4.year
     

    twisted

    Well-known member
  • Feb 21, 2008
    34,398
    818
    113
    upon purple clouds
    DECODE(T2.TYPE,'ot',T2.AMOUNT) AS OT

    =

    ( CASE T2.TYPE WHEN 'ot' THEN T2.AMOUT )


    would work, give it a try

    machan me approach try karapan.

    habay math meke output gaththe ne. gedara DB ekak install karala ne.
    result set eka adjust karaganin group by clause eka one widiyata change karagene.
    meka ubata nikam idea ekak ganna witharai danne mata test karaganne widiyak nethi nisa.
    Oracle wala nam sysntax tikak wenas wenewa. ehema unoth DECODE function eka use karapan.

    SELECT t1.id, t1.name, t1.team,
    t2.month, MAX(t2.year) ,
    'OT' = case t2.type
    when 'OT' then t2.type,
    'Incent'= case t2.type
    when 'Incent' then t2.type
    FROM Table1 t1 INNER JOIN Table2 t2
    ON t1.id = t2.id
    GROUP BY t1.id, t1.name,t1.team, t2.month


    how is case expression going to solve this? afaik, it doesn't produce two separate columns :P and then may be it does, I'm such an IT noob
     

    ¤--bACarDi--¤

    Well-known member
  • Jan 9, 2009
    12,130
    288
    83
    124.43.xxx.xxx
    try this, I've no sql at home, so couldn't check. and this doesn't check for max month.

    select table1.id,table1.name,table1.team,table2.month,table2.year,table3.amount as ot,
    table4.amount as incent
    from table1 join table2 on table1.id = table2.id join
    (select id,max(year),amount from table2 where type = 'ot' group by id,year,amount) table3
    on table2.id = table3.id
    and table2.year = table3.year join
    (select id,max(year),amount from table2 where type = 'incent' group by id,year,amount) table4
    on table2.id = table4.id and table2.year = table4.year



    :shocked: :rofl:
     

    hrm

    Well-known member
  • Mar 19, 2008
    5,977
    3,435
    113
    Battaramulla, Sri Lanka
    how is case expression going to solve this? afaik, it doesn't produce two separate columns :P and then may be it does, I'm such an IT noob


    Just want to give some support for the guy who posted this thread. I might be wrong since I don't have any way to run and see the out put or an ERROR.
    I'm not a SQL guru.
     

    twisted

    Well-known member
  • Feb 21, 2008
    34,398
    818
    113
    upon purple clouds
    Just want to give some support for the guy who posted this thread. I might be wrong since I don't have any way to run and see the out put or an ERROR.
    I'm not a SQL guru.

    dude get over it, nobody's blaming you, your help is much appreciated, even I'm not sure If my koory was correct :P
     

    hrm

    Well-known member
  • Mar 19, 2008
    5,977
    3,435
    113
    Battaramulla, Sri Lanka
    mata me attach karala tiyena result eka ganna script ekak dennako
    :eek:

    Result eke enna ona max (year) eke max (month) eke figures
    :yes:

    machan you can also use T-SQL for this if a simple query doesn't serve the purpose.
    (group by clauses in simple queries tend to give some unexpected results when your data set becomes larger
    with possible duplications.)
     
    Last edited: