Multiple rows data into multiple columns in one table - sql

I have a table called Product Variant.
sequence No item
400 1 4.5
500 1 0
501 1 0
502 1 0
503 1 B-DP
504 2 0
400 1 2.5
500 2 0
501 2 0
502 2 0
503 2 B-PP
504 2 0
My Required output is :
sequence No item item1
503 1 B-DP 4.5
503 2 B-PP 2.5
I am trying but not coming as expected.. Can anyone suggest me on this please.
Thanks in Advance.

Something like this?
select max(case when item like 'B%' then sequence end),
no,
sum(try_convert(numeric(38, 6), item1))
from t
group by no;

Related

How to calculate leftovers of each balance top-up using first in first out technique?

Imagine we have user balances. There's a table with top-up and withdrawals. Let's call it balance_updates.
transaction_id
user_id
current_balance
amount
created_at
1
1
100
100
...
2
1
0
-100
3
2
400
400
4
2
300
-100
5
2
200
-200
6
2
300
100
7
2
50
-50
What I want to get off this is a list of top-ups and their leftovers using the first in first out technique for each user.
So the result could be this
top_up
user_id
leftover
1
1
0
3
2
50
6
2
100
Honestly, I struggle to turn it to SQL. Tho I know how to do it on paper. Got any ideas?

Turn MultiIndex Series into pivot table design by unique value counts

Sample Data:
Date,code
06/01/2021,405
06/01/2021,405
06/01/2021,400
06/02/2021,200
06/02/2021,300
06/03/2021,500
06/02/2021,500
06/03/2021,300
06/05/2021,500
06/04/2021,500
06/03/2021,400
06/02/2021,400
06/04/2021,400
06/03/2021,400
06/01/2021,400
06/04/2021,200
06/05/2021,200
06/02/2021,200
06/06/2021,300
06/04/2021,300
06/06/2021,300
06/05/2021,400
06/03/2021,400
06/04/2021,400
06/04/2021,500
06/01/2021,200
06/02/2021,300
import pandas as pd
df = pd.read_csv(testfile.csv)
code_total = df.groupby(by="Date",)['code'].value_counts()
print(code_total)
Date code
06/01/2021 400 2
405 2
200 1
06/02/2021 200 2
300 2
400 1
500 1
06/03/2021 400 3
300 1
500 1
06/04/2021 400 2
500 2
200 1
300 1
06/05/2021 200 1
400 1
500 1
06/06/2021 300 2
dates = set([x[0] for x in code_total.index])
codes = set([x[1] for x in code_total.index])
test = pd.DataFrame(code_total,columns=sorted(codes),index=sorted(dates))
print(test)
Is there a way to transpose the second index into a column and retain the value for the counts? Ultimately I'm trying to plot the count of unique error codes on a line graph. I've been searching up many different ways but am always missing something. any help would be appreciated.
Use Series.unstack:
df = df.groupby(by="Date",)['code'].value_counts().unstack(fill_value=0)

How to display table of top 5 URL with their status and percentage on splunk

Need a table to show the top 5 URL as given below in Splunk. Is this possible in Splunk? I tried many ways but I can't get all status of a URL as a single row.
API 200 204 400 401 499 500
/wodetails/ACP 895(50%) - - - - 1
This is a case where the chart command can be used:
index="main" source="access.log" sourcetype="access_combined"
| chart c(status) by uri, status
uri
200
204
400
499
/basic/status
11
1
1
1
/search/results
3
0
0
0
To add the percentages, you can use eventstats
index="main" source="access.log" sourcetype="access_combined"
| eventstats count as "totalCount" by uri
| eventstats count as "codecount" by uri, status
| eval percent=round((codecount/totalCount)*100)
| eval cell=codecount." (".percent."%)"
| chart values(cell) by uri,status
uri
200
204
400
499
/basic/status
11 (79%)
1 (7%)
1 (7%)
1 (7%)
/search/results
3 (100%)

Merge two dataframes on different named columns for multiple columns

I have two dataframes: Users and Item_map.
Users consists of user and fake_item_ids stored in three columns.
Item_map consists of real_item_ids and fake_item_ids.
What I want is to replace all of the the fake_item_ids with the real_item_ids.
To illustrate with dummy code:
DataFrame Users
user fake_0 fake_1
0 1 6786 3938
1 2 6786 6786
2 3 4345 4345
3 4 7987 3938
4 5 7987 5464
DataFrame Item_map
real_id fake_id
0 101 7987
1 202 6786
2 303 5464
3 404 4345
4 505 3938
Expected results:
DataFrame Users
user real_0 real_1
0 1 202 505
1 2 202 202
2 3 404 404
3 4 101 505
4 5 101 303
I have tried the following, based on an answer found here: how to concat two data frames with different column names in pandas? - python
users['fake_0'] = users.merge(items.rename(columns={'fake_id': 'fake_0'}), how='inner')['real_id']
which resulted in this:
user fake_0 fake_1
0 1 202 3938
1 2 202 6786
2 3 404 4345
3 4 101 3938
4 5 101 5464
This works, but it seems silly to do so for every column separately (I have nine columns that have fake_ids that need to be real_ids).
Any help is much appreciated!
Dummy code:
users = pd.DataFrame({
'user': [1, 2, 3, 4, 5],
'fake_0': [6786, 6786, 4345, 7987, 7987],
'fake_1': [3938, 6786, 4345, 3938, 5464]
})
item_map = pd.DataFrame({
'real_id': [101, 202, 303, 404, 505],
'fake_id': [7987, 6786, 5464, 4345, 3938]
})
We using replace
df.replace(dict(zip(df1.fake_id,df1.real_id)))
Out[46]:
user fake_0 fake_1
0 1 202 505
1 2 202 202
2 3 404 404
3 4 101 505
4 5 101 303
I'm not sure if this will be the most efficient solution, but it should work for your example with 10 columns without you having to edit anything.
First, create a lookup dictionary from your item_map:
d = pd.Series(index=item_map['fake_id'], data=item_map['real_id'].values).to_dict()
Then, use applymap to look up each column except 'user':
results = users.set_index('user').applymap(lambda x: d[x]).reset_index()
If you want, you can then rename the columns to get your desired output:
results.columns = [col.replace('fake', 'real') for col in results.columns]
Results:
user real_0 real_1
0 1 202 505
1 2 202 202
2 3 404 404
3 4 101 505
4 5 101 303

SQL Query to turn my columns un to rows

Im looking for a way to make this happen:
From this:
id date budget forecast actual
0 1/1/111 100 200 5
1 2/1/111 10 20 3
2 3/1/111 300 5000 1
3 4/1/111 400 800 0
To this:
column1 column2 column3 column4 column5
id 0 1 2 3
date 1/11/1111 2/11/1111 3/11/1111 4/11/1111
budget 100 10 300 400
forecast 200 20 5000 800
actual 5 3 1 0
Is there any way of doing this in SQL?
Thanks in advance.