Create the stored procedure to compare records and update system generated record - sql-server-2014-express

I want help for this: I have 4 vehicle records from different users:
vehicle_id vehicle_name vehicle_number date_update
usr1 10 My Ferrari 1000 NULL
usr2 11 My Ferrari 1000 NULL
usr3 12 My Ferrari 1000 NULL
usr4 13 My Ferrari 1001 NULL
On this records I want common record so, I have applied Group By and create separate record as my system generated record
vehicle_id vehicle_name vehicle_number date_update
sys 14 My Ferrari 1000 NULL
Now here is my condition:
Condition 1: 3 records are the same and one is different
If any user updates his own record, then system or script also re-calculate or compare all four records and update the column in my sys record
I am also setting record updated date.
E.g
vehicle_id vehicle_name vehicle_number date_update
usr1 10 My Ferrari 1000 NULL
usr2 11 My Ferrari 1001 09/03/2018
usr3 12 My Ferrari 1001 09/03/2018
usr4 13 My Ferrari 1001 NULL
So my sys record also get updated
vehicle_id vehicle_name vehicle_number date_update
sys 14 My Ferrari 1001 09/03/2018
Condition 2: Every 2 records having same value on this condition need to get latest UPDATED record
E.g
vehicle_id vehicle_name vehicle_number date_update
usr1 10 My Ferrari 1002 09/03/2018 8:55 AM
usr2 11 My Ferrari 1001 09/03/2018 7:55 AM
usr3 12 My Ferrari 1001 09/03/2018 7:30 AM
usr4 13 My Ferrari 1002 09/03/2018 8:57 AM
So my sys record also get updated
vehicle_id vehicle_name vehicle_number date_update
sys 14 My Ferrari 1002 09/03/2018
Condition 3: Every record contain different records on this condition need to get latest UPDATED record
E.g
vehicle_id vehicle_name vehicle_number date_update
usr1 10 My Ferrari 1001 09/03/2018 8:55 AM
usr2 11 My Ferrari 1003 09/03/2018 7:55 AM
usr3 12 My Ferrari 1004 09/03/2018 7:30 AM
usr4 13 My Ferrari 1005 09/03/2018 8:57 AM
So my sys record also get updated
vehicle_id vehicle_name vehicle_number date_update
sys 14 My Ferrari 1005 09/03/2018
So what is the best way to handle all three conditions in a stored procedure and update my Sys record accordingly?
Thank you.

To summarize ur problem..in all three condition sys record is always that record that is updated by user lately.
Sol: use insert and update trigger.
SP wont solve ur problem here.
Into that trigger script use order by date modified as key column to get latest vaule with dense_rank to evalute latest record for more than one value.
Hope this will solve ur problem

Related

Add columns values in pyspark for the same customerID

I have two DF one is full load and other is incremental, I want to add data for the customer_ID which are same in both the data_frames. So that my final output is that the customer_ID is same but the value is updated. I want to do it in data frame, I have another approach which is upsert but for that few staging tables have to be made which I want to avoid , how can I do add the data to the main table
Full Table
customer_ID
amount
ABC1234
20
ABC1235
10
ABC2222
20
ABC1345
10
ABC4444
5
ABC5555
9
incremental_load
customer_ID
amount
ABC1234
5
ABC1235
5
ABC2222
7
ABC1345
20
ABC9999
15
Desired_output
customer_ID
amount
ABC1234
25
ABC1235
15
ABC2222
27
ABC1345
30
ABC4444
5
ABC5555
9
ABC9999
15
You can union both dataframes and then sum all entries for the same customer:
from pyspark.sql import functions as F
dfFull= ...
dfInc=...
dfResult=dfFull.union(dfInc).groupBy('customer_ID').agg(F.sum('amount').alias('amount'))

EXCEL: Count of Column Items For Every Distinct Item In Another

In my Excel sheet, I have 2 columns. Names of restaurants, and ratings for each one. For each rating, a new row is created, so of course a restaurant name occurs multiple times.
Restaurant Rating
McDonalds 8
McDonalds 7
Red Robin 5
Qdoba 7
Etc.
How can I get the number of times each rating happens for each restaurant? We'll say rating goes from 1-10.
I want it to look like this:
Restaurant 1(rating) 2 3 4 5 6 7 8 9 10
McDonalds 889 22 45 77 484 443 283 333 44 339
Any help is appreciated!
Using Pivot Tables:
Use a pivot tables to set your rows at "Restaurant" and your columns as "Rating" and your values as "Count of Rating"
Using Countifs:

PowerPivot Cohort Analysis

I'm trying to do cohort analysis using Excel's PowerPivot. I have a table recording which users have purchased which products in which months eg.
UserID Product Date Quantity
1 Ham Mar 15 2
1 Cheese Jan 15 7
2 Ham Mar 15 8
3 Fish Mar 15 2
2 Cheese Apr 15 8
I want to use a calculated field to filter for a cohort of users who purchased a given product in a given month but be able to analyse all their purchases.
Eg cohort Ham, March 15
--> Users 1, 2
UserID Product Date Quantity
1 Ham Mar 15 2
1 Cheese Jan 15 7
2 Ham Mar 15 8
2 Cheese Apr 15 8
I know this could be done easily using SQL but I am working with colleagues who prefer to use Excel over Access/Some SQL interface.
Thankyou
Create a calculated column like this:
=if([UserID]&SlicerValue=[UserID]&[Product],[UserID])
where HAM would be selected from slicer created from a table of unique products.

Oracle Date Time strig format

I have the following oracle table, where datetime is in a single column & has datatype as string
f_ID f_type f_date
1001 A 3/30/14 12:20:00 PM
1001 B 3/30/14 10:20:00 AM
1002 A 2/3/14 11:0:00 AM
1002 B 2/3/14 9:00:00 AM
1003 A 2/13/14 10:00:00 AM
1003 B 12/13/14 10:00:00 AM
1111 B 12/13/14 10:00:00 AM
I wish to calculate average time taken for all shipments which have count > 1. So time difference in shipment of 1001 is 2 hours, 1002 is 2 hours, 1003 is 10 Months (303 x 24=7272) hours. 1111 has count =1 so it can be excluded from average.
So the average result should be (2+2+7272)/3 = 2425.33 hours.
How do I query that?
This one should work:
WITH t AS
(SELECT f_ID, ABS(b.f_date-a.f_date)*24 AS duration
FROM my_table a
JOIN my_table b USING (f_ID)
WHERE a.f_type = 'A'
AND a.f_type = 'B')
SELECT AVG(duration)
FROM t;

Excel VLOOKUP gives wrong value

I have a VLookup cell which gives me the wrong value:
This is the table:
PID Product Price User User name Deal On Amount After
in 1001 table 1001 1 Milly No 1000
in 1001 table 100 13 Vernetta Yes 900
out 1001 table 50 14 Mireya No 900
out 1001 table 100 15 Rosanne Yes 1000
out 1001 table 101 16 Belinda No 1000
in 1001 table 200 1 Milly Yes 800
in 1234 chair 300 2 Charlena Yes 500
in 1234 chair 100 3 Mina Yes 400
in 1234 chair 50 4 Sabina Yes 350
in 8231 couch 20 5 Joni Yes 330
in 1001 table 150 6 Armando Yes 180
in 1001 table 100 7 Noemi Yes 80
in 8231 couch 40 8 Ashlie Yes 40
in 8231 couch 30 9 Ann Yes 10
out 1001 table 201 10 Angelina Yes 211
out 1234 chair 300 11 Melvina Yes 511
out 8231 couch 21 12 Mattie Yes 532
The product column is a VLOOKUP with the following formula
VLOOKUP(B6,$L$2:$M$10, 2)
B is the PID column. The table in L2:M10 is the following:
PID Prodcut
1001 table
1234 chair
8231 desk
2311 closet
9182 book_shelf
1822 bed
1938 coffee_book_table
2229 couch
Now as you can see. PID 8231 is a desk, but it appears as a couch. Can you see what the problem is?
The crux of your problem is the way you've written the formula, you forgot the last parameter, "FALSE" or 0 which means you want an EXACT MATCH. So your formula should look like this:
VLOOKUP(B6, $L$2:$M$10, 2, FALSE)
OR
VLOOKUP(B6, $L$2:$M$10, 2, 0)
Both do the same thing.
The default setting for it is TRUE, which looks for the closest match, which you only want if the index you're looking up off of is sorted.

Resources