Transform columns in lines

I have a table with this fields and i need transform in lines and sum by vat. In some cases the same vat tax could be repeated:


--------------------------------------------------------------------------------------------------
| TX1 | Val1  | Vat1| TX2 | Val2  | Vat2|TX3 | Val3  | Vat3|TX4 | Val4  | Vat4|
--------------------------------------------------------------------------------------------------
| 5% | 10      |0.5   |10%| 15     | 1.5  |16%|10      | 1.6  | 5%| 20     |  1     |


Ex;

       %|  val  | vat

      5%| 30  | 1,5
     10%| 15   |1,5
     16%|  20  | 1
rflorencioAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
for more on this technique see: Unpivotting by CROSS APPLY and VALUES
**[Results]**:
    
    | IDENT |  TX | VAL | VAT |
    |-------|-----|-----|-----|
    |  rec1 |  5% |  10 | 0.5 |
    |  rec1 | 10% |  15 | 1.5 |
    |  rec1 | 16% |  10 | 1.6 |
    |  rec1 |  5% |  20 |   1 |

Open in new window

That result was produced by the following query (the first column is optional):
    CREATE TABLE Table1
    	([TX1] varchar(3), [Val1] int, [Vat1] money, [TX2] varchar(4), [Val2] int, [Vat2] money, [TX3] varchar(4), [Val3] int, [Vat3] money, [TX4] varchar(3), [Val4] int, [Vat4] int)
    ;
    	
    INSERT INTO Table1
    	([TX1], [Val1], [Vat1], [TX2], [Val2], [Vat2], [TX3], [Val3], [Vat3], [TX4], [Val4], [Vat4])
    VALUES
    	('5%', 10, 0.5, '10%', 15, 1.5, '16%', 10, 1.6, '5%', 20, 1)
    ;

**Query 1**:

    SELECT
            'rec1' AS ident
          , ca1.*
    FROM table1
    CROSS apply (
             VALUES
                  (tx1, val1, vat1),
                  (tx2, val2, vat2),
                  (tx3, val3, vat3),
                  (tx4, val4, vat4)
                ) ca1 (tx, val, vat)
    


  [1]: http://sqlfiddle.com/#!3/d1863/1

Open in new window

0
rflorencioAuthor Commented:
Hi PortletPaul,

For ex. Line 1 and line 4 in your example has vat 5%, it´s possible sum this two lines. Because i need vat tax grouped by tax.
The link you provided not work.
0
Éric MoreauSenior .Net ConsultantCommented:
If your data is already in a datatable in your .Net application, I have an article showing how to do the equivalent of a transpose (because it is not natively supported by .net): http://emoreau.com/Entries/Articles/2012/02/ADONet-DataTable-transpose.aspx
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

PortletPaulfreelancerCommented:
Is this what you mean

| IDENT |  TX | VAL | VAT |
|-------|-----|-----|-----|
|  rec1 |  5% |  10 | 0.5 | *
|  rec1 | 16% |  10 | 1.6 | *
          21%         2.1  --<< is this what you want as output (not the * rows)

|  rec1 | 10% |  15 | 1.5 |
|  rec1 |  5% |  20 |   1 |

Open in new window

I simply copied the data you displayed in the question. It would be FAR more accurate if you supplied real data - at the moment you will see that the "5%" is a varchar - it appers it should be a calculation. What is actually stored in the table?

& please provide the expected result from the sample data.

& url fixed, it is:
http://www.experts-exchange.com/blogs/PortletPaul/B_7511-Unpivotting-by-CROSS-APPLY-and-VALUES.html
0
rflorencioAuthor Commented:
Hi PortletPaul,

For ex. Line 1 and line 4 in your example has vat 5%, i need vat and val grouped by tax.


    | IDENT |  TX | VAL | VAT |
    |-------  |----- |----- |-----  |
    |  rec1 |  5% |  10  | 0.5 |  *
    |  rec1 | 10% |  15 | 1.5 |
    |  rec1 | 16% |  10 | 1.6 |
    |  rec1 |  5% |  20  |   1  | *

* Same vat

What i need:

 | IDENT |  TX | VAL | VAT |
    |-------  |----- |----- |-----  |
    |  rec1 |  5% |  30  | 1.5 |  
    |  rec1 | 10% |  15 | 1.5 |
    |  rec1 | 16% |  10 | 1.6 |
0
PortletPaulfreelancerCommented:
Thanks, now I know the target :)
This matches the expected results:
 **[Results][2]**:
    
    |  TX | VAL | VAT |
    |-----|-----|-----|
    |  5% |  30 | 1.5 |
    | 10% |  15 | 1.5 |
    | 16% |  10 | 1.6 |


**Query 1**:

    SELECT
            tx
          , SUM(val) as val
          , SUM(vat) as vat
    FROM (
            SELECT
                    ca1.tx, ca1.val, ca1.vat
            FROM table1
            CROSS apply (
                     VALUES
                          (tx1, val1, vat1),
                          (tx2, val2, vat2),
                          (tx3, val3, vat3),
                          (tx4, val4, vat4)
                        ) ca1 (tx, val, vat)
          ) as derived
    GROUP BY
            tx
    ORDER BY
            right('00000' + tx,5)
    ;

------------------------------------------
   CREATE TABLE Table1
    	([TX1] varchar(3), [Val1] int, [Vat1] money, [TX2] varchar(4), [Val2] int, [Vat2] money, [TX3] varchar(4), [Val3] int, [Vat3] money, [TX4] varchar(3), [Val4] int, [Vat4] int)
    ;
    	
    INSERT INTO Table1
    	([TX1], [Val1], [Vat1], [TX2], [Val2], [Vat2], [TX3], [Val3], [Vat3], [TX4], [Val4], [Vat4])
    VALUES
    	('5%', 10, 0.5, '10%', 15, 1.5, '16%', 10, 1.6, '5%', 20, 1)
    ;

  [1]: http://sqlfiddle.com/#!3/d1863/5

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rflorencioAuthor Commented:
Thanks
0
PortletPaulfreelancerCommented:
Thanks for the grading. Cheers, Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.