Partition Items by Item# and Location in SQL Server - sql

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

Related

How to input value from different table range in SQL

I want to ask how to enter NULL in Table 2 with values in LITHOLOGY column.
Thanks
Table 1
LITHOLOGY
SITE_ID
DEPTH_FROM
DEPTH_TO
BXg
DB-01
0.00
4.50
BXg2
DB-01
4.50
10.00
BXg2
DB-02
4.00
10.00
BXg2
DB-02
0.00
4.00
Table 2
SITE_ID
DEPTH_FROM
DEPTH_TO
CORE_RECOV
LITHOLOGY
DB-01
0.00
2.00
0.20
DB-01
2.00
4.00
0.30
DB-01
4.00
6.00
0.22
DB-01
6.00
8.00
0.32
DB-01
8.00
10.00
0.42
DB-02
0.00
3.00
0.12
DB-02
3.00
5.00
0.42
DB-02
5.00
10.00
0.92
As per your question, the below will put NULL in every LITHOLOGY column where LITHOLOGY is not null.
If you want to replace blank fields with null use WHERE LITHOLOGY = '' instead
UPDATE Table2
SET LITHOLOGY = NULL
WHERE LITHOLOGY IS NOT NULL;

Select Rows based on a condition in Pandas dataframe with groupby

I have a pandas dataframe as below -
Federation Game Medal_each_game
0 AFG Athletics 1.00
1 AFG Boxing 0.00
2 AFG Football 1.00
3 AFG Hockey 0.00
4 AFG Taekwondo 2.00
5 AFG Wrestling 0.00
6 AHO Athletics 0.00
7 AHO Boxing 3.00
8 AHO Fencing 2.00
9 AHO Football 0.00
I need to find highest medal count per 'federation' and get the 'Game'
output should be something like this
Federation Game Medal_each_game
0 AFG Taekwondo 2.00
1 AHO Boxing 3.00
Use groupby_idxmax:
>>> df.loc[df.groupby('Federation')['Medal_each_game'].idxmax()]
Federation Game Medal_each_game
4 AFG Taekwondo 2.0
7 AHO Boxing 3.0

SQLServer - Pivoting a table with Group

I am wondering if what I am trying to do is possible. I believe it is using the PIVOT function in TSQL but don't have enough experience with the PIVOT function to know where to start.
Basically I'm trying to take the following # table called #tmpbudgetdata (truncated for simplicity):
Account Description BudgetAmount Period
-------------------- ---------------------------------------------------------------------------------------------------- --------------------- --------------------
4001 Mood Embedded Account 0.00 1
4001 Mood Embedded Account 0.00 2
4001 Mood Embedded Account 0.00 3
4001 Mood Embedded Account 0.00 4
4001 Mood Embedded Account 0.00 5
4001 Mood Embedded Account 0.00 6
4001 Mood Embedded Account 0.00 7
4001 Mood Embedded Account 0.00 8
4001 Mood Embedded Account 0.00 9
4001 Mood Embedded Account 0.00 10
4001 Mood Embedded Account 0.00 11
4001 Mood Embedded Account 0.00 12
4003 DBS Music 0.00 1
4003 DBS Music 0.00 2
4003 DBS Music 0.00 3
4003 DBS Music 0.00 4
4003 DBS Music 0.00 5
4003 DBS Music 0.00 6
4003 DBS Music 0.00 7
4003 DBS Music 0.00 8
4003 DBS Music 0.00 9
4003 DBS Music 0.00 10
4003 DBS Music 0.00 11
4003 DBS Music 0.00 12
4010 Sales - Software 5040.00 1
4010 Sales - Software 0.00 2
4010 Sales - Software 6280.56 3
4010 Sales - Software 6947.93 4
4010 Sales - Software 4800.00 5
4010 Sales - Software 0.00 6
4010 Sales - Software 2400.00 7
4010 Sales - Software 2550.00 8
4010 Sales - Software 4800.00 9
4010 Sales - Software 2400.00 10
4010 Sales - Software 0.00 11
4010 Sales - Software 2400.00 12
4015 New Install Revenue 0.00 1
4015 New Install Revenue 0.00 2
4015 New Install Revenue 0.00 3
4015 New Install Revenue 3844.79 4
4015 New Install Revenue 0.00 5
4015 New Install Revenue 0.00 6
4015 New Install Revenue 0.00 7
4015 New Install Revenue 0.00 8
4015 New Install Revenue 0.00 9
4015 New Install Revenue 0.00 10
4015 New Install Revenue 0.00 11
4015 New Install Revenue 0.00 12
and turning it into something like this:
Account Description Period1 Period2 Period3 Period4 Period5 Period6 Period7 Period8 Period9 Period10 Period11 Period12
------- --------------- -------- ------- -------- ------ ------- ------- -------- ------ ------- -------- -------- --------
4001 Mood Enabled... 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
4003 Dbs Music 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
4010 Sales - Software 5040.00 0.00 6280.56 6947.93 4800.00 0.00 2400.00 2550.00 4800.00 2400.00 0.00 2400.00
...etc...
Basically just grouping via the Account column (the description is the same per account) and then taking the period values and pivoting them horizontally.
I know I could do it with a cursor and loop through but wondering if this is possible with a pivot or by other means.
Thanks in advance
I simple PIVOT should do the trick
Example
Select *
From (
Select [Account]
,[Description]
,Period = concat('Period',Period)
,[BudgetAmount]
From YourTable
) src
Pivot (sum([BudgetAmount]) for Period in ( [Period1],[Period2],[Period3],[Period4],[Period5],[Period6],[Period7],[Period8],[Period9],[Period10],[Period11],[Period12] ) ) pvt
Returns

SQL Server 2012 First_Value Function

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

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