Build a field in the Select Q - sql

I am trying to add a field in my Select statment that will create a field with my desired output or make the field results empty. My Boss doesn't want to scan the entire mopdescription field if they have to.
Please see code ** to see if you can make sense of this question/request.
SELECT MOPACTIVITY.MOPID,
**UPPER(MOPACTIVITY.MOPDESCRIPTION) LIKE '%FTTT%'
OR UPPER(MOPACTIVITY.MOPDESCRIPTION) LIKE '%VZW%' AS "NOTE_DISP"**,
MOPACTIVITY.MOPDESCRIPTION
FROM MOPUSER.MOPACTIVITY

SELECT mopid
, CASE
WHEN INSTR(UPPER(mopdescription), 'FTTT') > 0
THEN SUBSTR(mopdescription, INSTR(UPPER(mopdescription), 'FTTT'))
WHEN INSTR(UPPER(mopdescription), 'VZW') > 0
THEN SUBSTR(mopdescription, INSTR(UPPER(mopdescription), 'VZW'))
ELSE NULL
END AS note_disp
FROM mopuser.mopactivity';

Related

How to exclude SQL variable in output

I have a complex SQL query where I have a few cases that use END AS variableName. I then use variableName to do some logic and then create a new variable which I want in the output result. However when I run the query, all the END AS variableNames that I have used are also outputted in the results.
Is there a way that I can exclude these variables as I only want the final variable that uses these variableNames.
Thanks
EDIT, here is a query explaining my problem
SELECT DISTINCT
mt.src_id AS “SRC_ID”,
CASE
WHEN mt.cd = ‘TAN’ THEN
(
(
SELECT SUM(src_amt)
FROM source_table st
WHERE mt.id = st.id
AND st._cd = ‘TAN’
AND st.amt_cd = ‘ABL’)
)
END AS src_amt
FROM MAIN_TABLE mt
WHERE
mf.dt >= 2021-12-12
AND SRC_AMT > 10
I need SRC_AMT to be used as some sort of logic but when I run the query, it prints out in the output as it's own column. I want to ignore this variable
you can wrap the whole thing into a new select-statement:
select SRC_ID from ( <entire previous query here> )

find diffrences between 2 tables sql and how can i get the changed value?

i have this query
insert into changes (id_registro)
select d2.id_registro
from daily2 d2
where exists (
select 1
from daily d1
where
d1.id_registro = d2.id_registro
and (d2.origen, d2.sector, d2.entidad_um, d2.sexo, d2.entidad_nac, d2.entidad_res,
d2.municipio_res, d2.tipo_paciente,d2.fecha_ingreso, d2.fecha_sintomas,
d2.fecha_def, d2.intubado, d2.neumonia, d2.edad, d2.nacionalidad, d2.embarazo,
d2.habla_lengua_indig, d2.diabetes, d2.epoc, d2.asma, d2.inmusupr, d2.hipertension,
d2.otra_com, d2.cardiovascular, d2.obesidad,
d2.renal_cronica, d2.tabaquismo, d2.otro_caso, d2.resultado, d2.migrante,
d2.pais_nacionalidad, d2.pais_origen, d2.uci )
<>
(d1.origen, d1.sector, d1.entidad_um, d1.sexo, d1.entidad_nac, d1.entidad_res,
d1.municipio_res, d1.tipo_paciente, d1.fecha_ingreso, d1.fecha_sintomas,
d1.fecha_def, d1.intubado, d1.neumonia, d1.edad, d1.nacionalidad, d1.embarazo,
d1.habla_lengua_indig, d1.diabetes, d1.epoc, d1.asma, d1.inmusupr, d1.hipertension,
d1.otra_com, d1.cardiovascular, d1.obesidad,
d1.renal_cronica, d1.tabaquismo, d1.otro_caso, d1.resultado, d1.migrante,
d1.pais_nacionalidad, d1.pais_origen, d1.uci ))
it results in an insersion data that doesn't exist in another table, that's fine. but i want know exactly which field has changed to store it in a log table
You don't mention precisely what you expect to see in your output but basically to accomplish what you're after you'll need a long sequence of CASE clauses, one for each column
e.g. one approach might be to create a comma-separated list of the column names that have changed:
INSERT INTO changes (id_registro, column_diffs)
SELECT d2.id_registro,
CONCAT(
CASE WHEN d1.origen <> d2.origen THEN 'Origen,' ELSE '' END,
CASE WHEN d1.sector <> d2.sector THEN 'Sector,' ELSE '' END,
etc.
Within the THEN part of the CASE you can build whatever detail you want to show
e.g. a string showing before and after values of the columns CONCAT('Origen: Was==> ', d1.origen, ' Now==>', d2.origen). Presumably though you'll also need to record the times of these changes if there can be multiple updates to the same record throughout the day.
Essentially you'll need to decide what information you want to show in your logfile, but based on your example query you should have all the information you need.

SQL - When a column has a value from a list and a value not in that same list

Not sure the best way to word this but I'm looking for a way to specify a condition when a value in a column has at least one value in a given list AND avalue not in the same list, then that column's value should show up. An example table:
email program
john#john.com program1
john#john.com program2
john#john.com program3
jeff#jeff.com program3
jeff#jeff.com program4
steve#steve.com program1
steve#steve.com program2
If I have this table and a list of (program1, program2), I would like the corresponding email to show up if the programs associated with a given email match at least one in the given list AND if the given email has a program NOT in the given list
So for the table above and the given list above all we would have show up with the correct query would be:
email
john#john.com
Any help on this would be greatly appreciated. Note: this would be in Redshift/PostgreSQL
I like doing this with group by and having. Here is a pretty general approach:
select email
from t
group by email
having sum( (program = 'program1')::int ) > 0 and
sum( (program = 'program2')::int ) = 0;
In this case, "program1" is required and "program2" is not. And, you can keep adding conditions -- as many as you like.
I forget if Redshift supports the :: syntax. You can always express this using standard SQL:
having sum( case when program = 'program1' then 1 else 0 end ) > 0 and
sum( case when program = 'program2' then 1 else 0 end ) = 0;
EDIT:
I think #dnswit is right on the parsing of the OP's question. The logic would be:
having sum( (program in ('program1', 'program2'))::int ) > 0 and
sum( (program not in ('program1', 'program2'))::int ) > 0;
if you just want a single list of emails no matter how many times they are on the list by having multiple programs
it is just select distinct email from tablename
First your Data Table is constructed wrong, you should use an unique Identifier so you can retrieve the program version you are specifying.
so your database should look like this:
> email program1 program2 program3
john#john.com ProgVersion1 ProgVersion2 ProgVersion3
steve#steve.com ProgVersion1 ProgVersion2 ProgVersion3
If you notice of the table above you can now query to get the program value you need for the specified Email. Use SQL Query, your Data Fields for your table are email, Program 1 Program 2 Program 3, when retrieving the value of the fields to be displayed, you are using redundancy you do not need to repeat the email address multiple times for each version of the program. This would not be expectable methodology.
SQL Query you can use:
instructions: you will create a parameter to use as a variable to query the data table from the list.
> CREATE PROCEDURE spLoadMyProgramVersion
>
> #email nvarchar(50),
>
> AS
>
>BEGIN
>SELECT program1,program2,program3
>FROM MyTableName
>WHERE (email LIKE #email) RETURN
This will allow you to load all your program version in a list by just specify the email address you want to load, this is a loading stored procedure just use it when you make a SQLCommand Object you can call your stored procedure.

CASE in WHERE clause returns Error

I'm running this code:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%' AND
CASE src
WHEN 0 THEN
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
END
I get this error:
Incorrect syntax near the keyword 'IN'
I have no idea what's wrong.
A CASE statement is not appropriate in this case. Just use a simple OR condition:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND (
src <> 0 -- add a COALESCE here if src can be NULL
OR uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
)
... which basically is the equivalent of only applying the uID filtering if src = 0, which is what you appeared to be trying to accomplish with your query.
It sounds like you don't have to use CASE please test this
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND ( (src = 0 --scr is 0 => must have uid in the list
AND UID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481))
OR src <> 0) --else src is not 0 and there is no additional condition
i have a doubt on your not like condition have you tested it alone?
Guess this is what you are looking for
WHERE aIDs NOT LIKE '%''101''%' or
(
src = 0
AND
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481
)
It looks like your statement is not formatted properly. you placed a condition rather than a value to set within "When 0 then ...... END
the uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481) you have there is a condition and shouldn't have been there
The when part of a case statement should select a single value. what you are trying to do is to check for a condition.
Or if you are checking for uid in those values, you should do
case when src = 0 then
case when uID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
then 'It is a Client ID'
-- add another when or else part here if required
end
else 'Not a UID and not a Client ID'
end

Dynamic appending constraints in SQL query

I have this SQL
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
and <<either bdvalue or string value based on user selected value datatype>>
Here in the Query based on the devudp1.valueType I want to append below attribute
If the valueType is 3 then I want to append my above select clause with devudp1.bdvalue ='10', else it should be appended by devudp1.bdvalue = 'Hello'
So the above query when valueType is 3 will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.bdvalue = '10'
else it will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.stringValue = 'Hello'
Can anyone suggest me how to put this logic in place
Try this:
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND (
(<USER-SELECTED-VALUE> = 3 AND devudp1.bdvalue ='10') OR
(<USER-SELECTED-VALUE> <> 3 AND devudp1.stringvalue ='Hello')
)