I have a query below. i can check two conditions. If 'AAA' occurs it will show "AAA" with its id else it will show 'BBB'. I need to check more conditions like
if 'AAA' occurs print "AAA" with its id
else 'BBB' occurs print "BBB" with its id
else 'ccc' occurs print "CCC" with its id
how to do this?
SELECT DISTINCT institution_id,
CASE
WHEN count(*) over(PARTITION BY institution_id) >=5 THEN 'AAA'
WHEN count(*) over(PARTITION BY institution_id) >=5 THEN 'BBB'
END AS status
FROM table
WHERE data LIKE '% AAA%'
WITH
INSTITUTION_COUNT
AS
( SELECT INSTITUTION_ID, COUNT (*) AS INSTITUTION_COUNT
FROM INSTITUTION
GROUP BY INSTITUTION_ID)
SELECT E.INSTITUTION_ID AS INSTITUTION_ID,
DC.INSTITUTION_COUNT AS INSTITUTION_COUNT,
CASE
WHEN DC.INSTITUTION_COUNT < 5 THEN 'Low'
WHEN DC.INSTITUTION_COUNT >= 5 THEN 'High'
END AS STATUS
FROM INSTITUTION E, INSTITUTION_COUNT DC
WHERE E.INSTITUTION_ID = DC.INSTITUTION_ID;
Related
I am trying to get all the customers whether they have install service or not from TRANS_TABLE.
NOA- query to get only the MAX product and join again with TRANS_TABLE by email id to get the all the MAX customers details (wwhther they have install service by adding Y OR N, but this query return duplicate with REP Product as well
Below is my Oracel Query which give duplicated
with CTE as (SELECT NOA.*,
CASE
WHEN TRANS_TABLE.product_name LIKE '%Installation%' THEN 'Y'
ELSE 'N'
END AS Installaion ,
ROW_NUMBER() OVER (PARTITION BY TRANS_TABLE.email_address ORDER BY TRANS_TABLE.email_address) AS rn
FROM (SELECT DISTINCT email_address
FROM TRANS_TABLE
WHERE email_address IS NOT NULL
and pdct_name like '%MAX%'
) NOA
LEFT JOIN TRANS_TABLE
ON NOA.email_address = TRANS_TABLE.email_address
select * from cte where rn='1'
The following code will help:
CTE AS (
SELECT
NOA.*,
CASE
WHEN TRANS_TABLE.PDCT_NAME LIKE '%INSTALLATION%' THEN 'Y' -- case sensitive name is used
ELSE 'N'
END AS INSTALLAION,
ROW_NUMBER() OVER(
PARTITION BY TRANS_TABLE.EMAIL_ADDRESS
ORDER BY
TRANS_TABLE.EMAIL_ADDRESS
) AS RN
FROM
(
SELECT DISTINCT
PDCT_NAME,
EMAIL_ADDRESS
FROM
TRANS_TABLE
WHERE
EMAIL_ADDRESS IS NOT NULL
AND PDCT_NAME LIKE '%MAX%'
) NOA
LEFT JOIN TRANS_TABLE ON NOA.EMAIL_ADDRESS = TRANS_TABLE.EMAIL_ADDRESS
WHERE TRANS_TABLE.PDCT_NAME NOT LIKE '%REP%') -- added this WHERE condition
SELECT
PDCT_NAME, EMAIL_ADDRESS, INSTALLAION
FROM
CTE
WHERE
RN = '1'
db<>fiddle demo
Cheers!!
I am working on an existing query for SSRS report that focuses on aggregated financial aid data split out into 10 aggregations. User wants to be able to select students included in that aggregated data based on new vs. returning and 'selected for verification.' For the new/returning status, I added a CTE to return the earliest admit date for a student. 2 of the 10 data fields are created by a subquery. I have been trying for 3 days to get the subquery to use the CTE fields for a filter, but they won't work. Either they're ignored or I get a 'not a group by expression' error. If I put the join to the CTE within the subquery, the query time jumps from 45 second to 400 seconds. This shouldn't be that complicated! What am I missing? I have added some of the code... 3 of the chunks work - paid_something doesn't.
with stuStatus as
(select
person_uid, min(year_admitted) admit_year
from academic_study
where aid_year between :AidYearStartParameter and :AidYearEndParameter
group by person_uid)
--- above code added to get student information not originally in qry
select
finaid_applicant_status.aid_year
, count(1) as fafsa_cnt --works
, sum( --works
case
when (
package_complete_date is not null
and admit.status is not null
)
then 1
else 0
end
) as admit_and_package
, (select count(*) --does't work
from (
select distinct award_by_aid_year.person_uid
from
award_by_aid_year
where
award_by_aid_year.aid_year = finaid_applicant_status.aid_year
and award_by_aid_year.total_paid_amount > 0 )dta
where
(
(:StudentStatusParameter = 'N' and stuStatus.admit_year = finaid_applicant_status.aid_year)
OR
(:StudentStatusParameter = 'R' and stuStatus.admit_year <> finaid_applicant_status.aid_year)
OR :StudentStatusParameter = '%'
)
)
as paid_something
, sum( --works
case
when exists (
select
1
from
award_by_person abp
where
abp.person_uid = fafsa.person_uid
and abp.aid_year = fafsa.aid_year
and abp.award_paid_amount > 0
) and fafsa.requirement is not null
then 1
else 0
end
) as paid_something_fafsa
from
finaid_applicant_status
join finaid_tracking_requirement fafsa
on finaid_applicant_status.person_uid = fafsa.person_uid
and finaid_applicant_status.aid_year = fafsa.aid_year
and fafsa.requirement = 'FAFSA'
left join finaid_tracking_requirement admit
on finaid_applicant_status.person_uid = admit.person_uid
and finaid_applicant_status.aid_year = admit.aid_year
and admit.requirement = 'ADMIT'
and admit.status in ('M', 'P')
left outer join stuStatus
on finaid_applicant_status.person_uid = stuStatus.person_uid
where
finaid_applicant_status.aid_year between :AidYearStartParameter and :AidYearEndParameter
and (
(:VerifiedParameter = '%') OR
(:VerifiedParameter <> '%' AND finaid_applicant_status.verification_required_ind = :VerifiedParameter)
)
and
(
(:StudentStatusParameter = 'N' and (stuStatus.admit_year IS NULL OR stuStatus.admit_year = finaid_applicant_status.aid_year ))
OR
(:StudentStatusParameter = 'R' and stuStatus.admit_year <> finaid_applicant_status.aid_year)
OR :StudentStatusParameter = '%'
)
group by
finaid_applicant_status.aid_year
order by
finaid_applicant_status.aid_year
Not sure if this helps, but you have something like this:
select aid_year, count(1) c1,
(select count(1)
from (select distinct person_uid
from award_by_aid_year a
where a.aid_year = fas.aid_year))
from finaid_applicant_status fas
group by aid_year;
This query throws ORA-00904 FAS.AID_YEAR invalid identifier. It is because fas.aid_year is nested too deep in subquery.
If you are able to modify your subquery from select count(1) from (select distinct sth from ... where year = fas.year) to select count(distinct sth) from ... where year = fas.year then it has the chance to work.
select aid_year, count(1) c1,
(select count(distinct person_uid)
from award_by_aid_year a
where a.aid_year = fas.aid_year) c2
from finaid_applicant_status fas
group by aid_year
Here is simplified demo showing non-working and working queries. Of course your query is much more complicated, but this is something what you could check.
Also maybe you can use dbfiddle or sqlfiddle to set up some test case? Or show us sample (anonimized) data and required output for them?
I'm trying to get the maximum value of a count. The code is as follows
SELECT coachID, COUNT(coachID)
FROM coaches_awards GROUP BY coachID
HAVING COUNT(coachID) =
(
SELECT MAX(t2.awards)
FROM (
SELECT coachID, count(coachID) as awards
FROM coaches_awards
GROUP BY coachID
) t2
);
Yet something keeps failing. The inner query works and gives the answer that I want and the outer query will work if the inner query is replaced by the number required. So I'm assuming I've made some syntax error.
Where am I going wrong?
If you are just looking for one row, why not do:
SELECT coachID, COUNT(coachID) as cnt
FROM coaches_awards
GROUP BY coachID
ORDER BY cnt DESC
LIMIT 1;
If you want ties, then use RANK() or DENSE_RANK():
SELECT ca.*
FROM (SELECT coachID, COUNT(*) as cnt,
RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
FROM coaches_awards
GROUP BY coachID
) ca
WHERE seqnum = 1;
H,
How to use Pattern Matching function in "group By" clause.
Below is the Data
emp_name transaction_id
John 1001
John= 1002
Peter 1003
I want to group by based on emp_name. Here 'John' and 'John=' both are same employee. I want to ignore if the employee name has '=' sign at the end of the column.
Expected Result:
Emp_name countt
John 2
Peter 1
replace works fine and is fast. But since you asked for pattern matching, here is an answer with a pattern:
SELECT regexp_replace(emp_name, '=$', ''), count(*) AS countt
FROM employees
GROUP BY regexp_replace(emp_name, '=$', '');
A simple case statement replacing only the right most = if one exists.
SELECT case when right(emp_name,1) = '=' then left(emp_Name,len(emp_name-1))
else emp_name end as EmpName, count(Transaction_ID) countt
FROM dataTable
GROUP BY case when right(emp_name,1) = '=' then left(emp_Name,len(emp_name-1))
else emp_name end
select
replace (emp_name, '=', '') as emp_name,
count (*) as countt
from employees
group by replace (emp_name, '=', '')
Edit, since you said the name can contain an =
select
case
when emp_name like '%='
then substr (emp_name, 1, length (emp_name) - 1)
else emp_name
end as emp_name,
count (1) as countt
from employees
group by
case
when emp_name like '%='
then substr (emp_name, 1, length (emp_name) - 1)
else emp_name
end
I have these codes:
select case
when exists (select 1
from table1
where id=608071)
then column1
else 0
end as abc
from table1 where id=608071;
and
select decode(count(column1), 0, column1) abc
from (select column1
from table1
where id=608071) group by column1;
and none of them returns me no column1, no 0, no error. It gives me null, i.e. nothing. No rows return. I need to get 0 when particular id does not exist. What is wrong in here?
try this!!
select case when exists (select 1 from table1 where id= '608071') then (select column1 from table1) else '0' end as abc from dual
Assuming this is your requirement: "I need to get 0 when particular id does not exist"
select
nvl(T.COLUMN1, 0) COLUMN1_OR_ZERO
from
DUAL D left join(
select
*
from
TABLE1
where
id = '608071') T on (1=1)
With this query, you only scan the table once.