| Author |
Message |
Davey69
Newbie
Joined: 29 Nov 2013
Location: United Kingdom
Online Status: Offline
Posts: 4
|

Topic: SQL Selection Query Posted: 17 Dec 2013 at 1:58am |
|
Hi
I am hoping you can help me with a problem I have with Crystal.
I need to collate the 100 most recent tests by each tester between two dates. Once the most recent orders list equals 100 I need the selection process to stop regardless of any other tests the tester may have done in the time period. Also if the tester has not completed at least 100 tests in the time period then the results for that tester need to be discarded.
I have managed a work around for this but what I really need to know is if there is a SQL sequence which will do the selection process I need leaving me with the selected data on which I can run further processes.
I did think this may be possible using a loop in SQL but do not know where to start with this.
I hope this makes sense but if not I will answer any questions.
Any help would be gratefully appreciated.
Cheers
Daveyt
|
|
Cheers Beers
|
IP Logged |
|
|
|
Sastry
Moderator
Joined: 16 Jul 2012
Online Status: Offline
Posts: 537
|

Posted: 17 Dec 2013 at 2:20am |
|
hi
In Crystal there is an option called Add Command, you can write your own SQL here.
I am giving a sample SQL here this is very easy to create :
Select tester,count(tests) as TotalTests from Table where Tdate>={startdate} and Edate<={enddate} Group by tester having count(tests) >=100
In the Add command itself, create two date parameters like startdate and Enddate and use them in where clause to filter between dates.
|
|
Thanks,
Sastry
|
IP Logged |
|
Davey69
Newbie
Joined: 29 Nov 2013
Location: United Kingdom
Online Status: Offline
Posts: 4
|

Posted: 17 Dec 2013 at 11:17pm |
|
Hi Sastry
Thanks for the quick response it was very helpful.
Using your response I have completed the following code:
SELECT TESTER.STAFF_NO, count(TESTER.STAFF_NO) AS TOTALTESTS
FROM Db1.TESTER TESTER INNER JOIN Db1.PERIOD PERIOD
ON TESTER.DATEOFTEST_ID=PERIOD.DATEOFTEST_ID
WHERE PERIOD.DATEOFTEST>={ts '2013-01-01 00:00:00'} AND PERIOD.DATEOFTEST<{ts '2014-01-01 00:00:00'}
GROUP BY TESTER.STAFF_NO
HAVING count(TESTER.STAFF_NO)>=100
This code produces a list of all testers who have completed over 100 tests but does not allow me to Select any other fields to run comparisons on. Any time I try to add additional fields into the code as part of the Select command I get the following error message:
---------------------------
Crystal Reports
---------------------------
Failed to retrieve data from the database.
Details: HY000:[Oracle][ODBC][Ora]ORA-00979: not a GROUP BY expression
[Database Vendor Code: 979 ]
---------------------------
OK
---------------------------
Also I only need the 100 most recent tests information is there a way to specify this?
Cheers in advance.
|
|
Cheers Beers
|
IP Logged |
|
Sastry
Moderator
Joined: 16 Jul 2012
Online Status: Offline
Posts: 537
|

Posted: 18 Dec 2013 at 12:42am |
|
Hi Davey,
The error is just because whatever fields you are adding to select list all fields should be added to group by clause also.
Ex:
Select a,b,c,d,e,f,sum(xx) as h from table where... Group by a,b,c,d,e,f Having sum(xx) > 100
To get recent transactions, you may have to sort records in ascending order then pick.
Add the following statement to SQL
Order by PERIOD.DATEOFTEST Asc
|
|
Thanks,
Sastry
|
IP Logged |
|
Davey69
Newbie
Joined: 29 Nov 2013
Location: United Kingdom
Online Status: Offline
Posts: 4
|

Posted: 18 Dec 2013 at 2:16am |
|
Hi Sastry
Thanks again sorry to be monopolising your time it is really appreciated.
I see where you are going with this and think I can understand it but
(and you just knew there was going to be a but )
when I Select additional fields and then add them to the group by function I lose all the data I need to investigate.
The SQL below seems to work but I get zero records found:
SELECT PERIOD.DATEOFTEST, TESTER.TEST_RESULT, TESTER.STAFF_NO, count(TESTER.STAFF_NO) AS TOTALTESTS FROM Db1.TESTER TESTER
INNER JOIN Db1.PERIOD PERIOD ON TESTER.DATEOFTEST_ID=PERIOD.DATEOFTEST_ID
WHERE PERIOD.DATEOFTEST>={ts '2013-01-01 00:00:00'} AND PERIOD.DATEOFTEST<{ts '2014-01-01 00:00:00'}
GROUP BY PERIOD.DATEOFTEST, TESTER.TEST_RESULT, TESTER.STAFF_NO, count(TESTER.STAFF_NO)
HAVING count(TESTER.STAFF_NO)>=100
ORDER BY TESTER.STAFF_NO DESC
Any idea’s?
|
|
Cheers Beers
|
IP Logged |
|
lockwelle
Moderator
Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
|

Posted: 18 Dec 2013 at 4:56am |
|
I would use a hybrid of solutions.
I would the original solution that Sastry gave to get the list of testers. Then I would link that table to the actual tables in your database by the tester, again filter the tests/results by your date parameters and then take the top 100 records for the report.
Basically, you are using the Command that Sastry suggested as a big filtering parameter.
HTH
|
IP Logged |
|
|
|