Insert Trigger without duplicates by comparing last row from destination table - tsql

Hi I read two days and i cannot find solution, that's why i please for h
I'm trying to do trigger which insert data highest from last record for inserted table without duplicates.
I have destination table:
CREATE TABLE [dbo].[exported] ([Repair_Date] [datetime] NULL,
[idKards] [int] NULL, [Position] [nvarchar](32) NULL,
[error] [int] NULL)
and trigger:
Create TRIGGER [dbo].[tr2] ON [dbo].[mes] AFTER INSERT
AS
INSERT INTO dbo.[exported]
([idKard],[Repair_Date],[Position],[Error],[name],[model_name],[parent_number])
SELECT c.idKards,i.[Repair_Date],i.[Position],i.error,s.name, mo.Model_Name, pn.Parent_Number FROM inserted i
left outer join dbo.test t on i.idTest=t.idTest
left outer join dbo.Kards k on t.idKards=k.idKards
where [Repair_Error] in (300,400)
and K.idKards > (select max(idKards) from dbo.exported)
Example data in dbo.exported - in this table all values are deleted except last:
Repair_Date idKards Position error
2012-10-10 00:03:25 91996 IC4303 4
When data is inserted to table dbo.mes (on which trigger is on) from another table (below example data):
Repair_Date idTest Position error
2012-10-10 00:00:58 91996 C524 1
2012-10-10 00:00:56 91996 C522 1
2012-10-10 00:00:54 91996 C537 1
2012-10-10 04:34:31 95694 P1104 1
2012-10-10 06:48:33 97405 P1104 1
2012-10-10 01:31:17 93088 P1104 1
2012-10-10 01:34:04 92747 P1104 1
2012-10-10 12:49:22 102773 P1104 1
2012-10-10 14:19:03 102773 P1104 4
2012-10-15 16:27:24 149693 P1104 1
Trigger should compare last idTest on dbo.exported with inserted data and add data without duplicates to table dbo.exported.
But it add all:
Repair_Date idTest Position error
2012-10-10 00:03:25 91996 IC4303 4
2012-10-10 00:00:58 91996 C524 1
2012-10-10 00:00:56 91996 C522 1
2012-10-10 00:00:54 91996 C537 1
2012-10-10 04:34:31 95694 P1104 1
2012-10-10 06:48:33 97405 P1104 1
2012-10-10 01:31:17 93088 P1104 1
2012-10-10 01:34:04 92747 P1104 1
2012-10-10 12:49:22 102773 P1104 1
2012-10-10 14:19:03 102773 P1104 4
2012-10-15 16:27:24 149693 P1104 1

You're using a left join against the inserted table, so there will always be rows returned by your query

Related

calculate roll_rank using prev value for each row

I have a table where we need to set and sum roll_rank except from roll_rank 0,1,2
we dont need to touch the rows where roll_rank in 0,1,2
we want to calculate sums of roll_rank by date where not roll_rank in 0,1,2.
example table:
tmp:([]date:`date$();name:`symbol$();roll_rank:`int$())
`tmp insert (2010.01.01;`sym1;1);
`tmp insert (2010.01.01;`sym2;2);
`tmp insert (2010.01.01;`sym3;0Ni);
`tmp insert (2010.01.01;`sym4;0Ni);
`tmp insert (2010.01.02;`sym1;0);
`tmp insert (2010.01.02;`sym2;1);
`tmp insert (2010.01.02;`sym3;2);
`tmp insert (2010.01.02;`sym4;0Ni);
`tmp insert (2010.01.02;`sym5;0Ni);
`tmp insert (2010.01.02;`sym6;0Ni);
`tmp insert (2010.01.03;`sym1;1);
`tmp insert (2010.01.03;`sym2;0Ni);
`tmp insert (2010.01.03;`sym3;0Ni);
`tmp insert (2010.01.03;`sym4;0Ni);
Expected output is
This might also achieve your desired result:
update sums 1^deltas roll_rank by date from tmp
One method using a vector conditional and over:
q){update{?[null x;1+prev x;x]}roll_rank from x}/[tmp]
date name roll_rank
-------------------------
2010.01.01 sym1 1
2010.01.01 sym2 2
2010.01.01 sym3 3
2010.01.01 sym4 4
2010.01.02 sym1 0
2010.01.02 sym2 1
2010.01.02 sym3 2
2010.01.02 sym4 3
2010.01.02 sym5 4
2010.01.02 sym6 5
2010.01.03 sym1 1
2010.01.03 sym2 2
2010.01.03 sym3 3
2010.01.03 sym4 4

Pivot Table in SQL (using Groupby)

I have a table structured as below
Customer_ID Sequence Comment_Code Comment
1 10 0 a
1 11 1 b
1 12 1 c
1 13 1 d
2 20 0 x
2 21 1 y
3 100 0 m
3 101 1 n
3 102 1 o
1 52 0 t
1 53 1 y
1 54 1 u
Sequence number is the unique number in the table
I want the output in SQL as below
Customer_ID Sequence
1 abcd
2 xy
3 mno
1 tyu
Can someone please help me with this. I can provide more details if required.
enter image description here
This looks like a simple gaps/islands problem.
-- Sample Data
DECLARE #table TABLE
(
Customer_ID INT,
[Sequence] INT,
Comment_Code INT,
Comment CHAR(1)
);
INSERT #table
(
Customer_ID,
[Sequence],
Comment_Code,
Comment
)
VALUES (1,10 ,0,'a'),(1,11 ,1,'b'),(1,12 ,1,'c'),(1,13 ,1,'d'),(2,20 ,0,'x'),(2,21 ,1,'y'),
(3,100,0,'m'),(3,101,1,'n'),(3,102,1,'o'),(1,52 ,0,'t'),(1,53 ,1,'y'),(1,54 ,1,'u');
-- Solution
WITH groups AS
(
SELECT
t.Customer_ID,
Grouper = [Sequence] - DENSE_RANK() OVER (ORDER BY [Sequence]),
t.Comment
FROM #table AS t
)
SELECT
g.Customer_ID,
[Sequence] =
(
SELECT g2.Comment+''
FROM groups AS g2
WHERE g.Customer_ID = g2.Customer_ID AND g.Grouper = g2.Grouper
FOR XML PATH('')
)
FROM groups AS g
GROUP BY g.Customer_ID, g.Grouper;
Returns:
Customer_ID Sequence
----------- ----------
1 abcd
1 tyu
2 xy
3 mno

How to select only one result per condition met inside an individual table (no joins)?

I have a table containing all the trips taken by different cars. I've filtered down this table to trips that had multiple stops specifically. Now all i want to do is get the first stop that each car had.
What i've got is:
Car ID
Date_depart
Date_arrive
Count (from a previous table creation)
I've filtered this table by using Car ID + Date Depart and making a count where there are multiple date_arrives for a single date_depart. Now i'm trying to figure out how to only get back the first stop but am completely stuck. Outside of doing the lateral join X, order by Z limit 1 etc method; i have no idea how to get back only the first result in this table.
Here's some sample data:
Car ID Date_depart Date_arrive Count
949 2017-01-01 2017-01-05 2
949 2017-01-01 2017-01-09 2
1940 2017-01-09 2017-01-11 3
1940 2017-01-09 2017-01-14 3
1940 2017-01-09 2017-01-28 3
949 2018-04-19 2018-04-23 2
949 2018-04-19 2018-04-26 2
and the expected result would be:
Car ID Date_depart Date_arrive Count
949 2017-01-01 2017-01-05 2
1940 2017-01-09 2017-01-11 3
949 2018-04-19 2018-04-23 2
Any help?
You need DISTINCT ON
SELECT DISTINCT ON (date_depart, car_id)
*
FROM
trips
ORDER BY date_depart, car_id, date_arrive
This gives you the first (ordered) row of each group (date_depart, car_id)
demo: db<>fiddle

How to insert row data between consecutive dates in HIVE?

Sample Data:
customer txn_date tag
A 1-Jan-17 1
A 2-Jan-17 1
A 4-Jan-17 1
A 5-Jan-17 0
B 3-Jan-17 1
B 5-Jan-17 0
Need to fill every missing txn_date between date range (1-Jan-17 to 5-Jan-2017). Just like below:
Output should be:
customer txn_date tag
A 1-Jan-17 1
A 2-Jan-17 1
A 3-Jan-17 0 (inserted)
A 4-Jan-17 1
A 5-Jan-17 0
B 1-Jan-17 0 (inserted)
B 2-Jan-17 0 (inserted)
B 3-Jan-17 1
B 4-Jan-17 0 (inserted)
B 5-Jan-17 0
select c.customer
,d.txn_date
,coalesce(t.tag,0) as tag
from (select date_add (from_date,i) as txn_date
from (select date '2017-01-01' as from_date
,date '2017-01-05' as to_date
) p
lateral view
posexplode(split(space(datediff(p.to_date,p.from_date)),' ')) pe as i,x
) d
cross join (select distinct
customer
from t
) c
left join t
on t.customer = c.customer
and t.txn_date = d.txn_date
;
c.customer d.txn_date tag
A 2017-01-01 1
A 2017-01-02 1
A 2017-01-03 0
A 2017-01-04 1
A 2017-01-05 0
B 2017-01-01 0
B 2017-01-02 0
B 2017-01-03 1
B 2017-01-04 0
B 2017-01-05 0
Just have the delta content i.e the missing data in a file(input.txt) delimited with the same delimiter you have mentioned when you created the table.
Then use the load data command to insert this records into the table.
load data local inpath '/tmp/input.txt' into table tablename;
Your data wont be in the order you have mentioned , it would get appended to the last. You could retrieve the order by adding order by txn_date in the select query.

T-SQL: SELECT related column data for the max two other columns

I have table data like the following, where order type is 1 for a quote, and 2 for an order. Any given po_num can have 0 to many of order_type 1, but should only have only 0 or 1 of order_type 2, or all of the above.
I need to return the max order_num of the max order_type of a given po_num, where the order_num is just an additional (but important) column in my result.
Table data:
order_type po_num order_num
1 E0102 1013200
1 E0102 1013162
1 E0104 1012161
2 E0104 1012150
1 E0104 1011449
2 E0107 1010034
2 E0108 1011994
Desired result:
order_type po_num order_num
1 E0102 1013200
2 E0104 1012950
2 E0107 1010034
2 E0108 1011994
The closest I can get is this, which still includes the max(order_no) for both order_type of 1, and order_no of order type 2:
order_type po_num order_num
1 E0102 1013162
1 E0104 1012161
2 E0104 1012150
2 E0107 1010034
2 E0108 1011994
I think you want this:
select order_type
, po_num
, max(order_num)
from orders o1
where order_type = (
select max(order_type)
from orders o2
WHERE o2.po_num = o1.po_num
)
group by po_num,order_type
The inclusion of order_type in the group by is an artifact, it is required because of how the table is designed.
FWIW, the quotes and orders should be split out into two tables. When you get weird SQL like this an difficult or conditional unique constraints it is a table design issue.
I assume you are using a group by clause. Could you add a
having order_type = max(order_type)
to your sql?
See http://msdn.microsoft.com/en-us/library/ms180199.aspx for more details on the 'having' statement.