简体   繁体   中英

Select all rows that have the same value in two columns, while simultaneously having different values in another column?

I have a table where caretaker visits are recorded. The table contains caretakerCode , patientID , visitDate and visitAddress .

I need to show all information from rows where the same caretaker went to more than one patient on the same day. Here is an example.

caretakerCode      patientID     visitDate      visitAddress
---------------------------------------------------------------
 John Q               13         2022/01/13     27 Hamilton Rd
 John Q               13         2022/01/14     27 Hamilton Rd
 John Q               15         2022/01/14     101 Congress St
 Melanie B            22         2022/01/15     3 Redroad Ct

In the example, the output would be

caretakerCode      patientID     visitDate      visitAddress
---------------------------------------------------------------
   John Q             13         2022/01/14     27 Hamilton Rd
   John Q             15         2022/01/15     101 Congress St

I have tried joins but I'm not sure how to make it take more than 2 columns. Any help would be greatly appreciated!

Thank you.

I think your example is wrong, since you wanted to achieve to show the visits on the same day. Your example shows 2 different days: 2022/01/14 and 2022/01/15.

Assume the table name is dbo.tVisits.

Create test table

USE tempdb;

CREATE TABLE dbo.tVisits
(
    caretakerCode nvarchar(50)  NOT NULL,
    patientID     int           NOT NULL,
    visitDate     date          NOT NULL,
    visitAddress  nvarchar(512) NOT NULL
);

Insert test data

INSERT INTO dbo.tVisits
(
    caretakerCode,
    patientID,
    visitDate,
    visitAddress
)
VALUES
( N'John Q',    13, '20220113', N'27 Hamilton Rd'  ),
( N'John Q',    13, '20220114', N'27 Hamilton Rd'  ),
( N'John Q',    15, '20220114', N'101 Congress St' ),
( N'Melanie B', 22, '20220115', N'3 Redroad Ct'    );

Execute query

;WITH CTE AS
(
    SELECT 
        V.caretakerCode,
        V.visitDate
    FROM dbo.tVisits V
    GROUP BY 
        V.caretakerCode,
        V.visitDate
    HAVING COUNT(*) > 1
)
SELECT 
    V.caretakerCode,
    V.patientID,
    V.visitDate,
    V.visitAddress
FROM dbo.tVisits V
INNER JOIN CTE ON 
    CTE.caretakerCode = V.caretakerCode
    AND CTE.visitDate = V.visitDate
ORDER BY 1,3,2;

Returned rows

caretakerCode patientID visitDate visitAddress
John Q 13 2022-01-14 27 Hamilton Rd
John Q 15 2022-01-14 101 Congress St

Cleanup

DROP TABLE IF EXISTS dbo.tVisits;

Explanation: In the Common Table Expression CTE you select rows where the same caretaker has multiple rows at the same visit date. In the 2nd step you use this information on the original table to filter the rows by inner join.

If this post answers your question, please do not forget to mark it as an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM