Can anyone help with the following SQL query? I need to pull Total Sales per server for one Category of products called BAR. Second column should times Total Sales by 10%.
BAR Server Sales Totals:
Server name: Total BAR Sales x10%
Joe $100.00 $10.00
Susan $200.00 $20.00
Jackie $300.00 $30.00
select
O.CreatingUserName as Server,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity) as Money),1) Total,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity*0.1)as Money),1) Share
from Orders as O
inner join MenuItems as MI on MI.Id = O.MenuItemId
where MI.GroupCode = 'Bar'
group by O.CreatingUserName
I don’t have suitable data atm for testing but I’m sure you’ll be able to correct that.
The query does not seem to produce the right results.
Query:
select
O.CreatingUserName as Server,
Convert(nvarchar,Cast(Sum(O.PriceO.Quantity) as Money),1) Total,
Convert(nvarchar,Cast(Sum(O.PriceO.Quantity*0.1)as Money),1) Share
from Orders as O
inner join MenuItems as MI on MI.Id = O.MenuItemId
where
MI.GroupCode = ‘Beers’
or MI.GroupCode = ‘Red Wine’
or MI.GroupCode = ‘White Wine’
or MI.GroupCode = ‘Brandy’
or MI.GroupCode = ‘Gin’
or MI.GroupCode = ‘Rum’
or MI.GroupCode = ‘Scotch’
or MI.GroupCode = ‘Tequila’
or MI.GroupCode = ‘Vodka’
or MI.GroupCode = ‘Whiskey’
or MI.GroupCode = ‘Cordials’
or MI.GroupCode = ‘Mixed Drinks’
or MI.GroupCode = ‘Open Bar’
group by O.CreatingUserName
That produces whole results. I think you’re expected to see it for a single work period so you should also include date filter in where clause.
select
O.CreatingUserName as Server,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity) as Money),1) Total,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity*0.1)as Money),1) Share
from Orders as O
inner join MenuItems as MI on MI.Id = O.MenuItemId
where MI.GroupCode = 'Bar' and CreatedDateTime > '{Start}' and CreatedDateTime < '{End}'
group by O.CreatingUserName
Also you can exclude voids
select
O.CreatingUserName as Server,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity) as Money),1) Total,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity*0.1)as Money),1) Share
from Orders as O
inner join MenuItems as MI on MI.Id = O.MenuItemId
where MI.GroupCode = 'Bar' and CreatedDateTime > '{Start}' and CreatedDateTime < '{End}' and DecreaseInventory = 1
group by O.CreatingUserName
User Sales is the WPR right? It is not a custom report… correct? You are selecting Just the Work Period Date in the date selecting at top right? Also User Sales from WPR is picking up ALL Menu items… are those items in the script the ONLY items those servers sold?
If they rang up food or anything not in your script… it will be reflected on User Sales from WPR but it wont show in your script because you told it exactly what to calculate for sales.
The ONLY way User Sales from WPR and your script will match exactly… is if those items in your script are the ONLY items each user sells.
I have feeling they sell more than just those drinks listed in your script
EDIT: You wanted Tips to be calculated off Bar sales only right? If so your script is working just fine.
You would not compare it to User Sales from WPR that would not be accurate.
You could compare it and subtract the Sales from your Bar report from the WPR User Sales and do some math to get your % of Bar sales vs Total sales.
To sum it up… it looks like your script is running as you intended it… you are just reading the WPR:User Sales wrong
OK I still believe we have something to fix in SQL Script. If you need to check multiple categories with or you should use brackets to define scope for or.
For example
Where PC = 'X' AND DATE > Y
means Product code should be x AND date should be greater than Y.
Now I want to check if PC = X or PC = Z
Where PC = 'X' OR PC = 'Y' AND DATE > Z
That will make Date Constraint useless. We should use parenthesis to define a priority. So it should be
Where (PC = 'X' OR PC = 'Y') AND DATE > Z
So we should add multiple groups in brackets.
select
O.CreatingUserName as Server,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity) as Money),1) Total,
Convert(nvarchar,Cast(Sum(O.Price*O.Quantity*0.1)as Money),1) Share
from Orders as O
inner join MenuItems as MI on MI.Id = O.MenuItemId
where
(MI.GroupCode = 'Bar' OR MI.GroupCode = 'Open Bar')
and CreatedDateTime > '{Start}' and CreatedDateTime < '{End}'
and DecreaseInventory = 1
group by O.CreatingUserName