Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

Conditional Join of tables based on joined table condition

$
0
0

I have 2 tables to join

Sales

Customer

Sales table has customer id, customer billing id and product/sale price/qty etc info.

Customer table has customer id, customer billing id and buying group info

What I want to do is return sales summary by customer billing info unless the billing account is marked as a buying group - then I want it by customer ID

so something like

Select customer code, customer name, sum(sales)

 from sales

  left join customer on sales.billing id = customer id unless customer buying group = 1 then make the join sales customer id = customer id

I can't quite get my head around how to do that (it is friday afternoon and it's hot.)


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>