Update table values using 2 tables - sql

I have 2 tables as follows:
COMMENT TEXT
RECORD_ID_FK SORT_ORDER COMMENT_TEXT
-----------------------------------------
101 1 TEST TEXT
101 1 SOME MORE TEST TEXT
101 1 SOME MORE...
101 2 SOME MORE...
101 2 SOME MORE...
101 2 SOME MORE...
101 2 SOME MORE...
101 3 SOME MORE...
101 3 SOME MORE...
101 3 SOME MORE...
102 1 TEST TEXT
102 1 SOME MORE TEST TEXT
102 1 SOME MORE...
102 2 SOME MORE...
102 2 SOME MORE...
102 2 SOME MORE...
102 2 SOME MORE...
102 3 SOME MORE...
102 3 SOME MORE...
102 3 SOME MORE...
PRODUCTS
PRODUCT_ID SORT_ORDER RECORD_ID_FK
-----------------------------------------
111 1 101
222 2 101
333 3 101
444 1 102
555 2 102
666 3 102
I need to be able to change the sort order of the comments from values supplied by a form.
Any SORT_ORDER entry in the comments table can be swapped around, so all SORT_ORDER value 3 can be swapped to value 2, or all SORT_ORDER value 1's can be swapped for value 2 and value 2 can be swapped with value 3 etc etc.
The function where the update sql will reside excepts the SORT_ORDER and a PRODUCT_ID value and is hit each time via a loop in an action file.
I have tried this by using an sql update statement and using EXISTS() so looking to see if the PRODUCT_ID exists and joining the RECORD_ID and SORT_ORDER but this fails as soon as the SORT_ORDER is changed the next row to change fails as the SORT_ORDER is no longer valid.
I don't think this is acheivable with what I have here, as I am limited to how I can link the 2 tables to change the SORT_ORDER en masse.
I am using Oracle 11g and Coldfusion 9.
Any ideas would be greatly appeciated or confirmation that this is not actually possible with what I have here.
Thanks
JC

Related

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

Create hierarchical data from 2 tables

I have two tables like this:
ProdId ProdName
1 Mike
2 Carol
3 John
4 Marc
OrdId OrdName ProdId
100 Pizza 1
101 Subs 1
102 Calzone 2
102 Sausage 2
102 Beef 2
105 Pork 4
106 Coke 4
I need a query to populate a result like this. The hierarchy should be parent and their child and again parent followed by child.
MarkerId MarkerName MarkerParentId
1 Mike NULL
100 Pizza 1
101 Subs 1
2 Carol NULL
102 Calzone 2
103 Sausage 2
104 Beef 2
3 John NULL
4 Marc NULL
105 Pork 4
106 Coke 4
Please help . Thanks.
Has nothing to do with recursion. Basically you can get what you want with UNION
SELECT ProdId as MarkerId, ProdName as MarkerName, NULL as MarkerParentId from t1
UNION ALL
SELECT OrdId as MarkerId, OrdName as MarkerName, ProdId MarkerParentId from t2
ORDER BY MarkerId, MarkerParentId

I'm having this error when compiling my code: value with SQL

Team numbers are found by dividing the employee id by four. If 4 divides the employee id evenly, then that employee is part of team 1. For the other cases, a remainder of 1 places that employee on team 2, etc.
I got this result when I run the Code
SELECT EMPLOYEE_ID, MOD(EMPLOYEE_ID, 4) AS "TEAM"
100 0
101 1
102 2
103 3
104 0
105 1
106 2
107 3
but I need to get the following result:
100 1
101 2
102 3
103 4
104 1
105 2
106 3
107 4
Trivially, you may just add one to result of the MOD function:
SELECT
EMPLOYEE_ID,
MOD(EMPLOYEE_ID, 4) + 1 AS "TEAM"
FROM yourTable;

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.

How to equally and randomly assign records to user ID's in SQL

Looking for some basic SQL help (SQL Server 2005). I have two tables that look like the following and I need to assign each record with a unique user ID:
Record table:
Record_ID UserID
101 0
102 0
103 0
104 0
105 0
106 0
107 0
User table:
UserID
1
2
3
Update results:
Record_ID UserID
101 1
102 2
103 3
104 1
105 2
106 3
107 1
You trying to have mutually exclusive requirements ... 'randomly' and 'equally'. You can't do both.