Postgresql , updating existing table row with another tables data - sql

I am trying to update a null column using another tables value but it doesn't seems to work right. below codes were tried
SET
"Test name "= "Test"(
SELECT Transformertest.Test,Transformertest.TestID
FROM public.Transformertest WHERE TestID='Tes3')
WHERE test2table.Type='Oil Immersed Transformers'
UPDATE
public.test2table
SET
"Test name" = subquery."Test"
FROM
(
SELECT
"Test"
FROM Transformertest WHERE "TestID"='Tes2'
) AS subquery
WHERE
"Type"='Auto Transformer' AND "Phase"='3' AND "Rated Frequency"='60';
enter image description here

don't use space in column name.
Integers don't need to be quoted
See the result here (enter link description here)

what you need to do here (assuming Phase and Rated Frequency are integers)
remove unnecessary "" and spaces on column names
UPDATE
public.test2table
SET
test_name = subquery.Test
FROM
(
SELECT
test
FROM Transformertest WHERE Test_ID='Tes2'
) AS subquery
WHERE
Type='Auto Transformer' AND Phase=3 AND Rated_Frequency=60;
this should be working now

Related

Remove string from multiple rows SQL

I have a table that has "HQ created..." in 100+ rows. Instead of manually updating those rows I want to trim that part off.
I tried:
UPDATE Table SET Desc = LTRIM('HQ created') WHERE Desc LIKE '%HQ created%'
and that changed all 100+ rows to "HQ created" instead of cutting that part off.
(I also tried SET HADEDesc = RTRIM('HQ created'))
A replace will not work as outside of the "HQ created..." the names are different for example "HQ created Date" and "HQ created Time".
update table
set desc = replace(Desc, 'HQ created' , '')
WHERE Desc LIKE '%HQ created%'

Get all entries for a specific json tag only in postgresql

I have a database with a json field which has multiple parts including one called tags, there are other entries as below but I want to return only the fields with "{"tags":{"+good":true}}".
"{"tags":{"+good":true}}"
"{"has_temps":false,"tags":{"+good":true}}"
"{"tags":{"+good":true}}"
"{"has_temps":false,"too_long":true,"too_long_as_of":"2016-02-12T12:28:28.238+00:00","tags":{"+good":true}}"
I can get part of the way there with this statement in my where clause trips.metadata->'tags'->>'+good' = 'true' but that returns all instances where tags are good and true including all entries above. I want to return entries with the specific statement "{"tags":{"+good":true}}" only. So taking out the two entries that begin has_temps.
Any thoughts on how to do this?
With jsonb column the solution is obvious:
with trips(metadata) as (
values
('{"tags":{"+good":true}}'::jsonb),
('{"has_temps":false,"tags":{"+good":true}}'),
('{"tags":{"+good":true}}'),
('{"has_temps":false,"too_long":true,"too_long_as_of":"2016-02-12T12:28:28.238+00:00","tags":{"+good":true}}')
)
select *
from trips
where metadata = '{"tags":{"+good":true}}';
metadata
-------------------------
{"tags":{"+good":true}}
{"tags":{"+good":true}}
(2 rows)
If the column's type is json then you should cast it to jsonb:
...
where metadata::jsonb = '{"tags":{"+good":true}}';
If I get you right, you can check text value of the "tags" key, like here:
select true
where '{"has_temps":false,"too_long":true,"too_long_as_of":"2016-02-12T12:28:28.238+00:00","tags":{"+good":true}}'::json->>'tags'
= '{"+good":true}'

Update all rows where contains 5 keys

I have Ticket table that has some columns like this :
ID : int
Body : nvarchar
Type : int
I have many rows where the Body column has value like this :
IPAddress = sometext, ComputerName = sometext , GetID = sometext, CustomerName=sometext-sometext , PharmacyCode = 13162900
I want update all rows' Type column where the Body column has at least five of the following keys:
IPAddress, ComputerName, GetID, CustomerName, PharmacyCode
You could do it with a simple update statement like that
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%'
and Body LIKE '%ComputerName%'
and Body LIKE '%GetID%'
and Body LIKE '%CustomerName%'
and Body LIKE '%PharmacyCode%'
if you know the 'keys' are always in the same order you could concatenate the LIKE conditions like so
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%ComputerName%GetID%CustomerName%PharmacyCode%'
If you have the possibility to change the data model it would be much better to explode this key & value column into an own table and link it back to this table as it is done in a proper relational model.
If you could calculate number of key value pair by number of = present in your string you could use this query
Update tblname set col=val where len(colname) - len(replace(colname,'=','')>5
The where part actually gives number of equal signs present in your string.

Invalid column issue

I created a view as below from my existing tables:
Create View LostCase
As
Select L.LNO,Count(*) "NoOfCasesLost"
From L, LWCS,C
Where L.LNO=LWCS.LNO
And LWCS.CASENO=C.CNO
And C.OUTCOME='LOSE'
Group By L.LNO ;
However, whenever I run query as per below , I always get
invalid column name for LostCase.NoOfCasesLost
Select L.LNO,L.LNAME,LostCase.NoOfCasesLost
From L, LostCase
Where L.LNO = LostCase.LNO;
I don't get it why it happens.
If you have specified the column name in the quotation marks, you have to refer it afterwards as:
Select L.LNO,L.LNAME,LostCase."NoOfCasesLost"
From L,LostCase
Where L.LNO=LostCase.LNO;

What to do with the error "Ambiguous Columns Defined"?

This is my sql command:
select INCOME_TYPE_ID,
REGION_CODE,
FIN_YEAR_CODE,
PORTION_AMOUNT
from INCOME.INCOME_TYPE,
COMMON.REGION,
INCOME.RECEIVE_DOC_PORTION,
INCOME.ASSESS_ORDER_ITEM,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC,
ACCOUNTING.VOUCHER_ITEM,
ACCOUNTING.VOUCHER,
ACCOUNTING.FIN_YEAR
where INCOME.RECEIVE_DOC_PORTION.ASSESS_ORDER_ITEM_ID = INCOME.ASSESS_ORDER_ITEM.ASSESS_ORDER_ITEM_ID
and INCOME.ASSESS_ORDER_ITEM.INCOME_TYPE_ID=INCOME.INCOME_TYPE.INCOME_TYPE_ID
and INCOME.RECEIVE_DOC_PORTION.RECEIVE_DOC_PORTION_ID = ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.RECEIVE_DOC_PORTION_ID
and ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.VOUCHER_ITEM_ID = ACCOUNTING.VOUCHER_ITEM.VOUCHER_ITEM_ID
and ACCOUNTING.VOUCHER_ITEM.VOUCHER_ID = ACCOUNTING.VOUCHER.VOUCHER_ID
and ACCOUNTING.VOUCHER.REGION_CODE = COMMON.REGION.REGION_CODE
and ACCOUNTING.VOUCHER.FIN_YEAR_CODE = ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE
and I got this error:
Ambiguous Columns Defined
I'm Using SQL Developer as Oracle client.
Apparently one (or more) column names in your select list exists in more than one table of the FROM list.
You need to prefix every column in the SELECT list with the table it's coming from (it's also a good practice to always do that, regardless of the fact if they are ambigous)
Mention name of table before every column in select query.
Ambiguous column means that you have more than one column with the same name in one of the SELECT statements.
Try this instead, prefgixing all selected columns with their fully qualified names (as you have done elsewhere in your SELECT):
select INCOME.INCOME_TYPE.INCOME_TYPE_ID,
COMMON.REGION.REGION_CODE,
ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.PORTION_AMOUNT
from INCOME.INCOME_TYPE,
COMMON.REGION,
INCOME.RECEIVE_DOC_PORTION,
INCOME.ASSESS_ORDER_ITEM,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC,
ACCOUNTING.VOUCHER_ITEM,
ACCOUNTING.VOUCHER,
ACCOUNTING.FIN_YEAR
where INCOME.RECEIVE_DOC_PORTION.ASSESS_ORDER_ITEM_ID = INCOME.ASSESS_ORDER_ITEM.ASSESS_ORDER_ITEM_ID
and INCOME.ASSESS_ORDER_ITEM.INCOME_TYPE_ID = INCOME.INCOME_TYPE.INCOME_TYPE_ID
and INCOME.RECEIVE_DOC_PORTION.RECEIVE_DOC_PORTION_ID = ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.RECEIVE_DOC_PORTION_ID
and ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.VOUCHER_ITEM_ID = ACCOUNTING.VOUCHER_ITEM.VOUCHER_ITEM_ID
and ACCOUNTING.VOUCHER_ITEM.VOUCHER_ID = ACCOUNTING.VOUCHER.VOUCHER_ID
and ACCOUNTING.VOUCHER.REGION_CODE = COMMON.REGION.REGION_CODE
and ACCOUNTING.VOUCHER.FIN_YEAR_CODE = ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE
I had to guess the filly qualified name for
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.PORTION_AMOUNT
It might be
INCOME.RECEIVE_DOC_PORTION.PORTION_AMOUNT
But you should be able to resolve that easily.
Hope it helps...