473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Join columns

5
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!
Nov 9 '09 #1

✓ answered 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.  

6 1827
nbiswas
149 100+
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
Nov 9 '09 #2
nbiswas
149 100+
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.
Nov 10 '09 #3
amitpatel66
2,367 Expert 2GB
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.  
Nov 13 '09 #4
Broun
5
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
Nov 13 '09 #5
Broun
5
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.
Nov 16 '09 #6
amitpatel66
2,367 Expert 2GB
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
Nov 16 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
2
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
7
by: stabbert | last post by:
I am attempting to join two tables together on two different unix servers. Here is some relevant info about the tables. TABLE 1 Setup ----------------------- DB2 UDB 7.2 EE
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
2
by: Bennett Haselton | last post by:
I know how to create a DataAdapter that loads data from a data source into a table in a typed DataSet, and how to set the DataSource and DataMember properties of a DataGrid so that at run time it...
7
by: Chris | last post by:
I'm using ASP.NET and SQL Server and this might be an obviuos question for most, but if I have a table that contains several fields that I need to relate to just one field in another table, how do...
6
by: PW | last post by:
I've created an ASP application which uses an Access database. I've created an outer join query, but for some reason the "Property_Def" column is not aligning with the "ESPN" column. They should...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
4
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
3
by: Annyka | last post by:
Platform: SQL Server 2000 (8.00.2040, SP4, Enterprise edition) I've got a complex query I'm trying to build, which will select all requests that have a status_code of 1, and who's related incident...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.