I have a requirement to work on an update. The requirement is to update table 2 using the data from table 1. Please find below sample records from the two tables:
TABLE A
-----------------
colA | colB | colC
-----------------
1 AAA ABC
2 BBB DEF
3 CCC GHI
3 CCC HIJ
TABLE B
-----------------
colA1 | colB1 | colC1
-----------------
1 AAA
2 BBB
3 CCC
3 CCC
I need to update the colC1 with values of ColC. Expected output is shown below
TABLE B
-----------------
colA1 | colB1 | colC1
-----------------
1 AAA ABC
2 BBB DEF
3 CCC GHI
3 CCC HIJ
Do we need to use a cursor for this or a simple update statement like shown below would do?
Update table B
set colC1 = table A.colC
from TABLE A, TABLE B
where colA1 = colA
and colB1 = colB;
Your SQL seems perfectly fine.
Cursors are normally used for programmatic access to a database, where the program is stepping through the results one-at-a-time, with the cursor pointing to the 'current record'. That isn't needed in normal SQL update statements.
One thing to note... In Amazon Redshift, using an UDPATE on a row causes the existing row to be marked for deletion and a new row is created. (This is a side-effect of using a columnar database.) If many rows are updated, it means that the disk storage becomes less efficient. It can be improved by occasionally running VACUUM tablename, which will remove the deleted storage.
Related
So i have 2 dataset, lets say table A and table B.
Table A always has new data come in and it associate with a date column.
Table B is a fact table and its temporary wont change.
So, i need to create a visual and i need data from both table, so i decide to merge both table on PowerBI.
My question is if i merge the current data and next day if new data coming into table A, will it also have the data from table B?
(depend on the complex of my table, so the i dont want to do it on sql side)
Here is the example for this question
Table A
ID
Name
Date
1
Apple
1/1
2
Banana
1/1
Table B
ID
Name
Something
1
Apple
aaaa
2
Banana
bbbb
the result i want to see on my report
ID
Name
Date
Something
1
Apple
1/1
aaaa
2
Banana
1/1
bbbb
and next day table A has some new records(yes, it will have duplicate record)
ID
Name
Date
2
Banana
1/2
1
Apple
1/2
1
Apple
1/1
2
Banana
1/1
Will i get?
ID
Name
Date
Something
2
Banada
1/2
bbbb
1
Apple
1/2
aaaa
1
Apple
1/1
aaaa
2
Banana
1/1
bbbb
Yes, it will. When you refresh, the new data will follow your merge or relationship rules.
I am doing unload and copy from Redshift to S3 -
unload (select * from tbl)
to <S3 location>
credentials <creds>
addquotes escape
copy tbl2
from <S3 location>
credentials <creds>
removequotes escape
My table is like - int, text,text,text .
Copy command is adding random numbers in first int column and shifting further columns to right, removing last column.
Does anyone have any idea why this could happen?
Original table -
col1 col2 col3 col4
1 abc def ghi jkl
2 mno pqr stu vwx
Copy Table -
col1 col2 col3 col4
123 1 abc def ghi
456 2 mno pqr stu
Unloaded table is correct
At a guess, two things might be wrong. The first is that your to and from column order is different.
I would try
opening the file at 'S3 location' in S3
copy the header column (line 1)
edit the column text changing your delimiter to "," if not already
paste the edited column header into your copy command
copy tbl2('column list from file')
from
credentials
removequotes escape
If your S3 file lacks a header than go back to your original export process and figure out what it is.
Less likely, you may be missing the
IGNOREHEADER 1
parameter on your copy. Let us know what you find.
I have some data which is broken down by Session and Activity, there being many sessions and within each session, many activities. What I would like to be able to do is to count the number of activities a person performs in each session (with times where a person performs the same activity at two separate times in a session counted separately). The data I am working with looks like this, with the count column being the one I want to create;
data have;
input Session $ Activity $ Count;
datalines;
AAAA VVVV 1
AAAA XXXX 2
AAAA XXXX 2
AAAA YYYY 3
AAAA ZZZZ 4
AAAA XXXX 5
AAAA XXXX 5
BBBB VVVV 1
BBBB XXXX 2
BBBB YYYY 3
BBBB YYYY 3
BBBB YYYY 3
CCCC VVVV 1
CCCC VVVV 1
CCCC XXXX 2
CCCC XXXX 2
CCCC YYYY 3
CCCC ZZZZ 4
CCCC VVVV 5
CCCC XXXX 6
;
run;
I have managed to count the number of observations in each Session using this code;
proc sort
data = a.input;
by session eventtime;
run;
data a.example;
set a.input;
count +1;
if first.session then count =1;
by session eventtime;
tun;
I have been exploring this method using nested if statements along with the SAS first. function, however this seems to require the data to be sorted, which as an activity might occur at more than one points in a session I cannot do.
My first thoughts were to use a 'do' or 'do while' loop however I am not sure how to set the condition so that it iterates whilst an unknown value is constant.
Any help most appreciated.
Thanks!
Assuming you want the count variable calculated, then you should be able to do it like this. by does not require sorting:
data want;
set have;
by session activity notsorted;
if first.session then new_count=0;
if first.activity then new_count+1;
run;
This assumes it's sorted the way you want it (by event time) going into this dataset. notsorted tells by not to worry about the actual sort order; first now detects changes from the previous value only.
I want to be able to compare two tables within the same SQLite Database using a C++ interface for matching records. Here are my two tables
Table name : temptrigrams
ID TEMPTRIGRAM
---------- ----------
1 The cat ran
2 Compare two tables
3 Alex went home
4 Mark sat down
5 this database blows
6 data with a
7 table disco ninja
++78
Table Name: spamtrigrams
ID TRIGRAM
---------- ----------
1 Sam's nice ham
2 Tuesday was cold
3 Alex stood up
4 Mark passed out
5 this database is
6 date with a
7 disco stew pot
++10000
The first table has two columns and 85 records and the second table has two columns with 10007 records.
I would like to take the first table and compare the records within the TEMPTRIGRAM column and compare it against the TRIGRAM columun in the second table and return the number of matches across the tables. So if (ID:1 'The Cat Ran' appears in 'spamtrigrams', I would like that counted and returned with the total at the end as an integer.
Could somebody please explain the syntax for the query to perform this action?
Thank you.
This is a join query with an aggregation. My guess is that you want the number of matches per trigram:
select t1.temptrigram, count(t2.trigram)
from table1 t1 left outer join
table2 t2
on t1.temptrigram = t2.trigram
group by t1.temptrigram;
If you just want the number of matches:
select count(t2.trigram)
from table1 t1 join
table2 t2
on t1.temptrigram = t2.trigram;
I have class A in database for example:
ID COL1 COL2 COL3
1 a b c
And column B in database:
ID COL1 COL2 FK_COL
1 d e 1
2 dd ee 1
3 ddd eee 1
class B have foreign key to class A. How can I create a query to get all objects from table B where foreign key is 1 but instead of ID from A to be COL1 from A.
ID COL1 COL2 FK_COL
1 d e a
2 dd ee a
3 ddd eee a
If I understand your question correctly, you can use ForeignKey.to_field. From the documentation:
ForeignKey.to_field
The field on the related object that the relation is to. By default, Django uses the primary key of the related object.
EDIT: For the additional question in the comment, you'd do something like:
b_obj = B.objects.filter(a=1).values('colX')
Or, if you're coming from the A object:
a_obj = A.objects.get(pk=1)
b_objs = a_obj.b_subset.values('colX')
colX is the column you want displayed, and b_subset will depend on the name of the related model. If you do dir(a_obj) it will be obvious which one is it, but it should be the name of the model plus "_set".
SECOND EDIT: values can also follow relations, so to answer what is ultimately your question, you would do something like:
b_objs = B.objects.filter(a=1).values('COl1', 'COL2', 'FK_COL__COL1')
This gets you the value of the COL1 from the A table (note the double underscore to follow the relation).