Hello can anyone help me with this code?
Iam trying to select employee IDs from employees table TabCisZam. Every employee has an alias which is either ERP, ESh or TO or a combination of these - 'ERP, TO' or 'ESh,TO' depending to which department they belong (TO - technicians, ERP - Enterprice Resource Planning, ESh - EShop)
DECLARE #DruhPozadavku NVARCHAR(3) = 'OST'
SELECT ID FROM TabCisZam where Alias LIKE
CASE WHEN #DruhPozadavku IN ('Heo','Set') THEN '%ERP%'
WHEN #DruhPozadavku IN ('SW','Inf') THEN '%TO%'
WHEN #DruhPozadavku = 'OST' THEN '%TO%'+'%ERP%'
END
)
When the #DruhPozadavku is equal to 'Ost' I want to get all the users with the string ERP or TO in their aliases. So far it does not return any value.
Thanks in advance.
In WHERE clauses you can always use simple boolean logic instead of CASE
SELECT ID
FROM TabCisZam
WHERE (#DruhPozadavku IN ('Heo','Set') AND Alias LIKE '%ERP%')
OR (#DruhPozadavku IN ('SW','Inf') AND Alias LIKE '%TO%')
OR (#DruhPozadavku = 'OST' AND (Alias LIKE '%TO%' OR Alias LIKE '%ERP%'))
Related
I am trying to write a google BigQuery to select some records based on this condition:
if ID_parameter is not null:
SELECT * WHERE ID=ID_parameter
else:
SELECT * WHERE country_id=country_id_parameter and (name LIKE %name_parameter% OR name is null)
The name_parameter should be optional but one of the ID_parameter or country_id_parameter must be provided.
Sorry, I am a newbie with SQL but I hope I stated what I want clearly.
consider below approach
select *
from your_table
where case
when not id_parameter is null then id = id_parameter
else country_id = country_id_parameter and (regexp_contains(name, name_parameter) or name is null)
end
I miss the table name in the query, let's assume it's 'personnel'.
Suppose ID-parameter = 200, country_id_parameter = 'NL', name_parameter = '%bart%'
Maybe like this when the name is 'bart':
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL' and
name LIKE '%bart%')
Use this when the name is NULL:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL')
I'm looking at doing some report development for one of our Training softwares. I finally got some queries working in FB Maestro, as I'm only familiar with SQL and Oracle.
I have the following query that works and returns results, but when trying to set up a parameter for the display name, the query runs (at least it returns no errors) however the dataset does not return any data. Has anyone worked with these before?
Here's the query:
Select CertStatus, DisplayName, Count(CertStatus) From ( With cte as (Select * From (Select COURSEVERSIONSWITHAGGREGATES.CourseTitle, COURSEVERSIONSWITHAGGREGATES.CourseNumber, "MaxTrainedCompletionDate", "Course_ID", PersonnelView.DISPLAYNAME, COURSEVERSIONSWITHAGGREGATES.RecertificationValue, COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID,
CASE
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 3 THEN DATEADD(year, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 2 THEN DATEADD(month, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 1 THEN DATEADD(week, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 0 THEN DATEADD(day, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate") END
AS ExpirationDate
From MAXTRAININGVIEW
INNER JOIN PERSONNELVIEW ON (MAXTRAININGVIEW."Personnel_ID" = PERSONNELVIEW.PERSONNELID) INNER JOIN COURSEVERSIONSWITHAGGREGATES ON (MAXTRAININGVIEW."Course_ID" = COURSEVERSIONSWITHAGGREGATES.COURSEID)
WHERE Personnelview.DisplayName = 'Aaron')) Select CourseTitle, CourseNumber, "MaxTrainedCompletionDate", "Course_ID", DisplayName, RecertificationValue, Recertificationunit_ID, ExpirationDate,
CASE WHEN ExpirationDate > current_date Then 'Active' WHEN ExpirationDate < current_date Then 'Expired' END As CertStatus from cte) Group By CertStatus, DisplayName
This returns values with the static value of 'Aaron' in report builder. But trying to use a parameter, it does not throw an error in report builder, however it just does not return any data.
For example this:
WHERE Personnelview.DisplayName = '#DisplayName'))
I've got the parameter based off another data set query, and that seems to work (it gives me the option to select employees)
Here is an example of it passing 'Aaron' (with personal info removed)
Example of passing #FName Parameter:
If you want to pass the parameter in report, other type database might not recognize query like "where [field] in #parameter", so I think you could try to use filter to achieve this goal(create a filter in dataset, and create a parameter, then specify it in filter properties).
I want to extract names from two columns in one table and join it with another table. If the name in the 'name' column is 'NOT FOUND', I would like to extract it from the 'Description' column.
The 'Description' column will follow 3 patterns:
Name was not found. Name :ab33c and client
So in this case I want to extract ab33c out.
Name j2fc_being was not found:j2fc_being_decom_2017
I want to extract j2fc out.
Name w3fkk was not found:Summary:
I want to extract w3fkk out.
Below are the codes I write:
SELECT inc.name, inc.Description, inc.new_name, srv.dv_category, srv.virtual,
FROM (
SELECT inc.name, inc.Description,
CASE
WHEN inc.name NOT LIKE 'NOT FOUND%' THEN inc.name
WHEN inc.Description LIKE '%Name :%' THEN REGEXP_REPLACE(inc.Description, '.*Name :(.+)(\s).*','\1')
WHEN inc.Description LIKE 'Name%being%' THEN REGEXP_REPLACE(inc.Description, '.*(\s)(.+)_.+','\1')
WHEN inc.Description LIKE 'Name%was%' THEN REGEXP_REPLACE(inc.Description, '.*(\s)(.+)(\s).+','\1')
ELSE inc.name
END as new_name
FROM incident inc
) inc
LEFT JOIN server srv
ON inc.new_name = srv.dv_name
The context will be Impala SQL.
Could I get some help on how to extract? Thank you very much.
I have the following function:
CREATE OR REPLACE FUNCTION calc_a(BIDoctor number) RETURN number
IS
num_a number;
BEGIN
select count(NAppoint)
into num_a
from Appointment a
where BIDoctor = a.BIDoctor;
RETURN num_a;
END calc_a;
What we want is adding a column to a report that shows us the number of appointments that doc have.
select a.BIdoctor "NUM_ALUNO",
a.NameP "Nome",
a.Address "Local",
a.Salary "salary",
a.Phone "phone",
a.NumberService "Curso",
c.BIdoctor "bi",
calc_media(a.BIdoctor) "consultas"
FROM "#OWNER#"."v_Doctor" a, "#OWNER#"."Appointment" c
WHERE a.BIdoctor = c.BIdoctor;
and we got this when we are writing the region source on apex.
But it shows a parse error, I was looking for this about 2 hours and nothing.
Apex shows me this:
PARSE ERROR ON THE FOLLOWING QUERY
This is probably because of all your double quotes, you seem to have randomly cased everything. Double quotes indicate that you're using quoted identifiers, i.e. the object/column must be created with that exact name - "Hi" is not the same as "hi". Judging by your function get rid of all the double quotes - you don't seem to need them.
More generally don't use quoted identifiers. Ever. They cause far more trouble then they're worth. You'll know when you want to use them in the future, if it ever becomes necessary.
There are a few more problems with your SELECT statement.
You're using implicit joins. Explicit joins were added in SQL-92; it's time to start using them - for your future career where you might interact with other RDBMS if nothing else.
There's absolutely no need for your function; you can use the analytic function, COUNT() instead.
Your aliases are a bit wonky - why does a refer to doctors and c to appointments?
Putting all of this together you get:
select d.bidoctor as num_aluno
, d.namep as nome
, d.address as local
, d.salary as salary
, d.phone as phone
, d.numberservice as curso
, a.bidoctor as bi
, count(nappoint) over (partition by a.bidoctor) as consultas
from #owner#.v_doctor a
join #owner#.appointment c
on d.bidoctor = a.bidoctor;
I'm guessing at what the primary keys of APPOINTMENT and V_DOCTOR are but I'm hoping they're NAPPOINT and BIDOCTOR respectively.
Incidentally, your function will never have returned the correct result because you haven't limited the scope of the parameter in your query; you would have just counted the number of records in APPOINTMENT. When you're naming parameters the same as columns in a table you have to explicitly limit the scope to the parameter in any queries you write, for instance:
select count(nappoint) into num_a
from appointment a
where calc_a.bidoctor = a.bidoctor; -- HERE
What I'm trying to do is return a mailbox location for a select few users and a catch all mailbox location for everyone else. In postfix you can pass it a query in the of the form
SELECT mailbox FROM virtual_mailboxes WHERE email = '%s'
and it will replace %s with the email user it's looking for. I have a table that contains a set of users that need their own mailbox(root, support, postmaster) and I want to be able to create a query that returns the configured mailbox or 'all/'. For example if the username is 'support' then I want it to pull the mailbox column for username 'support'. If the username is 'anybody' which doesn't match an rows in the table I want it to return 'all/'.
Here is the query I came up with.
SELECT
CASE
WHEN v2.username IS NOT NULL THEN v2.mailbox
ELSE 'all/'
END AS mailbox2
FROM virtual_mailboxes v1
LEFT JOIN virtual_mailboxes v2 ON v2.username = SUBSTRING_INDEX('support#gmail.com', '#', 1)
LIMIT 1
It works but I feel like it's very wrong to do it that way and I'm curious if there is a better way.
One other thing, postfix will only replace the first occurrence of %s. Thanks for any help.
SELECT coalesce(
(SELECT mailbox FROM virtual_mailboxes WHERE email = '%s'),
'all/'
);