Query on a AS tablename in Oracle 10g - sql

Hey all, how can i go about query off a made AS Tablename in a query of mine?
(its a long query so i have shortened it to the needed lines for this example)
SELECT
TO_CHAR(SYSDATE, 'YYYYMMDD') AS CURDATE,
TO_CHAR(A.DUE_DATE, 'YYYYMMDD') AS DUE,
UGRP.GROUP_CODE AS UGRPCODE,
A.DETAILS,
TRIM(STF.IDENTIFIER_1) || ', ' || TRIM(STF.IDENTIFIER_2) || ' ' || TRIM(STF.IDENTIFIER_3) AS STAFF,
FROM REC.CUSTOM_ATTRIBUTES E
INNER JOIN REC.SERVICE_REQUESTS SR
INNER JOIN REC.IDENTIFIERS STF ON A.ASSIGNED_STAFF_EID = STF.OWNER_EID ON E.OWNER_EID = SR.EID
WHERE (TYP.TYPE_CODE = 'SRSRTYPE')
AND (A.ASSIGNED_STAFF_EID <> 2000478)
AND STAFF = 'BARKER, BOB'
ORDER BY SR.SERVICE_REQUEST_NUM
Naturally the AND STAFF = 'BARKER, BOB" will not work for me.
My question is how can I query on that column?
Thanks!
David

Repeat the formula in your WHERE clause.
... AND TRIM(STF.IDENTIFIER_1) || ', ' || TRIM(STF.IDENTIFIER_2) || ' ' || TRIM(STF.IDENTIFIER_3) = 'BARKER, BOB' ...

Related

SQL Sylob return a boolean

I am currently discovering the Sylob 5 ERP and I would like some help on one of my queries because the Sylob support is overwhelmed with demands.
I would like my query to display if one of the operations in my fabrication order is late.
I already have a query that displays every operations and if it's late or not:
SELECT
distinct
ordreFabrication.codeOF as Code_OF,
operationOrdreFabrication.libelle as Libelle,
operationOrdreFabrication.centreCharge.libelle || ' (' ||
operationOrdreFabrication.centreCharge.code || ')' as Centre_charge,
operationOrdreFabrication.dateDebutPrevue as Début_prévu,
(
CASE
WHEN current_date() > operationOrdreFabrication.dateDebutPrevue and
operationOrdreFabrication.etatAvancementOperationOF = '0' THEN ('<div style=''color:red''>' ||
'Retard sur le début' || ' </div>')
WHEN current_date() > operationOrdreFabrication.dateFinPrevue and
operationOrdreFabrication.etatAvancementOperationOF != '2' THEN ('<div style=''color:red''>' ||
'Retard sur la fin' || ' </div>')
ELSE ('Aucun retard')
END
) as Retard,
operationOrdreFabrication.dateDebutReelle as Début_Réel,
operationOrdreFabrication.dateFinPrevue as Fin_prévue
FROM
OperationOrdreFabricationEntite as operationOrdreFabrication
left outer join operationOrdreFabrication.ordreFabrication as ordreFabrication
WHERE
operationOrdreFabrication.id not like 'DefaultRecord_%'
AND
operationOrdreFabrication.dateFinValidite is null
AND
ordreFabrication.dateFinValidite is null
AND
operationOrdreFabrication.sousTraitance in ('false')
AND
((current_date() > operationOrdreFabrication.dateDebutPrevue and
operationOrdreFabrication.etatAvancementOperationOF = '0'
) OR (current_date() > operationOrdreFabrication.dateFinPrevue and
operationOrdreFabrication.etatAvancementOperationOF != '2'))
ORDER BY
1 asc
But I want this to return true or false when my case returns anything else than "Aucun retard"
so I can use it as a subquery
Looks like you want EXISTS (NOT EXISTS)
select exists(
select 1
from (
--you original query
) t
where retard = 'Aucun retard') flag

Generate a list of rentals, with the client's information for the outlet with the most rentals

I have 4 tables : RentalAgreement (Rentals), Clients, vehicle and outlet.
I need to list rentalAgreementNumbers of THE outlet with the highest rentals with client info and outlet address.
Thanks!
So far, I am getting all 11 rows - that is, all agreements for all outlets
SELECT outlet.Street || ' ' || outlet.City || ', ' || outlet.state || ' - ' || outlet.zipcode AS "Outlet Street Address", rentalNo AS "ID", startDate AS "Start Date", returnDate AS "Return Date", clientName, client.Phone
FROM
client JOIN (ragreement JOIN (vehicle JOIN outlet USING (outNo)) USING (licenseNo)) USING (clientNo)
GROUP BY outlet.Street || ' ' || outlet.City || ', ' || outlet.state || ' - ' || outlet.zipcode, rentalNo, startDate, returnDate, clientName, client.Phone
HAVING COUNT(rentalNo) = (SELECT
MAX(COUNT(rentalNo))
FROM
ragreement
GROUP BY (rentalNo));
How can I modify this to get only 5 rental agreements listed for outlet1 - which has the highest rental agreements in my table?
After the select command, you can set a limit. Mssql syntaxt for this is
Select top(5) [columns] from....
You also need to add a order by clause at the end of the query
... Order by [columns or calculations]

Postgres 9.1's concat_ws equivalent in Amazon redshift

I've got a query originally written for pg9.1 that I'm trying to fix for use on redshift as follows
select concat_ws(' | ', p.gb_id, p.aro_id, p.gb_name) c from (
select ca.p_id,
avg(ca.ab) as ab
from public.fca
join temp_s_ids s on ca.s_id = s.s_id
group by ca.p_id
) as x
join public.dim_protein as p on x.protein_id = p.protein_id;";
I've been trying to test it out on my own, but as it is created from temporary tables that are created by a php session, I haven't had any luck yet. However, my guess is that the concat_ws function isn't working as expected in redshift.
I don't believe there is an equivalent in redshift. You will have to roll your own. If there are no NULLS you can just use the concatenation operator ||:
SELECT p.gb_id || ' | ' || p.aro_id || ' | ' || p.gb_name c
FROM...
If you have to worry about nulls (and its separator):
SELECT CASE WHEN p.gb_id IS NOT NULL THEN p.gb_id || ' | ' END || CASE WHEN p.aro_id IS NOT NULL THEN p.aro_id || ' | ' END || COALESCE(p.gb_name, '') c
FROM
Perhaps that can be simplified, but I believe it will do the trick.
To handle NULLs, you can do:
select trim('|' from
coalesce('|' || p.gb_id) ||
coalesce('|' || p.p.aro_id) ||
coalesce('|' || p.gb_name)
)
from . . .

SQL first occurrence of a string

I have a query which returns results like
Jim 456
Joe 567
Joe 789
Jane 456
What I want to do is have 456 only appear once and take the first name (Jim).
Query looks like
select
p.id_id as num_id, p.FIRST_NAME || ' ' || p.LAST_NAME as Name_
from DWH.V_TABLE p
where p.id_id > 100
The reasons for this is I need each to have only one owner, not two
select p.id_id as num_id,
p.FIRST_NAME || ' ' || p.LAST_NAME as Name_
from DWH.V_TABLE p
where p.id_id>100
LIMIT 1
Does that what you need? It will return only the first result. Also see SQL Limit
Based on the comments above The ask is now I just need one person to take ownership of each result so that they can work on an assigned task. It doesn't matter which of the id's owners it is
the following Query will meet that need.
select p.id_id as num_id
,MIN(p.FIRST_NAME || ' ' || p.LAST_NAME) as Name_
from DWH.V_TABLE p
where p.id_id > 100
GROUP BY
p.id_id
Assuming the "456" is the value stored in the id_id column, then this should do it:
select num_id,
name_
from (
select p.id_id as num_id,
p.first_name || ' ' || p.last_name as name_
row_number() over (partition by p.id_id order by p.FIRST_NAME || ' ' || p.LAST_NAME) as rn
from dwh.v_table p
where p.id_id > 100
) t
where rn = 1;
Based on the comments and the question
Presuming you have a date field to check the real owner, or you can filter the records based on their occurrence in anothertable, I'd say that if you just want to get rid of the duplicates for the ID, use the "distinct" keyword.
select distinct p.id_id as num_id, p.FIRST_NAME || ' ' || p.LAST_NAME as Name_
from DWH.V_TABLE p
where p.id_id > 100
You can also make a self join table (table join to self using p.id_id) and filter the results where "First".name<"Second".name, "First" and "Second" being the aliases of the same table output. (This is only for the case, if you have any sorting criteria base on alphabetical precedence)

Re-qwrite Oracle SQL using ANSI Oracle

I am new to oracle and sql, I would like to know how can I re-write the following sql query using Oracle ANSI join conditions.
SELECT emp_no,
(SELECT emp_title
FROM hr_v_employee
WHERE organization_code LIKE
SUBSTR (emp.depart_code, 0, 4) || '00'
AND emp_position_code =
(SELECT MIN (emp_position_code)
FROM hr_v_employee
WHERE organization_code LIKE
SUBSTR (emp.depart_code,
0,
4
)
|| '00'))
|| ' '
|| NVL (employee_deptartment, '-')
employee_deptartment
FROM employees e, employee_details o
WHERE emp.emp_no = o.emp_no(+)
Edit 1
This what I have tried, my question is it possible to use join or ANSI standard for sub-query as well?
SELECT emp_no,
(SELECT emp_title
FROM hr_v_employee
WHERE organization_code LIKE
SUBSTR (emp.depart_code, 0, 4) || '00'
AND emp_position_code =
(SELECT MIN (emp_position_code)
FROM hr_v_employee
WHERE organization_code LIKE
SUBSTR (emp.depart_code,
0,
4
)
|| '00'))
|| ' '
|| NVL (employee_deptartment, '-')
employee_deptartment
FROM employees e left outer join employee_details o (emp.emp_no = o.emp_no)
Apart from the missing ON keyword for the LEFT OUTER JOIN, your query should be fine:
SELECT emp_no,
(SELECT emp_title
FROM hr_v_employee
WHERE organization_code LIKE SUBSTR(emp.depart_code,
0,
4) || '00'
AND emp_position_code =
(SELECT MIN(emp_position_code)
FROM hr_v_employee
WHERE organization_code LIKE SUBSTR(emp.depart_code,
0,
4) || '00')) || ' ' ||
NVL(employee_deptartment,
'-') employee_deptartment
FROM employees e
LEFT OUTER JOIN employee_details o
ON emp.emp_no = o.emp_no
Please note that you have a typo in your column names - it should be called employee_department instead of employee_deptartment.