| Author |
Topic |
|
midavis
Starting Member
23 Posts |
Posted - 2011-03-16 : 08:38:06
|
| I have three tables, TableA, TableB and TableC. TableA holds the primary Ids. TableB holds a relationship back to A, but also stores an amount and a record type. I need to be able to get a sum of all the amounts from TableB including all RecordTypes from C that have not been found in TableB. See the data below.DECLARE @tableA Table ( id int )DECLARE @tableB Table ( id int , id_from_a int , amount decimal(8,2) , RecordType char(1) )DECLARE @tableC Table ( RecordType char(1) )INSERT INTO @tableA Values( 1 ),( 2 ),( 3 )INSERT INTO @tableBValues( 1 , 2 , 12.3, 'G' ),( 1 , 2 , 10.0, 'G' ),( 2 , 1 , 53.0 , 'L' )INSERT INTO @tableCValues( 'L' ), ( 'G' )The result of the joins should be belowid_from_A , id_from_B , amount , RecordType1 , 2 , 22.30 , G1 , 2 , 0.00 , L2 , 1 , 0.00 , G2 , 1 , 53.00 , L3 , 0 , 0.00 , G3 , 0 , 0.00 , L |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-03-16 : 09:37:03
|
[code]DECLARE @tableA Table ( id int )DECLARE @tableB Table ( id int , id_from_a int , amount decimal(8,2) , RecordType char(1) )DECLARE @tableC Table ( RecordType char(1) )INSERT INTO @tableA Values( 1 ),( 2 ),( 3 )INSERT INTO @tableBValues( 1 , 2 , 12.3, 'G' ),( 1 , 2 , 10.0, 'G' ),( 2 , 1 , 53.0 , 'L' )INSERT INTO @tableCValues( 'L' ), ( 'G' )-- Solution hereSELECT a.id AS id_from_A, COALESCE(MAX(b.id_from_A) OVER (PARTITION BY a.id), 0) AS id_from_B, COALESCE(b.Amount, 0) AS amount, c.recordtypeFROM @tableA AS aCROSS JOIN @tableC AS cLEFT JOIN ( SELECT id, id_from_a, SUM(amount) AS Amount, RecordType FROM @tableB GROUP BY id, id_from_a, RecordType ) AS b ON b.id = a.id AND b.RecordType = c.RecordTypeORDER BY a.id, c.RecordType[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
midavis
Starting Member
23 Posts |
Posted - 2011-03-16 : 10:04:27
|
| Ok, that works, but lets take this a step further please. Lets say that TableD now holds the sum of all values possible from a Left Join between TableA and TableB. I now need to combine TableD and TableC to grab all the data from TableC with their missing values. Below is the data structure again. You can see (from the insert into tableD) that if I have no data at all in TableB I default the grouping of data to a RecordType of 'G'.Thanks in advance.DECLARE @tableC Table ( RecordType char(1) )DECLARE @tableD Table ( id int , id_from_a int , amount decimal(8,2) , RecordType char(1) )INSERT INTO @tableCValues( 'L' ), ( 'G' )INSERT INTO @tableDValues( 1 , 2 , 22.3 , 'G' ),( 2 , 1 , 53.0 , 'L' ),( 3 , 0 , 0.0 , 'G' )I should have the same resultid_from_A , id_from_B , amount , RecordType1 , 2 , 22.30 , G1 , 2 , 0.00 , L2 , 1 , 0.00 , G2 , 1 , 53.00 , L3 , 0 , 0.00 , G3 , 0 , 0.00 , L |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-03-16 : 15:35:10
|
Ok, I will hold posting a solution until you reveal ALL possible combinations. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|