Connecting Tech Pros Worldwide Forums | Help | Site Map

Join columns

Newbie
 
Join Date: Sep 2006
Posts: 5
#1: 2 Weeks Ago
i WANTO JOIN A TABLE ON ITSELF (IS THAT WHAT IT IS, I DONT KNOW).
I HAVE A TABLE SAY:
Expand|Select|Wrap|Line Numbers
  1. TIME                    READING                        READER
  2. =============================================
  3. 10:15                       8                                   A
  4. 10:20                      11                                  A
  5. 10:25                       9                                   A
  6. 10:30                       7                                   A
  7. 10:10                      875                                 B
  8. 10:20                      431                                 B
  9. 10:30                      678                                 B
  10. 10:11                      28.3                                C
  11. 10:16                      31.9                                C
  12. 10:41                       25.9                               C
  13.  
I want to extract the following table
Expand|Select|Wrap|Line Numbers
  1. TIME                         READER_A           READER_B          READER_C
  2. -=======================================================
  3. 10:10                                                           875
  4. 10:11                                                                                     28.3
  5. 10:15                            8
  6. 10:16                                                                                     31.9               
  7. 10:20                           11                             431                  
  8. 10:25                            9                    
  9. 10:30                            7                             678
  10. 10:41                                                                                     25.9    
  11.  
Tried a couple of methods and am now lost. Any help guys!
best answer - posted by amitpatel66
Expand|Select|Wrap|Line Numbers
  1. SQL> select tt Time, rd reading, rdr reader FROM test11;
  2. TIME                                                                                                    READING READER
  3. ----------------------------------------------------------------------------------------------------
  4. 10:15                                                                                                         8 A
  5. 10:20                                                                                                        11 A
  6. 10:20                                                                                                       111 B
  7.  
  8. SQL> SELECT tt Time, MAX(DECODE(rdr,'A',rd,NULL)) READER_A,MAX(DECODE(rdr,'B',rd,NULL)) READER_B FRO
  9. M test11 GROUP BY tt;
  10.  
  11. TIME                                                                                                   READER_A   READER_B
  12. ----------------------------------------------------------------------------------------------------
  13. 10:15                                                                                                         8
  14. 10:20                                                                                                        11        111
  15.  
  16. SQL> 
  17.  
  18.  

nbiswas's Avatar
Member
 
Join Date: May 2009
Location: India
Posts: 35
#2: 2 Weeks Ago

re: Join columns


Try this

Expand|Select|Wrap|Line Numbers
  1. select times , case when reader='A' then reading end as READER_A 
  2.                 ,case when reader='B' then reading end as READER_B 
  3.                 ,case when reader='C' then reading end as READER_C  
  4. from mytbl 
  5. order by times 
Not tested though

.Hope this helps
nbiswas's Avatar
Member
 
Join Date: May 2009
Location: India
Posts: 35
#3: 2 Weeks Ago

re: Join columns


Ignore the earlier query which does not takes care of merging the rows based on dates.

Try this

Expand|Select|Wrap|Line Numbers
  1. select 
  2.     case    when t1.ReadingTime is null and t3.ReadingTime is null then t2.ReadingTime
  3.                 when t1.ReadingTime is null and t2.ReadingTime is null then t3.ReadingTime
  4.                 else t1.ReadingTime end as Time ,
  5.     t1.Reading as Reader_A ,    
  6.     t2.Reading as Reader_B ,    
  7.     t3.Reading as Reader_C 
  8. from
  9. (select ReadingTime, Reading,Reader from tblMeterReading where Reader = 'A') t1
  10. full outer join
  11. (select ReadingTime, Reading,Reader from tblMeterReading where Reader = 'B') t2
  12. on t1.ReadingTime = t2.ReadingTime
  13. full outer join
  14. (select ReadingTime, Reading,Reader from tblMeterReading where Reader = 'C') t3
  15. on t1.ReadingTime = t3.ReadingTime and t2.ReadingTime = t3.ReadingTime
  16. order by ReadingTime
Let me know in case of any concern.I have tested this and found to be working as per the data given.

Note:- Change the table name and respective columns as per your requirement.
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#4: 2 Weeks Ago

re: Join columns


Expand|Select|Wrap|Line Numbers
  1. SQL> select tt Time, rd reading, rdr reader FROM test11;
  2. TIME                                                                                                    READING READER
  3. ----------------------------------------------------------------------------------------------------
  4. 10:15                                                                                                         8 A
  5. 10:20                                                                                                        11 A
  6. 10:20                                                                                                       111 B
  7.  
  8. SQL> SELECT tt Time, MAX(DECODE(rdr,'A',rd,NULL)) READER_A,MAX(DECODE(rdr,'B',rd,NULL)) READER_B FRO
  9. M test11 GROUP BY tt;
  10.  
  11. TIME                                                                                                   READER_A   READER_B
  12. ----------------------------------------------------------------------------------------------------
  13. 10:15                                                                                                         8
  14. 10:20                                                                                                        11        111
  15.  
  16. SQL> 
  17.  
  18.  
Newbie
 
Join Date: Sep 2006
Posts: 5
#5: 2 Weeks Ago

re: Join columns


This looks good, Mr. Patel. I have tried it, deleted some Null values, looks good.
Will have to check if data was left out or mixed up or repeated.
Regards
Newbie
 
Join Date: Sep 2006
Posts: 5
#6: 1 Week Ago

re: Join columns


Mr.Patel I have alot of Data here and the sql is not tranparent to me. Max(Decode ...) is difficult to understand. Max to me should give me maximum only and Decode compares searches. So my actual sql looks something like this. Does it make sense?
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT READ_TIME_STAMP, 
  3.                         MAX(DECODE(DATA_POINT_ID, 68, value, NULL)) co2, 
  4.                         MAX(DECODE(DATA_POINT_ID,'154',value,NULL)) humidity, 
  5.                         MAX(DECODE(DATA_POINT_ID,86,value,NULL)) radiance,
  6.                         MAX(DECODE(DATA_POINT_ID,118,value,NULL)) room_temp,
  7.                         MAX(DECODE(DATA_POINT_ID,994,value,NULL)) air_humid, 
  8.                         MAX(DECODE(DATA_POINT_ID,400,value,NULL)) airtemp,
  9.                         MAX(DECODE(DATA_POINT_ID,706,value,NULL)) wind_dir, 
  10.                         MAX(DECODE(DATA_POINT_ID,303,value,NULL)) wind_speed, 
  11.                         MAX(DECODE(DATA_POINT_ID,221,value,NULL))  solar_temp,
  12.                         MAX(DECODE(DATA_POINT_ID,503,value,NULL))  dm_uf3,
  13.                         MAX(DECODE(DATA_POINT_ID,512,value,NULL))  dm_uf4
  14. FROM Read_Sensors GROUP BY READ_TIME_STAMP;
  15.  
  16.  
The data has lots of duplicate and null values. So i run a sript to delete where all row values are null.
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#7: 1 Week Ago

re: Join columns


Please post the actual data that you are trying to query? that would help me check if the query you prepared is correct or not
Reply