Re: SV: [firebird-support] Re: Grouping without sorting alphabetically

2015-02-17 Thread Venus Software Operations venussof...@gmail.com [firebird-support]













SV: [firebird-support] Re: Grouping without sorting alphabetically

2015-02-16 Thread Svein Erling Tysvær svein.erling.tysv...@kreftregisteret.no [firebird-support]
>Thanks Set for starting me on the right track.  Please see my finalized query 
>below which gets me to the expected result.

Good to see that you got the result you wanted, Bhavbhuti! I do have two 
comments regarding your final result:

1) Why do you use ORDER BY within the CTEs? In the outer select, yes, there 
ORDER BY is useful, but within a CTE, I think ORDER BY is mainly useful in 
combination with window functions, which aren't available until Firebird 3. 
With CTEs like yours, I'd say avoid ORDER BY within the CTE.

2) Using cteFinalOrder is of course one possible option. However, I think it 
would be simpler to read the code if you just had joined cteTestGroups and 
cteTitleOrder directly in your main select. Though I do admit that is a matter 
of preference, and there's nothing wrong in using cteFinalOrder like you do.

Set

Re: [firebird-support] Re: Grouping without sorting alphabetically

2015-02-14 Thread Venus Software Operations venussof...@gmail.com [firebird-support]













Re: [firebird-support] Re: Grouping without sorting alphabetically

2015-02-13 Thread Venus Software Operations venussof...@gmail.com [firebird-support]













RE: [firebird-support] Re: Grouping without sorting alphabetically

2015-02-13 Thread Svein Erling Tysvær svein.erling.tysv...@kreftregisteret.no [firebird-support]
Dataset:
1Haemogram Report  1Routine 
Haemogram
1Haemogram Report  5Diff. Count 
(Mature Cells)
1Haemogram Report  10   Special 
Investigations
2Haemogram Report  1Diff. Count 
(Mature Cells)
3C-Reactive Protine1
4Urine Analysis1Physical 
Examination
4Urine Analysis8Chemical 
Examination
4Urine Analysis10   Microscopic 
Examination of Centrifugalised Deposit
5Haemogram Report  1E.S.R.
 
Bhavbhutis attempt:
WITH cteTestGroups AS (SELECT MIN(A.iSrNo) AS iGroupSrNo
, B.cTitle
, MIN(C.iSrNo) AS iSubGroupSrNo
, C.cSubGroup
FROM sReqSlipTestGroup A
JOIN mTestGroups B
ON  B.iID = A.iTestGroupID
JOIN lTestGroupsTest C
ON C.iPID = B.iID
JOIN mTests D
ON D.iID = C.iTestID
WHERE A.iPID = 1
GROUP BY B.lNewPage
, A.iSrNo
, B.cTitle
, C.cSubGroup
ORDER BY 1, 2, 3, 4)

SELECT cteTG.*
FROM cteTestGroups cteTG

>My final output I want something like this:
>1    Haemogram Report      1    Routine 
>Haemogram
>1    Haemogram Report      5    Diff. Count 
>(Mature Cells)
>1    Haemogram Report      10   Special 
>Investigations
>5    Haemogram Report      1    E.S.R.
>3    C-Reactive Protine        1
>4    Urine Analysis        1    Physical 
>Examination
>4    Urine Analysis        8    Chemical 
>Examination
>4    Urine Analysis        10   Microscopic 
>Examination of Centrifugalised Deposit
>
>ie.  The Row 4 is merged with row 2 and row 9 becomes row 4
>
>The idea is that all the cTitle are clubbed together but in the order they 
>were originally selected, thus MIN(A.iSrNo).
>The second grouping is cSubGroup, together but in the order they were defined, 
>thus MIN(C.iSrNo)

I think an additional CTE may be what you want:

WITH cteTestGroups (iGroupSrNo, cTitle, iSubGroupSrNo, cSubGroup AS
(SELECT MIN(A.iSrNo), B.cTitle, MIN(C.iSrNo), C.cSubGroup
 FROM sReqSlipTestGroup A
 JOIN mTestGroups B ON B.iID = A.iTestGroupID
 JOIN lTestGroupsTest C ON C.iPID = B.iID
 JOIN mTests D ON D.iID = C.iTestID
 WHERE A.iPID = 1
 GROUP BY B.lNewPage, A.iSrNo, B.cTitle, C.cSubGroup),
 cteOrderGroups(cTitle, cOrder) AS
 (select cTitle, min(iGroupSrNo)
  from cteTestGroups
  group by 1)
        
SELECT cteTG.*
FROM cteTestGroups cteTG
JOIN cteOrderGroups cteOG on cteTG.cTitle = cteOG.cTitle
ORDER BY cteOG.cOrder, cteTG.iSubGroupSrNo

Maybe things could be simplified further, but this at least ought to work.
Set


[firebird-support] Re: Grouping without sorting alphabetically

2015-02-12 Thread venussof...@gmail.com [firebird-support]
The screen shot was stripped please bear with me I will paste the original 
query and the original output now:

WITH cteTestGroups AS (SELECT MIN(A.iSrNo) AS iGroupSrNo
, B.cTitle
, MIN(C.iSrNo) AS iSubGroupSrNo
, C.cSubGroup
FROM sReqSlipTestGroup A
JOIN mTestGroups B
ON  B.iID = A.iTestGroupID
JOIN lTestGroupsTest C
ON C.iPID = B.iID
JOIN mTests D
ON D.iID = C.iTestID
WHERE A.iPID = 1
GROUP BY B.lNewPage
, A.iSrNo
, B.cTitle
, C.cSubGroup
ORDER BY 1, 2, 3, 4)

SELECT cteTG.*
FROM cteTestGroups cteTG


1Haemogram Report  1Routine 
Haemogram 
1Haemogram Report  5Diff. Count 
(Mature Cells)
1Haemogram Report  10Special 
Investigations
2Haemogram Report  1Diff. Count 
(Mature Cells)
3C-Reactive Protine1
  
4Urine Analysis1Physical 
Examination  
4Urine Analysis8Chemical 
Examination  
4Urine Analysis10Microscopic 
Examination of Centrifugalised Deposit
5Haemogram Report  1E.S.R.  
  

Thanks
Bhavbhuti