Ask the Expert

Showing one line per patient per day with GROUP BY, part 2

Continued from Part 1...

    Requires Free Membership to View

Creating and Populating the Tables

--  ***********************
--  **  p r o v i d e r  **
--  ***********************

DROP TABLE    provider;

CREATE TABLE  provider
(
    provider_id         VARCHAR2 (10)  PRIMARY KEY,
    provider_specialty  NUMBER (8)     UNIQUE
);

INSERT INTO provider (provider_id, provider_specialty)
              VALUES ('M',         1);
INSERT INTO provider (provider_id, provider_specialty)
              VALUES ('R',         3);
INSERT INTO provider (provider_id, provider_specialty)
              VALUES ('Y',         2);


--  *********************
--  **  s e r v i c e  **
--  *********************

DROP TABLE    service;

CREATE TABLE  service
(
    patient_id    NUMBER (8),
    service_date  DATE,
    provider_id   VARCHAR2 (10)
);

INSERT INTO service (patient_id, service_date, provider_id)
     VALUES         (1           '1-Jan-2003', 'M');
INSERT INTO service (patient_id, service_date, provider_id)
     VALUES         (1,          '1-Jan-2003', 'Y');
INSERT INTO service (patient_id, service_date, provider_id)
     VALUES         (2,          '1-Jan-2003', 'R');
INSERT INTO service (patient_id, service_date, provider_id)
     VALUES         (2,          '1-Jan-2003', 'M');
INSERT INTO service (patient_id, service_date, provider_id)
     VALUES         (3,          '1-Jan-2003', 'Y');

Initial Query

COLUMN provider_id FORMAT A11
COLUMN service_date FORMAT A12

SELECT    patient_id,
          TO_CHAR (service_date, 'MM/DD/YYYY')
              AS service_date,
          provider_id,
          provider_specialty
FROM      service  s    JOIN
          provider p    USING (provider_id)
ORDER BY  service_date,
          patient_id,
          provider_specialty;

This was first published in September 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: