SQL小提琴
MS SQL Server 2008 架构设置:
CREATE TABLE LVS_TKT_HIST
([customer_no] int, [perf_no] int, [perf_dt] datetime, [perf_name] varchar(18))
;
INSERT INTO LVS_TKT_HIST
([customer_no], [perf_no], [perf_dt], [perf_name])
VALUES
(1046359, 11038, '2014-05-15 20:00:00', 'Patriotic Pops #1'),
(1046359, 13950, '2014-05-15 20:00:00', 'Wine Tier 3'),
(1046359, 10927, '2014-04-25 20:00:00', 'Pops #4 Fri'),
(1046359, 10833, '2014-04-04 20:00:00', 'Evita #4'),
(1046359, 11269, '2014-03-02 19:00:00', 'Lewis & Pizzarelli')
;
查询 1:
SELECT lth.perf_no, lth.perf_name, lth.perf_dt
FROM LVS_TKT_HIST lth
INNER JOIN (
SELECT customer_no, CAST(perf_dt AS DATE) AS perf_dt
FROM LVS_TKT_HIST
GROUP BY customer_no, CAST(perf_dt AS DATE)
HAVING count(*) > 1
) dt ON dt.customer_no = lth.customer_no AND
CAST(lth.perf_dt AS DATE) = dt.perf_dt
WHERE lth.customer_no=1046359
结果:
| PERF_NO | PERF_NAME | PERF_DT |
|---------|-------------------|----------------------------|
| 11038 | Patriotic Pops #1 | May, 15 2014 20:00:00+0000 |
| 13950 | Wine Tier 3 | May, 15 2014 20:00:00+0000 |