Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 days ago.
Improve this question
Is there a way I can get previous SKU data in present row in SQL Server:
Date
Fol
SKU
Amount
01-01-2021
44
1
2
01-01-2021
44
2
3
05-03-2021
45
1
4
05-03-2021
45
2
5
08-06-2021
46
1
6
08-06-2021
46
2
7
13-08-2021
47
1
8
13-08-2021
47
2
9
Expected
Date
Fol
SKU
Amount
Previous Amount
01-01-2021
44
1
2
---------
05-03-2021
45
1
4
2
08-06-2021
46
1
6
4
13-08-2021
47
1
8
6
01-01-2021
44
2
3
---
05-03-2021
45
2
5
3
08-06-2021
46
2
7
5
13-08-2021
47
2
9
7
I've tried lag function but it's not resulting as expected
Date
Fol
SKU
Amount
Previous Amount
01-01-2021
44
1
2
---------
01-01-2021
44
2
3
2
05-03-2021
45
1
4
3
05-03-2021
45
2
5
4
08-06-2021
46
1
6
5
08-06-2021
46
2
7
6
13-08-2021
47
1
8
7
13-08-2021
47
2
9
8
Thanks!
The window functions are well worth your time to get comfortable with them
Select *
,PrevAmt = lag(AMOUNT,1) over (partition by SKU order by date)
From YourTable
Results
Related
I wanted to select data based on the below considerations -
Now we have two tables -
TABLE 1 -
DLR_ID
CALL_ID
VEHICLE
PLAN_NO
CALL_STATUS
1
11
AA
5
Generated
2
12
AA
5
Generated
1
13
AA
10
Generated
2
14
AA
10
Not Generated
1
15
BB
5
Generated
1
16
BB
10
Generated
2
17
CC
5
Not Generated
3
18
CC
5
Generated
1
19
DD
5
Not Generated
4
20
DD
5
Not Generated
3
21
EE
5
Generated
2
22
FF
10
Generated
4
23
FF
10
Generated
5
24
GG
20
Generated
6
25
GG
20
Generated
TABLE 2 -
DLR_ID
CALL_ID
CALL_COUNT
CALL_RESULT_STATUS
CALL_DATE(DD/MM/YYYY)
1
11
1
Continue
16/03/2021
1
11
2
Give-up
20/03/2021
2
12
1
Completed
15/03/2021
1
13
1
Continue
01/04/2021
1
15
1
Completed
21/02/2021
1
16
1
Give-up
20/03/2021
3
18
1
Continue
21/05/2021
3
21
1
Give-up
24/04/2021
2
22
1
Completed
19/03/2021
4
23
1
Completed
03/05/2021
5
24
1
Continue
11/02/2021
5
24
2
Completed
11/05/2021
6
25
1
Continue
10/02/2021
6
25
2
Continue
21/02/2021
6
25
3
Continue
21/04/2021
OUTPUT -
DLR_ID
VEHICLE
PLAN_NO
CALL_STATUS
CALL_ID
CALL_DATE
CALL_RESULT_STATUS
1
AA
5
Generated
12
15/03/2021
Completed
2
AA
5
Generated
12
15/03/2021
Completed
1
AA
10
Generated
13
01/04/2021
Continue
2
AA
10
Not Generated
13
01/04/2021
Continue
1
BB
5
Generated
15
21/02/2021
Completed
1
BB
10
Generated
15
21/02/2021
Completed
2
CC
5
Not Generated
18
21/05/2021
Continue
3
CC
5
Generated
18
21/05/2021
Continue
1
DD
5
Not Generated
4
DD
5
Not Generated
3
EE
5
Generated
21
21/04/2021
Give-up
2
FF
10
Generated
23
03/05/2021
Completed
4
FF
10
Generated
23
03/05/2021
Completed
5
GG
20
Generated
24
11/05/2021
Completed
6
GG
20
Generated
24
11/05/2021
Completed
Kindly help me out in extracting the building oracle query to extract the data like mentioned in OUTPUT table.
Code which I was trying is -
SELECT t1.DLR_id, t1.VEHICLE,t1.PLAN_NO,t1.CALL_STATUS,
NVL(MAX(CASE WHEN t1.CALL_STATUS='Generated' and t2.CALL_RESULT_STATUS = 'Completed' THEN t2.CALL_ID END),
MAX(CASE WHEN t1.CALL_STATUS!='Generated' and t2.CALL_RESULT_STATUS != 'Completed' THEN t2.CALL_ID END)) as CALL_ID
FROM Table1 t1
left JOIN Table2 t2
ON t1.DLR_ID=t2.DLR_ID
and t2.call_id = t1.call_id
group by T1.DLR_ID,t1.VEHICLE,t1.PLAN_NO,
T1.CALL_STATUS
order by t1.VEHICLE,t1.plan_no,t1.dlr_id
I have a dataset, in which it has a lot of entries for a single location. I am trying to find a way to sum up all of those entries without affecting any of the other columns. So, just in case I'm not explaining it well enough, I want to use a dataset like this:
Locations Cyclists maleRunners femaleRunners maleCyclists femaleCyclists
Bedford 10 12 14 17 27
Bedford 11 40 34 9 1
Bedford 7 1 2 3 3
Leeds 1 1 2 0 0
Leeds 20 13 6 1 1
Bath 101 20 33 41 3
Bath 11 2 3 1 0
And turn it into something like this:
Locations Cyclists maleRunners femaleRunners maleCyclists femaleCyclists
Bedford 28 53 50 29 31
Leeds 21 33 39 1 1
Bath 111 22 36 42 3
Now, I have read up that a groupby should work in a way, but from my understanding a group by will change it into 2 columns and I don't particularly want to make hundreds of 2 columns and then merge it all. Surely there's a much simpler way to do this?
IIUC, groupby+sum will work for you:
df.groupby('Locations',as_index=False,sort=False).sum()
Output:
Locations Cyclists maleRunners femaleRunners maleCyclists femaleCyclists
0 Bedford 28 53 50 29 31
1 Leeds 21 14 8 1 1
2 Bath 112 22 36 42 3
Pivot table should work for you.
new_df = pd.pivot_table(df, values=['Cyclists', 'maleRunners', 'femalRunners',
'maleCyclists','femaleCyclists'],index='Locations', aggfunc=np.sum)
I have a data set like this:
id_tecnico dia hora total
<chr> <dbl> <int> <int>
1 0011ab4f-6871-40f4-91f2-818e309baa41 8 13 1
2 0011ab4f-6871-40f4-91f2-818e309baa41 45 10 1
3 0011ab4f-6871-40f4-91f2-818e309baa41 46 9 1
4 0011ab4f-6871-40f4-91f2-818e309baa41 50 14 1
5 0011ab4f-6871-40f4-91f2-818e309baa41 58 12 1
6 0011ab4f-6871-40f4-91f2-818e309baa41 70 12 1
7 0011ab4f-6871-40f4-91f2-818e309baa41 81 11 1
8 0011ab4f-6871-40f4-91f2-818e309baa41 86 11 1
9 0011ab4f-6871-40f4-91f2-818e309baa41 89 9 1
10 0011ab4f-6871-40f4-91f2-818e309baa41 92 11 1
I would like to group the data the column total by hour, but I would like the result by column, not by row, creating a new column for each hour sum : hour1, hour2, hour3...
Can someone help me?
Thanks in advance for any assistance you can provide. I have spent hours on this with no luck.
I'm working with an indented bill of material table which has an end part id, a sequence number, a level and a quantity. The goal is to determine the total cumulative quantity of each row on this table.
What makes this difficult is that to determine the total quantity, the child quantity needs to be multiplied by it's parent quantity. The parent quantity of that parent needs to be multiplied by it's parent quantity and so on.
For example, a level 3 part has a component quantity of 5. It's parent the level 2 part has a component quantity of 2. It's parent the level 1 part has a component quantity of 3. This means the level 3 part total quantity is 30 (3 X 2 X 5).
With the assistance of SO (specifically #KKK) the parent quantity was able to be calculated in the below query. After this was resolved I realized I now need two additional columns, one that shows the Rolled/Cumulative quantities of the parent rows and one that shows the total quantity of the child.
The attached screenshot has the two columns that need to be added highlighted in yellow. Here is the current SQL (using Oracle 10.2) for the columns that are not in yellow:
select
end_part_id, sort_seq_no, indented_lvl, comp_qty,
(select distinct first_value(a.comp_qty) over (order by a.sort_seq_no desc, TRIM(a.indented_lvl) desc)
from
report_table a
where
a.end_part_id = b.end_part_id
and a.sort_seq_no < b.sort_seq_no
and TRIM(a.indented_lvl) < TRIM(b.indented_lvl)) as "PARENT_QTY"
from report_table b
Expected Results
END_PART_ID SORT_SEQ_NO INDENTED_LVL COMP_QTY PARENT_QTY ROLLED_PARENT QTY TOTAL_QTY
PARTX 1 1 2 1 1 2
PARTX 2 2 5 2 2 10
PARTX 3 3 2 5 10 20
PARTX 4 4 1 2 20 20
PARTX 5 5 1 1 20 20
PARTX 6 6 1 1 20 20
PARTX 7 5 4 1 20 80
PARTX 8 6 1 4 80 80
PARTX 9 2 7 2 2 14
PARTX 10 3 2 7 14 28
PARTX 11 3 2 7 14 28
PARTX 12 4 1 2 28 28
PARTX 13 4 1 2 28 28
PARTX 14 3 8 7 14 112
PARTX 15 1 1 1 1 1
PARTX 16 2 7 1 1 7
PARTX 17 3 2 7 7 14
PARTX 18 3 2 7 7 14
PARTX 19 4 1 2 14 14
PARTX 20 4 1 2 14 14
I am a beginner with basic knowledge.
I have a single table that I am trying to pull all UID's that have not had a particular code in the table within the past year.
My table looks like this: (but much larger of course)
FACID DPID EID DID UID DT Code Units Charge ET Ord
1 1 6 2 1002 15-Mar-07 99204 1 180 09:36.7 1
1 1 7 5 10004 15-Mar-07 99213 1 68 02:36.9 1
1 1 24 55 25887 15-Mar-07 99213 1 68 43:55.3 1
1 1 25 2 355688 15-Mar-07 99213 1 68 53:20.2 1
1 1 26 5 555654 15-Mar-07 99213 1 68 42:22.6 1
1 1 27 44 135514 15-Mar-07 99213 1 68 00:36.8 1
1 1 28 2 3244522 15-Mar-07 99214 1 98 34:59.4 1
1 1 29 5 235445 15-Mar-07 99213 1 68 56:42.1 1
1 1 30 3 3214444 15-Mar-07 99213 1 68 54:56.5 1
1 1 33 1 221444 15-Mar-07 99204 1 180 37:44.5 1
I am attempting to use the following, but this is not working for my time frame limits.
select distinct UID from PtProcTbl
where DT<'20120101'
and NOT EXISTS (Select Distinct UID
where Code in ('99203','99204','99205','99213',
'99214','99215','99244','99245'))
I need to know how to make sure the UID's that I am pulling are the ones don't have a DT after the 1/1/2012 cut off date that contains one of the NOT Exists codes.
The above query returned UID's that actually dates after 1/1/2012 that does contain one of the above codes...
Not sure what I am doing wrong or if I am totally off base on this..
Thanks in advance.
Are you sure you need the NOT EXISTS? How about instead:
AND Code NOT IN ('99203','99204','99205','99213','99214','99215','99244','99245')