在同一行中显示具有不同值的相同列

时间:2014-12-14 21:31:23

标签: sql postgresql-9.3

我有3个表,这是Sql Fiddle Demo http://www.sqlfiddle.com/#!15/89ac5/3/0

create table entities (id int,  credit int,   debit  int, value  int,etype int, date date);

insert into entities values 
    (1,101,100,5000,1,'01/01/2014'),
    (1,101,100,1000,2,'01/01/2014'),
    (1,102,100,2000,1,'01/01/2014'),
    (1,102,100,4000,2,'01/01/2014');

create table accounts (id int,  name varchar(20));

insert into accounts values 
    (100,  'Clinic'),
    (101,  'Mark'),
    (102,  'Jone');

create table etype (id int, name varchar(20));

insert into etype values
    (1, 'Medicine'),
    (2, 'Diagnoise');

当我运行此查询时:

select 
    e.id, 
    credit_account.name as CreditName, 
    debit_account.name as DebitName,
    t.name,    
    e.date 
from entities e
join accounts as credit_account on e.credit = credit_account.id
join accounts as debit_account  on e.debit  = debit_account.id
Join etype as t on e.etype = t.id

我有这个结果:

ID  CREDITNAME  DEBITNAME   VALUE   NAME    DATE
1          Mark     Clinic  5000    Medicine    January, 01 2014 00:00:00+0000
2          Mark     Clinic  1000    Diagnoise   January, 01 2014 00:00:00+0000
3          Jone     Clinic  2000    Medicine    January, 01 2014 00:00:00+0000
4          Jone     Clinic  4000    Diagnoise   January, 01 2014 00:00:00+0000

最后,我想要一个视图来显示这个结果:

ID  CREDITNAME  DEBITNAME   Medicine    Diagnoise   DATE
1          Mark     Clinic  5000        1000        January, 01 2014 00:00:00+0000
2          Jone     Clinic  2000        4000        January, 01 2014 00:00:00+0000

如果我们可以动态制作它,例如我们添加'Lab'

3 个答案:

答案 0 :(得分:0)

您可以使用条件聚合:

select row_number() over (order by creditname, debitname) as id, creditname, debitname,
       sum(case when name = 'Medicine' then value end) as medicine,
       sum(case when name = 'Diagnoise' then value end) as diagnoise,
       date
from table t 
group by creditname, debitname, date;

编辑:

根据您的查询:

select row_number() over (order by creditname, debitname) as id, 
       credit_account.name as CreditName, 
       debit_account.name as DebitName,
       sum(case when t.name = 'Medicine' then e.value end) as medicine,
       sum(case when t.name = 'Diagnoise' then e.value end) as diagnoise,
       e.date
from entities join
     accounts credit_account
     on e.credit = credit_account.id join
     accounts debit_account 
     on e.debit  = debit_account.id Join
     etype t
     on e.etype = t.id
group by credit_account.name, debit_account.name, e.date

答案 1 :(得分:0)

你可以这样做:

select distinct 
    e.id,
    credit_account.name as CreditName, 
    debit_account.name as DebitName,
    (select e1.value 
     from entities e1
     where e1.etype = 1   //Medicine type
     and e1.credit = e.credit) as Medicine,
    (select e1.value 
     from entities e1
     where e1.etype = 2   //Diagnoise type
     and e1.credit = e.credit) as Diagnoise,
    e.date 
from entities e
join accounts as credit_account on e.credit = credit_account.id
join accounts as debit_account  on e.debit  = debit_account.id

当然,如果您不想使用1和2对类型进行硬编码,则可以使用以下命令修改子查询:

select e1.value 
from entities e1, etypes et
where e1.etype = et.id  
and et.name = 'Medicine'
and e1.credit = e.credit

但我不认为这是最好的方法。

答案 2 :(得分:0)

谢谢大家,我对查询做了一些更改:

select 
    credit_account.name as CreditName, 
    debit_account.name as DebitName,
    sum(case when t.name = 'Medicine' then e.value end) as medicine,
    sum(case when t.name = 'Diagnoise' then e.value end) as diagnoise,    
    e.date 
from entities e
join accounts as credit_account on e.credit = credit_account.id
join accounts as debit_account  on e.debit  = debit_account.id
Join etype as t on e.etype = t.id
group by credit_account.name, debit_account.name, e.date