0

I'm running around in circles and have been doing so in the last hours.

I'm doing a select in a table where have all stock of each store and I'm filtering by product id. What i need is: have a list of all stores even if I have no data for that store, but the select only returns 4 stores or less.

Here's an example:

That's the query:

select 
    store_id, product_id, start_date, quantity 
from 
    stock 
where 
    product_id = 407214

That's the result:

store_id | product_id |       start_date      | quantity |
    2    |   407214   |  2015-05-26 08:32:53  |    10    |
    3    |   407214   |  2015-03-16 12:10:00  |    25    |
    4    |   407214   |  2015-01-06 11:45:15  |    16    |
    7    |   407214   |  2015-05-14 00:00:00  |    8     |

And that's what I want:

store_id | product_id |       start_date      | quantity |
    1    |    NULL    |          NULL         |   NULL   |
    2    |   407214   |  2015-05-26 08:32:53  |    10    |
    3    |   407214   |  2015-03-16 12:10:00  |    25    |
    4    |   407214   |  2015-01-06 11:45:15  |    16    |
    5    |    NULL    |          NULL         |   NULL   |
    6    |    NULL    |          NULL         |   NULL   |
    7    |   407214   |  2015-05-14 00:00:00  |    8     |

I would really need a help, it's driving me crazy!!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

5 Answers5

5

the solution depends on your db structure

if stock table only contains available products, you need left join of tables stores and stock

select 
  s.id as store_id, 
  st.product_id, 
  st.start_date, 
  st.quantity 
from 
stores s
left join stock st on s.Id = st.store_id and st.product_id = 407214
ASh
  • 34,632
  • 9
  • 60
  • 82
3

Add OR product_id IS NULL to the WHERE clause

TTeeple
  • 2,913
  • 1
  • 13
  • 22
0

Is null will return the rows where there is no value. The or in the where clause is for condition. If the specific row means one of these conditions.

select store_id, product_id, start_date, quantity 
     from stock where 
          product_id = 407214 OR Product_id iS null

Or your null is a string ?

 select store_id, product_id, start_date, quantity 
     from stock where 
          product_id = 407214 OR Product_id like null

Or maybe you are inputting product_id as null you can try this:

select store_id, product_id, start_date, quantity 
    from stock where 
       product_id = 407214 OR Product_id = null

Or maybe if you want everything, remove the where clause but you order by product_id: (maybe this is what you mean)

select store_id, product_id, start_date, quantity from stock order by product_id
Coding Enthusiast
  • 3,865
  • 1
  • 27
  • 50
  • ...I would never expect this to work, because you're not supposed to use null values in keys. The design of the table runs counter to data (probably) being stored this way. – Clockwork-Muse May 28 '15 at 14:04
0
select store_id, product_id, start_date, quantity from stock where product_id = 407214 or product_id = null
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Dhru 'soni
  • 1,024
  • 7
  • 23
  • ...I would never expect this to work, because you're not supposed to use null values in keys. The design of the table runs counter to data (probably) being stored this way. – Clockwork-Muse May 28 '15 at 14:08
0

Knowing how to generate fixed number of rows in a table you can:

--select 5 as store_id, 407214 as product_id, 3 as start_date, 3 as quantity into stock

DECLARE @maxStoreId int
SELECT 
    @maxStoreId = MAX(store_id) 
FROM 
    stock;

WITH r AS (
    SELECT 1 AS n
    UNION ALL
    SELECT n+1 FROM r WHERE n+1<=@maxStoreId
)
SELECT 
    r.n as store_id, product_id, start_date, quantity  
FROM 
    r left outer join stock on  r.n = stock.store_id
WHERE 
    product_id = 407214 or product_id is null

--drop table stock
Community
  • 1
  • 1
sbiz
  • 321
  • 3
  • 10