Skip to content
Advertisement

SQL Server pivot query – questions

I have a table with 3 columns: order_id, product_id, product_count

The first column is an order passed by a client, the second is the product unique id and the third is the quantity of a product bought in an order.

I want to create a matrix of order_id / product_id with number of items bought.

As a result I would like to have something that looks like this:

enter image description here

If I make this request:

My issue is that I have more than 200 different products to retrieve. Is there a way to make it without entering all values?

Advertisement

Answer

Based on @seanb answer that saved me, I tried to replace the NULL values with 0. I understood the principle (the base). Here is how I updated the SQL request to replace the NULL values.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement