Today i have faced a problem with pivot element. Actually i want apply inner join of a record set of one table say Q1 with a query after applying pivot on it say Q2.
I know there will be a way out to it but i found a another way to show the result that somehow pivot performs.
Table: Category
Query 1: select * from category
Result1:
CatName AlbumImage
Jazz img1.png
Pop img2.png
Hindi img3.png
Here is the query
Query 2:
SELECT
MIN(CASE catname WHEN 'Jazz' THEN AlbumImage ELSE NULL END) as Jazz,
MIN(CASE catname WHEN 'Pop' THEN AlbumImage ELSE NULL END) as Pop,
MIN(CASE catname WHEN 'Hindi' THEN AlbumImage ELSE NULL END) as Hindi
FROM category
Result 2:
This will show the records like this
Jazz Pop Hindi
img1.png img1.png img1.png
Applying Inner join
Now you can use this Query 2 in some inner join like this.
Query 3:
select q2.jazz,q2.pop,q2,hindi,q1.price from
(
select id, price from table 2
) as q1,
left outer join
(
SELECT id,
MIN(CASE catname WHEN 'Jazz' THEN AlbumImage ELSE NULL END) as Jazz,
MIN(CASE catname WHEN 'Pop' THEN AlbumImage ELSE NULL END) as Pop,
MIN(CASE catname WHEN 'Hindi' THEN AlbumImage ELSE NULL END) as Hindi
FROM category
) as q2
on q1.id=q2.id
I hope this will help you in any way. My emphasis was to show an alternative method which i too found from somewhere else and it help me.
I know there will be a way out to it but i found a another way to show the result that somehow pivot performs.
Table: Category
Query 1: select * from category
Result1:
CatName AlbumImage
Jazz img1.png
Pop img2.png
Hindi img3.png
Here is the query
Query 2:
SELECT
MIN(CASE catname WHEN 'Jazz' THEN AlbumImage ELSE NULL END) as Jazz,
MIN(CASE catname WHEN 'Pop' THEN AlbumImage ELSE NULL END) as Pop,
MIN(CASE catname WHEN 'Hindi' THEN AlbumImage ELSE NULL END) as Hindi
FROM category
Result 2:
This will show the records like this
Jazz Pop Hindi
img1.png img1.png img1.png
Applying Inner join
Now you can use this Query 2 in some inner join like this.
Query 3:
select q2.jazz,q2.pop,q2,hindi,q1.price from
(
select id, price from table 2
) as q1,
left outer join
(
SELECT id,
MIN(CASE catname WHEN 'Jazz' THEN AlbumImage ELSE NULL END) as Jazz,
MIN(CASE catname WHEN 'Pop' THEN AlbumImage ELSE NULL END) as Pop,
MIN(CASE catname WHEN 'Hindi' THEN AlbumImage ELSE NULL END) as Hindi
FROM category
) as q2
on q1.id=q2.id
I hope this will help you in any way. My emphasis was to show an alternative method which i too found from somewhere else and it help me.