Creating Sub Reports Total And Grand Total In Crystal Report - vb.net

I have a Crystal Report getting data from SQL Server. I want to generate GroupReport Total and also Generate Grand Total Report
I have been able to generate the report from SQL, the following are the fields i have: Item_Name, qty, Price, Amount
I have created a Formular to sum all the amount which the total will give me the GrandTotal
But for each subreport, i want to get the total amount too but this seems to be operating in a running total form.
Below is what i am getting now.
OrderDate Item qty Price Amount
2019-07-24 rice 2 3,000 6,000
2019-07-24 beans 2 5,000 10,000
2019-07-24 fish 2 3,000 6,000
sub_Total 22,000
OrderDate Item qty Price Amount
2019-07-25 rice 2 3,000 6,000
2019-07-25 beans 2 5,000 10,000
2019-07-25 fish 2 3,000 6,000
sub_Total 44,000
OrderDate Item qty Price Amount
2019-07-26 rice 2 3,000 6,000
2019-07-26 beans 2 5,000 10,000
2019-07-26 fish 2 3,000 6,000
sub_Total 66,000
GrandTotal 66,000
tonumber({DTtablesales.Amount})
The below is how i want the report to be
OrderDate Item qty Price Amount
2019-07-24 rice 2 3,000 6,000
2019-07-24 beans 2 5,000 10,000
2019-07-24 fish 2 3,000 6,000
sub_Total 22,000
OrderDate Item qty Price Amount
2019-07-25 rice 2 3,000 6,000
2019-07-25 beans 2 5,000 10,000
2019-07-25 fish 2 3,000 6,000
sub_Total 22,000
OrderDate Item qty Price Amount
2019-07-26 rice 2 3,000 6,000
2019-07-26 beans 2 5,000 10,000
2019-07-26 fish 2 3,000 6,000
sub_Total 22,000
GrandTotal 66,000
Note that the GrandTotal is a TotalRunning Field calculating sum(Amount)
My problem is getting the sub-total

Try the Reset option on the subtotal under Edit Running Totals menu and reset at the correct group.

Related

Getting price difference between two dates

There is a table where once a day/hour lines are added that contain the product ID, price, name and time at which the line was added.
CREATE TABLE products
(
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
product_id integer NOT NULL,
title text NOT NULL,
price double precision NOT NULL,
checked_at timestamp with time zone DEFAULT now()
);
The data in the products table looks like this:
id
product_id
title
price
checked_at
1
1000
Watermelon
50
2022-07-19 10:00:00
2
2000
Apple
30
2022-07-19 10:00:00
3
3000
Pear
20
2022-07-19 10:00:00
4
1000
Watermelon
100
2022-07-20 10:00:00
5
2000
Apple
50
2022-07-20 10:00:00
6
3000
Pear
35
2022-07-20 10:00:00
7
1000
Watermelon
150
2022-07-21 10:00:00
8
2000
Apple
50
2022-07-21 10:00:00
9
3000
Pear
60
2022-07-21 10:00:00
I need to pass a date range (for example, from 2022-07-19 to 2022-07-21) and get the difference in prices of all unique products, that is, the answer should be like this:
product_id
title
price_difference
1000
Watermelon
100
2000
Apple
20
3000
Pear
40
I only figured out the very beginning, where I need to get the ID of all unique products in the table using DISTINCT. Next, I need to find the rows that are closest to the date range. And finally find the difference in the price of each product.
You could use an aggregation approach here:
SELECT product_id, title,
MAX(price) FILTER (WHERE checked_at::date = '2022-07-21') -
MAX(price) FILTER (WHERE checked_at::date = '2022-07-19') AS price_difference
FROM products
GROUP BY product_id, title
ORDER BY product_id;

SQL Help, Splitting Tax by Tenders

I have a Point of Sale system where all checks and tender details exist in a single table. I'm trying to write a SQL query in a way that I can see check totals by tenders to reconcile with cash flow and bank statements. Unfortunately the schema is not mine and can't change it.
One problem I ran into is that there are cases where one check has multiple transactions involving various tenders, therefore I need to do implement (business set) rules to allocate taxes evenly. Those rules might change in the future to say, allocate taxes to CC first if any, so I need to built in some flexibility.
The SQL table looks like this:
CheckID
LineType
TenderName
LineTotal
Tax
1
ItemSold
5.00
0.25
1
TenderTotal
Cash
5.25
2
ItemSold
10.00
0.50
2
TenderTotal
Cash
5.00
2
TenderTotal
VISA
5.50
3
ItemSold
10.00
0.25
3
ItemSold
10.00
0.25
3
TenderTotal
AMEX
10.25
3
TenderTotal
VISA
10.25
4
ItemSold
10.00
0.50
4
TenderTotal
Cash
20.00
4
TenderTotal
Cash
-9.50
The resulting report needs to have one row per tender, with tax equally distributed among check tenders, and net revenue being the difference between total sale and tax.
TenderName
TotalSale
NetRevenue
TaxCollected
Cash
20.75
19.75
1.00
VISA
15.75
15.25
0.50
AMEX
10.25
10.00
0.25
I tried using Select with Exists, also CTE and recursive CTEs, but can't quite figure it out how to do the tax part cleanly. Any other SQL tricks I could try?
We are using SQL Server 2012 at the moment, but have plans in plan to upgrade to 2016 in the near future.
I don't know if the logic is right, but it gets you the results you are after:
WITH Tenders AS(
SELECT V.CheckID,
V.LineType,
V.TenderName,
V.LineTotal,
SUM(CASE WHEN V.TenderName IS NULL THEN V.Tax END) OVER (PARTITION BY V.CheckID) AS Tax
FROM (VALUES(1,'ItemSold',NULL,5.00,0.25),
(1,'TenderTotal','Cash',5.25,NULL),
(2,'ItemSold',NULL,10.00,0.50),
(2,'TenderTotal','Cash',5.00,NULL),
(2,'TenderTotal','VISA',5.50,NULL),
(3,'ItemSold',NULL,10.00,0.25),
(3,'ItemSold',NULL,10.00,0.25),
(3,'TenderTotal','AMEX',10.25,NULL),
(3,'TenderTotal','VISA',10.25,NULL),
(4,'ItemSold',NULL,10.00,0.50),
(4,'TenderTotal','Cash',20.00,NULL),
(4,'TenderTotal','Cash',-9.50,NULL))V(CheckID,LineType,TenderName,LineTotal,Tax))
SELECT T.TenderName,
SUM(T.LineTotal) AS TotalSale,
SUM(T.LineTotal - T.Tax) AS NetRevenue,
SUM(T.Tax) AS TaxCollected
FROM Tenders T
WHERE T.TenderName IS NOT NULL
GROUP BY T.TenderName;

How to handle the stock table quantity after sale

I have a problem. I am not understanding that how to deduct the quantity from stock after sale and we have multiple entries of same stock
like:
id
Medicine
Company
quantity
item price
is deleted
1
Panadol
Abbott
200
10.00
0
2
Panadol
Abbott
100
11.00
0
3
Panadol
Abbott
100
12.00
0
now how to deduct the quantity of stock and one thing is more that this product/Medicine has separate manufacturing and expiry date
please tell me how to handle it.
I was thinking that to make separate table for tracking the remaining stock quantity.
or runtime management to calculate all the sale quantity minus(-) sum of all quantities entered to get
the current stock quantity.
Please guide me.

MDX left-join of two sets

I've developed data into to sets.
The first set looks like this:
set1
acctA CatA budget 10
acctA CatB budget 20
acctA CatC budget 30
acctB CatA budget 10
acctB CatB budget 20
acctB CatC budget 30
acctC CatA budget 10
acctC CatB budget 20
acctC CatC budget 30
and the second set:
set2
acctA CatA expense 7
acctA CatB expense 8
acctB CatB expense 20
acctC CatB expense 19
acctC CatC expense 3
The output report needs to be matched up like this.
desired output
CatA CatB CatC
budget expense budget expense budget expense
acctA 10 7 10 8 10 -
acctB 20 - 20 23 20 -
acctC 30 - 30 19 30 3
In all accts there will be a budget amount in all Cat(s).
Each budget Cat will be represented in all accts just on time.
An acct will not always have an expense for a given Cat.
Any expense value has to be given to a distinct acct-Cat pair.
Would you rather return these sets to SSRS and join them in the report?
How would you join them in MDX?
Thanks.
We don't know much about your cube.
To return what you mention in straight mdx is something along these lines:
SELECT
[Category].[Category].MEMBERS
*
{[Measures].budget, [Measures].expenses} ON COLUMNS
[Account].[Account].MEMBERS ON ROWS
FROM [YourCube];
Can you amend the above please so we have an idea of your mdx? - it will need to amended to work with SSRS as only the Measures hierarchy will be allowed on columns.
Lol. The correct answer is ... get those nulls our of your cube's data

DAX / PP Aggregate a variable project margin down a column

I need a solution similar to this:
DAX running total (or count) across 2 groups
However slightly more complex.
I have the following:
(apologies for the layout - i can't post pictures)
Name Date Monthly Rev Total Rev Margin( % Rev)
Proj 1 1/08/2014 0 7000 15%
Proj 1 1/09/2014 1000 7000 15%
Proj 1 1/10/2014 1000 7000 15%
Proj 1 1/11/2014 1000 7000 15%
Proj 1 1/12/2014 0 7000 15%
Proj 1 1/01/2015 0 7000 15%
Proj 1 1/02/2015 2000 7000 15%
Proj 1 1/03/2015 2000 7000 15%
Proj 2 1/11/2014 0 16000 10%
Proj 2 1/12/2014 1500 16000 10%
Proj 2 2/12/2014 1500 16000 10%
Proj 2 3/12/2014 1500 16000 10%
Proj 2 4/12/2014 1500 16000 10%
Proj 2 5/12/2014 2000 16000 10%
Proj 2 6/12/2014 2000 16000 10%
Proj 2 7/12/2014 0 16000 10%
Proj 2 8/12/2014 2000 16000 10%
Proj 2 9/12/2014 2000 16000 10%
Proj 2 10/12/2014 2000 16000 10%
Monthly rev is the revenue received in a month, total is the total project value and margin is the percentage of revenue. The table is linked to a dates table by Date.
I need to show margin by date (there are other descriptive columns in the table for slicing) however the margin calc is not straightforward.
In an excel table it would look something like this:
Cumm simple margin | Completion| Cumm complex margin | Margin earnt
0 0% 0 0
150 20% 30 30
300 40% 120 90
450 60% 270 150
450 60% 270 0
450 60% 270 0
750 80% 600 330
1050 100% 1050 450
0 0% 0 0
150 11% 17 17
300 22% 67 50
450 33% 150 83
600 44% 267 117
800 56% 444 178
1000 67% 667 222
1000 67% 667 0
1200 78% 933 267
1400 89% 1244 311
1600 100% 1600 356
Where:
Simple margin is calculated on a cumulative basis as % of monthly Rev
Percentage complete of the project is calculated based on "active" months where revenue is earned
Cumulative simple margin is multiplied by the % complete
Actual margin earned in a particular month is the difference between two months.
Note that Monthly revenue is not necessarily continuous.
No idea how to recreate this in power pivot, any suggestions would be well received.
Cheers
Assuming
That your Project 2 data should run monthly from 1/11/2015 to 1/09/2015 (rather than individual December dates)
You have your data in a table called 'ProjectMargins'
Your DateDim table is called 'Reporting Dates'
Then these are the DAX Measures you need (although there may be simpler methods for achieving these results):
[MonthlyRev]:=SUM(ProjectMargins[Monthly Rev])
[ActiveMonth]:=CALCULATE(COUNTROWS('ProjectMargins'),FILTER('ProjectMargins',[MonthlyRev]>0))
[AllActiveMonths]:=CALCULATE([ActiveMonth],ALL('Reporting Dates'[Date]))
[Completion]:=DIVIDE(CALCULATE([ActiveMonth],FILTER(ALL('Reporting Dates'[Date]),'Reporting Dates'[Date] <= MAX(ProjectMargins[Date]))),[AllActiveMonths])
If you need to calculate TotalRev, from your Monthly Rev, Rather than it appearing in the original source table:
[TotalRev]:=IF(ISBLANK(MAX(ProjectMargins[Margin( % Rev)])),BLANK(),CALCULATE([MonthlyRev],ALL('Reporting Dates'[Date])))
[Rev%]:=MAX(ProjectMargins[Margin( % Rev)])
[Cumm Simple Margin]:=CALCULATE([MonthlyRev]*[Rev%],FILTER(ALL('Reporting Dates'[Date]),'Reporting Dates'[Date] <= MAX(ProjectMargins[Date])))
[Cumm Complex Margin]:=[Completion]*[Cumm Simple Margin]
[Previous Month Cumm Complex]:=CALCULATE([Cumm Complex Margin], DATEADD('Reporting Dates'[Date],-1,MONTH))
[Margin Earnt]:=IF([Cumm Complex Margin]>0,[Cumm Complex Margin]-[Previous Month Cumm Complex],BLANK())
NOTE: This assumes that the margin is never negative.
Ensure that the date field from the DateDim table is used in your pivot, not the date field from the Fact table.