>> Let's say I have a Customers table and an Orders table. In the Orders table, I have the customer name. <<
That is a design error. A customer's name is an attribute of a
Customer, not an Order. An order is related to a customer, probably
thru a customer id or account number of some kind. You also failed to
post any DDL or sample data. You used a reserved word, "date" for a
column name -- is it the order_date or another attribute of the
customer? Likewise "amount" is too vague to be useful.
>> I'd like to know every order_id affected by this query [queries do not affect data] so I could make a join on the order_id instead of the customer_name. <<
This should give you the orders placed in that date range by customers
who had 3 or more orders
SELECT order_id
FROM (SELECT customer_id, order_id,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY
order_id)
FROM Orders
WHERE order_date BETWEEN '2008-06-01' AND '2008-07-01')
AS X (customer_id, order_id, order_seq)
WHERE order_seq >= 3;
Aaron Bertrand [SQL Server MVP] - 31 Jul 2008 17:23 GMT
> Customer, not an Order. An order is related to a customer, probably
> thru a customer id or account number of some kind.
Ooh, I like where this is going. Where does the magical "customer id" or
"account number" come from?
A