sql server 2005 - SQL code for DB query by date -


I'm a bit of a SQL Newbad, but to work with some code to get value from a historian database Work has been done. I am retrieving the name of the item and its value at a specific time every month of the year. However, the user coming from a report of the year will be the prescribed parameter. So if the user selects 2014, then it should ask 1/1/2014/2/1/2014 and so on.

I have come up with a simple part of the query which is looking at tag names and associated values:

  SELECT tag name, from runtime.dbo to value History WHERE Tagname IN ('Tag1', 'Tag2') and wwVersion = 'Latest' and DateTime = @ Date   

The problems I have to solve are: 1. I manually Can set up @data, but I want to do it as mentioned above. I am thinking that some kind of loop can be used? 2. I'm not sure how to view it in the table format:

  Tag 1, (mass value), (fb value), (merge value) etc. Tag 2, (mass value), (fab value), (mary value) etc ...   

Any help would be appreciated. Thanks!

If you have only one cost per tag per month, you can use conditional aggregate Choose my record I've gone for the MAX function, but if you have only one value then it's arbitrary:

  DECLARE @Year INT; SET @ wear = 2013; - To change a date where the date of acceptance of a join date @ SMALLDATETIME; Set @ date = convert (SMALLDATETIME, converter (four (4), @ age) + '0101', 112); SELECT Tagname, Jan = MAX (case, when DATEPART (month, date time) = 1 THEN value ADD), für = Max (case, when DATEPART (month, date time) = 2 THEN value ADD), March = MAX In the case of DATEPART (month), date =) = 3 then value ADD, April = maximum (case when DATEPART (month, date time) = 4 THEN value ADD), May = Max (in case when DATEPART (month, date time) = 5 THEN value ADD), June = maximum (case when DATEPART (month, date time) = 6 THEN value ADD), July = maximum (case when DATEPART (month, date time) = 7 THEN value ADD), AUGU = max (case when DATEPART (month, date time) = 8 now value ADD), September = maximum (case when DATEPART (month, date time) = 9 THEN value ADD), October = max ( Case when DATEPART (month, date time) = 10 THEN value ADD), November = MAX (case when DATEPART (month, date time) = 11 then value ADD), December = maximum (case when the part (month, date time) = 12 then value AD) to runtime W. WHAE Tag Name IN ('Tag 1', 'Tag2') in history and wwVersion = 'latest' and date time & gt; = @ Date and date time & lt; Tagged by DATEADD (year, 1, @date) group;   

If you have multiple values, then you will need to apply some kind of logic to select the right type. In the example below I went for the first price for each month:

  DECLARE @Year INT; SET @ wear = 2013; - To change a date where the date of acceptance of a join date @ SMALLDATETIME; Set @ date = convert (SMALLDATETIME, converter (four (4), @ age) + '0101', 112); SELECT Tagname, Jan = MAX (case, when DATEPART (month, date time) = 1 THEN value ADD), für = Max (case, when DATEPART (month, date time) = 2 THEN value ADD), March = MAX In the case of DATEPART (month), date =) = 3 then value ADD, April = maximum (case when DATEPART (month, date time) = 4 THEN value ADD), May = Max (in case when DATEPART (month, date time) = 5 THEN value ADD), June = maximum (case when DATEPART (month, date time) = 6 THEN value ADD), July = maximum (case when DATEPART (month, date time) = 7 THEN value ADD), AUGU = max (case when DATEPART (month, date time) = 8 now value ADD), September = maximum (case when DATEPART (month, date time) = 9 THEN value ADD), October = max ( Case when DATEPART (month, date time) = 10 THEN value ADD), November = MAX (case when DATEPART (month, date time) = 11th value ADD), December = maximum (case when month part (month, date time) = 12 then value AD) (tag name, date time, value, row name = ROW_NUMBER () tag (tag name Order, DATEPART (MONTH, date time), DATEPART (year, date time) from time-to-date) runtime.dbo.H to ITERRE WHERE tag name ('tag1', 'tag2') and wwVersion = 'latest' and Date Time & gt; Date and date time & lt; DATEADD (year, 1, date @)) where H. Tag name by ROON = 1 group;    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -