如何获取数据列表,特定日期仅从每个日期获取 6 条记录而不是更多

Posted

技术标签:

【中文标题】如何获取数据列表,特定日期仅从每个日期获取 6 条记录而不是更多【英文标题】:How to get list of data, particular date wise only 6 record get from each date not more 【发布时间】:2020-08-06 22:57:12 【问题描述】:

我在 java 中使用带有安全性的 spring boot。我尝试在 startDate 和 endDate 之间合并所有事件列表并按 startDate 排序,但从每个日期开始,只有 6 条记录不会更多。假设在本月日期明智 -n 否。事件的存在。但我希望每个日期只记录六条记录,不需要更多记录。 在这个 eventsListTwoDates 对象中,我得到了所有事件的列表。

问题 - 获取事件列表为空

我已经尝试过以下代码

    public GenericResponse getAllList() 
        
            List<HashMap<String, Object>> eventsListTwoDates = eventsDao.findAllListOfEventsWithoutEventTypeIdWithOnlyTwoDate(startDate, endDate);
            int count = 0;
            List<HashMap<String, Object>> newList = new ArrayList<>();
            for (int i = 0; i < eventsListTwoDates.size(); i++) 
                if (map.contains(eventsListTwoDates.get(i).containsValue("startDate"))) 
                    if (count < 6) 
                        newList.add(i, map.get(i));
                        count++;
                     else 
                        count = 0;
                    
                
            
            return APIResponseBuilder.build(true, newList, commonMessages.findAllTrue);
        

2.查询

Query("SELECT new map(e.id as id, e.eventImg as eventImg, e.multipleDays as multipleDays, e.startDate as startDate, e.endDate as endDate, e.startTime as startTime, e.endTime as endTime, e.eventStatus.status as status, e.paid as paid, e.fee as fee, e.passportTickets as passportTickets, e.location as location, e.city.name as city, e.myFavourite as myFavourite, e.remainingTickets as remainingTickets, e.eventName as eventName, e.passport as passport, e.typesOfEvents.type as eventType, e.eventPassportOptions.event_passport_option_id as passportOption, e.promoCode as promoCode, e.subTitle as subTitle, e.expectedAttendance as expectedAttendance)" +
            " from Events e WHERE e.startDate BETWEEN :startDate AND :endDate AND e.endDate BETWEEN :startDate AND :endDate ORDER BY e.startDate")
    List<HashMap<String, Object>> findAllListOfEventsWithoutEventTypeIdWithOnlyTwoDate(String startDate, String endDate);

3.响应


  "success": true,
  "message": "Record's find successfully.",
  "data": [
    
      "passportTickets": 20,
      "endDate": "2020/04/17",
      "city": "Bombuflat",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Product Launches",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "sales",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 3,
      "endTime": "15:30:45",
      "startDate": "2020/04/15",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Incentive Trips",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 5,
      "endTime": "15:30:45",
      "startDate": "2020/04/15",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Incentive Trips",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 6,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Dancing",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 7,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Dancing",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 8,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Party",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 9,
      "endTime": "15:30:45",
      "startDate": "2020/04/16",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/19",
      "city": "Bombuflat",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Musician1",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "sales",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 1,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Historical/Remembrance",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 17,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Party",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 10,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Party",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 11,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Charity",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 12,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Charity",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 13,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Non-profit",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 14,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Non-profit",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 15,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/26",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Rotary - Service Club",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 16,
      "endTime": "15:30:45",
      "startDate": "2020/04/17",
      "status": "Draft"
    ,
    
      "passportTickets": 20,
      "endDate": "2020/04/30",
      "city": "Rapar",
      "fee": 50,
      "remainingTickets": 40,
      "expectedAttendance": "34",
      "passportOption": 1,
      "eventType": "Product Launches",
      "multipleDays": false,
      "myFavourite": false,
      "passport": true,
      "subTitle": " Celebration of World Wildlife Day",
      "paid": true,
      "eventName": "Wild animal day",
      "eventImg": "https://newsd.in/wp-content/uploads/2019/03/Arua-to-Host-National-Wildlife-Day-Celebrations.png",
      "startTime": "20:04:45",
      "location": "Rajpath club",
      "promoCode": "NetPh123",
      "id": 4,
      "endTime": "15:30:45",
      "startDate": "2020/04/20",
      "status": "Draft"
    
  ]

【问题讨论】:

为什么不用findTop6 替换findAll?这个问题和spring-security有什么关系? findAll 是获取所有不会自定义每个日期的 6 条记录的事件列表 findTop6 是从所有 eventList 中获取前 6 名记录 我现在完全搞不清你想要什么了。您能否提供表结构、一些示例数据以及基于该数据的预期结果? 好的,我会分享回复 【参考方案1】:

您可以使用这样的sql语句,一次选择所需的数据。

select * from (
    select e.*,
        count(*) over (partition by eventDate order by id) cnt 
    from Events e where eventDate between :startDate and :endDate
) e where e.cnt <= 6

当然,您必须调整列名。您可能必须在 eventDate 上使用 truncformat 才能将不同的时间戳转换为同一日期,具体取决于数据的存储方式。

【讨论】:

我有 startDate 和 endDate 两列。在此列中仅存储日期,不存储时间戳 那么我想您需要澄清“每个日期记录六个”应该是什么意思,因为它只谈论一个日期。我认为该语句的基本结构仍然有效。 我希望每个日期有 6 条记录,如果有超过 6 条记录,那么我想将其总数作为计数 此查询仅获取记录,即每个日期为 =6 的每个记录日期

以上是关于如何获取数据列表,特定日期仅从每个日期获取 6 条记录而不是更多的主要内容,如果未能解决你的问题,请参考以下文章

如何仅从日期时间列中获取日期? [复制]

仅从 sql server 中的日期数据类型中获取日期 [重复]

当用户从日期选择器中选择日期时,如何在下拉列表中获取特定的星期几?

仅从 CoreDate 或所有内容中获取所需的数据?

如何获取特定月份的每个星期四或一周中的其他日期的日期?

如何在 MySQL 中的两个日期之间获取特定日期