I am fairly new to SQL Server and trying to do something that seems like it ought to be fairly basic, but I must be missing a significant concept.
First, here is the story:
- I have table X which contains line items from orders
- I have table Y which contains the orders themselves
I want to divide the number of line items by the number of orders, thus generating the number of line items on an average order.
I've tried a few options:
I tried joining the two tables together, and then dividing the count() of X by the count of Y. Unfortunately, by X to Y it creates an entry for every Y, so the result of the division is one.
select count(x) / count (y) from x join yI tried making half of the select statement "distinct" by trying to count "distinct" orders but not distinct line items. Turns out you can't make half the select statement be distinct.
select count(x) / distinct count (y) from x join yI tried making a new table, and outputting the sum of the number of orders into the new table. Then I tried to go divide the number of line items by the value of that table. That doesn't work unless I join the tables together, which returns me to the original problem.
select count(x) / temptable from x join temptableI tried looking up how to output a
SQLstatement to a variable so I could then use that variable for the division, but got nowhere on that.I have read diligently through lots of stack overflow posts that hint at this issue but don't actually resolve it.
Any help will be sincerely appreciated!