Вы находитесь на странице: 1из 2

I need someone to develop this SQL script further please?

The following description may not be 100% perfect - but feel free to message me for
clarification or questions.

I have 2 tables, in a straighforward one to many relationship.


1 ENGAGEMENT has many EVALUATIONS.

Appendix 1 has the SQL script to create the tables


Appendix 2 has my current query - it retrieves the last evaluation of every
Engagement.

THE JOB
========
I would like you to extend the script in Appendix 2 so that the output contains 2
additional columns: CHECKUP and EVALUATIONCOUNT

1) Each row must have a calculated CHECKUP column showing the status of the
EVALUATION
2) Each row must have a calculated count of how many EVALUATIONS there are, for
that ENGAGEMENT in total

The CHECKUP column for each ENGAGEMENT, can have one of 3 possible values...
"Neglected", "In Progress", or "Completed".
For CHECKUP to be "Completed": The ENGAGEMENT's LAST report must be
REPORTTYPE="Final"
For CHECKUP to be "InProcess": The ENGAGEMENT's LAST report REPORTDATE must be less
than 2 x REPORTFREQUENCY
For CHECKUP to be "Neglected": The ENGAGEMENTs LAST ENGAGEMENT's report's
REPORTDATE must be older than 2 x REPORTFREQUENCY

So, the current output is as follows:


|----------------|------------------|--------------------|
| Name | EvaluationType | EvaluationPeriod |
|----------------|------------------|--------------------|
| Mr John Smith | Final | Mar 2017 |
| Ms Janet Smith | Draft | Jun 2018 |

And I want it extended to show


|----------------|------------------|--------------------|------------|------------
-----|
| Name | EvaluationType | EvaluationPeriod | Checkup|
EvaluationCount |
|----------------|------------------|--------------------|------------|------------
-----|
| Mr John Smith | Final | Mar 2017 | Complete | 3
|
| Ms Janet Smith | Draft | Jun 2018 | In process | 2
|
APPENDIX 1 - create the tables
====================================
BEGIN TRANSACTION;

CREATE TABLE "engagements" ("id" integer not null primary key autoincrement,
"EngagementTitle" varchar null, "EvaluationFrequency" varchar null);

INSERT INTO engagements VALUES (1, 'Contract #123445', 'Monthly');


INSERT INTO engagements VALUES (2, 'Contract #882734-4', 'Quarterly');
INSERT INTO engagements VALUES (3, 'Contract #99##92', 'Annually');

CREATE TABLE "evaluations" ("id" integer not null primary key autoincrement,
"engagement_id" integer not null, "EvaluationPeriod" varchar null, "EvaluationType"
varchar null, foreign key("engagement_id") references "engagements"("id"));

INSERT INTO evaluations VALUES (1,1,'Jan 2017', 'Draft');


INSERT INTO evaluations VALUES (2,1,'Feb 2017', 'Draft');
INSERT INTO evaluations VALUES (3,2,'Jun 2018', 'Draft');
INSERT INTO evaluations VALUES (4,2,'Jul 2018', 'Draft');
INSERT INTO evaluations VALUES (5,1,'Mar 2017', 'Final');

COMMIT;

APPENDIX 2 - current Script that needs extending


=======================================================

SELECT t.engagementTitle,
t.EvaluationFrequency,
t1.EvaluationType AS LastEvaluationType,
t1.EvaluationPeriod as LastEvaluationPeriod,
"***" AS Checkup,
"***" AS EvaluationCount
FROM engagements as t INNER JOIN
(
SELECT *, (SELECT COUNT(*) FROM evaluations tt WHERE
tt.engagement_id = t1.engagement_ID and tt.evaluationperiod>=t1.evaluationperiod)
rn
FROM evaluations t1
)
AS t1 ON t1.engagement_id = t.id and rn = 1
ORDER BY t1.EvaluationPeriod;

TESTS
======================================================
Here is how I will test the result set:
1. Each ENGAGEMENT only occurs once (i.e. 1 row for the ENGAGEMENT)
2. The report in the resultset is the last report, in terms of the Report date
3. The count of reports matches the count of reports for that engagement,
regradless of anything else.

Вам также может понравиться