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
Related
I wish to use some sort of SQL array to subtract values from a certain row (QTYOnHand) that decreases that row value every time and throws it into a rolling calculation for the other rows. I've been thinking of some sort of Self Join/Temp Table solution, but not sure how to formulate. Also, All the results will be partitioned by the ItemID below. Help would be appreciated.
Here's some data, If I do a simple row by row subtraction I will get this: 17-3 = 14, 17-5 = 12 and so on.
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 12
123 4 17 13
456 7 12 5
456 8 12 4
456 2 12 10
456 3 12 9
789 2 6 4
789 2 6 4
789 2 6 4
These are the results that I want, where I subtract every next value from the new QTYOnHand-ItemQty column value. Looks like 17-3 then 14 -5 then 9 -4 for Item_ID (123):
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 9
123 4 17 5
456 7 12 5
456 8 12 -3
456 2 12 -5
456 3 12 -8
789 2 6 4
789 2 6 2
789 2 6 0
try the following:
;with cte as
(
select *, ROW_NUMBER() over (partition by Item_ID order by Item_ID) rn
from YourTable
)
, cte2 as
(
select Item_ID, ItemQty, QTYOnHand, Case when rn = 1 then QTYOnHand else 0 end - ItemQty as calc, rn
from cte
)
select Item_ID, ItemQty, QTYOnHand, sum(calc) over (partition by Item_ID order by rn) as [QtyOnHand - ItemQty]
from cte2 t1
Please find the db<>fiddle here.
This question already has answers here:
SQL How to Update SUM of column over group in same table
(3 answers)
Closed 2 years ago.
I have a table in SQL which I want to update
NAME Emp_ID Points TotalPoints
ABC 1 50 0
ABC 1 40 0
XYZ 2 20 0
LMN 3 30 0
LMN 3 50 0
XYZ 2 10 0
LMN 3 5 0
Please help me to update the same table as shown below by summing up the points
NAME Emp_ID Points TotalPoints
ABC 1 50 90
ABC 1 40 90
XYZ 2 20 30
LMN 3 30 85
LMN 3 50 85
XYZ 2 10 30
LMN 3 5 85
I only tried the below SQL on Oracle 18c database, but I believe it is fairly standard SQL and so should work with all the major DBMS
update EMPS E1
set E1.TOTAL_POINTS = (select sum(E2.POINTS)
from EMPS E2
where E2.EMP_ID = E1.EMP_ID)
Try the following, here is the DEMO. This code work for MySQL 8.0, SQL Server and PostgreSQL.
select
name,
emp_id,
points,
sum(points) over (partition by emp_id) as total_points
from yourTable
order by
name
Does anyone know how can I mix up the data order while insert the records into another table in SQL?
Example:
I have 2 tables, Table A and Table B, I would like to insert the record from Table A to Table B, how ever I would like the records mix up while insert into Table B insert of follow the order in Table A. Will it be possible to do it in SQL?
Table A:
ID Postcode Total
1 3000 10
2 3000 20
3 3000 5
4 3001 10
5 3001 6
6 3002 6
7 3002 9
8 3002 10
9 3003 85
10 3004 1
After insert into Table B (the records randomly mixed up):
ID Postcode Total
1 3001 10
2 3002 20
3 3000 5
4 3003 85
5 3002 6
6 3001 6
7 3002 9
8 3000 20
9 3000 10
10 3004 1
Yes you can:
INSERT INTO TableB
SELECT *
FROM (SELECT TOP 10 * FROM TableA ORDER BY NEWID()) AS tmp
Make sure TableB does not have a clustered key or else it won't work. Change TOP 10 to the number of rows you have in TableA.
Edit: a commenter pointed that the original version won't work in SQL Server 2012 and above. In that case you need a more heavy handed version (updated).
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
I have a table with an ID and multiple informative columns. Sometimes however, I can have multiple data for an ID, so I added a column called "Sequence". Here is a shortened example:
ID Sequence Name Tel Date Amount
124 1 Bob 873-4356 2001-02-03 10
124 2 Bob 873-4356 2002-03-12 7
124 3 Bob 873-4351 2006-07-08 24
125 1 John 983-4568 2007-02-01 3
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
So, I would like to obtain only these lines:
124 3 Bob 873-4351 2006-07-08 24
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
Anyone could give me a hand on how I could build a SQL query to do this ?
Thanks !
You can calculate the maximum sequence using group by. Then you can use join to get only the maximum in the original data.
Assuming your table is called t:
select t.*
from t join
(select id, MAX(sequence) as maxs
from t
group by id
) tmax
on t.id = tmax.id and
t.sequence = tmax.maxs