Show same columns with different values in same row

user3614504

I have 3 tables, this is 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');

when i run this query :

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

I have this result:

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

finally, I want a view to show this result:

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

if we can make it Dynamically, example if we add 'Lab'

Gordon Linoff

You can use conditional aggregation:

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;

EDIT:

Based on your query:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Show same columns with different values in same row

From Dev

Update multiple row values to same row and different columns

From Dev

Update multiple row values to same row and different columns

From Dev

Deleting rows that contain a the same a values as a different row but in different columns

From Dev

MSSQL same collation columns show different data

From Dev

Comparing strings in same series (row) but different columns

From Dev

Compare 2 columns values (in the same row)

From Dev

Getting values of multiple columns on the same row of a ListView

From Dev

PHP show same variable with different values

From Dev

Identify Rows with Same Values in 2 Different Columns

From Dev

Sum same values in different columns (Google Spreadsheet)

From Dev

Return values in same row by searching different column

From Dev

Copy row with 2 different values into same table

From Dev

Copy cells from different columns on same row, and paste to different columns on same row on another sheet

From Dev

Delete one row out of two with same values alternating in two different columns

From Dev

Encoding the same values in different columns with same integer in python

From Dev

Two Queries on same table showing same row with different values?

From Dev

Is it possible to have a row in HBase with same rowkey, same column family, same column qualifier but different values?

From Dev

Seperating same values into columns

From Dev

Row-wise sum of values grouped by columns with same name

From Dev

Ordering row having same values in two columns on top

From Dev

How to combine the values of various columns in a tibble by the same row ID

From Dev

Insert different values in array in MySQL in the same columns with just 1 insert

From Dev

COLUMNS_UPDATED() return different values when ColumnId is the same

From Dev

Access select rows with same ID but different values in 2 other columns

From Dev

Excel sort maintaining equal values in different columns in the same position

From Dev

Looking for same values in two different columns in data.table

From Dev

Remove duplicate values appearing in different columns, not in the same order

From Dev

Solutions to put different values for a row-key but the same timestamps in hbase?

Related Related

  1. 1

    Show same columns with different values in same row

  2. 2

    Update multiple row values to same row and different columns

  3. 3

    Update multiple row values to same row and different columns

  4. 4

    Deleting rows that contain a the same a values as a different row but in different columns

  5. 5

    MSSQL same collation columns show different data

  6. 6

    Comparing strings in same series (row) but different columns

  7. 7

    Compare 2 columns values (in the same row)

  8. 8

    Getting values of multiple columns on the same row of a ListView

  9. 9

    PHP show same variable with different values

  10. 10

    Identify Rows with Same Values in 2 Different Columns

  11. 11

    Sum same values in different columns (Google Spreadsheet)

  12. 12

    Return values in same row by searching different column

  13. 13

    Copy row with 2 different values into same table

  14. 14

    Copy cells from different columns on same row, and paste to different columns on same row on another sheet

  15. 15

    Delete one row out of two with same values alternating in two different columns

  16. 16

    Encoding the same values in different columns with same integer in python

  17. 17

    Two Queries on same table showing same row with different values?

  18. 18

    Is it possible to have a row in HBase with same rowkey, same column family, same column qualifier but different values?

  19. 19

    Seperating same values into columns

  20. 20

    Row-wise sum of values grouped by columns with same name

  21. 21

    Ordering row having same values in two columns on top

  22. 22

    How to combine the values of various columns in a tibble by the same row ID

  23. 23

    Insert different values in array in MySQL in the same columns with just 1 insert

  24. 24

    COLUMNS_UPDATED() return different values when ColumnId is the same

  25. 25

    Access select rows with same ID but different values in 2 other columns

  26. 26

    Excel sort maintaining equal values in different columns in the same position

  27. 27

    Looking for same values in two different columns in data.table

  28. 28

    Remove duplicate values appearing in different columns, not in the same order

  29. 29

    Solutions to put different values for a row-key but the same timestamps in hbase?

HotTag

Archive