I have a table called supplier_rating
. This table is used to store the rating and the no_of_stars received for each supplier. Rating can range from 1-5. A supplier can have an entry for all the ratings or they can some of the ratings. I want to create a SELECT statement that will return all the rating for supplier.
Code snippets
CREATE TABLE dbo.supplier_rating(
supplier_id INT
,star INT
,no_of_stars INT)
INSERT INTO dbo.supplier_rating VALUES (100,5,50),(100,3,30),(200,2,20),(200,1,10);
I want a select statement that will return the resultset as the below for supplier_id = 100
supplier_id rating no_of_stars
100 5 50
100 4 0
100 3 30
100 2 0
100 1 0
Below is the query I am using. I am creating temp table that will have all the rating from 1-5 and I am doing LEFT JOIN with supplier_rating
table , but it is returning the resultset I want
CREATE TABLE #temp_rating(star INT);
INSERT INTO #temp_rating VALUES (1),(2),(3),(4),(5);
SELECT r.star,sr.no_of_stars
FROM #temp_rating r
LEFT JOIN dbo.supplier_rating sr ON r.star = sr.star
WHERE sr.supplier_id = 100;