SQL Server 2012 First_Value Function - sql
I am stuck in a case since 2 days and I need your assistance please.
the table as below :
Sample Table
SR UID Flag Fees
1 AAA 1 100.00
2 AAA 0 0.00
3 AAA 0 0.00
4 AAA 1 120.00
5 AAA 0 0.00
6 AAA 0 0.00
7 AAA 1 140.00
1 BBB 1 200.00
2 BBB 0 0.00
3 BBB 0 0.00
4 BBB 0 0.00
5 BBB 0 0.00
6 BBB 0 0.00
7 BBB 1 400.00
how I can use First_value function to replace the 0.00 values in fees column with the first value where Flag =1 partition by UID
the result should be as the following
Results
SR UID Flag Fees First_Value
1 AAA 1 100.00 100.00
2 AAA 0 0.00 100.00
3 AAA 0 0.00 100.00
4 AAA 1 120.00 120.00
5 AAA 0 0.00 120.00
6 AAA 0 0.00 120.00
7 AAA 1 140.00 140.00
1 BBB 1 200.00 200.00
2 BBB 0 0.00 200.00
3 BBB 0 0.00 200.00
4 BBB 0 0.00 200.00
5 BBB 0 0.00 200.00
6 BBB 0 0.00 200.00
7 BBB 1 400.00 400.00
One idea, using FIRST_VALUE like the Op was trying to:
CREATE TABLE #Sample (SR int, UID char(3), Flag bit, Fees decimal(5,2));
INSERT INTO #Sample
VALUES
(1,'AAA',1,100.00),
(2,'AAA',0,0.00 ),
(3,'AAA',0,0.00 ),
(4,'AAA',1,120.00),
(5,'AAA',0,0.00 ),
(6,'AAA',0,0.00 ),
(7,'AAA',1,140.00),
(1,'BBB',1,200.00),
(2,'BBB',0,0.00 ),
(3,'BBB',0,0.00 ),
(4,'BBB',0,0.00 ),
(5,'BBB',0,0.00 ),
(6,'BBB',0,0.00 ),
(7,'BBB',1,400.00);
WITH Groups AS(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY UID ORDER BY SR) -
ROW_NUMBER() OVER (PARTITION BY UID, Flag ORDER BY SR) AS Grp
FROM #Sample)
SELECT SR, UID, Flag, Fees,
FIRST_VALUE(Fees) OVER (PARTITION BY UID, CASE Grp WHEN 0 THEN 1 ELSE Grp END ORDER BY SR)
FROM Groups
ORDER BY UID, SR;
DROP TABLE #Sample
Related
Divide individual grouped items in pandas
Following df: appid tag totalvalue 0 1234 B 50.00 1 1234 BA 10.00 2 2345 B 100.00 3 2345 BA 25.00 4 2345 BCS 15.00 What we want is to group the df with appid and have some analysis based on tag column, is such that if each tag is divided by tag='B' with totalvalue. Just like follows: appid tag total %tage(B) 0 1234 B 50.00 1 1 1234 BA 10.00 0.2 2 2345 B 100.00 1 3 2345 BA 25.00 0.4 4 2345 BCS 15.00 0.15
You can use groupby: gmax = df['totalvalue'].where(df['tag'] == 'B').groupby(df['appid']).transform('max') df['%tage(B)'] = 1 / (gmax / df['totalvalue']) print(df) # Output appid tag totalvalue %tage(B) 0 1234 B 50.0 1.00 1 1234 BA 10.0 0.20 2 2345 B 100.0 1.00 3 2345 BA 25.0 0.25 4 2345 BCS 15.0 0.15
Calculation over pandas groupby object with condition within groups
I have a df as follows: appid month tag totalvalue 0 1234 02-'22 B 50.00 1 1234 02-'22 BA 10.00 2 1234 01-'22 B 100.00 3 2345 03-'22 BA 25.00 4 2345 03-'22 B 100.00 5 2345 04-'22 BB 100.00 Output what I want is follows: appid month tag totalvalue %tage 0 1234 02-'22 B 50.00 1.0 1 1234 02-'22 BA 10.00 0.2 2 1234 01-'22 B 100.00 1.0 3 2345 03-'22 BA 25.00 0.25 4 2345 03-'22 B 100.00 1.0 5 2345 04-'22 BB 100.00 inf I want to have group variables based on appid & month. Moreover want to check if there are tag=B is available in that group just divide other tag's totalvalue with it. If not shows the inf I have tried with df.groupby(['appid', 'month'])['totalvalue'] but unable to replicate them with condition of tag=B as denominator over groupby object.
IIUC, you can use a groupby.transform('first') on the masked totalvalue, then use it a divider: m = df['tag'].eq('B') df['%tage'] = (df['totalvalue'] .div(df['totalvalue'].where(m) .groupby([df['appid'], df['month']]) .transform('first').fillna(0)) ) output: appid month tag totalvalue %tage 0 1234 02-'22 B 50.0 1.00 1 1234 02-'22 BA 10.0 0.20 2 1234 01-'22 B 100.0 1.00 3 2345 03-'22 BA 25.0 0.25 4 2345 03-'22 B 100.0 1.00 5 2345 04-'22 BB 100.0 inf
Partition Items by Item# and Location in SQL Server
Given that I have the following dataset in TempTable: Item Item Desc Location Qty LeasedQty ---------------------------------------------------------------- IT2250 1/2CANTOP NYC 1.00 30.00 IT5550 FCM 2K NYC 6.00 8.00 IT2075 HPTL 750 LA 4.00 44.00 IT12506 DOUBLE DOOR 10" CALI 60.00 0.00 IT3606 BAG180 CALI 25.00 0.00 IT3606 BAG180 NYC 20.00 40.00 IT3606 BAG180 LA 5.00 45.00 IT50 2K NYC 6.00 8.00 IT50 2K LA 4.00 44.00 IT50 2K CALI 60.00 0.00 How can I partition this data so that It will be Like the following: Item Item Desc Location Qty LeasedQty RNK ---------------------------------------------------------------------- IT2250 1/2CANTOP NYC 1.00 30.00 1 IT5550 FCM 2K NYC 6.00 8.00 2 IT2075 HPTL 750 LA 4.00 44.00 3 IT12506 DOUBLE DOOR 10" CALI 60.00 0.00 4 IT3606 BAG180 CALI 25.00 0.00 5 IT3606 BAG180 NYC 20.00 40.00 5 IT3606 BAG180 LA 5.00 45.00 5 IT50 2K NYC 6.00 8.00 6 IT50 2K LA 4.00 44.00 6 IT50 2K CALI 60.00 0.00 6 Basically, I want the data to group by each item and gather the TOP 20 items based on the QTY (DESCENDING)
To get that rank this will help you SELECT Dense_rank() OVER ( ORDER BY Item) rnk, * FROM TempTable Added to get top 20 records SELECT TOP 20 * FROM (SELECT Dense_rank() OVER ( ORDER BY Item,qty desc) rnk, * FROM TempTable) a WHERE rnk <= 20
Adding values from different records
From the below table I want to add 2 values from the same records and one value from different record, that are (extraaamt from the first record)+(trnamt from the second record) 5140560000001183 1016.00 0.00 2014-05-23 R 0.00 1017 13 5140560000001183 1016.00 0.00 2014-05-24 N 30.00 1017 0 carno emi recamt lastrecdate status penamt trnamt extraamt 5140560000001183 1016.00 0.00 2014-05-23 R 0.00 1017 13 5140560000001191 880.00 0.00 2014-05-23 R 0.00 880 0 5140560000001142 934.00 0.00 2014-05-23 P 0.00 500 0 5140560000001209 963.00 0.00 2014-05-23 P 0.00 600 0 5140560000001175 1024.00 0.00 2014-05-23 N 0.00 0 0 5140560000001167 1117.00 0.00 2014-05-23 N 0.00 0 0 5140560000001159 834.00 0.00 2014-05-23 N 0.00 0 0 5140560000001183 1016.00 0.00 2014-05-24 N 30.00 1017 0 5140560000001191 880.00 0.00 2014-05-24 N 0.00 880 0 5140560000001142 934.00 0.00 2014-05-24 N 0.00 500 0 5140560000001209 963.00 0.00 2014-05-24 N 0.00 600 0 5140560000001175 1024.00 0.00 2014-05-24 N 0.00 0 0 5140560000001167 1117.00 0.00 2014-05-24 N 0.00 0 0 5140560000001159 834.00 0.00 2014-05-24 N 0.00 0 0 I have used the below query but still it is not helping: Select Case WHEN ( lastrecdate=( cast (GETDATE() as DATE))and CardNo=CardNo and Status in('N','P') ) then trnammt else 0 end + Case WHEN ( lastrecdate=( cast (GETDATE() as DATE))and CardNo=CardNo and Status in('N','P')) then pendamt else 0 end + Case WHEN (lastrecdate= (select MAX(lastrecdate ) from Tbl_Emi WHERE Status ='R' and CardNo=CardNo) ) then extraamt else 0 end as totalamount from Tbl_Emi where CardNo=CardNo
Please google on CrossTab query/Pivot Query. You can achieve this task using this. CrossTab query is amazing, which helps generating reports and play with aggregate values. Excel/Ms Access gives nice user interface for Pivot Table. It’s way to transfer rows into column. It is more often used to generate matrix form of report. Look at this blog.
select tt.carno, t1.extraamt+t2.trnamt total from (select t.carno, MIN(t.lastrecdate) first , MAX(t.lastrecdate) second from dbo.[Table] t group by t.carno) tt inner join dbo.[Table] t1 on t1.carno=tt.carno and t1.lastrecdate=tt.first inner join dbo.[Table] t2 on t2.carno=tt.carno and t2.lastrecdate=tt.second
Simple SQL math operation gives incorrect results
I am running into an issue with a simple SQL math operation of qty * price is returning an incorrect value. This is SQL server 2005. Compatibility is set to 80 for 2000 SQL server. Any help on understanding why I am having the problem shown below For example: Transaction Table: id price qty 1 2.77 20.00 1 2.77 25.00 1 2.77 10.00 2 0.10 50.00 2 0.10 80.00 3 0.10 50.00 3 0.10 60.00 SQL Select id, price, qty, (qty * price) from transact The actual problem was this and it was my fault :( Select id, CAST(price AS DECIMAL(5,2)), qty, (qty * price) from transact Returns the following: id price qty Total 1 2.77 20.00 55.400000 Correct 1 2.77 25.00 69.250000 Correct 1 2.77 10.00 27.700000 Correct 2 0.10 50.00 4.800000 Should be 5.0000 2 0.10 80.00 7.680000 Should be 8.0000 2 0.10 50.00 5.050000 Should be 5.0000 2 0.10 60.00 6.060000 Should be 6.0000 3 39.00 1.00 39.000000 Correct 3 39.00 2.00 78.000000 Correct 3 39.00 3.00 117.000000 Correct
You price is being rounded somewhere. The select you are running is not showing the actual price. select round(0.096, 2) price, 0.096 * 50.00 total Result: price total 0.10 4.80000