I have a query as follows: Column c has a case statement for case 'P' and case 'M'.
The condition inside case 'P' is not working-> "and cs.case_id not in (SELECT case_id FROM case_status where (case_status_id = 16 and case_status_date < sysdate - 365)) AND cs.case_status_id = lk.id".
case Status for P is one to many so it's in a different table and for 'M' it's one to one so it's in the same cases table.
Also case status 'P' may or may not have record for a case.
SELECT
c.case_id,
c.id id,
lkp_alt.descr AS name,
(CASE WHEN c.case_type = 'P' THEN
(SELECT listagg(lk.descr, ', ') AS status
FROM case_status cs,
lkp_case_status lk
WHERE c.case_id = cs.case_id
and cs.case_id not in (SELECT case_id
FROM case_status
where (case_status_id = 16
and case_status_date < sysdate - 365)
)
AND cs.case_status_id = lk.id
GROUP BY c.case_id
)
ELSE (SELECT listagg(status, ', ') AS status
FROM (SELECT case_id,
lk.descr AS status
FROM lkp_hqmc_case_status lk
WHERE c.case_type = 'M'
AND lk.id = c.hamc_case_status
UNION
SELECT c.case_id,
lk.descr AS status
FROM lkp_fl_case_status lk
WHERE c.case_type = 'M'
AND lk.id = c.fl_case_status
)
GROUP BY case_id
)
END
) AS status
from cases c,
lkp_alt_show_cause_authority lkp_alt
WHERE c.ascAt = lkp_alt.id(+)
AND c.delete_date IS NULL
and not (nvl(c.hamc_case_status,0) = 8
and c.CLOSE_DATE < sysdate - 365
)
ORDER BY c.case_id desc
Case Table
id name hamc_case_status case_type
1 AAA 1 P
2 BBB 1 P
3 CCC 1 P
4 DDD 1 P
case_status
1 16 01-NOV-19 03.42.37.420000000 PM
1 5 01-NOV-19 03.42.37.420000000 PM
2 1 18-NOV-19 12.36.11.268000000 PM
2 3 18-NOV-19 12.36.11.268000000 PM
3 5 18-NOV-21 12.36.11.268000000 PM
3 16 18-NOV-21 12.36.11.268000000 PM
lkp_alt_show_cause_authority
id desc
1 TEST
2 TEST1
lkp_hqmc_case_status
id descr
1 status
2 status1..
8 Closed
lkp_fl_case_status
id descr
1 fstatus1
2 fstatus2
lkp_case_status
id Descr
1 Active
2 Intermediate
3 InActive
5 Prep
16 Closed
The output should show as follows:
Result:
id name status
2 BBB Active, InActive
3 CCC Prep, Closed
4 DDD
It shouldnt pull case with id = 1 since the case closed status 16 is in '01-NOV-19'
Output I get with this query is:
id name status
1 null
2 BBB Active, InActive
3 CCC Prep, Closed
4 DDD
It is a legacy application with 150 fields and multiple joins on reporting. I cannot change query with 'outer join'.
I have to use same format of tablea, tableb and use (+) for joins.
Thanks in advance.
Related
i have 2 tables like below:
lk_premier:
code descr
P Premier
N Non Premier
Case:
id taxPin
1 123
2 789
Status:
id voting_status premier
1 5 P
1 5 P
1 5 P
2 5 P
2 5 N
2 5 null
3 5 null
3 5 null
3 5 null
3 5 null
3 5 null
I used the below sql
select
decode(premier,
'P',
'PREMIER',
'N',
'NON PREMIER') as caseStatus,
count(*) as count
from
status s,
case c
where
c.id = s.id
and c.id = 1
and s.voting_status = 5
group by
premier
I want to join the lk_premier table, so my output looks like for id=2,
caseStatus count
PREMIER 1
NON PREMIER 1
for id = 3
caseStatus count
PREMIER 0
NON PREMIER 0
You should never do joins this way -- this way of doing joins was depreciated about 20 years ago. the join should look like this:
join status s on c.id = s.id
once you do joins the right way then how you do left joins is easier as seen below:
here you go
select
coalesce(p.desc,'unknown') as caseStatus,
count(*) as count
from case c
join status s on c.id = s.id
left join lk_premier p on p.code = c.premier
where c.id = 1 and s.voting_status = 5
group by coalesce(p.desc,'unknown')
Please help me to create query. I have table with languages like
Id Code
---------
1 EN
2 DE
3 RU
and table with translations
Id Code LanguageId Value
------------------------------------------
1 1 1 EnglishTranslation
2 1 3 RussianTranslation
3 2 1 EnglishTranslation
4 2 2 DeutschTranslation
5 3 1 EnglishTranslation
I'm trying to get this result
Id Code LanguageId Value
------------------------------------------
1 1 1 EnglishTranslation
1 1 2 NULL
2 1 3 RussianTranslation
3 2 1 EnglishTranslation
4 2 2 DeutschTranslation
4 2 3 NULL
5 3 1 EnglishTranslation
5 3 2 NULL
5 3 3 NULL
Need to get translations for all languages by Code from Translations table So far I try
select
T.id, T.Code, L.Id, T.Value
from Languages L
left join Translations T on T.LanguageId = L.Id
but I got not expected result. Could you please suggest
http://sqlfiddle.com/#!6/e9bed/1
You can use CROSS JOIN operator to construct a cartesian product of (LanguageId, Code) pairs, and left-join translation table to it:
SELECT
t.Id, y.Code, x.LanguageId, t.Value
FROM
((SELECT Id AS LanguageId FROM Languages) AS x
CROSS JOIN
(SELECT DISTINCT(Code) AS Code FROM Translations) AS y)
LEFT OUTER JOIN Translations t ON y.Code=t.Code AND x.LanguageId=t.LanguageId
ORDER BY t.Code, t.LanguageId
Note that this wouldn't produce a valid translation Id for rows missing from Translations, i.e. the result would look like this:
Id Code LanguageId Value
---------------------------------------------
1 1 1 EnglishTranslation
NULL 1 2 NULL
2 1 3 RussianTranslation
3 2 1 EnglishTranslation
4 2 2 DeutschTranslation
NULL 2 3 NULL
5 3 1 EnglishTranslation
NULL 3 2 NULL
NULL 3 3 NULL
Demo.
I resolved the issue and got the result that you expected.
Run the below query:
SELECT
(CASE
WHEN T.Id is null and lc.Code = 1 THEN 1
WHEN T.Id is null and lc.Code = 2 THEN 4
WHEN T.Id is null and lc.Code = 3 THEN 5
ELSE T.Id
END) as Id,
lc.Code, lc.Id as LanguageId, T.Value from
(SELECT x.Id, y.Code from (SELECT Id FROM Languages) x cross join (SELECT DISTINCT(Code) as Code FROM Translations) y) as lc
left outer join Translations T ON lc.Id = T.LanguageId and lc.Code = T.Code
order by Id, Code, LanguageId
You can also see the solution in the below link:
http://sqlfiddle.com/#!6/e9bed/30
Hopefully it will work as you like.
My table looks like this, let's call it Table1:
ID | value | formID
----------------------
25 Business 1001
16 John 1001
5 2/20/17 1001
30 FormXYZ 1001
25 Nursing 2345
16 Sam 2345
5 1/15/17 2345
30 FormXYZ 2345
25 Tech 4500
16 Sam 4500
5 2/1/17 4500
30 FormC 4500
The ID is the unique identifier of that field:
25 = Department
16 = Name
5 = Date
30 = Form Name, we have multiple different Forms, and I just need FormXYZ data.
FormID is a unique ID for each form submitted, the form contains 3 fields.
I have been trying to write a single query that retrieves all data looking something like this if possible:
Department | Name | Date
Business John 2/20/17
Nursing Sam 1/15/17
Here is what I have been trying, nesting and CASE didn't seem to work right for me, so I am posting here and I am right back where I started at.
SELECT value
FROM Table1
WHERE ID = '25'
UNION ALL
SELECT value
FROM Table1
WHERE ID = '16'
UNION ALL
SELECT value
FROM Table1
WHERE ID = '5'
UNION ALL
SELECT value
FROM Table1
WHERE ID = '30' and value = 'FormXYZ'
One way to transpose data in SQL is to use case statements and roll up the data using a group by:
select
formID,
max(case when ID=25 then value else null end) as Department,
max(case when ID=16 then value else null end) as Name,
max(case when ID=5 then value else null end) as Date
from Table1
group by formID
This produces:
formid Department Name Date
1001 Business John 2/20/17
2345 Nursing Sam 1/15/17
4500 Tech Sam 2/1/17
You can add a where clause as needed. This should get the data in a single scan.
select dp.value as department,
n.value as name,
dt.value as date
from
(select formID, value from table1 where id = 25) as dp
inner join (select formID, value from table1 where id = 16) as n
on dp.formID = n.formID
inner join (select formID, value from table1 where id = 5) as dt
on dp.formID = dt.formID
inner join (select formID, value from table1 where id = 30) as f
on dp.formID = f.formID
where f.value = 'FormXYZ';
OR
select
case when id = 25 then value end as department,
case when id = 5 then value end as date,
case when id = 16 then value end as name
from table1
where formId in (select formID from table1
where id = 30 and value = 'FormXYZ')
and id in (5,16,25);
My query
with with_user_earns as (
-- get father information (start)
select father.id, father.str_name, father.id_father, father.ind_father_side_type, 1 as int_user_level from tb_user father where id = #v_user_id
union all
-- get son information (stop condition)
select son.id, son.str_name, son.id_father, son.ind_father_side_type, WUE.int_user_level + 1
from tb_user as son inner join with_user_earns as WUE on son.id_father = WUE.id
where son.id_father is not null and WUE.int_user_level < #v_max_level
)
select aux.*
from (
-- show result
select with_user_earns.id id_son, with_user_earns.str_name str_son_name, with_user_earns.id_father, father.str_name str_father_name, with_user_earns.ind_father_side_type, with_user_earns.int_user_level, isnull(sum(o.int_score), 0) as int_score
from with_user_earns inner join tb_order o on o.id_user = with_user_earns.id
inner join tb_user as father on father.id = with_user_earns.id_father
where o.dt_insert between #v_cycle_begin and #v_cycle_end and o.ind_payment_status = 3
group by with_user_earns.id, with_user_earns.str_name, with_user_earns.id_father, with_user_earns.ind_father_side_type, with_user_earns.int_user_level, father.str_name
) as aux
order by aux.int_user_level, aux.id_son
The table with_user_earns contains a lot of users (mult nivel hierarchy).
Then I want to join with tb_order to get int_score 0 if the user sell nothing and if the user sell anything I want the sum of it.
I tried put left join, full outer join, ... But no one works perfect
What a need to do?
My result:
id_son int_score
1 100
2 11100
3 100
10 300
Expected result:
id_son int_score
1 100
2 11100
3 100
4 0
5 0
6 0
7 0
8 0
9 0
10 300
I have a WHERE clause in a query that needs to see whether the latest entry in a related table meets certain criteria. However, I'm not able to inject the PK of the top query directly into the clause for a number of different reasons.
Is there any way to rewrite the following query to depend on the outer alias (ie. make ALIAS.pk work)? foo has a composite primary key.
(SELECT CASE WHEN EXISTS (
SELECT * FROM (
SELECT n.val1, n.val2 FROM (
SELECT * FROM foo f
WHERE f.val0 = 100 AND f.outerid = ALIAS.pk
ORDER BY f.date DESC
) n
WHERE n.rownum = 1
) t
WHERE t.val1 = 1 AND t.val2 = 2
) THEN 1 ELSE 0 END FROM dual) = 1
Edit: Outer table (bar):
id name city
1 Bob London
2 Mike Atlanta
3 Susan Toronto
Inner table (foo):
outerid date val1 val2 val100 fk1 fk2 fk3
1 2014-11-11 1 2 100 11 523 15
1 2014-11-11 1 2 101 14 12 87
1 2014-11-10 1 2 100 17 1667 12
2 2014-11-11 1 1 100 91 12 188
The primary key for foo is a composite key over fk1..3.
So what I need is to select the latest entry from foo that corresponds to a certain user and check that it has certain characteristics.
Edit 2:
SELECT CASE WHEN ({inner query})=1 THEN 1 ELSE 0 END WHERE id = 1 should return "1" SELECT SELECT CASE WHEN ({inner query})=1 THEN 1 ELSE 0 END WHERE id = 2 should return "0".
This may give you the output you require:
SELECT b.name
FROM bar b
INNER JOIN
(SELECT DISTINCT
f.outerid
FROM
(SELECT f.outerid
, f.val1
, f.val2
, f.date
, max(f.date) OVER
(PARTITION BY f.outerid
ORDER BY f.date) max_date
FROM foo f
WHERE f.val0 = 100) f
WHERE f.date = f.max_date
AND f.val1 = 1
AND f.val2 = 2) f
ON (f.outerid = b.id)