i’ve really bad problem with this sql query.
base on my tables i need some thing like this result:
table1
Id | Type | Size |Count | OwnerId
___________________________________
1 A 1 12 1
2 A 2 12 1
3 B 1 14 1
4 B 1 20 1
5 A 1 12 2
6 A 1 17 2
table2
Id | name
_________
1 A
2 B
The Result
______________________
Name | Size1Type1 Count | Size2Type1 Count | Size1Type2 Count
thanks indeeeeed .
You did not specify what RDBMS you are using but you should be able to get the result by implementing an aggregate function with a
CASEstatement. This process is similar to aPIVOT:See SQL Fiddle with Demo
Result:
If you want to include your
countfield, then you would use something like this:See SQL Fiddle with Demo
Result:
Or you could even perform multiple joins on the tables to get the result that you want:
See SQL Fiddle with Demo