I have to write a query that lists orders and partitions them by customer id# and displays the date, customerid, name, order_amount and count on each line. I then need to match that against a retailer table to get the retailer id to display as well. The problem is it is a many to many relationship because the retailer ID is on the individual, not the store and the individual can have more than one store. Here is my query:WITH cteAS( SELECT tbl1.Date, tbl1.ID, tbl1.Order_Amount, tbl2.StoreNum, COUNT(tbl1.id) over(partition by tbl1.id) as count FROM tbl1 right join tbl2 ON tbl1.ID = tbl2.ID)SELECT * FROM cteWHERE count > 10 So as I stated above the store owner has a single ID but can have multiple rows with different store numbers. So the owner with multiple stores has each order listed twice each Store number, which of course also throws off my counts and my WHERE clause.I tried creating a variable outside of the CTE that lists the store numbers together and then use that in place of my tbl2.StoreNum column but that gave me an error:[quote]No column name was specified for column 5 of 'CTE'.[/quote]Any suggestions are appreciated.
↧