Is it possible to calculate this sales report in one query? I'm not so hot with SQL and having trouble wrapping my head around joins considering the all-inclusive where clause.
TABLES (assume 1-N for foreign keys):
TABLE contacts
id
TABLE reservations
id
created_at
contact_id
TABLE reservation_trips
id
reservation_id -- never null
amount
quantity
trip_type_id -- never null
TABLE cost_adjustments
id
reservation_trip_id -- never null
cost_type_id -- never null
TABLE trip_types
id
title
TABLE cost_types
id
title
kind
EXAMPLE WHERE:
reservations.created_at >= '2013-01-01T09:00:00+00:00' AND
reservations.created_at < '2014-01-01T09:00:00+00:00' AND
reservation_trips.trip_type_id in (1, 2, 3, 4) AND
cost_adjustments.trip_type_id in (5, 6, 7, 8) AND
contact_id in (9, 10, 11, 12)
RESULT COLUMNS:
trip_types.title as trip_type_title,
COUNT(DISTINCT reservations.id) as bookings, -- distinct reservations
SUM(reservations.commissions) as commissions, -- distinct reservations
SUM(reservation_trips.id) as guests, -- distinct reservation_trips
SUM(reservation_trips.amount * reservation_trips.quantity) as gross_sales, -- distinct reservation_trips
SUM(cost_adjustments.amount * cost_adjustments.quantity) as adjustments, -- distinct cost_adjustments where cost_types.kind != 'tax'
SUM(gross_sales + adjustments - commissions) as net_sales
The table columns would look like this:
TRIP TYPE | BOOKINGS | GUESTS | GROSS SALES | ADJUSTMENTS | COMMISSIONS | NET SALES
Thanks!
The main problem is to avoid counting the intermediate levels multiple times. One way to do it is do divide by the number of items at each level.
Another approach, which can be more efficient is to put each level in it's own subquery and then join them all up.
Here's an untested stab at the first method. It might need some IsNull(xxx, 0) fixup.
Select
tt.title as trip_type_title,
count(distinct r.id) as bookings,
sum(r.commissions) / count(distinct r.id) as commissions,
count(distinct rt.id) as guests, -- this seems odd, but it's in the example
sum(rt.amount * rt.quantity) / count(distinct rt.id) as gross_sales,
sum(ca.amount * ca.quantity) as adjustments,
sum(rt.amount * rt.quantity) / count(distinct rt.id) + sum(r.commissions) / count(distinct r.id) + sum(ca.amount * ca.quantity) as net_sales
From
trip_types tt
inner Join reservation_trips rt on tt.id = rt.trip_type_id
inner Join reservations r on rt.reservation_id = r.id
left outer join cost_adjustments ca on ca.reservation_trip_id = rt.id
left outer join cost_types ct on ca.cost_type_id = ct.id
Where
r.created_at >= '2013-01-01T09:00:00+00:00' and
r.created_at < '2014-01-01T09:00:00+00:00' and
rt.trip_type_id in (1, 2, 3, 4) and
ca.trip_type_id in (5, 6, 7, 8) and
contact_id in (9, 10, 11, 12) and
ct.kind != 'tax'
Group By
tt.title
Give this a whirl, I think it covers it
SELECT trip_types.title as trip_type_title,
COUNT(DISTINCT reservations.id) as bookings,
SUM(reservations.commissions) as commissions, distinct reservations,
SUM(reservation_trips.id) as guests, distinct reservation_trips,
SUM(reservation_trips.amount * reservation_trips.quantity) as gross_sales,
SUM(cost_adjustments.amount * cost_adjustments.quantity) as adjustments, distinct cost_adjustments where cost_types.kind != 'tax'
SUM(gross_sales + adjustments - commissions) as net_sales
FROM trip_types tt
JOIN reservation_trips on tt.id = reservation_trips.trip_type_id
JOIN reservations on reservation_trips.reservation_id = reservations.id
JOIN cost_adjustments ON cost_adjustments.reservation_trip_id = reservation_trips.id
WHERE
reservations.created_at >= '2013-01-01T09:00:00+00:00' AND
reservations.created_at < '2014-01-01T09:00:00+00:00' AND
reservation_trips.trip_type_id in (1, 2, 3, 4) AND
cost_adjustments.trip_type_id in (5, 6, 7, 8) AND
contact_id in (9, 10, 11, 12)
GROUP BY trip_types.title
I conferred with a friend and figured it out. The trick that I didn't explain very well in the question (sorry) was that the search should include all trips and adjustments for reservations patching the search terms in my example where clause. So you have to use a 'with' query to first find the appropriate reservations, and then filter trips and adjustments based on that. Very tricky.
Here is the answer we found with slightly different fields actually used in production, but operate to the same effect:
with reservations_search as (
select
distinct reservations.id
from
reservations
left outer join contacts on
reservations.contact_id = contacts.id
left outer join reservation_trips on
reservations.id = reservation_trips.reservation_id
left outer join cost_adjustments on
reservation_trips.id = cost_adjustments.cost_target_id
where
#{Reservation.warez_sql(params)[0].join(' AND ')}
)
select
res_level.breeze_id,
res_level.trip_category_id,
res_level.bookings,
res_level.commissions,
res_level.guests,
adjustments_level.adjustments,
res_level.gross_sales + adjustments_level.adjustments - res_level.commissions as net_sales
FROM
(select
breeze_id,
trip_category_id,
sum(trip_commission) as commissions,
count(distinct reservation_id) as bookings,
count(reservation_trip_id) as guests,
sum(gross_sales) as gross_sales
from
-- Commissions by reservation_trips.id
(select
reservations.id as reservation_id,
reservation_trips.id as reservation_trip_id,
reservation_trips.amount * reservation_trips.quantity as gross_sales,
case
when reservations.total_cost_commissionable > 0
then coalesce(reservations.commission, 0) * reservation_trips.total_cost_commissionable / reservations.total_cost_commissionable
else 0
end as trip_commission,
trip_types.breeze_id,
trip_types.trip_category_id
from
reservations
left outer join contacts on
reservations.contact_id = contacts.id
left outer join reservation_trips on
reservations.id = reservation_trips.reservation_id
left outer join trip_types on
reservation_trips.trip_type_id = trip_types.id
where
reservations.id in (select id from reservations_search) ) as trip_level
group by
breeze_id,
trip_category_id) as res_level
-- Adjustments
left outer join
(select
trip_types.breeze_id,
sum(cost_adjustments.amount * cost_adjustments.quantity) as adjustments
from
reservation_trips
left outer join reservations on
reservation_trips.reservation_id = reservations.id
left outer join cost_adjustments on
cost_target_type = 'ReservationTrip' and
reservation_trips.id=cost_adjustments.cost_target_id
left outer join cost_types on
cost_type_id = cost_types.id
left outer join trip_types on
reservation_trips.trip_type_id=trip_types.id
where
reservations.id in (select id from reservations_search) and
cost_types.commissionable IS TRUE
group by
trip_types.breeze_id) as adjustments_level on
res_level.breeze_id = adjustments_level.breeze_id
Related
This SQL query needs to be done in ACCESS.
I am trying to do a subquery on the total sales, but I want to link the sale to the province AND to product. The below query will work with one or the other: (po.product_name = allp.all_products) AND (p.province = allp.all_province); -- but it will no take both.
I will be including every month into this query, once I can figure out the subquery on with two criteria.
Select
p.province as [Province],
po.product_name as [Product],
all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id)
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province
)
as allp
on (po.product_name = allp.all_products) AND (p.province = allp.all_province);
Make the first select sql into a table by giving it an alias and join table 1 to table 2. I don't have your table structure or data to test it but I think this will lead you down the right path:
select table1.*, table2.*
from
(Select
p.province as [Province],
po.product_name as [Product]
--removed this ,all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id) table1
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province --check your group by, I dont think you want pp1.price here if you want to aggregate
) as table2 --changed from allp
on (table1.product = table2.all_products) AND (table1.province = table2.all_province);
I'm trying to get a report for figures from multiple transaction tables. Each table has a foreign key that is a lookup for the day it was taken on, and what location it was taken at called Site_Lookup_Id. It's giving me figures that are much larger than they should be.
#Site_Lookup_Ids dbo.Id_List READONLY
SELECT SL.Site_Lookup_Id, D.[Start],SUM(I.Amount) AS Income,
SUM(P.Amount) AS Payouts,SUM(DP.Amount) AS Deposit
FROM CashUp_Site_Lookup SL
INNER JOIN #Site_Lookup_Ids IDs ON Ids.Id = SL.Site_Lookup_Id
INNER JOIN CashUp_Day D ON SL.Day_Id = D.Day_Id
LEFT JOIN CashUp_Deposit DP ON DP.Redeemed_Site_Lookup_Id = SL.Site_Lookup_Id
AND DP.No_Show != 1
LEFT JOIN CashUp_Income I ON I.Site_Lookup_Id = SL.Site_Lookup_Id
LEFT JOIN CashUp_Payout P ON P.Site_Lookup_Id = SL.Site_Lookup_Id
GROUP BY SL.Site_Lookup_Id, D.[Start]
Not all sums will have a value as some days no transactions for a given table will be taken on the day - In this the value should be zero.
The issue is that running this gives me crazy high values - £7500 for income against one day, when if I do a simple check it's £40 for that day like so.
SELECT SUM(Amount) FROM Cashup_Income WHERE Site_Lookup_Id IN (values...)
Maybe something like...
It really depends on the relationships and when you want the values to be summed.
SELECT SL.Site_Lookup_Id
, D.[Start]
, SUM(I.Amount) over (partition by Key of I table) AS Income
, SUM(P.Amount) over (partition by Key of P table) AS Payouts
, SUM(DP.Amount) over (partition by Key of DP Table) AS Deposit
FROM CashUp_Site_Lookup SL
INNER JOIN #Site_Lookup_Ids IDs ON Ids.Id = SL.Site_Lookup_Id
INNER JOIN CashUp_Day D ON SL.Day_Id = D.Day_Id
LEFT JOIN CashUp_Deposit DP ON DP.Redeemed_Site_Lookup_Id = SL.Site_Lookup_Id
AND DP.No_Show != 1
LEFT JOIN CashUp_Income I ON I.Site_Lookup_Id = SL.Site_Lookup_Id
LEFT JOIN CashUp_Payout P ON P.Site_Lookup_Id = SL.Site_Lookup_Id
GROUP BY SL.Site_Lookup_Id, D.[Start]
The problem stems from the fact that your tables are 1:M Causing values to repeat. These repeated values are then getting added to your sum. The joins cause this issue. So I think you can sum using a partition to eliminate the duplicates or:
Use derived tables or a CTE and sum the values BEFORE you join.
Using CTE's (Common Table Expressions)
WITH DP AS (SELECT sum(Amount) As Deposit
, Redeemed_Site_Lookup_ID
FROM CashUp_Deposit
WHERE No_Show !=1
GROUP BY Redeemed_Site_Lookup_ID),
I AS (SELECT sum(Amount) as Income
, Site_Lookup_Id
FROM CashUp_Income
GROUP BY Site_Lookup_Id),
P AS (SELECT sum(Amount) as Payouts
, Site_Lookup_Id
FROM CashUp_Payout
GROUP BY Site_Lookup_Id)
SELECT SL.Site_Lookup_Id
, D.[Start]
, Income
, Payouts
, Deposit
FROM CashUp_Site_Lookup SL
INNER JOIN #Site_Lookup_Ids IDs
ON Ids.Id = SL.Site_Lookup_Id
INNER JOIN CashUp_Day D
ON SL.Day_Id = D.Day_Id
LEFT JOIN DP
ON DP.Redeemed_Site_Lookup_Id = SL.Site_Lookup_Id
LEFT JOIN I
ON I.Site_Lookup_Id = SL.Site_Lookup_Id
LEFT JOIN P
ON P.Site_Lookup_Id = SL.Site_Lookup_Id
Presumably, you are generating a Cartesian product with your joins. Because you have no filtering, do the aggregation before the joins:
LEFT JOIN
(SELECT DP.Redeemed_Site_Lookup_Id, SUM(DP.Amount) AS Deposit
FROM CashUp_Deposit DP
WHERE DP.No_Show != 1
GROUP BY DP.Redeemed_Site_Lookup_Id
) DP
ON DP.Redeemed_Site_Lookup_Id = SL.Site_Lookup_Id LEFT JOIN
(SELECT I.Site_Lookup_Id, SUM(I.Amount) AS Income
FROM CashUp_Income I
GROUP BY I.Site_Lookup_Id
) I
ON I.Site_Lookup_Id = SL.Site_Lookup_Id LEFT JOIN
(SELECT P.Site_Lookup_Id, SUM(P.Amount) AS Payout
FROM CashUp_Payout P
GROUP BY I.Site_Lookup_Id
) P
ON P.Site_Lookup_Id = SL.Site_Lookup_Id
Then adjust the rest of your query to remove the GROUP BY and SUM()s.
I have a query that selects customers from a table CustomerDetails, and left joins onto another table (CustomerActivity) to get their last login time and finally left joins onto another table (OpenOrderDetails) to get their last open order (if applicable). I also have a big WHERE clause filtering this data
A customer can only have one record in the OpenOrderDetails table at anytime. My query looks like the following:
SELECT CD.*, H.LastCustomerLoginTime, OD.OrderFiledDate, OD.OrderCompletedDate
FROM CustomerDetails CD
LEFT JOIN CustomerActivity H ON H.CustomerID = CD.CustomerID
LEFT JOIN OpenOrderDetails OD ON CD.CustomerID = OD.CustomerID
WHERE CD.OrderStatus IN (1,2,3)
AND (CustomerType = 1 or (CustomerType = 3 and CustomerActive IN (1,2)))
AND (OD.OrderFiledDate IS NULL OR CD.TimeStamp >= OD.OrderFiledDate)
AND (OD.OrderCompletedDate IS NULL OR CD.TimeStamp <= OD.OrderCompletedDate)
My issue is that this query only returns customer records that have a record in the OpenOrderDetails table. How do I return every customer, and OrderFiledDate/OrderCompletedDate if present, and NULL if a record for that customer does not exist in the OpenOrderDetails table?
When doing a LEFT JOIN, referencing ANY of the right side table columns in the WHERE clause will turn it into an INNER JOIN. To get around this, simply remove any and all predicates that reference "right side" columns from the WHERE clause and move them to the LEFT JOIN condition.
Something along these lines...
SELECT
CD.*,
H.LastCustomerLoginTime,
OD.OrderFiledDate,
OD.OrderCompletedDate
FROM
CustomerDetails CD
LEFT JOIN CustomerActivity H
ON H.CustomerID = CD.CustomerID
LEFT JOIN OpenOrderDetails OD
ON CD.CustomerID = DLECS.CustomerID
AND ( OD.OrderFiledDate IS NULL
OR CD.TimeStamp >= OD.OrderFiledDate
)
AND ( OD.OrderCompletedDate IS NULL
OR CD.TimeStamp <= OD.OrderCompletedDate
)
WHERE
CD.OrderStatus IN ( 1, 2, 3 )
AND (
CustomerType = 1
OR
(
CustomerType = 3
AND CustomerActive IN ( 1, 2 )
)
);
Just move some OpenOrderDetails conditions to join clause
SELECT CD.*, H.LastCustomerLoginTime, OD.OrderFiledDate, OD.OrderCompletedDate
FROM CustomerDetails CD
LEFT JOIN CustomerActivity H ON H.CustomerID = CD.CustomerID
LEFT JOIN OpenOrderDetails OD ON CD.CustomerID = DLECS.CustomerID
AND (OD.OrderFiledDate IS NULL OR CD.TimeStamp >= OD.OrderFiledDate)
AND (OD.OrderCompletedDate IS NULL OR CD.TimeStamp <= OD.OrderCompletedDate)
WHERE CD.OrderStatus IN (1,2,3)
AND (CustomerType = 1 or (CustomerType = 3 and CustomerActive IN (1,2)))
Sigh ... can anyone help? In the SQL query below, the results I get are incorrect. There are three (3) labor records in [LaborDetail]
Hours / Cost
2.75 / 50.88
2.00 / 74.00
1.25 / 34.69
There are two (2) material records in [WorkOrderInventory]
Material Cost
42.75
35.94
The issue is that the query incorrectly returns the following:
sFunction cntWO sumLaborHours sumLaborCost sumMaterialCost
ROBOT HARNESS 1 12 319.14 236.07
What am I doing wrong in the query that is causing the sums to be multiplied? The correct values are sumLaborHours = 6, sumLaborCost = 159.57, and sumMaterialCost = 78.69. Thank you for your help.
SELECT CASE WHEN COALESCE(work_orders.location, Work_Orders_Archived.location) IS NULL
THEN '' ELSE COALESCE(work_orders.location, Work_Orders_Archived.location) END AS sFunction,
(SELECT COUNT(*)
FROM work_orders
FULL OUTER JOIN Work_Orders_Archived
ON work_orders.order_number = Work_Orders_Archived.order_number
WHERE COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = '919630') AS cntWO,
SUM(Laborhours) AS sumLaborHours,
SUM(LaborCost) AS sumLaborCost,
SUM(MaterialCost*MaterialQuanity) AS sumMaterialCost
FROM work_orders
FULL OUTER JOIN Work_Orders_Archived
ON work_orders.order_number = Work_Orders_Archived.order_number
LEFT OUTER JOIN
(SELECT HoursWorked AS Laborhours, TotalDollars AS LaborCost, WorkOrderNo
FROM LaborDetail) AS LD
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = LD.WorkOrderNo
LEFT OUTER JOIN
(SELECT UnitCost AS MaterialCost, Qty AS MaterialQuanity, OrderNumber
FROM WorkOrderInventory) AS WOI
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = WOI.OrderNumber
WHERE COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = '919630'
GROUP BY CASE WHEN COALESCE(work_orders.location, Work_Orders_Archived.location) IS NULL
THEN '' ELSE COALESCE(work_orders.location, Work_Orders_Archived.location) END
ORDER BY sFunction
Try using the SUM function inside a derived table subquery when doing the full join to "WorkOrderInventory" like so...
select
...
sum(hrs) as sumlaborhrs,
sum(cost) as sumlaborcost,
-- calculate material cost in subquery
summaterialcost
from labordetail a
full outer join
(select ordernumber, sum(materialcost) as summaterialcost
from WorkOrderInventory
group by ordernumber
) b on a.workorderno = b.ordernumber
i created a simple sql fiddle to demonstrate this (i simplified your query for examples sake)
Looks to me that work_orders and work_orders_archived contains the same thing and you need both tables as if they were one table. So you could instead of joining create a UNION and use it as if it was one table:
select location as sfunction
from
(select location
from work_orders
union location
from work_orders_archived)
Then you use it to join the rest. What DBMS are you on? You could use WITH. But this does not exist on MYSQL.
with wo as
(select location as sfunction, order_number
from work_orders
union location, order_number
from work_orders_archived)
select sfunction,
count(*)
SUM(Laborhours) AS sumLaborHours,
SUM(LaborCost) AS sumLaborCost,
SUM(MaterialCost*MaterialQuanity) AS sumMaterialCost
from wo
LEFT OUTER JOIN
(SELECT HoursWorked AS Laborhours, TotalDollars AS LaborCost, WorkOrderNo
FROM LaborDetail) AS LD
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = LD.WorkOrderNo
LEFT OUTER JOIN
(SELECT UnitCost AS MaterialCost, Qty AS MaterialQuanity, OrderNumber
FROM WorkOrderInventory) AS WOI
ON COALESCE(work_orders.order_number, Work_Orders_Archived.order_number) = WOI.OrderNumber
where wo.order_number = '919630'
group by sfunction
order by sfunction
The best guess is that the work orders appear more than once in one of the tables. Try these queries to check for duplicates in the two most obvious candidate tables:
select cnt, COUNT(*), MIN(order_number), MAX(order_number)
from (select order_number, COUNT(*) as cnt
from work_orders
group by order_number
) t
group by cnt
order by 1;
select cnt, COUNT(*), MIN(order_number), MAX(order_number)
from (select order_number, COUNT(*) as cnt
from work_orders_archived
group by order_number
) t
group by cnt
order by 1;
If either returns a row where cnt is not 1, then you have duplicates in the tables.
I have to create an association between 2 products, which has unique product_ids and insert them into an already constructed table. The association is created based on unique part number these product ids have. For instance:
Product_id = 7578711
Part Number = 0101-2478
Product Id = 7957948
Part Number = 0101-2478
Product Id = 10558140
Part Number = 0101-2478
and my current table has the following columns:
ID (int) identity (1, 1)
product_id
date
guid
where data is in the form of:
1, 7578711, 12345, 2010-08-24 04:29:04.000,00286AFB-3880-4085-BAA0-DBCC0D59A391
I have a query which has the ability to roll Product_id to part number level and then a query to roll the part number to product_id level.
Based on the above data, where they have same part number, i want to create an association and generate insert statements which will add 2 records in the form of:
2, 7957948, 12345, 2010-08-24 04:29:04.000,00286AFB-3880-4085-BAA0-DBCC0D59A391
3, 10558140, 12345, 2010-08-24 04:29:04.000,00286AFB-3880-4085-BAA0-DBCC0D59A391
There are going to be many product IDs in that table. The above one is just an example:
I have 2 Common Table Expressions: 1 rolls the product Id to part number level, and another rolls back the part number to multiple product Ids. I am trying to avoid a cursor.
Could anyone here help me with this problem?
My 2 CTEs as as follows:
;WITH cte (product_id, item_number)
AS
(
SELECT DISTINCT --TOP 1000
pds.product_id
--,pd.productOwner_id
, i.item_number
FROM SurfwatcherEndeavorStats.dbo.productDetailBySite pds WITH ( NOLOCK )
INNER JOIN ProductData.dbo.productDimensions pd with ( NOLOCK ) ON pds.product_id = pd.product_id
INNER JOIN ProductData.dbo.options o with ( NOLOCK ) ON pds.product_id = o.product_id
INNER JOIN ProductData.dbo.items i with ( NOLOCK ) ON o.option_id = i.item_id
WHERE pds.productDetail_date > DATEADD(yyyy, -1, GETDATE())
AND i.item_number IS NOT NULL
--AND i.item_number = '0101-3258'
)
SELECT TOP 1 item_number
FROM cte WITH (NOLOCK)
WHERE product_id = 7957948
;WITH cte1 (product_id, item_number)
AS
(
SELECT DISTINCT --TOP 1000
pds.product_id
--,pd.productOwner_id
, i.item_number
FROM SurfwatcherEndeavorStats.dbo.productDetailBySite pds WITH ( NOLOCK )
INNER JOIN ProductData.dbo.productDimensions pd with ( NOLOCK ) ON pds.product_id = pd.product_id
INNER JOIN ProductData.dbo.options o with ( NOLOCK ) ON pds.product_id = o.product_id
INNER JOIN ProductData.dbo.items i with ( NOLOCK ) ON o.option_id = i.item_id
WHERE pds.productDetail_date > DATEADD(yyyy, -1, GETDATE())
AND i.item_number IS NOT NULL
)
SELECT product_id
FROM cte1 WITH (NOLOCK)
WHERE item_number = '0101-2478'
try this Sql. it should give you a complete list of all associations between two products that both use the same part number... Is that what you want ?
Select Distinct A.Product_Id, B.Product_ID
From YourTable A
Join YourTable B
On B.PartNumber = A.PartNumber
And B.Product_Id > A.Product_Id