A good example of this would be
SELECT * FROM A WHERE M LEFT OUTER JOIN N, P LEFT OUTER JOIN Q..
1.
select M.MASTER_REF, case B.Type when 450 Then 'RED'
when 420 Then 'RVL'
END Note_Type, B.CODE, B.NOTE_TEXT, (E.REFNO_PFIX || ''|| E.REFNO_SERL)AS Event_Ref, B.CREATED_AT, B.ACTIVE
from Note B, MASTER M, BASEEVENT E, TIDataItem N
where B.KEY97=N.KEY97 and N.MASTER_KEY=M.KEY97 and E.KEY97 = B.NOTE_EVENT
and M.EXEMPLAR in ('8806648499869051681','8806648499869023154','8806648499869054292','8806648499869006425')
and M.STATUS in ('LIV','EXP')
and B.Code not in ('Migration')
order by M.Master_Ref Asc, B.CREATED_AT desc
2.
select M.MASTER_REF, case B.Type when 450 Then 'RED'
when 420 Then 'RVL'
END Note_Type, B.CODE, B.NOTE_TEXT, B.Note_Event AS Event_Ref, B.CREATED_AT, B.ACTIVE
from Note B, MASTER M, TIDataItem N
where B.KEY97=N.KEY97 and N.MASTER_KEY=M.KEY97 and B.NOTE_EVENT is null
and M.EXEMPLAR in ('8806648499869051681','8806648499869023154','8806648499869054292','8806648499869006425')
and M.STATUS in ('LIV','EXP')
and B.Code not in ('Migration')
order by M.Master_Ref Asc, B.CREATED_AT desc
Use UNION ALL to add similar result sets together. Column order and data types must match between each set. The ORDER BY should be at the last SELECT only and can reference only column alias.
select
M.MASTER_REF,
case B.Type
when 450 Then 'RED'
when 420 Then 'RVL' END Note_Type,
B.CODE,
B.NOTE_TEXT,
(E.REFNO_PFIX || ''|| E.REFNO_SERL) AS Event_Ref,
B.CREATED_AT,
B.ACTIVE
from
Note B,
MASTER M,
BASEEVENT E,
TIDataItem N
where
B.KEY97=N.KEY97 and
N.MASTER_KEY=M.KEY97 and
E.KEY97 = B.NOTE_EVENT and
M.EXEMPLAR in ('8806648499869051681','8806648499869023154','8806648499869054292','8806648499869006425') and
M.STATUS in ('LIV','EXP') and
B.Code not in ('Migration')
UNION ALL
select
M.MASTER_REF,
case B.Type
when 450 Then 'RED'
when 420 Then 'RVL' END Note_Type,
B.CODE,
B.NOTE_TEXT,
TO_CHAR(B.Note_Event) AS Event_Ref,
B.CREATED_AT,
B.ACTIVE
from
Note B,
MASTER M,
TIDataItem N
where
B.KEY97=N.KEY97 and
N.MASTER_KEY=M.KEY97 and
B.NOTE_EVENT is null and
M.EXEMPLAR in ('8806648499869051681','8806648499869023154','8806648499869054292','8806648499869006425') and
M.STATUS in ('LIV','EXP') and
B.Code not in ('Migration')
order by
Master_Ref Asc,
CREATED_AT desc
I strongly suggest you avoid using old join syntax as it makes the code less readable (use explicit INNER or LEFT JOIN with the proper ON clause):
Change:
from
Note B,
MASTER M,
TIDataItem N
where
B.KEY97=N.KEY97 and
N.MASTER_KEY=M.KEY97
For:
FROM
Note B
INNER JOIN TIDataItem N ON B.KEY97 = N.KEY97
INNER JOIN MASTER M ON N.MASTER_KEY = M.KEY97
I found this example of nested group by in linq. How would it look if I want to add one more group by to it?
var queryNestedGroups =
from student in students
group student by student.Year into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.LastName)
group newGroup2 by newGroup1.Key;
First, let rename some variables of the sample query:
var queryNestedGroups =
from e in source
group e by e.Key1 into g1
from e1 in
(from e in g1
group e by e.Key2)
group e1 by g1.Key;
Now you can add another nested group level using the same pattern:
var queryNestedGroups =
from e in source
group e by e.Key1 into g1
from e1 in
(from e in g1
group e by e.Key2 into g2
from e2 in
(from e in g2
group e by e.Key3)
group e2 by g2.Key)
group e1 by g1.Key;
How can I do this conversion? Is it possible with some simple LINQ query?
If V is some other type not involved in the query, you can use the let keyword to create an instance and then group on it...
from x in Y
let v = new V(x.Whatever)
group v by v.Whatever into vGroup
select vGroup
Assuming that V inherits from U and you want to cast each U to a V :
IEnumerable<IGrouping<string, U>> groupingsOfU =
from u in listOfU
group u by u.Foo;
IEnumerable<IGrouping<string, V>> groupingsOfV =
from g in groupingsOfU
from u in g
group (V)u by g.Key;
SELECT
a,
last_note_user,
c,
d,
iso_src
FROM
X
CROSS JOIN Y
CROSS JOIN Z
LEFT OUTER JOIN W
ON W.last_note_user = Z.userid
AND W.user_ten = Y.iso_src
The above ANSI code fetch me 107 records,When I giving the same query without ANSI code it is fetching 875 records.The non ANSI query is below:
SELECT
a,
last_note_user,
c,
d,
iso_src
FROM
X,
Y,
Z,
W
WHERE
W.last_note_user = Z.userid(+)
AND W.user_ten = Y.iso_src(+)
why there is difference in the two query with ANSI and without ANSI standards??
By answering the above query please help me out!!!
Your old-style query has the (+) symbols on the wrong side of the predicate. It should be:
SELECT
a,
last_note_user,
c,
d,
iso_src
FROM
X,
Y,
Z,
W
WHERE
W.last_note_user (+) = Z.userid
AND W.user_ten (+) = Y.iso_src
But I wouldn't use the old-style syntax any more really.
OUTER JOINS with old ANSI-syntax are ambiguous, so who knows what the query optimizer understands with this.
If the first SQL is producing the right rows, forget about the ANSI version and move on.