Note that there are some explanatory texts on larger screens.

plurals
  1. POData trending across table
    primarykey
    data
    text
    <p>I'm trying to work out a trend over a sql table which contains entries for files entering filesystem folders over a set time period. I have a table that contains the date periods and another containing all of the folders that I am interested in. When I join (right outer) the two I get a table like below:</p> <pre><code>Jan 2012, folder A 2 Jan 2012, folder B 4 Feb 2012, folder A 2 Feb 2012, folder B 2 </code></pre> <p>However if no information has been entered for a date interval I just get null values so March 2012, null, null</p> <p>Is there any way that I can add a zero into this table for each folder? This would give me:</p> <pre><code>Jan 2012, folder A 2 Jan 2012, folder B 4 Feb 2012, folder A 2 Feb 2012, folder B 2 Mar 2012, folder A 0 Mar 2012, folder B 0 </code></pre> <p>I then need to work out a trend over the months for each folder so that the difference between Jan and Feb for folder A is 0 and -200% between Feb and Mar. This would give me a final table of:</p> <pre><code>Jan 2012, folder A 2 0 (first month no data) Jan 2012, folder B 4 0 (first month no data) Feb 2012, folder A 2 200% (increase on Jan) Feb 2012, folder B 2 -50% (decrease on Jan) Mar 2012, folder A 0 -200% (decrease on Feb) Mar 2012, folder B 0 -200% (decrease on Feb) </code></pre> <p>This will eventually end up in an ssrs report so it doesn't matter if the trending happens in the query or through the report builder.</p> <p>Any help with this would be greatly appreciated.</p> <p>My table schemas look like:</p> <p>Folder Table:</p> <pre><code>DECLARE @folders TABLE ( folderId int, folderPath varchar(500)) </code></pre> <p>Files Table: </p> <pre><code>DECLARE @filessAdded TABLE ( fileId int, parentId int, folderPath varchar(500), eventDate DATETIME, folderId int ); </code></pre> <p>Date Table - populated dynamically based on date intervals in given period eg months:</p> <pre><code>DECLARE @dates TABLE ( DateOf VARCHAR(500) ); </code></pre> <p>My final query looks like the following:</p> <pre><code>select isnull(Total,0), folder, dates.DateOf, trend from @filesAdded as files right join @dates as dates on files.DateOf = dates.DateOf </code></pre>
    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.
 

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