Incorrect subtotals & Totals in mysql select - mysql

I've managed to separate the negatives & positives in the sql select below and I am trying to get the subtotals & totals as indicated in the table pic belom the third column is what the correct results should be.
Below is sql select I've used but its returning the results in column 2 of the above table and its not correct. How can I get the subtotal & total results in column 3
Thanks & Regards
SELECT * FROM
(SELECT
party AS "Account",
sum(debit-credit) AS "Balance"
FROM `tabGL Entry`
WHERE party_type ="Customer" AND MONTH(posting_date) = MONTH(Now()) -1
GROUP BY party WITH ROLLUP
HAVING sum(debit-credit) < 0
UNION ALL
SELECT
party AS "Account",
sum(debit-credit) AS "Balance"
FROM `tabGL Entry`
WHERE party_type ="Customer" AND MONTH(posting_date) = MONTH(Now()) -1
GROUP BY party WITH ROLLUP
HAVING sum(debit-credit) > 0
) rec
UNION ALL
SELECT NULL AS party, sum(dep) AS Balance
FROM (
SELECT SUM(credit-debit) AS dep
FROM `tabGL Entry`
UNION ALL
SELECT SUM(credit-debit) AS nodep
FROM `tabGL Entry`
) AS recb
GROUP BY party WITH ROLLUP

Use this syntax to prevent GROUP BY from being attached incorrectly:
( SELECT ... GROUP BY ... )
UNION ALL
( SELECT ... GROUP BY ... )
Versus
( SELECT ... )
UNION ALL
( SELECT ... )
GROUP BY ...
Where did the word 'Subtotal' come from? I don't see it in the query.
I suspect the inner WITH ROLLUP is tossed. What happens if you remove them?

Related

Create 3rd Table SQL with difference of 2 other tables

Afternoon all
I am trying to create a view with 3 tables.
Create view breakdown as
SELECT x.month_
, x.returns_
, y.sales_
, z.profit_
FROM
(SELECT SUM(Return_Value_Eur) AS returns_
, monthname(Return_Date) AS month_
FROM returns
GROUP BY month_) x
INNER JOIN
(SELECT SUM(Total_Price_Eur) AS sales_
, monthname(Order_Date) AS month_
FROM orders
GROUP BY month_) y ON x.month_ = y.month_
INNER JOIN
(SELECT (SUM(Total_Price_Eur)-SUM(Return_Value_Eur)) AS profit_
, monthname(Order_Date) AS month_
FROM returns, orders
GROUP BY month_) z ON y.month_ = z.month_
I have the following code however I'm struggling with the profit table. This table should be a difference between the y.sales and x.returns. However the figures are coming up as follows, the profit column should be showing 394.
Month_ Returns_ Sales_ Profit_
January 108 502 -251
Any help would be greatly appreciated.
Don't mix the old-school comma syntax for the join operation with the JOIN keyword; actually, just ditch the old-school comma syntax altogether for new queries.
Looks like the problem is the cartesian product of the join between returns and orders, the inline view returning "profit".
It's not clear what we are trying to achieve (broken SQL is like that, in that it fails to accurately communicate the actual specification)
It looks to me (and this is just a guess here) is that the intent is to subtract total returns from total sales, by month.
I'd do it like this:
SELECT v.month_
, IFNULL( SUM(v.returns_) ,0) AS returns_
, IFNULL( SUM(v.sales_ ) ,0) AS sales
, IFNULL( SUM(v.sales_ ) ,0)
- IFNULL( SUM(v.returns_) ,0) AS net_
FROM (
SELECT 0.0 AS sales_
, SUM(r.return_value_eur) AS returns_
, MONTHNAME(r.return_date) AS month_
, MONTH(r.return_date) AS month_num
FROM returns r
GROUP
BY MONTHNAME(r.return_date)
, MONTH(r.return_date)
UNION ALL
SELECT SUM(o.total_price_eur) AS sales_
, 0.0 AS returns_
, MONTHNAME(o.order_date) AS month_
, MONTH(o.order_date) AS month_num
FROM orders o
GROUP
BY MONTHNAME(o.order_date)
, MONTH(o.order_date)
) v
GROUP
BY v.month_
, v.month_num
ORDER
BY v.month_num
Note that this query ignores the year, which is a bit odd, mashing together November 2019, with November 2018, 2017, et al.
The example query orders rows by calendar order, January, February, March, ... that's the reason for the additional month_num column.
If there are no rows in returns or orders for a given month, then that month will not appear in the resultset.
To guarantee rows returned for all 12 calendar months, I would use row source that was guaranteed to return me one row for each month. Then do outer joins to aggregated returns by month and aggregated sales by month. (I'd also have the queries work with the integer value for the month, and then translate the integer to a string "month name" as a final output step.)
Something like this:
SELECT MONTHNAME(STR_TO_DATE(c.month_num,'%m')) AS month_
, IFNULL( tr.returns_ ) ,0) AS returns_
, IFNULL( ts.sales_ ) ,0) AS sales_
, IFNULL( ts.sales_ ) ,0)
- IFNULL( tr.returns_ ) ,0) AS net_
FROM ( -- row source for calendar month 1..12
SELECT 1 AS month_num UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) c
LEFT
JOIN ( -- total returns by month
SELECT SUM(r.return_value_eur) AS returns_
, MONTH(r.return_date) AS month_num
FROM returns r
GROUP
BY MONTH(r.return_date)
) tr
ON tr.month_num = c.month_num
LEFT
JOIN ( -- total orders by month
SELECT SUM(o.total_price_eur) AS sales_
, MONTH(o.order_date) AS month_num
FROM orders o
GROUP
BY MONTH(o.order_date)
) ts
ON ts.month_num = c.month_num
ORDER
BY c.month_num

Pivot With Calendar Table

I am trying to pivot this data but I am getting an error of
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ','.
Highlighting the comma after SUM([Total Count]) but it has to be there, what should I change so my query executes properly?
select *
FROM
(
select a.regionalLocale As [RL],
Count(ID) As [Total Count],
CONVERT(VARCHAR(20), dt.week) AS Week
FROM database14.dataTable a
INNER JOIN calendarDB.masterCalendar dt
ON a.SaleDate = dt.FullDate
WHERE a.SaleDate IS NOT NULL
) src
pivot
(
SUM([Total Count]), [RL]
For Week IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13])
) piv
You've got a few issues with your query.
One you've got the syntax SUM([Total Count]), [RL] in your PIVOT. You only want to include the column you are aggregating here.
Second, there is no need to use count(id) inside your subquery, let the PIVOT aggregation handle the total. Change your code to:
select [RL],
[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13]
FROM
(
select a.regionalLocale As [RL],
ID,
CONVERT(VARCHAR(20), dt.week) AS Week
FROM database14.dataTable a
INNER JOIN calendarDB.masterCalendar dt
ON a.SaleDate = dt.FullDate
WHERE a.SaleDate IS NOT NULL
) src
pivot
(
COUNT(ID)
For Week IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13])
) piv

SQL Server 2008 - Error Order by case

I'm getting the following error messages:
Msg 8120, Level 16, State 1, Line 1
Column 'customers.member_category' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 104, Level 16, State 1, Line 1
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
How can I order the results by the case?
Thanks
SELECT
Date,
order_id,
member_category,
COUNT(*) AS no_items,
SUM(item_amount) AS total_amount
FROM
order_items OI (NOLOCK) JOIN customers C (NOLOCK)
ON OI.CUSTOMER_NO = C.CUSTOMER_NO
WHERE
DATE = '01 FEB 2014'
GROUP BY
order_id,
member_category
UNION
SELECT
'',
'',
'Total',
COUNT(*) AS no_items,
SUM(item_amount) AS total_amount
FROM
order_items OI (NOLOCK) JOIN customers C (NOLOCK)
ON OI.CUSTOMER_NO = C.CUSTOMER_NO
WHERE
DATE = '01 FEB 2014'
GROUP BY Date WITH ROLLUP
ORDER BY Date ASC, CASE member_category WHEN 'VIP' THEN 1
WHEN 'STD' THEN 2
WHEN 'GLD' THEN 3
END
The second part of your UNION statement does not contain field member_category. This violates basic principle of UNION which is that number and order of columns in each statement should be same.
To resolve the issue, you need to add member_category both in the list of retrieved columns and in the GROUP BY clause.

MySQL SELECT Query - Subtract a SUM() value with the combined total of two other SUM() values

I have two SELECT statements that give the values "TotalSales" and "VendorPay_Com". I want to be able to subtract VendorPay_Com from TotalSales within the one MySQL statement to get the value "Outstanding_Funds" but I haven't found a reliable way to do so.
These are my two statements:
Query 1:
SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold';
Query 2:
SELECT SUM(AC.Amount) AS VendorPay_Comm
FROM (
SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS `Amount` FROM COMMISSION AS C WHERE C.`status` = 'Paid') AS AC
Any help on this matter would be greatly appreciated :)
You can do it as follows :
select (select ...) - (select ...)
In your example, simply :
select
(
SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold'
)
-
(
SELECT SUM(AC.Amount) AS VendorPay_Comm
FROM (
SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS `Amount` FROM COMMISSION AS C WHERE C.`status` = 'Paid') AS AC
) AS Outstanding_Funds
Try
SELECT TotalSales-VendorPay_Comm AS Outstanding_Funds
FROM
(SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold') t1,
(SELECT SUM(Amount) AS VendorPay_Comm
FROM (SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS Amount
FROM COMMISSION
WHERE Status = 'Paid') t0) t2
Here is sqlfiddle

SSRS Matrix percentages

Here's the report:
This is how I got the percentages for column the '%Change of most recent year".
=((Last(Fields!Quantity.Value,"Child") - First(Fields!Quantity.Value)) / First(Fields!Quantity.Value))`
= ((54675 - 55968)/55968 ) = -2.31%'
= ((54675 - 57849)/57849) = -5.49%'
It will always take the first year '2012' in this case and get the percentages against each other year. If I enter the years 2005,2004,2003,2002,2001 it will always take the first year and do a percentages against each additional year. 2005 to 2004, 2005 to 2003, 2005 to 2002 and so on. I can have as many as 2 column (year) to many columns.
I need to do it for the Total and Subtotal but it won't work because it's in a different scope.
data is = row Child group
Sub Total: = row Parent group
Total: = row Total group
Year = Column Period group
Query use to get result.
SELECT MEMBERSHIP_CODE
, PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
, SUM(QUANTITY) AS Quantity
, '01-Personal' AS Child
, '01-Overall' AS Parent
, 'Total' as Total
FROM vf_Sshot AS vfs
INNER JOIN vProd AS vP ON vfs.PRODUCT_CODE = vP.PRODUCT_CODE
INNER JOIN vMem_Type vMT on vMT.Member_Type = vfs.Member_Type
WHERE (PERIOD IN ( (SELECT Val from dbo.fn_String_To_Table(#Periods,',',1))))
AND (vMT.MEMBER_TYPE NOT IN ('a','b','c'))
AND (vfs.STATUS IN ( 'A', 'D', 'C'))
AND (MEMBERSHIP_CODE NOT IN ('ABC', 'DEF' ))
and vP.PROD_TYPE in ('DUE','MC','SC')
and vMT.Member_Record = '1'
GROUP BY MEMBERSHIP_CODE, PERIOD
Any ideas?
How would I produce this output?
TOTAL: 57,573 58,941 57,573 61,188 57,573 61,175 57,175
This is the easiest way of solving your problem. In your query, identify the sum for the latest period on a separate column (you can transform your query into a CTE, so that you don't have to change your base query a lot):
WITH query AS (
SELECT MEMBERSHIP_CODE
, PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
, SUM(QUANTITY) AS Quantity
, '01-Personal' AS Child
, '01-Overall' AS Parent
, 'Total' as Total
...
UNION
SELECT
...
)
SELECT
A.MEMBERSHIP_CODE,
A.PERIOD,
A.Distinct_ID,
A.Child,
A.Parent,
A.Total,
A.Quantity,
B.Quantity AS LastPeriodQuantity
FROM
query A INNER JOIN
(SELECT *, ROW_NUMBER() OVER(PARTITION BY MEMBERSHIP_CODE, Distinct_ID, Child, Parent ORDER BY PERIOD DESC) as periodOrder FROM query) B ON
A.MEMBERSHIP_CODE = B.MEMBERSHIP_CODE AND
A.DISTINCT_ID = B.DISTINCT_ID AND
A.Parent = B.Parent AND
A.Child = B.Child AND
A.Total = B.Total AND
B.PeriodOrder = 1
And then on all your totals/subtotals/columns you will be accessing a column that is being grouped/filtered by the same rules than your denominator. Your expression can remain, for all cells, something like this:
=(Fields!LastPeriodQuantity.Value - Fields!Quantity.Value) / Fields!Quantity.Value