Note that there are some explanatory texts on larger screens.

plurals
  1. POdiscontinious hourly data in datatable, possible to insert missing hours datetime value and null for datavalue?
    primarykey
    data
    text
    <p>I am using the following stored procedure to retrieve data from an SQL Server:</p> <pre><code>SELECT * FROM ( SELECT CAST(DateTimeUTC as SmallDateTime) as [DateTime], DataValue, VariableID FROM DataValues WHERE SiteID = @siteID and VariableID = 1 ) TableDate PIVOT (SUM(DataValue) FOR VariableID IN ([1])) PivotTable ORDER BY [DateTime] </code></pre> <p>I then fill a DataSet DataTable with those values:</p> <pre><code>DataSet myDataSet = new DataSet(); mySqlDataAdapter.Fill(myDataSet); DataTable precip = myDataSet.Tables["Table"]; if (precip.Rows.Count &gt; 0) { msPrec = precip.Rows[0].Field&lt;DateTime&gt;("DateTime"); sPrec = string.Join&lt;object&gt;(", ", from r in precip.Rows.OfType&lt;DataRow&gt;() select r[1]); } else { msPrec = DateTime.MinValue; sPrec = ""; } </code></pre> <p>In the above example msPrec is only a single value for the beginning of the dataset.</p> <p><strong>The problem is that I didn't consider that the hourly data may not be continuous.</strong><br /> Meaning that there may be missing data which isn't stored as NULL in the database. So it is entirely possible that one DB record is from 2013-04-26 04:00:00 and the next record in the DB is from 2013-04-26 08:00:00 with 05:00, 06:00, and 07:00 missing.</p> <p>It is also possible that multiple days of data may be missing, not just some hours within the same day.</p> <p>I am plotting this data with HighStock by HighCharts, much like shown in this example: <a href="http://www.highcharts.com/stock/demo/data-grouping" rel="nofollow">http://www.highcharts.com/stock/demo/data-grouping</a> except that they in the example pre-processed the data to include NULL values for missing data, as you can see by the data gaps in the plot.</p> <p>The pseudo code to solve this problem is clear to me:<br /> - loop through the output from the stored procedure<br /> - compare current record DateTime to next records DateTime<br /> - if nextRecordDateTime = currentRecordDateTime + 1 hour, proceed to next record<br /> - if nextRecordDateTime != currentRecordDateTime + 1 hour, then<br /> &nbsp;&nbsp;calculate the difference between currentRecordDateTime and nextRecordDateTime in hours<br /> &nbsp;&nbsp;insert DateTime and DataValue (NULL) for each missing hour into DataTable<br /> - continue loop at last inserted record</p> <p>I looked at the <a href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx" rel="nofollow">DataTable Class</a> on MSDN but didn't see anything that seemed relevant. The <a href="http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx" rel="nofollow">DataRow Class</a> would let me loop through the rows, but I am not clear on how I could obtain the value of the next row while in the current row.</p> <p>So I have two questions;</p> <p>How can I consider the DateTime value of the next record (row) while on the current record (row) of a DataSet DataTable?</p> <p>How can I insert rows into a DataSet DataTable at the correct location, rather than add it to the end of the DataTable? Or is the solution here to add it to the end and sort the table?</p>
    singulars
    1. This table or related slice is empty.
    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