TABLE one:
A B C
TABLE two:
A B D C
As you can see column D was added to the middle.
How to copy record from table one to table two setting value D to default.
Don't use D in the list of columns of Table2:
INSERT INTO Table2(A, B, C)
SELECT A, B, C FROM Table1
Maybe you need a WHERE clause also if you want to copy a specific row only.
D will get its default value, if there is one defined in the CREATE statement of Table2, or null if there isn't a default value.
I have 2 tables, which has one - many relation ship. The child table has 4 columns (Let say, A, B, C, D)
A - Table column
B - Foreign key column
C - Foreign key column
D - Table column
Requirement is to sort the data by A, B,then by C when displaying records
I tried with query script but not working. (Relation data source doesn't load)
I tried with specifying the sort field on Relations tab (Sorting work only for table column, fk columns are not displayed)
Is there a way to do this?
I have two tables. Config and Data. Config table has info to define what I call "Predefined Points". The columns are configId, machineId, iotype, ioid, subfield and predeftype. I have a second table that contains all the data for all the items in the config table linked by configId. Data table contains configId, timestamp, value.
I am trying to return each row from the config table with 2 new columns in the result which would be min timestamp of this particular predefined point and max timestamp of this particular predefined point.
Pseudocode would be
select a.*, min(b.timestamp), max(b.timestamp) from TrendConfig a join TrendData b on a.configId = b.configId where configId = (select configId from TrendConfig)
Where the subquery would return multiple values.
Any idea how to formulate this?
Try an inner join:
select a.*, b.min(timestamp), b.max(timestamp)
from config a
inner join data b
on a.configId = b.configID
I was able to find an answer using: Why can't you mix Aggregate values and Non-Aggregate values in a single SELECT?
The solution was indeed GROUP BY as CL mentioned above.
select a.*, min(b.timestamp), max(b.timestamp) from TrendConfig a join TrendData b on a.configId = b.configId group by a.configId
I'm an intern working with big data and this is my first question. If I'm not asking it well, please let me know how to improve.
I have a very large table that I'm querying through Hive via R's RODBC package.
Let's say that table has columns named A:ZZZ.
I'd like to pull one row, with all columns, for every unique combination of 3 columns, let's say B, F, and G.
I ran the below query to get all unique combinations of B, F and G and came up with a little over 7000:
select B, F, G, count(*)
from DB.tableName
group by B, F, G;
I did a lot of research and found this:
SELECT * FROM T WHERE (A,B) IN (('1', '1'),('2', '2'));
I currently have all my combinations of B, F and G stored as a data frame in R. I thought that if I could convert the data frame of combinations into a vector that I named TestVector, that I could try this:
SELECT * FROM DB.Table WHERE (B,F,G) IN TestVector LIMIT 1;
but I get these errors, and don't know how to fix the syntax:
[1] "HY000 110 [Cloudera][ImpalaODBC] (110) Error while executing a query in Impala: [HY000] : AnalysisException: Syntax error in line 5:\n (B, F, G)\n ^\nEncountered: COMMA\nExpected: AND, BETWEEN, DIV, IN, IS, LIKE, NOT, OR, REGEXP, RLIKE\n\nCAUSED BY: Exception: Syntax error\n"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'select *\n from \n DB.table \n WHERE \n (B, F, G)\n IN (vectorTest)\n LIMIT 1;'"
Please help!
Thanks for your time and patience.
I'd like to pull one row, with all columns, for every unique
combination of 3 columns, let's say B, F, and G.
Queries like this are typically solved using row_number to enumerate each row in a group and select rows with a certain row number.
select * from (
select * ,
row_number() over (partition by B, F, G order by id) rn
from DB.tableName
) t where rn = 1
The query above will pick the row with the lowest id for each B,F,G group.
I am trying to update Table B of a database looking like this:
Table A:
id, amount, date, b_id
1,200,6/31/2012,1
2,300,6/31/2012,1
3,400,6/29/2012,2
4,200,6/31/2012,1
5,200,6/31/2012,2
6,200,6/31/2012,1
7,200,6/31/2012,2
8,200,6/31/2012,2
Table B:
id, b_amount, b_date
1,0,0
2,0,0
3,0,0
Now with this query I get all the data I need in one select:
SELECT A.*,B.* FROM A LEFT JOIN B ON B.id=A.b_id WHERE A.b_id>0 GROUP BY B.id
id, amount, date, b_id, id, b_amount, b_date
1,200,6/31/2012,1,1,0,0
3,400,6/29/2012,1,1,0,0
Now, I just want to copy the selected column amount to b_amount and date to b_date
b_amount=amount, b_date=date
resulting in
id, amount, date, b_id, id, b_amount, b_date
1,200,6/31/2012,1,1,200,6/31/2012
3,400,6/29/2012,1,1,400,6/29/2012
I've tried COALESCE() without success.
Does someone experienced have a solution for this?
Solution:
Thanks to the answers below, I managed to come up with this. It is probably not the most efficient way but it is fine for a one time only update. This will insert for you the first corresponding entry of each group.
REPLACE INTO A SELECT id, amount, date FROM
(SELECT A.id, A.amount, B.id as Bid FROM A INNER JOIN B ON (B.id=A.B_id)
ORDER BY A.id DESC)
GROUP BY Bid;
So what you are looking for seems to be a JOIN inside of an UPDATE query. In mySQL you would use
UPDATE B INNER JOIN A ON B.id=A.b_id SET B.amount=A.amount, B.date=A.date;
but this is not supported by sqlite as this probably related question points out. However, there is a workaround using REPLACE:
REPLACE INTO B
SELECT B.id, A.amount, A.date FROM A
LEFT JOIN B ON B.id=A.b_id
WHERE A.b_id>0 GROUP BY B.id;
The query will simply fill in the values of table B for all columns which should keep their state and fill in the values of table A for the copied values. Make sure the order of the columns in the SELECT statement meet your column order of table B and all columns are mentioned or you will loose these field's data. This is probably dangerous for future changes on table B. So keep in mind to change the column order/presence of this query when changing table B.
Something a bit off topic, because you did not ask for that: A.b_id is obviously a foreign key to B.id. It seems you are using the value 0 for the foreign key to express that there is no corresponding entry in B. (Inferred from your SELECT with WHERE A.b_id>0.) You should consider using the null value for that. When you are using INNER JOIN then instead of LEFT JOIN you can drop the WHERE clause entirely. The DBS will then sort out all unsatisfied relations.
WARNING Some RDBMS will return 2 rows as you show above. Others will return the Cartesian product of the rows i.e. A rows times B rows.
One tricky method is to generate SQL that is then executed
SELECT "update B set b.b_amount = ", a.amount, ", b.b_date = ", a.date,
" where b.id = ", a.b_id
FROM A LEFT JOIN B ON B.id=A.b_id WHERE A.b_id>0 GROUP BY B.id
Now add the batch terminator and execute this SQL. The query result should look like this
update B set b.b_amount = 200, b.b_date = 6/31/2012 where b.id = 1
update B set b.b_amount = 400, b.b_date = 6/29/2012 where b.id = 3
NOTE: Some RDBMS will handle dates differently. Some require quotes.