Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 [SOLVED] This Should Be SIMPLE, Right!?

Author  Topic 

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 08:11:52
FOR THOSE OF YOU NEW TO READING THIS TOPIC, PLEASE READ THROUGH A FEW OF MY LATER POSTS TO BETTER
UNDERSTAND WHAT I NEEDED. I DID NOT DO A GREAT JOB OF EXPLAINING WHAT I NEEDED AS I AM A NOOB. IF
YOU TRY TO FIGURE OUT WHAT I NEED FROM THIS INITIAL POST YOU MAY NOT GET IT. I APOLOGIZE.

[font=system]
I have, what I believe, is a very simple problem that (since I am new) I cannot figure out.

I have a table called HISTORY that stores historical data. Initially the data is dumped from
another server which collects the data into the MS SQL database. I need the following items
from the table TIME, NAME, VALUE and then only where ID = 20 and NAME = NAME1 or NAME2 or
NAME3. To give you more information about this particular set of data, the values are
collected once an hour all day, every day. What I need to do is find the maximum value for NAME1
each day and get the values for NAME2 and NAME3 at that same time regardless if they were
maximum values for themselves.


So if I had something like this:
NAME1 NAME2 NAME3
10/1/2008 7:00 1000 834 99
10/1/2008 8:00 1140 710 250
---------carry on for day through 23:00 where no other values are >
10/2/2008 1:00 1569 800 123
10/2/2008 2:00 1500 334 456
---------carry on for the day through 23:00 where no other values are >

I would want to have the results like this
TIME NAME1 NAME2 NAME3
10/1/2008 8:00 1140 710 250
10/2/2008 1:00 1569 800 123
-----and so one for each DAY

Would also be helpful to have it parameterized by month and year
so I could ask for the results of the time of the maximum value for NAME1 during 10-2008 or 1-2009.

Would love it if someone could help me with this!!!
[/font=system]

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 08:20:07
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, TIME) ORDER BY TIME DESC) AS recID
FROM HISTORY
) AS d
WHERE recID = 1



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 08:22:18
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, TIME) ORDER BY NAME1 DESC) AS recID
FROM HISTORY
) AS d
WHERE recID = 1


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 08:30:21
Either of those two only return 1 record. I think maybe I didn't do a good job explaining. The results NAME1 NAME2 and NAME3 are in the NAME column along with others NAME4 NAME5...NAMEn. I only want NAME1 NAME2 and NAME3 from NAME where NAME1 is at its maximum value for the day. The result table I laid out above would be how it would look in a pivot table and not an SQL result. The SQL result would look more like this:

10/1/2008 8:00 NAME1 1140
10/1/2008 8:00 NAME2 710
10/1/2008 8:00 NAME3 250
10/2/2008 1:00 NAME1 1569
10/2/2008 1:00 NAME2 800
10/2/2008 1:00 NAME3 123
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 08:32:52
quote:
Originally posted by paulhood

TIME               NAME1     NAME2     NAME3
10/1/2008 8:00 1140 710 250
10/2/2008 1:00 1569 800 123
-----and so one for each DAY

This new request is totally different from original request...



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 08:38:41
[code]DECLARE @Sample TABLE
(
DT DATETIME,
NAME1 INT,
NAME2 INT,
NAME3 INT
)

INSERT @Sample
SELECT '10/1/2008 7:00', 1000, 834, 99 UNION ALL
SELECT '10/1/2008 8:00', 1140, 710, 250 UNION ALL
SELECT '10/2/2008 1:00', 1569, 800, 123 UNION ALL
SELECT '10/2/2008 2:00', 1500, 334, 456

;WITH Yak (DT, theName, theValue, recID)
AS (
SELECT u.DT,
u.theName,
u.theValue,
ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, u.DT), u.theName ORDER BY u.theValue DESC) AS recID
FROM @Sample AS s
UNPIVOT (
theValue
FOR theName IN (s.NAME1, s.NAME2, s.NAME3)
) AS u
)
SELECT DT,
theName,
theValue
FROM Yak
WHERE recID = 1
ORDER BY DATEDIFF(DAY, 0, DT),
theValue DESC[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 08:45:48
Another approach
DECLARE	@Sample TABLE
(
DT DATETIME,
NAME1 INT,
NAME2 INT,
NAME3 INT
)

INSERT @Sample
SELECT '10/1/2008 7:00', 1000, 834, 99 UNION ALL
SELECT '10/1/2008 8:00', 1140, 710, 250 UNION ALL
SELECT '10/2/2008 1:00', 1569, 800, 123 UNION ALL
SELECT '10/2/2008 2:00', 1500, 334, 456


SELECT DT,
theName,
theValue
FROM (
SELECT DT,
NAME1,
NAME2,
NAME3
FROM (
SELECT DT,
NAME1,
NAME2,
NAME3,
ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, DT) ORDER BY NAME1 DESC) AS recID
FROM @Sample
) AS d
WHERE recID = 1
) AS d
UNPIVOT (
theValue
FOR theName IN (d.NAME1, d.NAME2, d.NAME3)
) AS u
ORDER BY DT,
theName



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 09:09:50
Peso, first off let me thank you for your time and interest. Since I am VERY new to SQL I don't completely understand how to best convey my circumstance. I apologize for any frustration you are feeling dealing with me (aka noob).

The results from what you last posted only provided me data from the "example" data I gave you and not from the actual dataset itself. Additionally, it selected the maximum value of each not just NAME1.

I think what I need to do is try to explain a little better but use some real data.

If I run Select * FROM HISTORY I will get the following columns:
ID, DATASETNAME, STATION, NAME, VALUE, CONDVALUE, TIME, LIMITSTATE, MAX, TIMEMAX, CONDMAX, MIN, TIMEMIN, CONDMIN, AVG, CONDAVG, SUM, CONDSUM

I only need to SEE NAME, VALUE, and TIME, but I need to filter by ID where ID = 20 (See original post).

Furthermore, I need to see ONLY the rows of data where the column NAME is VEPCO or MWALL or MWTOT (those related to NAME1 NAME2 NAME3).

In addition to that, there are 24 rows of data for each day of each month of each year for each NAME so I would have something like this if I did a SELECT TIME, NAME, VALUE FROM HISTORY WHERE ID=20:

6/4/2009 3:00:00 VEPCO 11247
6/4/2009 4:00:00 VEPCO 11384
6/4/2009 5:00:00 VEPCO 11525
----and so on for that day 6/4 and the next and the next until there is no more data for VEPCO
6/4/2009 3:00:00 MWALL 89
6/4/2009 4:00:00 MWALL 89
6/4/2009 5:00:00 MWALL 90
----and so on for that day 6/4 and the next and the next until there is no more data for MWALL
6/4/2009 3:00:00 MWTOT 216
6/4/2009 4:00:00 MWTOT 214
6/4/2009 5:00:00 MWTOT 212
----and so on for that day 6/4 and the next and the next until there is no more data for MWTOT

This data is constantly updated every hour all the way up to 9am this morning so I have 24 rows of data (by hour) for each NAME for each day all the way up to now. I need to pick the time for EACH day
which has the LARGEST VALUE for VEPCO and display the TIME, NAME, and VALUE for VEPCO, MWALL, and MWTOT at that time. So I should end up with three rows of data (one for each NAME) for each day.

I hope this helps and again I do apologize for creating confusion. If there is something else I can do to clear it up please let me know.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 09:11:37
Tried the suggestion made 07/01/2009 : 08:45:48 ?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-07-01 : 09:13:06
Follow the 2nd link in my signature, and it tells you what information to provide, and how to provide it.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 09:14:40
Just tried it and it is still only returning the SAMPLE data however the values are correct it selects the NAME1 value as the largest and returns the other two values with the same time stamp so that worked. I just need to do that with my data rather than the sample data.
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 09:15:38
I will check that out now.
quote:
Originally posted by DonAtWork

Follow the 2nd link in my signature, and it tells you what information to provide, and how to provide it.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 09:16:24
Since I have no access to your data, I hade to make up an environment similar to yours.
Replace the @Sample table with your table name HISTORY, and you are set to go!
SELECT		DT,
theName,
theValue
FROM (
SELECT DT,
NAME1,
NAME2,
NAME3
FROM (
SELECT DT,
NAME1,
NAME2,
NAME3,
ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, DT) ORDER BY NAME1 DESC) AS recID
FROM HISTORY
) AS d
WHERE recID = 1
) AS d
UNPIVOT (
theValue
FOR theName IN (d.NAME1, d.NAME2, d.NAME3)
) AS u
ORDER BY DT,
theName



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 09:21:24
Okay, another NOOB question, sorry. Where do I put my VEPCO, MWALL, and MWTOT in here? Should it be all the SELECT statements, the 1st, 2nd, or 3rd or just 2nd and 3rd? I get INVALID COLUMN NAMES errors on DT, NAME1, NAME2, NAME3, and then a second DT.

quote:
Originally posted by Peso

Since I have no access to your data, I hade to make up an environment similar to yours.
Replace the @Sample table with your table name HISTORY, and you are set to go!
SELECT		DT,
theName,
theValue
FROM (
SELECT DT,
NAME1,
NAME2,
NAME3
FROM (
SELECT DT,
NAME1,
NAME2,
NAME3,
ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, DT) ORDER BY NAME1 DESC) AS recID
FROM HISTORY
) AS d
WHERE recID = 1
) AS d
UNPIVOT (
theValue
FOR theName IN (d.NAME1, d.NAME2, d.NAME3)
) AS u
ORDER BY DT,
theName



N 56°04'39.26"
E 12°55'05.63"


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 09:27:47
[code]SELECT [TIME],
theName,
theValue
FROM (
SELECT [TIME],
VEPCO,
MWALL,
MWTOT
FROM (
SELECT [TIME],
VEPCO,
MWALL,
MWTOT,
ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, [TIME]) ORDER BY VEPCO DESC) AS recID
FROM HISTORY
) AS d
WHERE recID = 1
) AS d
UNPIVOT (
theValue
FOR theName IN (d.VEPCO, d.MWALL, d.MWTOT)
) AS u
ORDER BY [TIME],
theName[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 09:33:42
It didn't like putting VEPCO etc in there. Gave the same errors. VEPCO, MWALL, and MWTOT are not column names, they are values in the NAME column. I tried using [TIME], [NAME], and [VALUE] and it gave me an error "Incorrect Syntax Near ("
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-01 : 09:44:14
Listen carefully to DonAtWork so we can assist you.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 09:48:31
Okay. I appreciate all the help I can get!! You've been very patient with me and my lack of SQL knowledge, thank you.
quote:
Originally posted by Peso

Listen carefully to DonAtWork so we can assist you.



N 56°04'39.26"
E 12°55'05.63"


Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 10:44:50
[code][font=system]
I modified what you did a little and it's getting close. Here is what the new SQL looks like and I will provide the results below.

SELECT [TIME],
theName,
theValue
FROM (
SELECT [TIME],
[NAME],
[VALUE]

FROM (
SELECT [TIME],
[NAME],
[VALUE],

ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, [TIME]) ORDER BY VALUE DESC) AS recID
FROM HISTORY where ID=20
) AS d
WHERE (recID = 1)
) AS d
UNPIVOT (
theValue
FOR theName IN (d.VALUE)
) AS u

ORDER BY [TIME],
theName

The results are:
TIME theName theValue
6/5/2009 2:00:00PM VALUE 11162
6/6/2009 5:00:00PM VALUE 10422
6/7/2009 5:00:00PM VALUE 12472
......
7/1/2009 10:00:00AM VALUE 12090


The date/time and actual value look fine. The name should be VEPCO and also I should be seeing the values and names for MWALL and MWTOT at the same date/time for each day. It's very close!!
[/font=system][/code]
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 11:28:06
[code][font=system]
Okay, I modified some more and got the name in there but still not all three names and values. I am wondering
if this would still work (even though it's not complete yet) if the values for the other two names were higher
than the one for VEPCO. I don't see a reference where it is picking the highest value for VEPCO specifically
rather it appears to be just picking the largest value for any NAME during each day. In this case it will always
be VEPCO, but how would I make sure it always only looks at VEPCO? Now how do I get the other values to appear.
Here is the modified SQL.

SELECT [TIME],
[NAME],
[VALUE]

FROM (
SELECT [TIME],
[NAME],
[VALUE],

ROW_NUMBER() OVER (PARTITION BY DATEDIFF(DAY, 0, [TIME]) ORDER BY VALUE DESC) AS recID
FROM HISTORY where ID=20
) AS d
WHERE (recID = 1)

ORDER BY [TIME],
NAME

[/font=system][/code]
Go to Top of Page

paulhood
Starting Member

16 Posts

Posted - 2009-07-01 : 12:40:57
If I add another WHERE like WHERE recID = 1 or recID = nn then I can get other record sets. It appeared at first that I could use 25 and 49 for nn and that looked like it worked, but I'm not sure it will work over all time. Plus I still haven't figured out if it is only looking for the largest value for all the NAMEs or just one (VEPCO).
Go to Top of Page
    Next Page

- Advertisement -