Pergunta

I have a table with columns date, system, hardness, conductivity, tankno.

date        system  cond    hard   tankno

2014-01-04  HT  55.67   2.68    1
2014-01-05  HT  64.67   3.1     2
2014-01-10  HT  54.77   2.44    1
2014-01-10  HT  66.3    3.47    2
2014-01-18  HT  55.63   2.38    1
2014-01-18  HT  65.9    1.44    2
2014-01-24  HT  53.1    1.76    2
2014-01-28  HT  64.53   2.1     1

I want report like this

Date         system    Tank 1 cond   Tank1 Hard      Tank 2 cond  Tank 2hard

2014-01-04    HT            55.67       2.68            NULL        NULL  
2014-01-05    HT            NULL        NULL            64.67       3.1
2014-01-10    HT            54.77       2.44            66.3        3.47
2014-01-18    HT            55.63       2.38            65.9        1.44
2014-01-24    HT            NULL        NULL            53.1        1.76
2014-01-28    HT            64.53       2.1              NULL       NULL

I am using this sql:

select t1.date, t1.cond as tk1cond, t1.hard as tk1hard,
       t2.cond as tk2cond, t2.hard as tk2hard   
from systemvalue t1 left join systemavalue t2 
     on t1.date=t2.date 
     and t2.tankno='2' 
     and  t2.system='HT' 
     and month(t2.date)='01' 
     and year(t2.date)='2014' 
where t1.tankno='1' and t1.system='HT'
      and month(t1.date)='01' 
      and year(t1.date)='2014'

but this return data pertaining to the dates where tankno 1 value is available like this:

Date         system    Tank 1 cond   Tank1 Hard        Tank 2 cond  Tank 2hard

2014-01-04    HT            55.67       2.68               NULL        NULL  
2014-01-10    HT            54.77       2.44               66.3        3.47
2014-01-18    HT            55.63       2.38               65.9        1.44
2014-01-28    HT            64.53       2.1                NULL        NULL

How to modify the sql so as to get the data for all dates

I want all dates from date columns and join it to all tank 1 vlaues and tank 2 values

Foi útil?

Solução

Here you are:

select 
    dt.date, 
    t1.cond as tk1cond, 
    t1.hard as tk1hard,
    t2.cond as tk2cond, 
    t2.hard as tk2hard  
from (select date from systemvalue group by date) dt
left join systemvalue t1 on t1.date = dt.date and t1.tankno = '1' and t1.system = 'HT'
left join systemvalue t2 on t2.date = dt.date and t2.tankno = '2' and t2.system = 'HT'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top