0

Just need a little help as new to SQL.

I have a table which contains readingtime (records every 4 seconds during the day) and an energy reading. I wanted to know how much energy was used in a day. this is the query that i used

SELECT date(readingtime), max(energy)-min(energy)as total 
FROM lights_1 
WHERE readingtime between 20140407000000 and 20140409235959 
GROUP BY date(readingtime) LIMIT 0,30;

This gave me the desired results for 1 table. I have 4 tables of these lights_2, Lights_3, lights_4 and i would like to display them together and hence match up the reading time so each day has the energy requirements for all for tables with the total. When i try and join tables i get a lot of errors!

Can anyone help.

thanks

Union gives

date(readingtime) total
2014-04-07        1244
2014-04-08        1039
2014-04-09        1389
2014-04-07        419
2014-04-08        1102
2014-04-09        482

I would like

date(readingtime) total_1  total_2
2014-04-07        1244      419
2014-04-08        1039      1039
2014-04-09        1389      1389
blobbles
  • 1,621
  • 1
  • 11
  • 18
user46507
  • 1
  • 2

2 Answers2

1
SELECT  date(lights_1.readingtime), 
        max(lights_1.energy)-min(lights_1.energy)as total_1, 
        max(lights_2.energy)-min(lights_2.energy)as total_2
FROM lights_1
join lights_2 on lights_1.readingtime = lights_2.readingtime
WHERE lights_1.readingtime between 20140407000000 and 20140409235959 
GROUP BY date(lights_1.readingtime) LIMIT 0,30
Richard101
  • 15
  • 5
0

You first need to UNION the tables:

SELECT date(readingtime), max(energy)-min(energy)as total 
FROM lights_1 
WHERE readingtime between 20140407000000 and 20140409235959 
GROUP BY date(readingtime) LIMIT 0,30
UNION
SELECT date(readingtime), max(energy)-min(energy)as total 
FROM lights_2 
WHERE readingtime between 20140407000000 and 20140409235959 
GROUP BY date(readingtime) LIMIT 0,30
UNION
SELECT date(readingtime), max(energy)-min(energy)as total 
FROM lights_3 
WHERE readingtime between 20140407000000 and 20140409235959 
GROUP BY date(readingtime) LIMIT 0,30
SELECT date(readingtime), max(energy)-min(energy)as total 
FROM lights_4 
WHERE readingtime between 20140407000000 and 20140409235959 
GROUP BY date(readingtime) LIMIT 0,30;

You then will need to pivot the result of this query. I assume you are using MySQL? I could go through and figure out everything for you, but its probably better if you go and learn how to pivot the data to get it the way you want, the following is a good summary! http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/

blobbles
  • 1,621
  • 1
  • 11
  • 18
  • I have done this but this lists the data. i want to match up each table with a date. – user46507 Aug 29 '14 at 12:44
  • Please edit the question to give an example of what you want - its not entirely clear what format you want the data in. – blobbles Aug 29 '14 at 12:45
  • 1
    Also tell us what database system you are using! – blobbles Aug 29 '14 at 12:46
  • each table has the same readingtime so i need to match these up. otherwise i get a list of each day for Lights_1 and then a list underneath for Lights_2. Does that make sense? – user46507 Aug 29 '14 at 12:47
  • will do now, sorry – user46507 Aug 29 '14 at 12:47
  • It sounds like you want to pivot the data so that you have a single row, fist column Date, 2nd column Light1 energy total, 3rd column Light2 energy total etc. Will wait for your edit... – blobbles Aug 29 '14 at 12:57
  • When i use union i get this.

    date(readingtime) total 2014-04-07 1244 2014-04-08 1039 2014-04-09 1389 2014-04-07 419 2014-04-08 1102 2014-04-09 482

    I would like this date(readingtime) total_1 total_2 2014-04-07 1244 419 2014-04-08 1039 1102 2014-04-09 1389 482

    – user46507 Aug 29 '14 at 13:01
  • sorry. my posts are not showing as i have put them – user46507 Aug 29 '14 at 13:02
  • when i use union i get this- date(readingtime) total 2014-04-07 1244 2014-04-08 1039 2014-04-09 1389 2014-04-07 419 2014-04-08 1102 2014-04-09 482 I am trying to send the table i want but it keeps just listing the data? – user46507 Aug 29 '14 at 13:03
  • Oh right, instead of putting in another answer, please edit your original question, adding on your "answer". Thanks. – blobbles Aug 29 '14 at 13:04
  • Actually RichardBSmiths solution above should work for you, check that one out (a lot more simple). – blobbles Aug 29 '14 at 14:11