SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query database by date and send results to table


Query database by date and send results to table

Author
Message
mlwiller2
mlwiller2
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
Points: 149 Visits: 37
I'm a bit of a SQL newb but have been tasked with coming up with some code to retrieve values from a historian database. I'm retrieving the name of an object (tag) and its value at a specific time every month of the year. However, the year will be user defined parameter coming from a report. So if the user selects 2014 it should query on 1/1/2014,2/1/2014 and so on.

I came up with the simple part of the query which is looking up the tag names and associated values:

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

Here are the issues I need to solve:
1. I can manually set the @Date but I'd like to have it do whats described above. I'm thinking some type of loop might be used?
2. I'm not sure how to output it into table format to look like:

Tag 1, (jan value), (feb value), (mar value), etc...
Tag 2, (jan value), (feb value), (mar value), etc...

Any help would be appreciated. Thanks!
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63877 Visits: 17974
You definitely don't need a loop for this. You can do this using a tally or a calendar table (not totally sure which one fits best). In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
desperadomar
desperadomar
SSC Veteran
SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)

Group: General Forum Members
Points: 205 Visits: 355
Can't tell exactly without the DDL as mentioned by Sean.

But as you said you need get all results when a user inputs a date and neet to search with Year part. One option is to do like this.

declare @date datetime;--'11-2-2014'

SELECT Tagname,value
From runtime.dbo.History
WHERE Tagname IN ('Tag1', 'Tag2')
and wwVersion = 'Latest'
and

DATEPART(YY,datetime)= DATEPART(YY,@date)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search