SQL left join without deleting data - sql

I have a table (T_Individus) in Microsoft Access, and I would like to:
append and update this table from another table (T_importTable)
without overwriting existing data in T_Individus with empty data from T_imporTable (unlike the upsert solution suggested here):
T_Individus
dbid name Age
------------------------------
100 xyz 5
101 aaa 41
102 bbb 25
103 ccc
T_importTable
dbid name Age
------------------------------
101 aaa 42
102 bbb
103 ccc 52
104 ddd 27
Desired output for T_Individus:
correction of age for dbid 101
adding age for dbid 103
adding row for dbid 104
no modification for dbid 102
dbid name Age
------------------------------
100 xyz 5
101 aaa 42
102 bbb 25
103 ccc 52
104 ddd 27
Currently I use this code
UPDATE T_importTable LEFT JOIN T_Individus ON T_importTable.dbid = T_Individus.dbid
SET
T_Individus.name = [T_importTable.name],
T_Individus.Age = [T_importTable.Age];
But with this solution, the age of dbid 102 is deleted (as for the upset solution). Is there another function, or should we use an if condition?
Best,

Related

Retrieving data from unnormalized vertically ordered table

I have the following relation:
employeevalue(id, name, value, code)
id name value code
101 bobby 150 100
101 bobby 12 150
101 bobby 14.6 200
102 mary 189 100
102 mary 128 150
102 mary 112 200
103 john 112 100
103 john 13 150
103 john 76 200
Where code 100 is value1, 150 is value2 and 200 is value3. How could I write an SQL statement to retrieve the following from this table?
id name value1 value2 value3
101 bobby 150 12 14.6
102 mary 189 128 112
103 john 112 13 76
You can do this with conditional aggregation:
select id,
max(case when code = 100 then value end) as value1,
max(case when code = 150 then value end) as value2,
max(case when code = 200 then value end) as value3
from table t
group by id;

oracle query - something similar to pivot, but

I have a table like this:
ID_ORIG ID_DEST DISTANCE
------- ------- --------
1 101 10
1 102 15
1 103 20
2 101 25
2 102 30
2 103 35
3 101 40
3 102 45
3 103 50
And I want the following result:
ID_ORIG 101 102 103
------- --- --- ---
1 10 15 20
2 25 30 35
3 40 45 50
I tried to use 'pivot', but obiously didn't achive this result. Database is Oracle 11g.
Any help would be appreciated.
Please try:
select * From(
select * from YourTable
) PIVOT (sum(DISTANCE) for (ID_DEST) IN (101, 102, 103));
SQL Fiddle Demo

update corresponding value from other table

I have two tables named sales and login.My table structure is given below.Some times my program update the custid instead of userid in sales table column userid, but the logid updated correctly in sales table. I have the another table tbl_log shown below. I want to update the sales table userid based on logid using the tbl_log.
sales table
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 S1003
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 S1006
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 S1009
9 S1010 104 31 d2124
10 S1011 104 60 S1011
Log Table
Fld_log_id Fld_user_id
101 d2121
102 d2122
103 d2123
104 d2124
Exact output
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 d2122
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 d2122
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 d2123
9 S1010 104 31 d2124
10 S1011 104 60 d2124
To update the values in sales based on the values in the log table you do:
UPDATE sales S
SET S.Fld_user_id = (SELECT l.Fld_user_id
FROM logSales l
WHERE l.Fld_log_id = s.Fld_log_id);
sqlfiddle demo

Update table values using 2 tables

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

MSAccess: Ranking rows based upon column criteria

I have a dataset that looks like this:
Account Cost Centre TransNo
aaa 111 43443
aaa 111 32112
aaa 111 43211
aaa 112 32232
aaa 113 56544
bbb 222 43222
bbb 222 98332
ccc 111 88778
I need a column added that is a counter of the number of rows that relate to that Account/Cost Centre combination:
Account Cost Centre TransNo rCounter
aaa 111 43443 1
aaa 111 32112 2
aaa 111 43211 3
aaa 112 32232 1
aaa 112 56544 2
bbb 222 43222 1
bbb 222 98332 2
ccc 111 88778 1
Is this possible to do in MSAccess using SQL? and how would I go about it (ie what would be the SQL script I would need to write)?
Thanks in advance.
Something like:
SELECT a.Account, a.[Cost Centre], a.TransNo, (SELECT Count(*)
FROM table4 b
WHERE b.Account=a.Account
AND b.[Cost Centre]=a.[Cost Centre]
AND b.TransNo<=a.TransNo) AS AccountNo
FROM Table4 AS a
ORDER BY a.Account, a.[Cost Centre], a.TransNo;