Technical Article

Script to generate BCP Statements for a Database

The Script below creates a Stored Procedure that returns a Macro SQL Statement. When this SQL statement is executed, it inturn returns BCP Out Statements for all the tables in a given database. The database name should be passed as a parameter to the procedure. Before the procedure is created, the SA Password must be […]

1 (1)

You rated this post out of 5. Change rating

2002-01-07

3,841 reads

Blogs

T-SQL Tuesday #180: Good enough is perfect

By

How can you achieve good enough without compromising the process/product? In the world of...

How to Convert FileTime to DateTime

By

One of my customers recently wanted to rename each of the SQL audit files...

The pros and cons of self-service BI: What every industry leader should know

By

The post The pros and cons of self-service BI: What every industry leader should...

Read the latest Blogs

Forums

What's New for the Microsoft Data Platform

By Steve Jones - SSC Editor

Comments posted to this topic are about the item What's New for the Microsoft...

Using Outer Joins

By Steve Jones - SSC Editor

Comments posted to this topic are about the item Using Outer Joins

Closest to ProcDate

By boehnc

Not sure I have this 2nd left join correct. I need to grab the...

Visit the forum

Question of the Day

Using Outer Joins

I have this data in a SQL Server 2019 database:

Customer table
CustomerID CustomerName
1          Steve
2          Andy
3          Brian
4          Allen
5          Devin
6          Sally

OrderHeader table
OrderID CustomerID OrderDate
1       1          2024-02-01
2       1          2024-03-01
3       3          2024-04-01
4       4          2024-05-01
6       4          2024-05-01
7       3          2024-06-07
8       2          2024-04-07
I want a list of all customers and their order counts for a period of time, including zero orders. If I run this query, how many rows are returned?
SELECT 
  c.CustomerName, COUNT(oh.OrderID)
 FROM dbo.Customer AS c
LEFT JOIN dbo.OrderHeader AS oh ON oh.CustomerID = c.CustomerID
WHERE oh.Orderdate > '2024/04/01'
GROUP BY c.CustomerName

See possible answers