I have a table like this.
+----+-------+---------+----------+---------+----------+---------+----------+ | id | Month | Debit_A | Credit_A | Debit_B | Credit_B | Debit_C | Credit_C | +----+-------+---------+----------+---------+----------+---------+----------+ | 1 | Jan | 100.50 | | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 2 | Jan | | 100.50 | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 3 | Jan | 150.25 | | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 4 | Jan | | | | | 300.00 | | +----+-------+---------+----------+---------+----------+---------+----------+ | 5 | Jan | | | | | | 300.00 | +----+-------+---------+----------+---------+----------+---------+----------+ | 6 | Feb | | | 79.80 | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 7 | Feb | | | | 79.80 | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 8 | Feb | | | 200.00 | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 9 | Feb | | | | 200.00 | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 10 | Mar | | | | | 1500.00 | | +----+-------+---------+----------+---------+----------+---------+----------+ | 11 | Mar | | | | | | 1500.00 | +----+-------+---------+----------+---------+----------+---------+----------+ | 12 | Apr | 100.00 | | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 13 | Apr | | 50.00 | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 14 | May | | | 50.75 | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 15 | May | | | | 50.70 | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 16 | Jun | | | | | 75.50 | | +----+-------+---------+----------+---------+----------+---------+----------+ | 17 | Jun | | | | | | 75.50 | +----+-------+---------+----------+---------+----------+---------+----------+ | 18 | Jun | | | | | 75.50 | | +----+-------+---------+----------+---------+----------+---------+----------+ | 19 | Jun | | | | | | 75.50 | +----+-------+---------+----------+---------+----------+---------+----------+ | 20 | Jul | 89.50 | | | | | | +----+-------+---------+----------+---------+----------+---------+----------+
What I want is combine the rows that have the same Month value and same Debit/Credit value to a new table. This will combine the Debit and Credit rows into one row. Example:
+----+-------+---------+----------+---------+----------+---------+----------+ | id | Month | Debit_A | Credit_A | Debit_B | Credit_B | Debit_C | Credit_C | +----+-------+---------+----------+---------+----------+---------+----------+ | 1 | Jan | 100.50 | 100.50 | | | 300.00 | 300.00 | +----+-------+---------+----------+---------+----------+---------+----------+ | 2 | Jan | 150.25 | | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 3 | Feb | | | 79.80 | 79.80 | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 4 | Feb | | | 200.00 | 200.00 | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 5 | Mar | | | | | 1500.00 | 1500.00 | +----+-------+---------+----------+---------+----------+---------+----------+ | 6 | Apr | 100.00 | | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 7 | Apr | | 50.00 | | | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 8 | May | | | 50.75 | 50.70 | | | +----+-------+---------+----------+---------+----------+---------+----------+ | 9 | Jun | | | | | 75.50 | 75.50 | +----+-------+---------+----------+---------+----------+---------+----------+ | 10 | Jun | | | | | 75.50 | 75.50 | +----+-------+---------+----------+---------+----------+---------+----------+ | 11 | Jul | 89.50 | | | | | | +----+-------+---------+----------+---------+----------+---------+----------+
How do I do this in SQL Server 2008 or SQL Server 2012?