SUM all prices from table SQL - sql

i have following SQL query which is summing tourist tax from selected period.
SELECT
BTR_DESCRIPTION AS TOURIST_TAX_NAME,
ISNULL(BREAG_TOURIST_TAX_PRICE,0) AS PRICE,
SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO)) AS QUANTITY,
ISNULL(BREAG_TOURIST_TAX_PRICE,0) * SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO)) AS FINAL_PRICE
FROM BOS_RESERVATION
LEFT OUTER JOIN BOS_RESADDGUEST ON BREAG_BRE_ID = BRE_ID
LEFT OUTER JOIN BOS_TAX_REASONS ON BTR_ID = BREAG_BTR_ID
WHERE BREAG_DATEFROM >= '2018-02-28' AND BREAG_DATETO <= '2018-03-31'
GROUP BY BTR_DESCRIPTION, BREAG_TOURIST_TAX_PRICE
Query result:
Now i want to sum all values from FINAL_PRICE row. So the result must be: 5,652
But i dont know how to do this. I tried with following SQL:
SUM(ISNULL(BREAG_TOURIST_TAX_PRICE,0) * SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO))) AS FINAL_PRICE_SUM
But this gives me error: Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Can you please help me how to sum all values from FINAL_PRICE row?
Thanks!

You can do it like this:
WITH AggregatedTable as (SELECT
BTR_DESCRIPTION AS TOURIST_TAX_NAME,
ISNULL(BREAG_TOURIST_TAX_PRICE,0) AS PRICE,
SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO)) AS QUANTITY,
ISNULL(BREAG_TOURIST_TAX_PRICE,0) * SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO)) AS FINAL_PRICE
FROM BOS_RESERVATION
LEFT OUTER JOIN BOS_RESADDGUEST ON BREAG_BRE_ID = BRE_ID
LEFT OUTER JOIN BOS_TAX_REASONS ON BTR_ID = BREAG_BTR_ID
WHERE BREAG_DATEFROM >= '2018-02-28' AND BREAG_DATETO <= '2018-03-31'
GROUP BY BTR_DESCRIPTION, BREAG_TOURIST_TAX_PRICE)
Select at.*,
(SELECT SUM(FINAL_PRICE) from AggregatedTable)) as Total_FP
from AggregatedTable AS at

If you want the total final price, use a CTE or subquery:
WITH r as (
SELECT BTR_DESCRIPTION AS TOURIST_TAX_NAME,
COALESCE(BREAG_TOURIST_TAX_PRICE, 0) AS PRICE,
SUM(DATEDIFF(day, BRE_DATEFROM, BRE_DATETO)) AS QUANTITY,
COALESCE(BREAG_TOURIST_TAX_PRICE, 0) * SUM(DATEDIFF(day, BRE_DATEFROM, BRE_DATETO)) AS FINAL_PRICE
FROM BOS_RESERVATION LEFT OUTER JOIN
BOS_RESADDGUEST
ON BREAG_BRE_ID = BRE_ID LEFT OUTER JOIN
BOS_TAX_REASONS
ON BTR_ID = BREAG_BTR_ID
WHERE BREAG_DATEFROM >= '2018-02-28' AND BREAG_DATETO <= '2018-03-31'
GROUP BY BTR_DESCRIPTION, BREAG_TOURIST_TAX_PRICE
)
SELECT SUM(FINAL_PRICE)
FROM r;

Related

Invalid subquery, trying to get the customer ID that goes along with the invoice

Here is my SQL, I am trying to get the DISTINCT item ids that have not been invoiced in the past two years that also have a qty on hand greater than 0. I am having an issue getting the last customer id that was invoiced for each distinct item id.
SELECT DISTINCT
im.item_id,
MAX(il.date_created),
(SELECT TOP 1 customer_id
FROM invoice_hdr ih2
WHERE ih.invoice_no = ih2.invoice_no) AS customer_id,
inv_loc.qty_on_hand,
ih.sales_location_id,
MAX(il.commission_cost)
FROM
invoice_hdr ih
INNER JOIN
invoice_line il ON ih.invoice_no = il.invoice_no
INNER JOIN
inv_mast im ON il.inv_mast_uid = im.inv_mast_uid
INNER JOIN
inv_loc ON im.inv_mast_uid = inv_loc.inv_mast_uid
WHERE
inv_loc.qty_on_hand > '0'
GROUP BY
im.item_id, inv_loc.qty_on_hand, ih.sales_location_id
HAVING
(MAX(il.date_created) < DATEADD(year, -2, GETDATE()))
I get this error:
Column 'invoice_hdr.invoice_no' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I understand the error, but no matter what I try I cannot get it to work.
I was able to get the correct data with the following query:
SELECT DISTINCT il.item_id,
ih.customer_id,
il.customer_part_number,
il.date_created,
SUM(inv_loc.qty_on_hand) AS [Qty On Hand],
ih.sales_location_id,
MAX(il.commission_cost) AS [Max Commission Cost]
FROM invoice_hdr ih
JOIN invoice_line il ON ih.invoice_no = il.invoice_no
JOIN inv_loc ON il.inv_mast_uid = inv_loc.inv_mast_uid
WHERE (SELECT TOP 1 il2.date_created FROM invoice_line il2 WHERE il2.item_id = il.item_id ORDER BY il2.date_created DESC) < DATEADD(YEAR, -2, GETDATE())
AND (SELECT TOP 1 il2.invoice_no FROM invoice_line il2 WHERE il2.item_id = il.item_id ORDER BY il2.date_created DESC) = ih.invoice_no
GROUP BY il.item_id,
ih.customer_id,
il.date_created,
ih.sales_location_id,
customer_part_number
HAVING SUM(inv_loc.qty_on_hand) > 0
ORDER BY il.item_id

Easy Left Join SQL Syntax

New to SQL and want to complete a LEFT JOIN.
I have two seperate tables with the below code:
SELECT
StockCode, SalesOrder, SUM(OrderQty)
FROM
dbo.IopSalesPerf
WHERE
dbo.IopSalesPerf.CustRequestDate BETWEEN '2017-07-01' AND '2017-07-31'
AND EntrySystemTime = 1
AND Warehouse = '01'
AND StockCode = '001013'
GROUP BY
StockCode,SalesOrder
ORDER BY
StockCode ASC
SELECT
SalesOrder, SUM(NetSalesValue), SUM(QtyInvoiced)
FROM
ArTrnDetail
GROUP BY
SalesOrder
I would like to LEFT JOIN the last table onto the first using SalesOrder as the joining column. Can anyone assist with the syntax?
Simpliest way would be:
SELECT * FROM
(
SELECT StockCode,SalesOrder,sum(OrderQty)
FROM dbo.IopSalesPerf
WHERE dbo.IopSalesPerf.CustRequestDate between '2017-07-01' and '2017-07-31'
and EntrySystemTime = 1 and Warehouse = '01' and StockCode = '001013'
GROUP BY StockCode,SalesOrder
Order BY StockCode ASc
) AS A
LEFT JOIN
(
SELECT SalesOrder,sum(NetSalesValue),sum(QtyInvoiced)
FROM ArTrnDetail
Group by SalesOrder
) AS B
ON A.SalesOrder = B.SalesOrder

Item rows duplicate and SUM for Total Expense

When the below query is executed the "TotalExpense" column duplicates to the number of rows that are present in the tblOrderProduct for a given SalesID.
SELECT ItemSales.OrderDate,tblExpense.DateOfExpense ,(Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice))) AS TotalSales, (Nz(SUM(tblExpense.Expense))) AS TotalExpense , (Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice)) - Nz(SUM(tblExpense.Expense)))AS Profit
FROM (ItemSales
LEFT JOIN tblOrderProduct ON ItemSales.SalesID = tblOrderProduct.SalesID)
LEFT JOIN tblExpense ON itemSales.OrderDate = tblExpense.DateOfExpense
GROUP BY ItemSales.OrderDate, tblExpense.DateOfExpense
ORDER BY ItemSales.OrderDate
UNION SELECT ItemSales.OrderDate, tblExpense.DateOfExpense, (Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice))) AS TotalSales, (Nz(SUM(tblExpense.Expense))) AS TotalExpense ,(Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice)) - Nz(SUM(tblExpense.Expense)))AS Profit
FROM (ItemSales
RIGHT JOIN tblExpense ON ItemSales.OrderDate = tblExpense.DateOfExpense)
LEFT JOIN tblOrderProduct ON ItemSales.SalesID = tblOrderProduct.SalesID
GROUP BY ItemSales.OrderDate,tblExpense.DateOfExpense
ORDER BY ItemSales.OrderDate
EDIT :-
Please see below for the result I get from the executed query:
The value highlighted in red only has expense worth 1000 for the given date, How ever the sales related to the same day has 22 rows of data hence the result is showing 22000
What is causing this duplication?
SELECT ItemSales.OrderDate,tblExpense.DateOfExpense ,(Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice))) AS TotalSales, (Nz(SUM(tblExpense.Expense))) AS TotalExpense , (Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice)) - Nz(SUM(tblExpense.Expense)))AS Profit
FROM (ItemSales
LEFT JOIN tblOrderProduct ON ItemSales.SalesID = tblOrderProduct.SalesID)
LEFT JOIN
( select DateOfExpense,sum(Expense) AS Expense FROM tblExpense Group by DateOfExpense)
tblExpense ON itemSales.OrderDate = tblExpense.DateOfExpense
GROUP BY ItemSales.OrderDate, tblExpense.DateOfExpense
ORDER BY ItemSales.OrderDate
UNION
SELECT ItemSales.OrderDate, tblExpense.DateOfExpense, (Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice))) AS TotalSales, (Nz(SUM(tblExpense.Expense))) AS TotalExpense ,(Nz(SUM(tblOrderProduct.Quantity * tblOrderProduct.ConsumerPrice)) - Nz(SUM(tblExpense.Expense)))AS Profit
FROM ItemSales
RIGHT JOIN
( select DateOfExpense,sum(Expense) AS Expense FROM tblExpense Group by DateOfExpense)
tblExpense ON ItemSales.OrderDate = tblExpense.DateOfExpense
LEFT JOIN tblOrderProduct ON ItemSales.SalesID = tblOrderProduct.SalesID
GROUP BY ItemSales.OrderDate,tblExpense.DateOfExpense
ORDER BY ItemSales.OrderDate

"Error near from" in UNION query

I'm getting an error "error near FROM" in SQLite, however I couldn't find any faults in this query. What am I doing wrong?
Select
tbltrans2_temp.itemcode,
tbltrans2_temp.itemname,
Sum(tbltrans2_temp.qty) qty
From
tbltrans_temp Inner Join
tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
where
tbltrans_temp.saleDate='11/07/2013'
Group By
tbltrans2_temp.itemcode
UNION
Select
tbltrans2.itemcode,
tbltrans2.itemname,
Sum(tbltrans2.qty) qty,
From
tbltrans Inner Join
tbltrans2 On tbltrans2.transid = tbltrans.transid
where
tbltrans.saleDate='11/07/2013'
Group By
tbltrans2.itemcode
There is an extra , before the second FROM remove it:
Select
tbltrans2_temp.itemcode,
tbltrans2_temp.itemname,
Sum(tbltrans2_temp.qty) qty
From
tbltrans_temp Inner Join
tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
where
tbltrans_temp.saleDate='11/07/2013'
Group By
tbltrans2_temp.itemcode
UNION
Select
tbltrans2.itemcode,
tbltrans2.itemname,
Sum(tbltrans2.qty) qty, <<<<------------------- This
From
tbltrans Inner Join
tbltrans2 On tbltrans2.transid = tbltrans.transid
where
tbltrans.saleDate='11/07/2013'
Group By
tbltrans2.itemcode
Update
To get the sum of qty from both the table, put that query in a subquery and sum it in the outer one, you can also omit the sum in the inner query and only do it in the outer one:
SELECT
itemcode,
itemname,
SUM(qty) TotalQty
FROM
(
Select
tbltrans2_temp.itemcode,
tbltrans2_temp.itemname,
Sum(tbltrans2_temp.qty) qty
From
tbltrans_temp Inner Join
tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
where
tbltrans_temp.saleDate='11/07/2013'
Group By
tbltrans2_temp.itemcode
UNION
Select
tbltrans2.itemcode,
tbltrans2.itemname,
Sum(tbltrans2.qty) qty
From
tbltrans Inner Join
tbltrans2 On tbltrans2.transid = tbltrans.transid
where
tbltrans.saleDate='11/07/2013'
Group By
tbltrans2.itemcode
) t
GROUP BY itemcode, itemname;

Complex Full Outer Join

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.