1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Left Join problem

Discussion in 'MySQL' started by Jamie T, Jan 21, 2016.

  1. #1
    Hello.
    I have two counters, one for units, one for waste.
    I need to pull the sum of the timers and separate by Year.
    The Left Join kinda works to get the combined, but as soon as I put a where condition to separate the timers, the think falls apart.
    Could someone please check this out and give point me in the right direction?
    Thanks.
    Jamie.

    SELECT year(counts.ts) as SalesYear, Q1.Res1 AS units, Q2.Res2 as rate
    FROM
    (counts
    LEFT JOIN (SELECT (counts.ts), SUM(counts.count) AS Res1
    FROM counts where counts.`name` = 104 and counts.count > 0 ) AS Q1 ON (counts.ts) = Q1.ts)

    LEFT JOIN (SELECT (counts.ts), Sum(counts.count) AS Res2
    FROM counts where counts.`name` = 107 and counts.count > 0) AS Q2 ON (counts.ts) = Q2.ts

    GROUP BY year(counts.ts)
    order by year(counts.ts)
     
    Jamie T, Jan 21, 2016 IP