S
S
Sergey2021-10-28 10:32:59
SQL
Sergey, 2021-10-28 10:32:59

How to make a left join if the second table has a condition that removes positions from the first table?

There are 2 tables. Let's say the first table with articles. There is a second table with articles/sales/date. The bottom line is that I need to pull up the amount of sales from the second table from a certain date to the first table.

If I do a left join without the date constraint condition on the second table. That all works well. If there were sales, he shows them. If there is no article in the second table, then it returns Null. All as I need. But if I put a condition that in the second table date>. Then the articles that are in the first table are not shown. in the second table there is a condition date>. How can I make a query so that all the articles from the first table remain. And the sales were summed up from a certain date.

SELECT t1.art,  sum(t2.sales) FROM table1 as t1 
LEFT JOIN table2 as t2
ON t1.art = t2.art
WHERE t2.date > CONVERT(DATE,'01.01.2021')
GROUP BY t1.art


And so articles which are not present in table 2 but are in table 1 disappear from selection. If to remove a condition > dates. Then the articles will be and the amount of sales will be NULL. How to make it so that it was the same but with a condition?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ipatiev, 2021-10-28
@googlgan

Write the desired condition in ON

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question