如何按日期选择数据并仅一次显示每个日期?

时间:2019-04-24 19:53:14

标签: c# sql-server

我使用ASP.NET C#构建了约会系统,我想从SQL-SERVER中选择数据并打印报表约会并按日期和日期显示约会。 我想选择约会数据并根据日期和日期显示数据,如以下示例

Date           visitor name        subject     time   
25-04-2019      Sami               meeting1     10:00 
                Rami               meeting2     12:00
                Ali                meeting3     13:00 
26-04-2019      Hani               meeting4     10:30
                Ahmad              meeting5     12:00
27-04-2019      John               meeting6     13:00 
                Dani               meeting7     15:00 

如何在select语句SQL-SERVER或ASP.NET的表示层中做到这一点?

这是我的选择语句:

SELECT apptdate,
  visitor_name,
  [subject],
  [time],     
FROM [Appointments] 
inner join days on days.day_id = Appointments.day_id 
inner join appointment_place on appointment_place.appt_place_id =Appointments.appt_place_id
inner join work_place on work_place.work_place_id = Appointments.work_place_id

1 个答案:

答案 0 :(得分:0)

如果您对使用SQL而不是在构建报表的任何地方都感到无所适从,则可能最简单的方法是使用行号。从每天开始,为每行分配一个从1开始的数字。然后,有条件地选择日期列:如果行号为1,则为日期,否则为NULL:

SELECT IIF([RN]=1, apptDate, NULL) [apptDate],
    visitor_name,
    subject,
    time
FROM
(
    SELECT apptdate,
    visitor_name,
    [subject],
    [time],
    ROW_NUMBER() OVER (PARTITION BY [apptDate] ORDER BY [Time]) [RN]

    FROM [Appointments] 
    inner join days on days.day_id = Appointments.day_id 
    inner join appointment_place on appointment_place.appt_place_id =Appointments.appt_place_id
    inner join work_place on work_place.work_place_id = Appointments.work_place_id
) appts
ORDER BY [apptDate], [Time]