I need to calculate sum of hours based on period column and insert into another table. Please see below example data.
[code]
tbl_Summary
Client_id Store_id attribute1 Period Hours attributes
1 16 1 2/25/2007 1054.8 1
1 16 1 3/11/2007 1112.8 1
1 16 1 3/25/2007 1164.8 1
1 16 1 4/8/2007 1383.2 1
1 16 1 4/22/2007 1351.6 1
1 16 1 9/21/2008 1523.6 1**
1 16 1 10/5/2008 1020.26 1
1 16 1 10/19/2008 939.94 1
1 16 1 11/2/2008 903.14 1
1 16 1 8/9/2009 866.66 1
1 16 1 8/23/2009 915.48 1
1 16 1 9/6/2009 894.26 1
1 16 1 9/20/2009 1458.58 1[/b]
1 18 1 6/1/2008 1112.8 1
1 18 1 6/15/2008 1164.8 1
1 18 1 6/29/2008 1383.2 1 [b]
1 18 1 7/13/2008 1351.6 1
1 18 1 12/28/2008 1523.6 1
1 18 1 1/11/2009 979.2 1
1 18 1 1/25/2009 913.2 1
1 18 1 2/8/2009 930.6 1
1 18 1 2/22/2009 1143.4 1
1 18 1 5/31/2009 1066.16 1
1 18 1 6/14/2009 1174.8 1
1 18 1 6/28/2009 1099.2 1
1 18 1 7/12/2009 1014.5 1
Out put table will be like this.
tbl_history
--------------
[code]client_id store_id attribute1 hours attributes
1 16 1 8521.92 1
1 18 1 11196.92 1
Conditions for sp
Parameters to sp are only #client_id, #attribute1, #attributes
Find Max(Period) and go back to 52 weeks and calculate Sum(Hours) where Store_id= ? (in example 16 and 18 or each store_id) and client_id= #client_id and attribute1 = #attribute1 and attributes = #attributes. ie. Sum(Hours) will change based on store_id and when store_id will change period will change. See side moved data
store_id = 16 and period=9/20/2009 to 9/20/2009 sum(hours) =8521.92
Insert into another table all output tbl_history.
Please give me solution. If you have any question ask me.
Thanks in advance
INSERT INTO tbl_history
SELECT client_id, store_id, attribute1, SUM(Hours), attributes
FROM tbl_Summary
WHERE PERIOD <=
(
SELECT MAX(PERIOD)
FROM tbl_Summary
WHERE client_id = #client_id
AND attribute1 = #attribute1
AND attributes = #attributes
)
AND PERIOD >=
(
SELECT (MAX(PERIOD) - 1 year) --(I dont remember the sintax for sql getdate() something but thats the idea)
FROM tbl_Summary
WHERE client_id = #client_id
AND attribute1 = #attribute1
AND attributes = #attributes
)
AND client_id = #client_id
AND attribute1 = #attribute1
AND attributes = #attributes
GROUP BY client_id, store_id, attribute1, attributes
Try it and tell me if it worked.
Regards.
Related
I have a table with Scenario,Product,AlgoKey,User Select,System Select columns, I have to select the algo key for each scenario, The first priority goes to user selected otherwise system selected.
I have Shared my Inout & output result below, could you please help me how to write query for this.
Scenario
Product
AlgoKey
User Select
SystemSelect
1
P101
1
0
1
1
P102
2
1
0
2
P101
1
0
1
2
P102
2
0
0
3
P101
1
1
1
3
P102
2
0
0
4
P101
1
0
0
4
P102
2
0
1
OutPut :
Scenario
AlgoKey
Columnselected
1
2
User
2
1
System
3
1
User
4
2
System
here is how you can do it
select scenario , AlgoKey, case when Userselect = 1 then 'User' else 'System' end Columnselected
from (
select *, ROW_NUMBER() over (partition by scenario,productkey order by userselect desc, systemselect desc) rn
from tableName
) t
where rn = 1
I am looking to try to create the row number below, I have tried so many options but none seem to work. I would appreciate help.
I want the row order to look at sub_id and for each one do a sequential number, 1, 2, 3... I then want it to look at disc, and if that changes, go back to 1 again. I have tried concatenating them and using a key, but I can't seem to get it to work.
ID sub_id disc ROW_NUMBER
1 1 OT 1
1 1 OT 2
1 1 OT 3
1 2 DT 1
1 2 DT 2
1 3 SL 1
1 3 SL 2
1 4 PH 1
1 4 PH 2
1 4 OT 1
1 5 OT 1
1 5 PH 2
1 5 DT 1
1 6 DT 1
1 6 DT 2
1 6 DT 3
1 7 SL 1
1 7 SL 2
1 7 DT 1
1 8 SL 1
1 8 SL 2
1 8 SL 3
1 9 DT 1
1 9 DT 2
1 9 DT 3
1 10 PH 1
1 10 DT 1
1 10 DT 2
1 11 OT 1
1 11 OT 2
1 11 OT 3
1 12 OT 1
1 12 OT 2
1 12 OT 3
Update
row_number() OVER (PARTITION BY sub_id ORDER BY disc) does not work. The ordering goes wrong when sub_id changes to 2. at this point it needs to revert back to 1, but it is 2.
You seem to want:
row_number() over (partition by id, sub_id, disc order by id)
Note: You do not have a column that specifies the ordering of the columns -- or even uniquely identifies each row. Hence, this just uses order by id. You might want to include some other column there.
SELECT ID , sub_ID , disc , ROW_NUMBER() OVER (PARTITION BY sub_ID ORDER BY disc) from your table
I have this table with the following records:
table1
id ele_id_1 ele_val ele_id_2
1 2 123 1
1 1 abc 1
1 4 xyz 2
1 4 456 1
2 5 22 1
2 4 344 1
2 3 6 1
2 2 Test Name 1
2 1 Hello 1
I am trying to add position for each id when ele_id_1 and ele_id_2 is order by ASC.
Here is the output:
id ele_id_1 ele_val ele_id_2 position
1 2 123 1 2
1 1 abc 1 1
1 4 xyz 2 4
1 4 456 1 3
2 5 22 1 5
2 4 344 1 4
2 3 6 1 3
2 2 Test Name 1 2
2 1 Hello 1 1
I have 34 million rows in table1, so would like to use an efficient way of doing this.
Any idea on how I can add position with values?
I think you want row_number() used like this:
select row_number() over (partition by id
order by ele_id_1, ele_id_2
) as position
Oracle can use an index for this, on (id, ele_id_1, ele_id_2).
I should note that for your example data order by ele_id_1, ele_id_2 and order by ele_id_2, ele_id_1 produce the same result. Your question suggests that you want the first.
So, you would get
id ele_id_1 ele_val ele_id_2 position
1 1 123 2 2
1 1 abc 1 1
1 4 xyz 2 4
1 4 456 1 3
Rather than:
id ele_id_1 ele_val ele_id_2 position
1 1 123 2 3
1 1 abc 1 1
1 4 xyz 2 4
1 4 456 1 2
EDIT:
If you want to update the data, then merge is probably the best approach.
MERGE INTO <yourtable> dest
USING (select t.*,
row_number() over (partition by id
order by ele_id_1, ele_id_2
) as new_position
from <yourtable> t
) src
ON dest.id = src.id AND
dest.ele_id_1 = src.ele_id_1 AND
dest.ele_id_2 = src.ele_id_2
WHEN MATCHED THEN UPDATE
SET desc.postition = src.new_position;
Note that updating all the rows in a table is an expensive operation. Truncating the table and recreating it might be easier:
create table temp_t as
select t.*,
row_number() over (partition by id
order by ele_id_1, ele_id_2
) as new_position
from t;
truncate table t;
insert into t ( . . . )
select . . . -- all columns but position
from temp_t;
However, be very careful if you truncate the table. Be sure to back it up first!
Have the following table tblTrans where
Trans_ID Trans Sequence Trans_PointsEarned Trans_PointsApplied
4452 1 1 1
4452 2 1 1
4452 3 0 1
4462 1 1 1
4462 2 1 1
4462 3 1 1
4462 4 1 1
4462 5 1 1
9101 1 0 1
9101 2 0 1
9101 3 0 1
9101 4 0 1
(useless table doesnt work)
I need to set the following on another field per every customer ID.
So Customer_OverallPoints
4452 = 2 (doesn't count 0's)
4462 = 4 (I want to cap the points to 4 based on the sequence and transID and customerID)
9101 = 0 (dont count 0's).
This needs to be applied to thousands of records based on customerID and TransID where Trans_Sequence is within the same Trans_ID and it only counts the first 4 rows that have the Trans_pointsEarned = 1.
I tried putting a psuedocode together but it just looked ridicilous and I can't even come up with the logic for this.
Thanks
Assuming that TransId is really the customer id, I think the basic logic is just an aggregation:
select t.TransId,
(case when sum(t.Trans_PointsEarned) > 4 then 4
else sum(t.Trans_PointsEarned)
end) as Customer_OverallPoints
from tblTrans t
group by t.TransId;
You can put this into an update statement as:
update customers c
set Customer_OverallPoints = (select (case when sum(t.Trans_PointsEarned) > 4 then 4
else sum(t.Trans_PointsEarned)
end)
from tblTrans t
where t.TransId = c.CustomerId
);
I have a variable that I need to retain for the rest of the visit if it changes. I am not sure how to do that in SQL
What it looks like now:
ID DATE VISIT MARKER1
1 4-5-10 1 0
1 4-6-10 1 1
1 4-7-10 1 0
1 8-9-10 2 0
What I need to happen:
ID DATE VISIT MARKER1
1 4-5-10 1 0
1 4-6-10 1 1
1 4-7-10 1 1
1 8-9-10 2 0
I need the change to be carried through for the rest of the visit.
UPDATE t1
SET t1.Marker1=1
FROM TABLE t1
INNER JOIN(
SELECT MIN(DATE) [MinDate],Visit,ID FROM TABLE WHERE Marker1=1 GROUP BY ID,Visit
) minDate on minDate.ID=t1.ID
AND minDate.Visit=t1.Visit
AND t1.[DATE]>MinDate.MinDate
WHERE t1.Marker1=0