increase rank based on particular value in column - sql

I would appreciate some help for below issue. I have below table
id
items
1
Product
2
Tea
3
Coffee
4
Sugar
5
Product
6
Rice
7
Wheat
8
Product
9
Beans
10
Oil
I want output like below. Basically I want to increase the rank when item is 'Product'. May I know how can I do that? For data privacy and compliance purposes I have modified the data and column names
id
items
ranks
1
Product
1
2
Tea
1
3
Coffee
1
4
Sugar
1
5
Product
2
6
Rice
2
7
Wheat
2
8
Product
3
9
Beans
3
10
Oil
3
I have tried Lag and lead functions but unable to get expected output

Here is solution using a derived value of 1 or 0 to denote data boundaries SUM'ed up with the ROWS UNBOUNDED PRECEDING option, which is key here.
SELECT
id,
items,
SUM(CASE WHEN items='Product' THEN 1 ELSE 0 END) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) as ranks
FROM

Related

Counting entries using a sub-query in SQL

commercial
clicks
1
0
2
1
2
1
3
1
4
0
5
0
5
0
6
1
7
1
7
1
8
0
9
1
9
1
10
0
The table described above (the name of the table is a table) has 2 columns. The commercial column shows the ads shown to the user (the chronological order is irrelevant). The clicks column shows whether a user who saw the link clicked on it or not (0 - did not click, 1 - clicked).
I need to make a SQL query for how many advertisements there are upon clicking one, two clicks, three clicks, etc. (so for every number of clicks in the table), using a sub-query.
Thank you.
SELECT commercial, SUM(click)
FROM table
GROUP BY commercial
ORDER BY commercial ASC
commercial
clicks
1
0
2
2
3
1
4
0
5
0
6
1
7
2
8
0
9
2
10
0
I made this so far but then i get stuck on how to make a sub-query to calculate how many commercials are clicked once or 2 times.
My expected result is:
clicks
commercial
0
1,4,5,8,10
1
3,6
2
2,7,9
3
I'm also wondering if i could get this result
commercial
clicks
1
0
2
2
3
1
4
0
5
0
6
1
7
2
8
0
9
2
10
0
Without using the SUM and GROUP BY function, please let me know if thats possible.
You need to compute a further aggregation and use BigQuery STRING_AGG for string aggregation.
SELECT num_clicks,
STRING_AGG(CAST(commercial AS STRING)) AS commercials
FROM (SELECT commercial,
SUM(clicks) AS num_clicks
FROM tab
GROUP BY commercial) clicks_per_commercial
GROUP BY num_clicks
"Without using the SUM and GROUP BY function, please let me know if thats possible.": no, you can't bypass the summing aggregation step.
Select clicks, string_agg(commercial::varchar,',' order by commercial asc) as commercials
from table
group by clicks
order by clicks asc;

CTE Hierarchy Question - 1 table, 2 columns

I'm new to CTEs, and I am slowly starting to understand the basics.
I have a table that essentially goes in this pattern:
CUSTOMER X
CUSTOMER Y
1
1
1
2
2
3
3
4
3
5
4
5
5
6
I was wondering if a CTE would help return the numbers 1 through 6 (CUSTOMER Y) if Number 1 in CUSTOMER X had a specific column flagged for relevancy.
Customer 1 would be considered the main customer, while 2 - 6 could be stores related to said customer.
My end goal would be propogating down this relevancy flag for customers 2 - 6 if customer 1 has it and I'm currently trying to figure out how to get that full list.
I'd want the CTE to return a distinct list of customers.
CUSTOMER
1
2
3
4
5
6

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;

DB Query matching ids and sum data on columns

Here is the info i have on my tables, what i need is to create a report based on certain dates and make a sum of every stock movement of the same id
Table one Table Two
Items Stocks
---------- ---------------------------
ID - NAME items_id - altas - bajas - created_at
1 White 4 5 0 8/10/2016
2 Black 2 1 5 8/10/2016
3 Red 3 3 2 8/11/2016
4 Blue 4 1 4 8/11/2016
2 10 2 8/12/2016
So based on a customer choice of dates (on this case lets say it selects all the data available on the table), i need to group them by items_id and then SUM all altas, and all bajas for that items_id, having the following at the end:
items_id altas bajas
1 0 0
2 11 7
3 3 2
4 6 4
Any help solving this?
Hope this will help:
Stock.select("sum(altas) as altas, sum(bajas) as bajas").group("item_id")

Sum quantity from 2 different tables where they have an id that which from other table

ProdStock
ID_Prod Description
1 tshirt
2 pants
3 hat
Donation
id_dona ID_Prod Quantity
1 1 10
2 2 20
3 1 30
4 3 5
Beneficiation
id_bene ID_Prod Quantity
1 1 -5
2 2 -10
3 1 -15
Table expected
ID_Prod Description Quantity
1 tshirt 20
2 pants 10
3 hat 5
Donation = what is given to the institution
beneficiation= institution gives to people in need
i need to achieve "Table expected" , i tried with sum. I dont have much knowledge in sql, it would be great if someone could help.
Since I have no idea what database you're actually working with, here is an idea how you might get in the right direction:
Select ProdStock.ID_Prod, ProdStock.Description,
(Sum(Donation.Quantity) + Sum(Beneficiation.Quantity)) as Quantity
From ProdStock
Inner Join Donation on ProdStock.ID_Prod=Donation.ID_Prod
Inner Join Beneficiation on ProdStock.ID_Prod=Beneficiation.ID_Prod
Group By ProdStock.ID_Prod, ProdStock.Description;