sql - Inserting results of two sums together to a table -



sql - Inserting results of two sums together to a table -

from previous question, i've managed things work using microsoft sql server , importing excel file new database. now, question dealing writing right sql commands. i'm trying sum same column of table twice using different criteria, , input numbers different table. know how insert sum table, wonder how can insert 2 sums came same column simultaneously (since each time insert, new row created) same row of table.

additional question: way should organize results dependent on values 3rd table? sample info follows.

some sample data:

deptid section 15 eng 16 eng 17 mkt 18 mkt | person | deptid | type | number | +--------+------------+------+---------+ | | 15 | p1 | 1 | | b | 18 | p2 | 5 | | c | 16 | p2 | 10 | | d | 17 | p1 | 7 | | e | 18 | p1 | 11 | | f | 16 | p2 | 12 |

so result should give such:

| section | sum of p1 | sum of p2 | +------------+------------+-----------+ | eng | 1 | 22 | | mkt | 18 | 5 |

what i've tried follows:

select sum(amount) engsump1 sheet1$ section = 'eng' , type = 'p1' select sum(amount) engsump2 sheet1$ section = 'eng' , type = 'p2' select sum(amount) mktsump1 sheet1$ section = 'mkt' , type = 'p1' select sum(amount) mktsump2 sheet1$ section = 'mkt' , type = 'p2'

run 1 time , after see results, perform insert function. i'm wondering if can 2 steps in 1 step.

to sum 'number' column in each group, can utilize sum() function; set case statement within there, can sum cases when type p1, , sum cases when type p2.

to separate departments, utilize grouping clause. seek this:

select department, sum(case when type = 'p1' number else 0 end) totalp1, sum(case when type = 'p2' number else 0 end) totalp2 mytable grouping department;

here sql fiddle example.

sql sum

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -