sum of query result form two different table that have nothing in common - sql

hi i have two query result:
Table A
Id u
1 50,00
2 60,00
3 70,00
and
Table B
id c
4 110,00
5 120,01
6 130,02
Now i have doing two query on this table and i want sum their query result.
I want update column c from table B with 160 that is sum of(110+50).
Table B
Id c
4 160,00
Table B and Table A they have nothing in common.
Now i have doing two query for select their value ed for sum two data:
$data=number_format($row['c']+$row1['u']);
$query_updatee="update B set c= (integer)$data where c=110,00";
Can i sum the data from two different table that don't have nothing in comon?
My output is pg_query(): Query failed: ERRORE: syntax error at or near "1" LINE 1: update B set punti = (integer)1680,00 where c=110,00 ^ in C:\xampp\htdocs\table_A.php on line 81

You can use a subquery to fetch the increment. The rest seems to just be filtering:
update B
set c = c + (select a.u from a where a.id = 1)
where B.id = 4;
Obviously, you can use where b.c = 110.00 if you want to filter by a number (or use a comma if that is how the database is set up).

Related

Get the column value that contains only one register in a table to an implicit cursor

I have these tables:
table "articles_description" (a)
code desc
1 article1
2 article2
3 article3
table "articles_type" (b)
code value
1 inf
1 def
2 inf
3 inf
And I need to get from the table b only the code value that get only and only one row with the 'inf' value (in the example I need to pass the code 2 and 3 to the cursor) to fetch values in an implicit cursor for a loop (next code is FOR reg IN c_cur LOOP ... END LOOP;).
This is the implicit cursor:
cursor c_cur is
select a.*
from articles_description a,
articles_type b
where a.code = b.code
and b.value = 'inf'; --(only need to get the article code that has 1 row with 'inf' value)
I have tried to build a function to return the article code, but doesn't work in this type cursor and the requirements forces to be built by this way.
select "code"
from b join a using("code")
group by "code"
having count(*) < 2
and max("value") = 'inf'
code
2
3
Fiddle

Extract only variables which is greater than other table in influxDB

I am using influxDB and I would like to extract some values which is greater than certain threshold in other table.
For example, I have two tables as shown in below.
Table A
Time value
1 15
2 25
3 9
4 22
Table B
Time threshold
1 16
2 12
3 13
4 15
Give above two tables, I would like to extract three values which is greater than first row in Table B. Therefore what I want to have is as below.
Time value
2 25
4 22
I tried it using below sql query, but it didn't give any correct result.
select * from data1 where value > (select spec from spec1 limit1);
Look forward to your feedback.
Thanks.
Integrate the condition in an inner join:
select * from tableA as a
inner join tableB as b on a.id=b.id and a.value > b.threshold
When your time column doesn't only include integer values, you have to format the time and join on a time range. Here is an example:
SQL join on time range

Oracle query to link records from same table which maps to mapping table

Apologies for the horrible question title,not sure how to articulate it better.
So to start out.
**Table Dummy_Table**
id description filter_key
1 Test Record1 filterkey1
2 Test Record2 filterkey1
3 Test Record1 filterkey2
4 Test Record2 filterkey2
The records with filterkey1 map to a table like this
**Table Mapping_table**
Dummy_Table_id someother_key (one(Dummy_Table_id) to many(someother_key)
1 x
1 y
1 z
1 r
2 y
2 r
Now : In a query I map the id's to each other in the Dummy_Table using the description,so I end up with a resultset like this
id_for_filter_key1 id_for_filterkey2
1 3
2 4
Ok,thats all good and well,it's the next step I'm having a issue with.I need to add records to Table Mapping_table which should end up looking like this
**Table Mapping_table**
Dummy_Table_id someother_key
3 x
3 y
3 z
3 r
4 y
4 r
So in essence whatever the id is for filterKey1 I would like to apply it's someother_key to the id's with filterkey2 (filterKey1 and filterkey2 relate to each other with their descriptions)
Now I don't know if I'm over complicating this.I'll tell you what my problem is.
I have records in the database with filterkey1 which map to the mapping table.Afterwords I added the records with filterkey2.These rows are duplicates just with another filter key.Now I need to apply the same mappings to the records with filterkey2
Changing the table structure is not a option atm.I need to give the DBA a insert query to achieve this.
Many thanks in advance.
This query gives missing values:
SELECT d.id_for_filterkey2, m.someother_key
FROM Mapping_table m
JOIN Dummy_Table d ON m.Dummy_Table_id = d.id_for_filter_key1
Demo: http://sqlfiddle.com/#!4/61ddfe/2
When we have missing values, then we can merge them into Mapping_table:
MERGE INTO Mapping_table m
USING( copy-the-above-query-and-paste-it-here) x
ON (x.id_for_filterkey2 = m.Dummy_Table_id)
WHEN NOT MATCHED THEN
INSERT( Dummy_Table_id, someother_key )
VALUES( x.id_for_filterkey2, x.someother_key );
Demo: http://sqlfiddle.com/#!4/d74304/3

Select query with multiple tables

I need a query to select a common record from four table based on single condition from a table
I used a query which returns 240 records but the condition returns only 2 result sets.
Reference no from all the given tables are same.
Select b.cdr_data
,a.cdr_data
,c.cdr_data
from itaukei_data_store b
,itaukei_data_store_key a
,ITAUKEI_BANK_ACCOUNT c
,payment_data_store d
where a.reference_no = b.reference_no
and a.reference_no=c.ITK_REFNO
and b.INDIVIDUAL_REFNO=d.INDIV_REF_NO
and d.remarks='Below 18 years';
But,
select * from payment_data_store where remarks='Below 18 years';
Returns 2 records alone.
You Try like this
Select b.cdr_data,a.cdr_data,c.cdr_data,d.cdr_data
from itaukei_data_store b,itaukei_data_store_key a,
ITAUKEI_BANK_ACCOUNT c,payment_data_store d
where a.reference_no = b.reference_no
and b.reference_no=c.ITK_REFNO
and b.INDIVIDUAL_REFNO=d.INDIV_REF_NO
and d.remarks='Below 18 years';

Update query using IIf() in a WHERE condition

I am writing an update query in Access which is based on a make table query.
In my temporary table (the output of my make table query), I have 3 fields [Tick], [DivLevel4] and [T]. I can have identical Ticks displaying different values for [DivLevel4] if [T] bears different values (only C or F):
Tick DivLevel4 T
AA 5 C
AA 0 F
BB 2 C
CC 3 C
CC 0 F
How can I incorporate this condition in my update query? I have tried adding an IIf() statement in the criteria field but it is not doing the job:
IIf([tblTrades].[T]="Y",0,[tbl_DIV_Temp].[[DivLevel4])
as in
UPDATE
tblTrades
INNER JOIN
tbl_DIV_Temp
ON tblTrades.Tick = tbl_DIV_Temp.Tick
SET tblTrades.DIV = [tbl_DIV_Temp].[DivLevel4]
WHERE (((tblTrades.DIV)=IIf([tblTrades].[T]="F",0,[tbl_DIV_Temp].[DivLevel4])));
Topic is close. Alternative method found.