Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DECLARE @orders TABLE
- (
- cust_id INT,
- ord_id INT,
- ord_date DATE
- )
- DECLARE @cust TABLE
- (
- cust_id INT,
- cust_name VARCHAR(200)
- )
- INSERT INTO @orders VALUES
- (1, 1, '1/1/2018'),
- (1, 2, '1/2/2018'),
- (2, 3, '1/1/2019'),
- (2, 5, '1/5/2019'),
- (3, 7, '6/12/2019'),
- (4, 8, '3/21/2019'),
- (5, 9, '1/14/2019'),
- (5, 12, '5/17/2019'),
- (6, 10, '6/8/2019')
- INSERT INTO @cust VALUES
- (1, 'Joes Bakery'),
- (2, 'Bobs Bread'),
- (3, 'HHA'),
- (4, 'Ma''s donuts'),
- (5, 'Empiric'),
- (6, 'Sally''s sweets')
- SELECT
- o.cust_id,
- c.cust_name,
- COUNT(*) AS order_count
- FROM
- @orders o INNER JOIN
- @cust c ON t.cust_id = o.cust_id
- WHERE
- DATEPART(YEAR, o.ord_date) = 2019
- GROUP BY
- o.cust_id,
- c.cust_name
- HAVING
- COUNT(*) > 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement