Select from table repeat first value for combination of two keys - sql

I would like to transfer some existing data into new data table.
I have table with substitutions:
- ID
- currentItemId
- formerItemId
- contentId
For the same content there is possibility I have multiple entries for combinations currentItemId and formerItemId.
Let me show how it is now:
ID_T1 currentItemId formerItemId contentId
1 100 200 300
2 100 200 301
3 100 200 302
4 105 201 303
5 105 201 304
6 110 205 320
7 111 206 321
8 120 204 322
9 130 208 323
10 130 208 324
Now, I would like to select TOP ID for each combination formerItemId and currentItemId:
ID ID_T1 contentId
1 1 300
2 1 301
3 1 302
4 4 303
5 4 304
6 6 320
7 7 321
8 8 322
9 9 323
10 9 324
Both tables also contains timestamp and some other data - I haven't included that in order example to be more understandable.
I tried self join (no success), nested select (gives me right value for the original combination, but it doesn't repeat, it gives me NULL on ID for other records), but nothing seems to work. Tried something like:
SELECT di1.ID,
(SELECT TOP(1) di1.ID
FROM TABLE
WHERE
di1.currentItemtId = di2.currentItemtId AND di1.formerItemId = di1.formerItemId
) AS repeat
,di2.deleteItemId
,di1.currentitemtId
,di1.formerItemId
,di1.contentId
FROM Table di1
LEFT JOIN
Table di2 ON di1.ID = di2.ID
But this way ID doesn't repeat - I get same values for ID as in ordinary select.
I am using SQL server 2008.
Any help would be greatly appreciated.

Please try:
SELECT
MIN(ID) OVER (PARTITION BY currentItemId, formerItemId) ID,
currentItemId,
formerItemId,
contentId
FROM YourTable
SELECT
ID,
MIN(ID) OVER (PARTITION BY currentItemId, formerItemId) ID_T1,
contentId
FROM YourTable

Related

Convert This SQL Query to ANSI SQL

I would like to convert this SQL query into ANSI SQL. I am having trouble wrapping my head around the logic of this query.
I use Snowflake Data Warehouse, but it does not understand this query because of the 'delete' statement right before join, so I am trying to break it down. From my understanding the row number column is giving me the order from 1 to N based on timestamp and placing it in C. Then C is joined against itself on the rows other than the first row (based on id) and placed in C1. Then C1 is deleted from the overall data, which leaves only the first row.
I may be understanding the logic incorrectly, but I am not used to seeing the 'delete' statement right before a join. Let me know if I got the logic right, or point me in the right direction.
This query was copy/pasted from THIS stackoverflow question which has the exact situation I am trying to solve, but on a much larger scale.
with C as
(
select ID,
row_number() over(order by DT) as rn
from YourTable
)
delete C1
from C as C1
inner join C as C2
on C1.rn = C2.rn-1 and
C1.ID = C2.ID
The specific problem I am trying to solve is this. Let's assume I have this table. I need to partition the rows by primary key combinations (primKey 1 & 2) while maintaining timestamp order.
ID primKey1 primKey2 checkVar1 checkVar2 theTimestamp
100 1 2 302 423 2001-07-13
101 3 6 506 236 2005-10-25
100 1 2 302 423 2002-08-15
101 3 6 506 236 2008-12-05
101 3 6 300 100 2010-06-10
100 1 2 407 309 2005-09-05
100 1 2 302 423 2012-05-09
100 1 2 302 423 2003-07-24
Once the rows are partitioned and the timestamp is ordered within each partition, I need to delete the duplicate checkVar combination (checkVar 1 & 2) rows until the next change. Thus leaving me with the earliest unique row. The rows with asterisks are the ones which need to be removed since they are duplicates.
ID primKey1 primKey2 checkVar1 checkVar2 theTimestamp
100 1 2 302 423 2001-07-13
*100 1 2 302 423 2002-08-15
*100 1 2 302 423 2003-07-24
100 1 2 407 309 2005-09-05
100 1 2 302 423 2012-05-09
101 3 6 506 236 2005-10-25
*101 3 6 506 236 2008-12-05
101 3 6 300 100 2010-06-10
This is the final result. As you can see for ID=100, even though the 1st and 3rd record are the same, the checkVar combination changed in between, which is fine. I am only removing the duplicates until the values change.
ID primKey1 primKey2 checkVar1 checkVar2 theTimestamp
100 1 2 302 423 2001-07-13
100 1 2 407 309 2005-09-05
100 1 2 302 423 2012-05-09
101 3 6 506 236 2005-10-25
101 3 6 300 100 2010-06-10
If you want to keep the earliest row for each id, then you can use:
delete from yourtable yt
where yt.dt > (select min(yt2.dt)
from yourtable yt
where yt2.id = yd.id
);
Your query would not do this, if that is your intent.

Duplex Data Preparation using SQL

I have a table with data and I need to prepare the data rows for duplex printing using defined fields and variables.
I have created set of temp tables as below:
#OriginalData:
Code Department Brand PageNumber SequenceNo Reverse
-----------------------------------------------------
101 201 LG 1 1 0
102 201 Samsung 1 2 0
105 203 Apple 1 3 0
106 203 Nokia 1 4 0
103 202 Sony 2 5 0
104 202 Sony 2 6 0
107 203 TCL 2 7 0
108 203 BenQ 2 8 0
#Required Data:
Code Department Brand PageNumber SequenceNo Reverse
-----------------------------------------------------
101 201 LG 1 1 0
102 201 Samsung 1 2 0
105 203 Apple 1 3 0
106 203 Nokia 1 4 0
101 201 LG 1 5 1 - Required
102 201 Samsung 1 6 1 - Required
105 203 Apple 1 7 1 - Required
106 203 Nokia 1 8 1 - Required
103 202 Sony 2 9 0
104 202 Sony 2 10 0
107 203 TCL 2 11 0
108 203 BenQ 2 12 0
103 202 Sony 2 13 1 - Required
104 202 Sony 2 14 1 - Required
107 203 TCL 2 15 1 - Required
108 203 BenQ 2 16 1 - Required
I need the data as per the second table. I am planning to use the original data for the front of the page and use data from RequireDupexData table for back of the page.
Is there a way to change the SequenceNo order using SQL? so when combined with Original data they can be printed correctly in terms of duplex printing.
Notes:
Example 2 data rows per page
Example 2 columns per page
The data is passed to an application that creates artwork
Same data rows are used for the back of the page hence why the duplicated tables.
Update: Modified my required data to make more sense. In my example I have considered 2 columns and 2 rows but these can change depending on the page/template design. Hence I believe columns or rows or both must be considered in order to reorder the SequenceNo for required data.
I think this will work to reverse your sequence number
--drop table #temp
create table #temp(
seq int,
)
insert into #temp
values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14)
declare #third int;
set #third = ceiling((select count(seq)/3.0 from #temp))
select #third
select seq
, case when seq <= #third
then seq + 2*#third
when seq <= 2*#third
then seq
when seq > 2*#third
then seq - 2*#third
end as rseq
from #temp
order by seq asc

postgresql query to delete duplicate entries in a table [duplicate]

This question already has answers here:
How to delete duplicate entries?
(16 answers)
Closed 6 years ago.
I have a table as :
id product id merchant id price upc
1 124 2 2000000 1234XDE
2 124 2 200000 1234XDE
3 124 2 200000 1234XDE
4 124 2 200000 1234XDE
5 124 2 200000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
I want to delete all the multiple entries from the table.
Delete from
table where id not in (Select min(id) from table group by(merchant id))
but no success. I want resulting table as:
id product id merchant id price upc
1 124 2 2000000 1234XDE
5 124 2 2000000 ASDER36
6 134 1 300 ASERT56
7 134 2 300 ASERT56
Can someone help me in writing a query for this.
This should do it:
delete from flash
where id not in (select min(id)
from flash
group by product_id, merchant_id, upc);
SQLFiddle example: http://sqlfiddle.com/#!15/9edef/1

Oracle select query based on multiple conditions

MESSAGE_ID GROUP_ID REV_NO
100 200 1
101 201 1
102 202 1
103 203 1
104 204 1
105 200 2
106 201 2
107 202 2
108 203 2
109 204 2
110 205 2
First I want to select all group ID's and their correpsponding lowest revision number.
Then I want select first X message ID's (Controllable X input) with condition that it should contain all the revisions of of any selected group. For e.g if I select first 5 messages by rownum then all revisions of group_id 200 is not selected.
Hope I made it clear.

SQL - Sqlite Query: obtain the fields where one number is greater than another, if less than 3 complete with the first, always 3

Well i made a query that is not working
i have a table like this
_id - E1
-----------
1 - 100
2 - 335
3 - 420
4 - 440
5 - 500
6 - 514
7 - 524
8 - 534
9 - 544
10 - 552
11 - 559
12 - 607
13 - 615
14 - 623
15 - 631
16 - 639
and the query that i made:
SELECT * FROM
(SELECT * FROM Table WHERE E1 > 633 AND _sentido = 'V'
UNION
SELECT * FROM Table) LIMIT 3
when i execute this i get
_id - E1
-----------
1 - 100
2 - 335
3 - 420
but what i really want is
_id - E1
-----------
1 - 639
2 - 100
3 - 335
if the last row and there are NOT 3 selected rows then complete with the first to reach 3
Always 3 rows!
I hope you can help me, John
You have a clever approach, but it is not going to work. The ordering of subqueries is not guaranteed. What you want is to order by your condition first and then fill out with the rest. Try this:
SELECT *
FROM table
ORDER BY (case when E1 > 633 AND _sentido = 'V' then 1 else 2 end)
LIMIT 3;
This puts the records you are interested in first. The limit 3 will retrieve those records (up to 3) and then pad remaining rows with the rest of the records.