简体   繁体   中英

sql query to find matching pairs in mysql

Hypothetical question for the community:

Given any 2 books in a library (book 1,book 2), how many customers have had both checked out at the same time within the year?

Table

CREATE TABLE `check0` (
  `lib_card_num` int NOT NULL,
  `first_name` varchar(45) NOT NULL,
  `last_name` varchar(45) NOT NULL,
  `checkout_timestamp_utc` timestamp(6) NOT NULL,
  `due_date` date NOT NULL,
  `type` varchar(10) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `unique_id` int NOT NULL,
  PRIMARY KEY (`unique_id`),
  KEY `parent_index1` (`lib_card_num`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;

Data

INSERT INTO testlibrary.check0
(lib_card_num,FIRST_NAME,LAST_NAME,CHECKOUT_TIMESTAMP_UTC,DUE_DATE,TYPE,NAME,UNIQUE_ID)
VALUES
(1,'ROB','BLOW',DATE_ADD(current_timestamp(), INTERVAL -30 day),DATE_ADD(current_date(),INTERVAL -5 DAY),'PHYSICAL','SHEBA',100),
(1,'ROB','BLOW',DATE_ADD(current_timestamp(), INTERVAL -30 day),DATE_ADD(current_date(),INTERVAL -5 DAY),'ELECTRONIC','THUNDERPOINT',101),
(2,'JOHN','DOE',DATE_ADD(current_timestamp(), INTERVAL -15 day),DATE_ADD(current_date(),INTERVAL -1 DAY),'ELECTRONIC','THUNDERPOINT',102),
(2,'JOHN','DOE',DATE_ADD(current_timestamp(), INTERVAL -15 day),DATE_ADD(current_date(),INTERVAL -1 DAY),'PHYSICAL','SHEBA',103),
(3,'JANE','DOE',DATE_ADD(current_timestamp(), INTERVAL -45 day),DATE_ADD(current_date(),INTERVAL -20 DAY),'PHYSICAL','SHEBA',104),
(3,'JANE','DOE',DATE_ADD(current_timestamp(), INTERVAL -45 day),DATE_ADD(current_date(),INTERVAL -20 DAY),'ELECTRONIC','THUNDERPOINT',105)

Maybe try this

select count(lib_card_num) as result
from
(select distinct check0.lib_card_num from sys.check0
where check0.checkout_timestamp_utc in 
(SELECT checkout_timestamp_utc 
 FROM 
(SELECT check0.checkout_timestamp_utc, COUNT(check0.checkout_timestamp_utc) count 
 FROM sys.check0
 GROUP BY check0.checkout_timestamp_utc) sub1
WHERE sub1.count > 1)) sub2;

Given any 2 books in a library (book 1,book 2), how many customers have had both checked out at the same time within the year?

This doesn't sound very theoretical. But you can approach this using aggregation. However, your description doesn't have a column called customer or book_title , so you'll need to adapt for your data model. This is a theoretical question after all:

To get the customers with both books:

select customer
from check0 c
where book_title in ('book 1', 'book 2')
group by customer
having count(distinct book_title) = 2;

You can then count these with an additional subquery:

select count(*)
from (select customer
      from check0 c
      where book_title in ('book 1', 'book 2')
      group by customer
      having count(distinct book_title) = 2
     ) c;

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