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 |
Related
have table in Teradata SQL like below:
SMS_ID | PRODUCT
-------------------
11 | A
22 | A
33 | A
87 | B
89 | B
14 | C
Column "SMS_ID" presents ID of SMS sent do client
Column "PRODUCT" presents ID of product which was a subject of SMS
My question is: How can I calculate in Teradata SQL mean number of SMS per PRODUCT ?
As a result I need something like below:
AVG | PRODUCT
-------
0.5 | A -> because 3 / 6 = 0.5
0.33 | B -> because 2 / 6 = 033
0.16 | C -> because 1 / 6 = 0.16
You want fractions of the total count:
SELECT
product
,COUNT(*) -- count per product
/ CAST(SUM(COUNT(*)) OVER () AS FLOAT) -- total count = sum of counts per procuct
FROM yourTable
GROUP BY PRODUCT
I would like to create a script that takes the rows of a table which have a specific mathematical difference in their ASCII sum and to add the rows to a separate table, or even to flag a different field when they have that difference.
For instance, I am looking to find when the ASCII sum of word A and the ASCII sum of word B, both stored in rows of a table, have a difference of 63 or 31.
I could probably use a loop to select these rows, but SQL is not my greatest virtue.
ItemID | asciiSum |ProperDiff
-------|----------|----------
1 | 100 |
2 | 37 |
3 | 69 |
4 | 23 |
5 | 6 |
6 | 38 |
After running the code, the field ProperDiff will be updated to contain 'yes' for ItemID 1,2,3,5,6, since the AsciiSum for 1 and 2 (100-37) = 63 etc.
This will not be fast, but I think it does what you want:
update t
set ProperDiff = 'yes'
where exists (select 1
from t t2
where abs(t2.AsciiSum - t.AsciiSum) in (63, 31)
);
It should work okay on small tables.
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%
I don't know if Stata can do this but I use the tabulate command a lot in order to find frequencies. For instance, I have a success variable which takes on values 0 to 1 and I would like to know the success rate for a certain group of observations ie tab success if group==1. I was wondering if I can do sort of the inverse of this operation. That is, I would like to know if I can find a value of "group" for which the frequency is greater than or equal to 15% for example.
Is there a command that does this?
Thanks
As an example
sysuse auto
gen success=mpg<29
Now I want to find the value of price such that the frequency of the success variable is greater than 75% for example.
According to #Nick:
ssc install groups
sysuse auto
count
74
#return list optional
local nobs=r(N) # r(N) gives total observation
groups rep78, sel(f >(0.15*`r(N)')) #gives the group for which freq >15 %
+---------------------------------+
| rep78 Freq. Percent % <= |
|---------------------------------|
| 3 30 43.48 57.97 |
| 4 18 26.09 84.06 |
+---------------------------------+
groups rep78, sel(f >(0.10*`nobs'))# more than 10 %
+----------------------------------+
| rep78 Freq. Percent % <= |
|----------------------------------|
| 2 8 11.59 14.49 |
| 3 30 43.48 57.97 |
| 4 18 26.09 84.06 |
| 5 11 15.94 100.00 |
+----------------------------------+
I'm not sure if I fully understand your question/situation, but I believe this might be useful. You can egen a variable that is equal to the mean of success, by group, and then see which observations have the value for mean(success) that you're looking for.
egen avgsuccess = mean(success), by(group)
tab group if avgsuccess >= 0.15
list group if avgsuccess >= 0.15
Does that accomplish what you want?
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))