sql query that shows me all the clients and those that did not buy on a certain date replace with null

1

How can I show all the clients and if they did not buy on that date, change them to null?

Enclosed consultation that shows me only the customers who bought on February 12, 2016

SELECT clienteid ,productoid , fechaorden 
FROM Ventas.Ordenes INNER JOIN Ventas.DetallesOrden
ON Ordenes.ordenid = DetallesOrden.ordenid
Where fechaorden  ='2016-02-12';
    
asked by Felipe Ignacio 05.09.2017 в 07:31
source

2 answers

1

You're posing it the other way around. Make a UPDATE and put null to all those who did not buy on that date:

UPDATE tabla
SET column1 = null
WHERE fechaorden NOT '2016-02-12';

Now you can make your selection to see who bought on that date and the rest will be null .

    
answered by 05.09.2017 в 07:40
0

It is very likely that you need to do the join with clients, probably you have to add a left or right join with clients or change the innerjoin for a leftjoin and force all the clients out, to say that the date can be the date or null you can use coalesce()

link

link

In such a way that coalesce(fechaorden, null) will return or the date of the order or null .

Here is a similar example

SELECT md.new_name,md.new_motivodecisionid,
  coalesce(rm.new_relacionmotivosid,'-1') as new_relacionmotivosid,
  rm.new_motivoeleccion, rm.new_motivorenuncia
FROM new_motivodecision md 
  left join new_relacionmotivos rm on rm.new_motivodecisionid = md.new_motivodecisionid
    and rm.new_opportunityid = 'xxx-xxx-xxx' Where md.statecode = 0

Here I am taking all the decision reasons whether or not they have a relationship with the Oportunity entity, with coalesce() if the entity new_relacionmotivos does not contain a new_motivoeleccionid next to Oportunityid marks the new_relacionmotivosid as '-1' , in your case it would be null

Since I do not know what structure of data you have a possible example of the case that you mention to me, it would be the following

SELECT clienteid ,productoid , coalesce(fechaorden, null)  
FROM Ventas.Ordenes INNER JOIN Ventas.DetallesOrden
ON Ordenes.ordenid = DetallesOrden.ordenid 
left join Ventas.Clientes on Ordenes.clienteid = Clientes.clienteid
Where fechaorden  ='2016-02-12';

or

SELECT clienteid ,productoid , coalesce(fechaorden, null)  
FROM Ventas.Ordenes INNER JOIN Ventas.DetallesOrden
ON Ordenes.ordenid = DetallesOrden.ordenid
Where fechaorden  ='2016-02-12';
    
answered by 05.09.2017 в 09:38