vba sum value in column except one - vba

I have a matrix in microsoft reports. It looks:
Product | Sold
apple | 1000
melon | 200
banana | 500
orange | 2000
sum(without orange) | x
sum | 3700
How to write expression in vba to sum all values without orange? Number of rows with fruits can be different so i cant use static index to identify product

=sum(IIf(Fields!Product.Value<>"orange", Fields!Sold.Value, 0))

Related

Sum dataset in SQL with conditional expressions

I'm working with a dataset of items with different values and I would like a SQL query to calculate the total USD value of the dataset.
Example Dataset:
id | type | numOrdered
0 | apple | 1
1 | orange | 3
2 | apple | 10
3 | apple | 5
4 | orange | 2
5 | apple | 1
Consider this dataset of fruit orders. Let's say apples are worth $1 and oranges are worth $2. I would like to know how much total USD in fruit orders we have.
I'd like to perform the same operation as this example Javascript function, but using SQL:
let sum = 0;
for(let fruitOrder of fruitOrders) {
if(fruitOrder.type == "orange"){
sum += fruitOrder.numOrdered*2;
} else {
sum += fruitOrder.numOrdered*1;
}
}
return sum;
So the correct answer for this dataset would be $27 USD total since there are 17 apples worth $1 and 5 oranges worth $2.
I know how to break it down into two distinct queries giving me the number I want split by type
SELECT
sum("public"."fruitOrders"."num"*2) AS "sum"
FROM "public"."fruitOrders"
WHERE "public"."fruitOrders"."type" = 'orange';
which would return $10, the total USD value of oranges
SELECT
sum("public"."fruitOrders"."num") AS "sum"
FROM "public"."fruitOrders"
WHERE "public"."fruitOrders"."type" = 'apple';
which would return $17, the total USD value of apples
I just don't know how to sum those numbers together in SQL to get $27, the total USD value of the dataset.
If you want the values 1 and 2 hardcoded then you can use a CASE statement with SUM():
SELECT
sum(case type
when 'apple' then 1
when 'orange' then 2
end * numordered
) AS "sum"
FROM "public"."fruitOrders"
See the demo.
Result:
| sum |
| --- |
| 27 |

Oracle SQL - Give each row in a result set a unique identifier depending on a value in a column

I have a result set, being returned from a view, that returns a list of items and the country they originated from, an example would be:
ID | Description | Country_Name
------------------------------------
1 | Item 1 | United Kingdom
2 | Item 2 | France
3 | Item 3 | United Kingdom
4 | Item 4 | France
5 | Item 5 | France
6 | Item 6 | Germany
I wanted to query this data, returning all columns (There are more columns than ID, Description and Country_Name, I've omitted them for brevity's sake) with an extra one added on giving a unique value depending on the value that is inside the field Country_name
ID | Description | Country_Name | Country_Relation
---------------------------------------------------------
1 | Item 1 | United Kingdom | 1
2 | Item 2 | France | 2
3 | Item 3 | United Kingdom | 1
4 | Item 4 | France | 2
5 | Item 5 | France | 2
6 | Item 6 | Germany | 3
The reason behind this, is we're using a Jasper report and need to show these items with an asterisk next to it (Or in this case a number) explaining some details about the country. So the report would look like this:
Desc. Country
Item 1 United Kingdom(1)
Item 2 France(2)
Item 3 United Kingdom(1)
Item 4 France(2)
Item 5 France(2)
Item 6 Germany(3)
And then further down the report would be a field stating:
1: Here are some details about the UK
2: Here are some details about France
3: Here are some details about Germany
I'm having difficulty trying to generate a unique number to go along side each country, starting at one each time the report is ran, incrementing it when a new country is found and keeping track of where to assign it. I would hazard a guess at using temporary tables to do such a thing, but I feel that's overkill.
Question
Is this kind of thing possible in Oracle SQL or am I attempting to do something that is rather large and cumbersome?
Are there better ways of doing this inside of a Jasper report?
At the moment, I'm looking at just having the subtext underneath each individual item and repeating the same information several times, just to avoid this situation, rather than having them aggregated and having the subtext once. It's not clean, but it saves this rather odd hassle.
You are looking for dense_rank():
select t.*, dense_rank() over (order by country_name) as country_relation
from t;
I don't know if this can be done inside Jasper reports. However, it is easy enough to set up a view to handle this in Oracle.

Calculate percentage on SSRS Expressions

I have the following table on SQL:
Category | Requests
Cat1 | 150
Cat2 | 200
Cat3 | 550
Cat4 | 100
Cat5 | 50
SUM | 1050
How can create an expression to calculate the percentage of Cat5 compared to the total? (4.7% in this case).
Try this:
=Lookup("Cat5",Fields!Category.Value,Fields!Requests.Value,"DataSetName")/
Sum(Fields!Requests.Value,"DataSetName")
Replace "DataSetName" by the actual name of your dataset.
Assuming you want 150 to represent 150% within the rdl you can do the following:
first apply the following formula: =Fields!field.Value/100
Where Fields!field.Value is the field you want to convert to percentage so if your field is called Requests then you will have =Fields!Requests.Value/100
Then you need to change the type of the textbox to be percentage from the TextboxProperties
you should get a result like this:

Microsoft Report Builder - Row totals percent of column total

I'd really appreciate some help with Report Builder. As seen below, I have a report that shows the number of items. In my SQL query I have used a CASE statement to tag some of the items with a y or a n.
What I want to do is add a calculated cell that sums all the values of the items tagged with y and divide by the total and * 100 to find the percent of the rows tagged y of the total amount.
Answer looking for is -
Apple | Y | 100
Pear | Y | 200
Orange| N | 500
Total | 800
Percent of Ys = 37.5% (100+200/800*100)
I'm new to report builder so please let me know if this doesn't make sense.
Many thanks.
You could add two more columns to your query, using similar logic as your CASE statement for the Y/N column. The first column is populated with the value only when the condition for "Y" is true, otherwise it is zero. The second column is populated with the value only when the condition for "N" is true, otherwise it is zero. This would give you a result set similar to this:
All Y N
Apple | Y | 100 | 100 | 0
Pear | Y | 200 | 200 | 0
Orange| N | 500 | 0 | 500
Total | 800 | 300 | 500
Then your calculation is something like this:
Percent of Ys = (Sum(Y) / Sum(All)) * 100
i.e.
Percent of Ys = (300 / 800) * 100 = 37.5%

Database Table: Quantity or redundancy

I'm building a database which have a lot of items for a bike shop. This bike shop have many of the same items such as 100 wheels of size 4 and color 'red'. My question is:
Is is better to add a 'Quantity' field to the entity set and put all similar items in one entity (example 1) or is it better to have an entity for each item (example 2)?
Example 1:
id | color | size | quantity
1 | red | 4 | 100
Example 2:
id | color | size
1 | red | 4
2 | red | 4
3 | red | 4
etc.
The first - qqhantity field - unless you have a reason to track for example serial numbers, and even then you may go to v1 and use a separate column.
Generally: get a copy of the Data Model Ressoure Book Vol 1 - it has a ton of discussions about standard business data problems, among them an inventory system. You will learn a lot.