views:

125

answers:

3

My problem:
I have a table with a Channel <int> and a Value <float> column, along with a timestamp and a couple of other columns with additional data. Channel is either 1 or 2, and there is either 1 or 2 rows that have everything except channel and value the same.

What I'd like to do is select this data into a new form, where the two channels show up as columns. I tried to do something with GROUP BY, but I couldn't figure out how to get the values into the correct columns based on the channel on the same row.

Example:
For those of you that rather look at the data I have and the data I want and figure it out from there, here it is. What I have:

 Channel    Value       Timestamp                OtherStuff
 1          0.2394      2010-07-09 13:00:00      'some other stuff'
 2          1.2348      2010-07-09 13:00:00      'some other stuff'
 1          24.2348     2010-07-09 12:58:00      'some other stuff'
 2          16.3728     2010-07-09 12:58:00      'some other stuff'
 1          12.284      2010-07-09 13:00:00      'unrelated things'
 2          9.6147      2010-07-09 13:00:00      'unrelated things'

What I want:

Value1     Value2      Timestamp                OtherStuff
0.2394     1.2348      2010-07-09 13:00:00      'some other stuff'
24.2348    16.3728     2010-07-09 12:58:00      'some other stuff'
12.284     9.6147      2010-07-09 13:00:00      'unrelated things'

Update in response to some questions that have arised in comments, and a few follow up questions/clarifications:

  • Yes, it is the combination of Timestamp and OtherStuff that links the two rows together. (OtherStuff is actually more than one column, but I simplified for brevity.) There are also a couple of other columns that are not necessarily equal, but should be kept just as they are.

  • The table in question is already joined from two tables, where Value, Channel and Timestamp comes from one of them, and the rest (a total of 7 more columns, out of which 4 are always equal for "linked" rows, and the other three are mostly not). There have been a couple of suggestions using INNER JOIN - will these still work if I'm already joining stuff together (even though I don't have a myTable to join to itself)?

  • There are a lot of rows with the same timestamp, so I need information from both the tables I'm joining to figure out which rows to link together.

  • I have a lot of data. The input comes from measurement devices stationed all over the country, and most of them (if not all) upload measurements (for up to 4 channels) every 2 minutes. Right now we have about 1000 devices online, so this means an addidtion of on average approximately 1000 rows every minute. I need to consider values that are up to at least 3, preferrably 6, hours old, which means 180 000 to 360 000 rows in the table with channel, value and timestamp.

+2  A: 

As long as you have something that links the 2 rows, something like this

SELECT
    c1.Value AS Value1, c2.Value AS Value2, c1.timestamp, c2.otherstuff
FROM
    MyTable c1
    JOIN
    MyTable c2 ON c1.timestamp = c2.timestamp AND c1.otherstuff = c2.otherstuff
WHERE
    c1.Channel = 1 AND c2.Channel = 2

If you don't have anything that links the 2 rows, then it probably can't be done because how do you know they are paired?

If you have 1 or 2 rows (edit: and don't know which channel value you have)

SELECT
    c1.Value AS Value1, c2.Value AS Value2, c1.timestamp, c2.otherstuff
FROM
    (
     SELECT Value, timestamp, otherstuff
     FROM MyTable
     WHERE Channel = 1
    ) c1           
    FULL OUTER JOIN
    (
     SELECT Value, timestamp, otherstuff
     FROM MyTable
     WHERE Channel = 2
    ) c2 ON c1.timestamp = c2.timestamp AND c1.otherstuff = c2.otherstuff                  
gbn
I do have 1 *or* 2 rows. Will the second approach even though I don't know necessarily which channel I have if I only have one row?
Tomas Lycken
@tomas lycken: I've updated it for this case
gbn
I'm afraid I over-simplified my table structure - see the updates to my question. Should I solve the fact that I have two joined tables by first joining to a table variable, or is there a nice way to do this even though not all the columns come from the same table? I assume I could just join the other table in, but where should I do this for optimal performance? (The query already takes almost a minute...)
Tomas Lycken
A: 
SELECT a.Value as Value1, b.Value as Value2,
a.TimeStamp, a.OtherStuff
FROM myTable a INNER JOIN myTable b
ON a.OtherStuff = b.OtherStuff and a.TimeStamp = b.TimeStamp
WHERE a.Channel = 1 AND b.Channel = 2

Written without a query editor. Please be kind :)

EDIT: INNER JOIN could also be used here. EDIT2: Corrected with INNER JOIN . Don't rush to downvote :)

shahkalpesh
-1 for not using a JOIN. It's pretty hard for other developers to see whats going on with the syntax FROM table1, table2.
JonH
it's wrong anyway as well as bad form. "a.TimeStamp = 1 AND b.TimeStamp = 2"?
gbn
@gbn: Yes. I have corrected it. Thanks to SO, there is an edit feature (imagine the downvotes otherwise).
shahkalpesh
@JonH: I agree. I used to write my queries without `JOIN` syntax (ms-access). And it took me some time to think of a `JOIN`.
shahkalpesh
OK, now you're using JOIN, why filter in the JOIN for channel? You should separate JOIN and WHERE...
gbn
@gbn: Is the SQL proper now?
shahkalpesh
A: 

Something like...

SELECT   MAX(CASE Channel WHEN 1 THEN Value ELSE 0 END) AS Value1,
         MAX(CASE Channel WHEN 2 THEN Value ELSE 0 END) AS Value2,
         Timestamp, 
         OtherStuff
FROM     {tablename}
GROUP BY Timestamp, OtherStuff

(I havent tested this!) (and this assumes your Value is always positive!)

Alternatively (see comments below)...

SELECT   SUM(CASE Channel WHEN 1 THEN Value ELSE 0 END) AS Value1, 
         SUM(CASE Channel WHEN 2 THEN Value ELSE 0 END) AS Value2, 
         Timestamp, 
         OtherStuff 
FROM     {tablename}
GROUP BY Timestamp, OtherStuff
barrylloyd
Unfortunately, I can't assume that the value is always positive. But thanks anyway =)
Tomas Lycken
Ok, well if there is always exactly 1 row for channel 1 and exactly 1 row for channel 2 (for each timestamp) then see my alternative (above)
barrylloyd
Unfortunately that is not the case either - I need *both* timestamp and the other stuff (several columns) to distinguish. As you see in my example, there can be two rows with channel 1 and timestamp 13:00 that don't belong to the same "linked" row.
Tomas Lycken
I think you need to explain how the rows are linked then. My alternative solution will give the correct result for your example. It will group all the rows by both Timestamp and OtherStuff - then sums up all the values where Channel=1 and calls this Value1, and same for Channel=2.
barrylloyd