Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble eliminating a SQL View
    primarykey
    data
    text
    <p>I have a query that I have been working on all week. I am still new at this, and I appreciate the help I have received thus far. </p> <p>This Query pulls data from 3 tables, dumps it into a view, then I have a second query to format the data the way the boss wants it. The trouble is, this has to run on SAP B1, which doesn't seem to support views. So I need to write this in a way that does it all in one query.</p> <p>Here's the first query:</p> <pre><code>SELECT t2.cardcode as 'BP_Code', --t0.Recontact as 'Date', t2.CardName as 'BP_Name', SubString(T3.Name,1,2) as 'Salesman', replace(T0.Street,',',' ') as 'Street_Address', T0.City, T0.State, SUM(CASE WHEN YEAR(t0.recontact) = 2011 THEN convert(decimal(10,2),t0.U_sold) ELSE 0 END) AS 'Year2011', SUM(CASE WHEN YEAR(t0.recontact) = 2012 THEN convert(decimal(10,2),t0.U_sold) ELSE 0 END) AS 'Year2012', SUM(CASE WHEN YEAR(t0.recontact) = 2013 THEN convert(decimal(10,2),t0.U_sold) ELSE 0 END) AS 'Year2013', convert(decimal(10,2),SUM(t0.U_sold)) as 'Total_Sold' From OCLG t0 -- OCLG is Activities inner join OCRD t2 -- OCRD is Customer Definitions on T0.cardcode like t2.cardcode inner join OCLS t3 -- OCLS is Activity Definitions on T0.CntctSbjct=T3.Code where t0.U_sold &gt; 0 and T0.CntctSbjct=T3.Code and T0.CardCode=T2.CardCode and T0.CntctType='3' and t2.CardCode = 'a239' --This was added to simplify output and T0.Recontact &gt;= Convert(date, '2011-01-01' ) and T0.Recontact &lt;= Convert(date, '2013-12-31' ) group by t2.cardcode, t0.city, t0.state, t0.street, t2.CardName, T3.Name, t0.Recontact order by t2.CardCode </code></pre> <p>Now, that spits out a table that looks like this:</p> <pre><code>BP_Code BP_Name Salesman Street_Address City State Year2011 Year2012 Year 2013 Total_Sold A239 Buddy's 01 123 WASHINGTON WASHINGTON MO 8993.84 0.00 0.00 8993.84 A239 Buddy's 01 123 WASHINGTON WASHINGTON MO 16474.54 0.00 0.00 16474.54 A239 Buddy's 01 123 WASHINGTON WASHINGTON MO 0.00 7170.79 0.00 7170.79 A239 Buddy's 01 123 WASHINGTON WASHINGTON MO 0.00 9207.73 0.00 9207.73 A239 Buddy's 01 123 WASHINGTON WASHINGTON MO 0.00 0.00 6960.20 6960.20 A239 Buddy's 01 123 WASHINGTON WASHINGTON MO 0.00 0.00 6787.73 6787.73 </code></pre> <p>Now I stick that output in a view called CallReport, and I run this Query on it:</p> <pre><code>;WITH x AS ( SELECT BP_Code, BP_Name, SalesMan, Street_Address, s = SUM(Total_Sold) OVER (PARTITION BY BP_Code), a = SUM(Year2011) OVER (PARTITION BY BP_Code), b = SUM(Year2012) OVER (PARTITION BY BP_Code), c = SUM(Year2013) OVER (PARTITION BY BP_Code), r = ROW_NUMBER() OVER (PARTITION BY BP_Code ORDER BY Date DESC) FROM CallReport ) SELECT BP_Code, BP_Name, Salesman, Street_Address, Year2011 = a, Year2012 = b, Year2013 = c, Total_Sold = s FROM x WHERE r = 1 and Salesman = 01; </code></pre> <p>Then I get my desired output:</p> <pre><code> BP_Code BP_Name Salesman Street_Address Year2011 Year2012 Year2013 Total_Sold A239 Buddy's 01 123 WASHINGTON 25468.38 16378.52 13747.93 106804.83 </code></pre> <p>See what the problem is? I have hundreds of BP_Codes, I am just narrowing A239 for this example. I need each BP_code to only be on one line, with sum sales data for each year, and grand total. I know there's a way to do this easier, but I am still new to the game. Any ideas? This is on MSSQL 2008 R2</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload