select sum on condition in Android Studio - sum

Please Help me.
I have two column.
First Column name "Product" and there are "Milk", "Cheese", "Butter" in "Product" column.
Second column name "Quantity"
Product Quantity
Milk 5
Butter 6
Milk 7
Cheese 8
Milk 6
Milk 9
Butter 12
I want to sum according to "Milk". Only "Milk" sum.
How can I do rawQuery?

Related

PostgreSQL how to find row IDs based on aggregated metrics

I have a table in a PostgreSQL database that looks more or less like this:
ID
Side
Amount
Price
1
BUY
8
107295.000000000
2
SELL
18
107300.000000000
3
SELL
21
107305.000000000
4
BUY
17
107310.000000000
And I have some aggregated metrics that look like this:
{'BUY': {'amount_sum': 6655, 'price_avg': 105961.497370398197}, 'SELL': {'amount_sum': 6655, 'price_avg': 106214.787377911345}}
And I need to find the row IDs that match these metrics. How would I go about doing this?
I've read a bit into PostgreSQL documentation and I've tried using GROUP BY on SIDE, then using the HAVING clause, but wasn't successful.
===================================================
To clarify, given this table and input:
ID
Side
Amount
Price
1
BUY
2
1
2
SELL
1
2
3
SELL
2
1
4
BUY
1
3
5
SELL
8
1
6
BUY
5
2
{'BUY': {'amount_sum': 3, 'price_avg': 2}, 'SELL': {'amount_sum': 10, 'price_avg': 1}}
I would the expected output to be:
BUY: ids[1,4] SELL: ids[3,5] that's because for ids 1 and 4, which have side as BUY, the sum of the amount column is 3, and the average of the price column is 2. And for ids 3 and 5, which have side as SELL, the sum of the amount column is 10, and the average of the price column is 1.
#Gabriel Tkacz, I have not 50 reputaton to comment) and ask my question as "answer".
From input table was expected:
{'BUY':{ 'Ids':[1,4,6]','amount_sum':8','price_avg':2}
{'SELL':{ 'Ids':[2,3,5]','amount_sum':11','price_avg':1.3333333333333333}
why excluded [6] on BUY side and [2] on SELL side in your explanation
BUY: ids[1,4] SELL: ids[3,5]

How to split value generating new extra row but leaving blank only one value from the new row in pandas [duplicate]

This question already has answers here:
Split (explode) pandas dataframe string entry to separate rows
(27 answers)
Closed 11 months ago.
I have a dataframe that looks like this:
ID Number Description Code Total Cost Store ID
A33 Ice cream; Chocolate 20 5
B44 Chips; Milk 15 6
C66 Cheese; Ice cream 10 6
V77 Pasta; Rice 8 8
I want to split the value of the Description Code into two, generating a new row based on the ";" symbol.
The output should be like this
ID Number Description Code Total Cost Store ID
A33 Ice cream 20 5
A33 Chocolate 20 5
B44 Chips 15 6
B44 Milk 15 6
C66 Cheese 10 6
C66 Ice cream 10 6
V77 Pasta 8 8
V77 Rice 8 8
Use str.split to create a list of Description Code then explode your dataframe:
out = df.assign(**{'Description Code': lambda x: x['Description Code'].str.split('; ')}) \
.explode('Description Code', ignore_index=True)
print(out)
# Output
ID Number Description Code Total Cost Store ID
0 A33 Ice cream 20 5
1 A33 Chocolate 20 5
2 B44 Chips 15 6
3 B44 Milk 15 6
4 C66 Cheese 10 6
5 C66 Ice cream 10 6
6 V77 Pasta 8 8
7 V77 Rice 8 8
Note: As Description Code is not a valid python identifier you have to use **{k1: v1, k2: v2} to expand parameters.

Increase Row Number when ever Product is encountered - T SQL - Single Query (More than one statement not allowed)

There is a table like below one
ID Vals
1 Product
2 Milk
3 Butter
4 Cheese
5 Yogurt
6 Product
7 Muesli
8 Porridge
9 Product
10 Banana
Output Needed like below
RWNUM ID Vals
1 1 Product
1 2 Milk
1 3 Butter
1 4 Cheese
1 5 Yogurt
2 6 Product
2 7 Muesli
2 8 Porridge
3 9 Product
3 10 Banana
Every time Product is encountered, the RWNUM column value will be increased by one.
This needs to be implemented in a single TSQL Query.
Any idea is welcome.
It looks like you want a cumulative sum of "product":
select t.*,
sum(case when val = 'Product' then 1 else 0 end) over (order by id) as rwnum
from t;

SQL - Sum a row of values based on Dates

I have following type of data
ingredients:
Milk
Apple
Rice
...
Then its purchased Date
26.10.2020
25.10.2020
etc
Each item is recorded when its purchased.
I want now to get at the right hand side to see how many times I bought apples, rice & milk.
As now I only see
Dates ---> 25.10.2020|24.10.2020
Rice 1 NULL
Milk 1 1
Apples NULL 1
My Goal is to see:
Dates ---> 25.10.2020|24.10.2020 SUM
Rice 1 NULL 1
Milk 1 1 2
Apples NULL 1 1
Thank you for your support!
The example of the data
Now I want to see at the end to total SUM, as there would be multiple days.

Convert multiple rows into a single comma separated string without XML

I want to combine data from multiple rows into a single comma separated string without using the XML function as it is not supported.
Here is an example:
ID Food
1 Apple
1 orange
2 wine
2 whiskey
2 beer
3 rice
3 wheat
3 maize
3 quinoa
Note that number rows per id is not fixed
Output:
ID Foods
1 Apple, orange
2 wine, whiskey, beer
3 rice, wheat, maize, quinoa
#standardSQL
select id, string_agg(food) as foods
from `project.dataset.table`
group by id