sql-添加的实际记录数

时间:2017-12-10 12:04:39

标签: sql sql-server sql-server-2012

我正在尝试基于咨询和实际生成报告,以查看有多少咨询转换为实际。

我的桌子有病人,预订类别,医生,预订。

SQL Fiddle

enter image description here

以下是业务规则

  1. 如果患者在咨询后预约,请取出所有预订
  2. 用户可以先预约咨询,并预订属于同一咨询小组的一项或多项预订。示例:他们可以进行第一次预订Counseling Dental,然后可以预订映射到该组的任何内容。一世。 e Dentail手术和牙齿修复。这些信息保存在CounselingXActualBookingCategoryMap
  3. 如果患者预订超过一个实际预订,则只考虑一个,但未取消
  4. 如果取消咨询,请不要考虑。
  5. 如果实际被取消,请不要考虑。
  6. 获得咨询预约的总数(不一定是实际预约医生)和每位医生的实际预约。任何人都可以成为咨询医生。但是报告我们只考虑做过实际的医生
  7. 这是我试过的SQL。但它取得了所有记录而不是该组的实际记录。

    SELECT  P.Name As Patient, SurgeryB.BookingId, SurgeryB.BookingDate, C.CategoryName, 
    d.name as doctor
    FROM BOOKING CounsB
    INNER JOIN BOOKING SurgeryB 
      ON CounsB.PatientId = SurgeryB.PatientId AND ISNULL(SurgeryB.IsCancelled,0)=0
    INNER JOIN CounselingXActualBookingCategoryMap MAP 
      ON MAP.CounselingCategoryId = CounsB.CategoryId 
         AND MAP.ActualBookingCategoryId = SurgeryB.CategoryId  
         AND SurgeryB.BookingDate > CounsB.BookingDate
     INNER JOIN PATIENT P ON P.PATIENTID = SurgeryB.PATIENTID
     inner join category c on c.categoryid= SurgeryB.CategoryId
     inner join doctor d on SurgeryB.doctorid = d.doctorid
     WHERE ISNULL(CounsB.IsCancelled,0)=0
     order by p.patientid
    

    问题:

    1. 我不想看到Alex为Teeth修理预订的记录,因为他已经为同一组牙科和类别Dental Surgery
    2. 做了实际操作

      我想获得咨询和实际预订。示例100进行了咨询,并且每个咨询类别只有70个实际预订。

2 个答案:

答案 0 :(得分:1)

我已添加到您的代码中,如下所示:

SELECT  P.Name As Patient, SurgeryB.BookingId, SurgeryB.BookingDate, C.CategoryName, map.GroupId, map.CounselingCategoryId, map.ActualBookingCategoryId,
d.name as doctor
into #temp
FROM BOOKING CounsB
INNER JOIN BOOKING SurgeryB 
  ON CounsB.PatientId = SurgeryB.PatientId AND ISNULL(SurgeryB.IsCancelled,0)=0
INNER JOIN CounselingXActualBookingCategoryMap MAP 
  ON MAP.CounselingCategoryId = CounsB.CategoryId 
     AND MAP.ActualBookingCategoryId = SurgeryB.CategoryId  
     AND SurgeryB.BookingDate > CounsB.BookingDate
 INNER JOIN PATIENT P ON P.PATIENTID = SurgeryB.PATIENTID
 inner join category c on c.categoryid= SurgeryB.CategoryId
 inner join doctor d on SurgeryB.doctorid = d.doctorid
 WHERE ISNULL(CounsB.IsCancelled,0)=0
 order by p.patientid

;with cte as 
(   
    select Patient, BookingId, BookingDate, CategoryName, Doctor, ROW_NUMBER() over (partition by Patient, GroupId order by BookingDate) row_num 
    from #temp t
)
select Patient, BookingId, BookingDate, CategoryName, Doctor from cte where row_num = 1

drop table #temp

HTH!

答案 1 :(得分:0)

我对您提供的查询作为示例给出了答案,在业务规则1和业务规则6中对此进行了描述。

我对你的表做了一些细微的修改,因为我不得不重新创建它们来测试查询。

create table patient
(
  id                     int,
  name                   varchar(8)
);

create table doctor
(
  id                     int,
  name                   varchar(8)
);

create table category
(
  id                     int,
  name                   varchar(20),
  counseling_or_surgery  varchar(10)
);

create table map
(
  counseling_category_id   int,
  surgery_category_id      int,
  group_id                 int
);

create table booking
(
  id                       int,
  patient_id               int,
  category_id              int,
  doctor_id                int,
  booking_date             date,
  cancelled                bit
);

以下是设置数据的插入内容:

insert into patient (id, name) values (1, 'Alex');
insert into patient (id, name) values (2, 'John');
insert into patient (id, name) values (3, 'Mark');
insert into patient (id, name) values (4, 'Stephen');

insert into doctor (id, name) values (1, 'Doctor 1');
insert into doctor (id, name) values (2, 'Doctor 2');
insert into doctor (id, name) values (3, 'Doctor 3');

insert into category (id, name, counseling_or_surgery) values (1, 'Counseling Dental', 'Counseling');
insert into category (id, name, counseling_or_surgery) values (2, 'Counseling Nose',   'Counseling');
insert into category (id, name, counseling_or_surgery) values (3, 'Dental surgery',    'Surgery');
insert into category (id, name, counseling_or_surgery) values (4, 'Teeth repair',      'Surgery');
insert into category (id, name, counseling_or_surgery) values (5, 'Nose Surgery',      'Surgery');
insert into category (id, name, counseling_or_surgery) values (6, 'Nose adjustment',   'Surgery');

insert into map (counseling_category_id, surgery_category_id, group_id) values (1, 3, 1);
insert into map (counseling_category_id, surgery_category_id, group_id) values (1, 4, 1);
insert into map (counseling_category_id, surgery_category_id, group_id) values (2, 5, 2);
insert into map (counseling_category_id, surgery_category_id, group_id) values (2, 6, 2);

insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 1, 1, 1, 1, '2017-11-20', 'false');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 2, 1, 3, 3, '2017-11-25', 'false');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 3, 1, 4, 2, '2017-11-25', 'false');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 4, 1, 2, 1, '2017-11-21', 'true');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 5, 1, 2, 2, '2017-11-26', 'false');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 6, 1, 5, 3, '2017-11-28', 'false');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 7, 3, 2, 2, '2017-11-26', 'false');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 8, 3, 6, 2, '2017-11-26', 'true');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values ( 9, 4, 2, 2, '2017-11-29', 'false');
insert into booking (id, patient_id, category_id, doctor_id, booking_date, cancelled) values (10, 4, 5, 1, '2017-11-29', 'false');

鉴于这些表,以下是业务规则1的查询:

select
    c.name          as patient,
    b.id            as booking_id,
    b.booking_date  as first_surgery_date,
    d.name          as first_surgery,
    e.name          as doctor
from
    (
        select
            r.patient_id,
            min(r.id)           as booking_id,
            t.group_id
        from
            booking r
            join
            category s
            on r.category_id = s.id
            join
            map t
            on t.surgery_category_id = r.category_id
        where
            r.cancelled = 0 and
            s.counseling_or_surgery = 'Surgery'
        group by
            r.patient_id,
            t.group_id
    ) a
    join
    booking b
    on a.booking_id = b.id
    join
    patient c
    on b.patient_id = c.id
    join 
    category d
    on b.category_id = d.id
    join
    doctor e
    on b.doctor_id = e.id;

结果是:

patient          booking_id  first_surgery_date first_surgery    doctor
---------------- ----------- ------------------ ---------------- ----------------
Stephen                   10         2017-11-29 Nose Surgery     Doctor 1
Alex                       2         2017-11-25 Dental surgery   Doctor 3
Alex                       6         2017-11-28 Nose Surgery     Doctor 3

(3 rows affected)

这里是业务规则6的查询。它也会取消取消;如果那不是你想要的,那么很容易改变。

select
    z.name                     as doctor,
    y.total_counseling_bookings,
    x.total_surgery_bookings
from
    (
        select
            doctor_id,
            count(*) as total_surgery_bookings
        from
            booking a
            join
            category b
            on a.category_id = b.id
        where
            b.counseling_or_surgery = 'Surgery'
        group by 
            doctor_id
    ) x
    full outer join
    (
        select
            doctor_id,
            count(*) as total_counseling_bookings
        from
            booking c
            join
            category d
            on c.category_id = d.id
        where
            d.counseling_or_surgery = 'Counseling'
        group by 
            doctor_id
    ) y
    on x.doctor_id = y.doctor_id
    join
    doctor z
    on x.doctor_id = z.id

以下是这个结果:

doctor   total_counseling_bookings total_surgery_bookings
-------- ------------------------- ----------------------
Doctor 1                         2                      1
Doctor 2                         3                      2
Doctor 3                      NULL                      2

(3 rows affected)