I'm trying with SQL to combine two rows into one in SQL with a new ID number
ID Amount
------------
1 100
2 200
3 300
4 400
5 500
6 600
into
ID Amount
-------------
1 100
2 200
101 700
5 500
6 600
I appreciate your help.
Check out SQL Procedures. I'm not sure about your goals but I think what you need is to define a SQL Procedure. It's been I while since I did that, but it should go like:
CREATE OR REPLACE PROCEDURE combine(
ID1 IN <TABLE_NAME>.ID%TYPE,
ID2 IN <TABLE_NAME>.ID%TYPE,
NEWID IN <TABLE_NAME>.ID%TYPE)
IS
BEGIN
INSERT INTO <TABLE_NAME> (ID, Amount) VALUES(NEWID, ((SELECT Amount FROM <TABLE_NAME> WHERE ID=ID1) + (SELECT Amount FROM <TABLE_NAME> WHERE ID=ID2)));
DELETE FROM <TABLE_NAME> WHERE ID = ID1;
DELETE FROM <TABLE_NAME> WHERE ID = ID2;
COMMIT;
END;
/
To get the state as described by you, you can call the routine by
combine(4, 5, 101);
Related
Lets say :
i have data on OracleDb like what i mentioned above.
TRANSFERNUMBER | VALUE1 | VALUE2
2250 | 1000 | 2000
2251 | 1000 | 3000
My main purpose is when add some data on table if data exists it should update the data . if data not exists on the table it should insert new row on table . That is why i want to use if exists on my query .
However i can't handle the query . Also i can't write procedure because of some reasons on the table . Is anyone help me for writing this by using query on Oracle ?
MERGE is what we usually do. Here's an example:
Test table and sample data:
SQL> create table test (tn number, val1 number, val2 number);
Table created.
SQL> insert into test
2 select 2250, 1000, 2000 from dual union all
3 select 2251, 1000, 3000 from dual;
2 rows created.
SQL> select * From test order by tn;
TN VAL1 VAL2
---------- ---------- ----------
2250 1000 2000
2251 1000 3000
How to do it? using represents data you're going to insert or update:
SQL> merge into test t
2 using (select 2250 tn, 1 val1, 2 val2 from dual union all --> for update
3 select 3000 , 8 , 9 from dual --> for insert
4 ) x
5 on (t.tn = x.tn)
6 when matched then update set t.val1 = x.val1,
7 t.val2 = x.val2
8 when not matched then insert values (x.tn, x.val1, x.val2);
2 rows merged.
Result:
SQL> select * From test order by tn;
TN VAL1 VAL2
---------- ---------- ----------
2250 1 2 --> updated
2251 1000 3000
3000 8 9 --> inserted
SQL>
My table contains data about Employee. However it is a temporary table and EmployeeID here isn't the primary key. The table may contain a given EmployeeID multiple times.
Now, I have to select batch of records of batchSize, let's consider 200 for now. I'll send these batches to multiple threads.
I have written this query:
WITH SingleBatch AS
(
SELECT
*,
ROW_NUMBER() OVER(ORDER BY EmployeeId) AS RowNumber
FROM
TemperoryTable
)
SELECT *
FROM SingleBatch
WHERE RowNumber BETWEEN 1 AND 200;
the result might be:
EmployeeID EffectiveDate
1 123 01/01/2016
2 541 01/01/2016
------------------------
------------------------
200 978 18/06/2015
for one batch.
This works fine and row numbers change with thread number.
Now suppose, second batch starts with EmployeeId 978. Then this employee will be in first batch as well as second batch. That is, same employee is being sent to multiple threads and may possibly cause conflict.
Although the scenario is very rare, I must avoid this.
What could be the possible solution here?
Sorry I don't get it before, you wish same empolyee can be gotten together? but the total return rows count possible is not fix number. May this is helpful for you.
;WITH t(RowNumber,EmployeeId,other)AS
(
SELECT 1,'a','1' UNION ALL
SELECT 2,'a','12' UNION ALL
SELECT 3,'a','13' UNION ALL
SELECT 4,'b','21' UNION ALL
SELECT 5,'d','41' UNION ALL
SELECT 6,'c','31' UNION ALL
SELECT 7,'c','32'
)
SELECT *,DENSE_RANK()OVER(ORDER BY EmployeeId) AS FilterID,RANK()OVER(ORDER BY EmployeeId) RowsCount FROM t
RowNumber EmployeeId other FilterID RowsCount
----------- ---------- ----- -------------------- --------------------
2 a 12 1 1
3 a 13 1 1
1 a 1 1 1
4 b 21 2 4
6 c 31 3 5
7 c 32 3 5
5 d 41 4 7
Same employeeid has same FilterID, and the RowsCount to control return rows count.
You should get data by RowsCount but rownumber.
For example:
Actual return 6 lines when the RowsCount between 1 and 5.
because the employeeID c have two lines.
Between mean RowNumber>=1 and RowNumber<=200
So next batch should be
RowNumber BETWEEN 201 AND 400
also you can change where clause to
RowNumber>=1 and RowNumber <200 (1-199)
RowNumber>=200 and RowNumber <400 (200-399)
This is my first time to ask question in here.
For example...
Table A
ID COUNT
---------------
123 4
124 3
125 1
I want to base on the table a COUNT values and separating the count records to '1' in Table B with below format with using SQL statement only(without PL/SQL). Is it possible?
Table B
ID COUNT
---------------
123 1
123 1
123 1
123 1
124 1
124 1
124 1
125 1
I know it is easy to do if using PL/SQL, but if there any way to solve below problem with using SQL statement only? Thanks.
SELECT ID,1
FROM YOURTABLE
CONNECT BY LEVEL <= COUNT1
AND PRIOR id = id
AND PRIOR sys_guid() IS NOT NULL;
SQL Fiddle Demo
There's a table named sample, and it has two columns id, and id2. Some records on id2 are filled with numbers, but some are null. So I need to fill them up with the same number as their closest record. That is, if each record on id2 is not null, move on to the next one, and if each record on id2 is null, fill it in with the previous one. How can I do this with vba?
sample
id id2
1 100
2
3 500
4 600
5
6 800
sample_result
id id2
1 100
2 100
3 500
4 600
5 600
6 800
In Access I'm not sure it can be done in pure SQL, but I think this should get you close to what you want:
UPDATE sample AS s
SET s.id2 = Dmax("id2", "sample", "id <" & [s].[id])
WHERE (( ( s.id2 ) IS NULL ));
Need help with this as I m having no luck.
Table A
id groupid
1 100
2 101
3 102
Table B
groupid newid
100 100
101 100
102 100
Update Table A so that Table A becomes
id groupid
1 100
2 100
3 100
which uses TableB to get the newid.
Thanks in advance
sqlite doesn't support joins in updates, but you could use a subquery. try something like this:
update a
set groupid = coalesce(
(select newid from b where groupid = a.groupid limit 1),
groupid
);