I'm having problems to add some values to a table. The query that i'm using is this one:
INSERT INTO ACTUAR
(CAPITULO,TEMPORADA,COD_GRUPO,COD_LOC)
SELECT NUM_CAP,NUM_TEMPO,CODIGO_GRUPO,CODIGO_LOC
FROM COMENTAR
MINUS
SELECT CAPITULO,TEMPORADA,COD_GRUPO, COD_LOC
FROM ACTUAR;
The ACTUAR table has 2 more values, which I want to add from a value on COMENTAR like this:
NVL(SUBSTR(COMENTARIO,0,10),'SIN PROBLEMA'),
NVL(SUBSTR(COMENTARIO,LENGTH(COMENTARIO)-4),'SIN SOLUCION')
Actuar table
CAPITULO,TEMPORADA,COD_GRUPO,COD_LOC,PROBLEMA,SOLUCION
Comentar table
NUM_CAP,NUM_TEMPO,CODIGO_GRUPO,CODIGO_LOC,COMENTARIO
I want to put the first 10 chars of COMENTARIO on ACTUAR'S PROBLEMA and the last 4 on ACTUAR'S SOLUCION or the text in the nvl if they are null.
I want to insert the result of the minus operation on ACTUAR plus the 10 first chars of COMENTARIO from COMENTAR into ACTUAR'S PROBLEMA and the last 4 on ACTUAR'S SOLUCION
The desired result should be something like
ACTUAR
CAPITULO,TEMPORADA,COD_GRUPO, and COD_LOC the values from the minus operation.
PROBLEMA - first 10 chars of COMENTAR COMENTARIO
SOLUCION - last 4
This solution worked for me. I used merge statement, without update clause.
merge into actuar a
using (
select num_cap, num_tempo, codigo_grupo, codigo_loc,
nvl(substr(comentario,1,10),'SIN PROBLEMA') prob,
nvl(substr(comentario, greatest(length(comentario)-4, 1)),'SIN SOLUCION') sol
from comentar) c
on (a.capitulo = c.num_cap and a.temporada = c.num_tempo
and a.cod_grupo = c.codigo_grupo and a.cod_loc = c.codigo_loc)
when not matched then insert (a.capitulo, a.temporada, a.cod_grupo,
a.cod_loc, a.problema, a.solucion)
values (c.num_cap, c.num_tempo, c.codigo_grupo, c.codigo_loc, c.prob, c.sol)
Related
I am working with the JSON_VALUE function and I need a kind of dynamic query
I have a column called Criteria and sometimes it has 1 value but sometimes it has 2 or 3 vales like:
Example of 1 value: $.IRId = 1
Example of 2 values: $.IROwner = 'james.jonson#domain.com' AND DaysTillDue < 10
So in order to read the values from a JSON column and taking the Criteria column I am using this logic:
DECLARE #CriteriaValue int
,#CriteriaStatement VARCHAR(50)
SELECT #CriteriaValue=SUBSTRING(Criteria, CHARINDEX('=',Criteria)+1, len(Criteria)) FROM #SubscriptionCriteria;
SELECT #CriteriaStatement= SUBSTRING(Criteria,0, CHARINDEX('=',Criteria)) FROM #SubscriptionCriteria;
SELECT #CriteriaValue,#CriteriaStatement
SELECT *
FROM [SAAS].[ObjectEvent]
WHERE
JSON_VALUE(JSONMessageData, #CriteriaStatement) = #CriteriaValue
That SQL code is taking only the Criteria Column with only 1 value ($.IRId = 1), but the idea is to have something that reads the criteria no matter the different filters and apply them into the final query. The idea I have is that the query would look like this:
SELECT *
FROM [SAAS].[ObjectEvent]
WHERE
JSON_VALUE(JSONMessageData, #CriteriaStatement1) = #CriteriaValue1 ADN JSON_VALUE(JSONMessageData, #CriteriaStatement2) = #CriteriaValue2 AND
JSON_VALUE(JSONMessageData, #CriteriaStatement3) = #CriteriaValue3
ETC
Any suggestion?
i create this code
INSERT INTO `ordini`(`ora`, `nome`, `indirizzo`, `numero`, `help`,`ID`,`fattorino`,`dreturn`,`flag`)
VALUES ((SELECT `ora`, `nome`, `indirizzo`, `numero`, `help`,`ID` FROM test.dbconsegne WHERE ID = 2),'STRING','CURRENT_TIMESTAMP','0');
and when i try to Execute the query , The PHPMyAdmin push out this error
Operand should contain 1 colum(s)
why? it's not correct
sorry for my bad english
You dont need VALUES, take it out
INSERT INTO `ordini`(`ora`, `nome`, `indirizzo`, `numero`, `help`,`ID`,`fattorino`,`dreturn`,`flag`)
SELECT `ora`, `nome`, `indirizzo`, `numero`, `help`,`ID`,'STRING',CURRENT_TIMESTAMP,'0' FROM test.dbconsegne WHERE ID = 2
i have long store procedure in whcih i want Add filter on this Column
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
where Unapplied = 22
but i run the query they give me error invalid column name. any one tell me the write way to Add filter on Unapplied Column
You can do it like below :
select * from
(
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
) as T where Unapplied = 22
Or like this :
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
where PaymentAmount-PaymentPosted = 22
It should be like this:
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
where (PaymentAmount-PaymentPosted) = 22
'Unapplied' is not a column name it is only alias.
I'm trying to transition from MySQL to SQLIte3 and running into an update problem. I'm using SQLite 3.6.20 on redhat.
My first line of code behaves normally
update atv_covar set noncomp= 2;
All values for noncomp (in the rightmost column) are appropriately set to 2.
select * from atv_covar;
A5202|S182|2
A5202|S183|2
A5202|S184|2
It is the second line of code that gives me problems:
update atv_covar
set noncomp= (select 1 from f4003 where
atv_covar.study = f4003.study and
atv_covar.rpid = f4003.rpid and
(rsoffrx="81" or rsoffrx="77"));
It runs without generating errors and appropriately sets atv_covar.noncomp to 1 where it matches the SELECT statement. The problem is that it changes atv_covar.noncomp for the non-matching rows to null, where I want it to keep them as 2.
select * from atv_covar;
A5202|S182|
A5202|S183|1
A5202|S184|
Any help would be welcome.
#Dan, the problem with your query is not specific to SQLite; you are updating all rows of atv_covar, but not all of them have correspondence in f4003, so these default to NULL. You should filter the update or provide a default value.
The following statement sets 1 only to the rows that macth the filtering condition:
UPDATE atv_covar
SET noncomp = 1
WHERE EXISTS (
SELECT 'x'
FROM f4003
WHERE atv_covar.study = f4003.study
AND atv_covar.rpid = f4003.rpid
AND (rsoffrx="81" or rsoffrx="77")
);
The following statement sets 1 or 2 for all rows of noncomp, depending on the filtering match (use this instead of two updates):
UPDATE atv_covar
SET noncomp = COALESCE((
SELECT 1
FROM f4003
WHERE atv_covar.study = f4003.study
AND atv_covar.rpid = f4003.rpid
AND (rsoffrx="81" or rsoffrx="77")
), 2);
Been working on this query for some time now... Keep getting the error "Corrosponding select-list expressions are not compatible. I am selecting the same # of columns in each select statement.
create volatile table dt as (
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gpv.c_val,
gpv.i_val,
gcv.c_pol,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
and gcv.c_pol between 'lac100001' and 'lac100004'
UNION
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gcv.c_pol,
gpv.i_val,
gpv.i_pln,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
where NOT EXISTS(
SELECT 1
FROM pearl_p.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
)
) with data
PRIMARY INDEX (i_sys_idv)
on commit preserve rows;
You should check the data types of each column. The data types must be compatible between each SELECT statement.
The last 4 values of your second select statement don't match the ones in your first statement. Try naming(using aliases) those columns the same thing(pairing them). To union you need to have the same columns between the sets.
Check out: http://msdn.microsoft.com/en-us/library/ms180026.aspx
The following are basic rules for combining the result sets of two
queries by using UNION:
The number and the order of the columns must be the same in all
queries.
The data types must be compatible.
Are the data types of each column the same in both portions of the query?
If the first character of the column name indicates the data type (i = integer, c = character, etc.) I'm guessing that the problem is with the second to last column in the select list. The first query is selecting gcv.c_pol, the second query is selecting gpv.i_pln.
Start commenting-out lines until it works. I.e., comment out all of the fields in each select list, and then one-by-one, un-comment the first one out, then the second, etc. You'll find it eventually.