If I have data like...
user_id user_revenue
10000 100
10001 150
10002 175
10003 200
10004 250
10005 300
10006 400
10007 500
10007 550
I'm trying to transform the data into this using union:
user_id user_revenue
10000 Low
10001 Low
10002 Low
10003 Medium
10004 Medium
10005 Medium
10006 High
10007 High
10007 High
Where "High" is when revenue is at least 400 and "medium" is less than 400 but greater than 175 then low is when the revenue is at most 175.
This is simple CASE statement on user_revenue
SELECT user_id,
CASE
WHEN t.user_revenue > 400 THEN 'High'
WHEN t.user_revenue > 175 THEN 'Medium'
ELSE 'Low'
END AS user_revenue
FROM [Table] t;
Related
I have a table structure like this:
|Account | MaxBalance | UsedAmount|
|--------|-------------|-----------|
Acc1 10000 2000
Acc2 20000 4000
Acc3 50000 2000
Acc3 50000 3000
Acc3 50000 8000
Acc4 20000 1000
Acc4 20000 5000
Acc5 5000 500
The output that I want is for same account usedAmount is dynamically deleted from new remainingAmount.
Account | MaxBalance | UsedAmount | Remaining
----------------------------------------------
Acc1 10000 2000 8000
Acc2 20000 4000 16000
Acc3 50000 2000 48000
Acc3 50000 3000 45000
Acc3 50000 8000 37000
Acc4 20000 1000 19000
Acc4 20000 5000 14000
Acc5 5000 500 4500
This is really cumulative sum -- and then subtraction using that. However, you need a column that specifies the ordering, so I'll assume you have one (otherwise the question doesn't make sense):
select t.*,
(maxbalance -
sum(usedamount) over (partition by account order by <ordering col>)
) as remaining
from t;
Here is a db<>fiddle showing that this does exactly what your question is asking for.
I am trying to build a logic to identify a duplicate value in my data. The data has many 4 columns SR number, Sales order, item number, and value.
The 1st column (SR number) is always going to be a unique number - primary key.
The logic I am looking at is: if the Sr number has the same sales order, item number, and the value then its a duplicate, give me a yes in front of the row.
Sample data :
SR Number Sales order Item no Amount
------------------------------------------
1001 A201 100 $5,000
1002 B403 200 $25
1003 B403 300 $25
1004 C303 100 $5,000
1005 C303 300 $25
1006 A201 100 $5,000
1007 A201 100 $5,000
1008 D707 500 $230
1009 C303 300 $25
1010 D707 500 $230
Expected results:
SR Number Saler order Item no Amount Duplicate
1001 A201 100 $5,000 Yes
1002 B403 200 $25 No
1003 B403 300 $25 No
1004 C303 100 $5,000 No
1005 C303 300 $25 Yes
1006 A201 100 $5,000 Yes
1007 A201 100 $5,000 Yes
1008 D707 500 $230 Yes
1009 C303 300 $25 Yes
1010 D707 500 $230 Yes
Thank you for your help in advance!
This is tagged SQL, so I'll give a SQL solution:
select t.*,
(case when count(*) over (partition by sr_number, sale_order) > 1
then 'Yes' else 'No'
end) as duplicate_flag
from t;
If you need this done in Tableau, you can use a Level of Detail calculation. Concatenate the three fields into one string and then count how many times they appear across the data set.
if {fixed [Sales Order]+str([Item Number])+str([Amount]): count([Number of Records])}>1 then 'Yes' else 'No' end
1) StructureTable:
StrId StrName Figure Base On
1 Basic 40 Percent Total
2 D.A. 3495 Amount Total
3 O.A. 0 Remaining Total
2) SalaryTable
StaffId StaffName Salary
1 Ram 25000
2 Shyam 40000
3 Hari 30000
4 Ganesh 15000
3) IncrementTable
StaffId IncAmt
1 5000
2 3000
3 2000
4 4000
Desired Columns Resulted Output by Pivot or others:
StaffId StaffName Basic_Salary D.A_Salary O.A_Salary Total_Salary Basic_Inc D.A_Inc O.A_Inc Total_Inc Basic_Total D.A_Total O.A_Total Total_Total
1 Ram 10000 3495 18495 25000 2000 0 3000 5000 12000 3495 21495 30000
2 Shyam 16000 3495 27495 40000 1200 0 1800 3000 17200 3495 29295 43000
3 Hari 12000 3495 21495 30000 800 0 1200 2000 12800 3495 22695 32000
4 Ganesh 6000 3495 12495 15000 1600 0 2400 4000 7600 3495 14895 19000
Total 44000 13980 79980 110000 5600 0 8400 14000 49600 13980 88380 124000
I am having two tables here from which I needed to add two columns.
table 1 table 2
1 ram 100 null 1 ram 100 1000
2 ram 200 1000 2 ram 200 null
3 ram 100 2000 3 ram 100 3000
4 ram 100 3000 4 ram 100 4000
5 ram 100 null 5 ram 100 5000
1 rahim 100 5000 1 rahim 100 null
2 ram 200 6000 2 ram 200 7000
3 ram 200 null 3 ram 200 8000
4 ram 200 null 4 ram 200 9000
5 rahim 100 9000 5 rahim 100 null
1 robert 100 10000 1 robert 100 11000
2 rahim 200 11000 2 rahim 200 12000
3 ram 300 12000 3 ram 300 null
4 rahim 400 13000 4 rahim 400 14000
5 robert 100 14000 5 robert 100 15000
result should be in the form:
1 ram 100 1000
2 ram 200 -1000
3 ram 100 1000
4 ram 100 1000
5 ram 100 5000
1 rahim 100 -5000
2 ram 200 1000
3 ram 200 8000
4 ram 200 9000
5 rahim 100 -9000
1 robert 100 1000
2 rahim 200 1000
3 ram 300 -12000
4 rahim 400 1000
5 robert 100 1000
You can use join with coalesce to remove the null values:
select t1.id, t1.somefield, t1.someint,
coalesce(t2.someint2,0)-coalesce(t1.someint2,0)
from table1 t1
join table2 t2 on t1.id = t2.id
and t1.somefield = t2.somefield
and t1.someint = t2.someint
SQL Fiddle Demo
Based on your input data, this joins on the first 3 columns. Not completely sure this is what you want, but should get you going in the correct direction.
I think try subtract table2.col4 with table1.col4.
SELECT a.col1,
a.col2,
a.col3,
NVL(a.col4, 0) - NVL(b.col4, 0) SUB
FROM table1 A
JOIN table2 B
ON A.col1 = b.col1
AND a.col2 = b.col2
I have a table like this:
Table A
Customer InvoiceNo Region Type Amount
A001 10001 Europe FG 100
B001 10002 Asia FG 200
C001 10003 America MISC 50
D001 10004 Asia FG 300
A001 10005 Europe MISC 20
C001 10006 America MISC 10
B001 10007 Asia FG 300
I wish to split the Amount into 2 columns, namely Sales_Amt & Misc. The Region is not required.
The result that I wish to have should look like this:
Customer InvoiceNo Type Sales_Amt MISC
A001 10001 FG 100 0
B001 10002 FG 200 0
C001 10003 MISC 0 50
D001 10004 FG 300 0
A001 10005 MISC 0 20
C001 10006 MISC 0 10
B001 10007 FG 300 0
Thanks.
That's pretty simple:
select
Customer,
InvoiceNo,
Type,
decode(Type, 'MISC', 0, 1) * Amount as Sales_Amt,
decode(Type, 'MISC', 1, 0) * Amount as Misc
from
TableA
;
Select Customer,InvoiceNo,Type,
Case when TYPE!='MISC' Then amount Else 0 End as Sales_Amt,
Case when TYPE='MISC' Then amount Else 0 End as Misc
From TabalA