# Suggestion Required in Name Query - Multiple Date Range

**URL:** <https://forum.inductiveautomation.com/t/suggestion-required-in-name-query-multiple-date-range/83831>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [January 5, 2024, 9:13am UTC](https://forum.inductiveautomation.com/t/suggestion-required-in-name-query-multiple-date-range/83831 "2024-01-05T09:13:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudhanthiralakshmi\_M](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Sudhanthiralakshmi\_M](https://forum.inductiveautomation.com/u/Sudhanthiralakshmi_M)\
**Post date:** [January 5, 2024, 9:13am UTC](https://forum.inductiveautomation.com/t/suggestion-required-in-name-query-multiple-date-range/83831/1 "2024-01-05T09:13:13Z")

</div>

```auto
DECLARE @StartDate DATE = :dd

DECLARE @StartTime1 TIME = '06:15:00';
DECLARE @EndTime1 TIME = '14:46:00';

DECLARE @StartTime2 TIME = '14:45:00';
DECLARE @EndTime2 TIME = '23:16:00';

DECLARE @StartTime3 TIME = '23:15:00';
DECLARE @EndTime3 TIME = '06:16:00';

SELECT (MAX(CONVERT(FLOAT, KWH)) - MIN(CONVERT(FLOAT, KWH))) AS Diff
FROM Main
WHERE BG = :bg
    AND Plant = :pl
    AND Line = :ll
    AND Machine = :ma
    AND KWH !='0'
    AND (
        (Date BETWEEN DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', @StartTime1), CAST(@StartDate AS DATETIME)) AND DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', @EndTime1), CAST(@StartDate AS DATETIME)))
        OR
        (Date BETWEEN DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', @StartTime2), CAST(@StartDate AS DATETIME)) AND DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', @EndTime2), CAST(@StartDate AS DATETIME)))
        OR
        (Date BETWEEN DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', @StartTime3), CAST(@StartDate AS DATETIME)) AND DATEADD(SECOND, DATEDIFF(SECOND, '00:00:00', @EndTime3), CAST(DATEADD(DAY, 1, @StartDate) AS DATETIME)))
    );

```

With this query I can able to fetch data for single date with the declared time ranges. What do I need to add if I want to fetch data for multiple dates I need to include the date range condition in the query.. If it is not possible to add within the query.. kindly give suggestion to handle this in property binding to add one more date parameter in the query

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 5, 2024, 10:08am UTC](https://forum.inductiveautomation.com/t/suggestion-required-in-name-query-multiple-date-range/83831/2 "2024-01-05T10:08:48Z")

</div>

It's not very clear what you're asking, but it seems like you want to parameterize your query ?

If that's the case, I suggest you read this:  
[https://docs.inductiveautomation.com/display/DOC81/Named+Query+Parameters](https://docs.inductiveautomation.com/display/DOC81/Named+Query+Parameters)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 5, 2024, 10:30am UTC](https://forum.inductiveautomation.com/t/suggestion-required-in-name-query-multiple-date-range/83831/3 "2024-01-05T10:30:55Z")

</div>

> [@Sudhanthiralakshmi\_M](#):
>
> ... if I want to fetch data for multiple dates ...

Your question is unclear. Do you mean a date range [e.g., 5th to 9th] or a list of dates [e.g., 5th, 11th, 14th, 15th, ...]?

It looks like you still have dates and times strored as strings in your data table. Again, this is going to slow down performance and what you are trying to do in this question will make it worse. You really need to fix this as suggested in the answers to your previous questions on this query.  
[Issue in data fetching from DB using Ignition Named query - #2 by Transistor](https://forum.inductiveautomation.com/t/issue-in-data-fetching-from-db-using-ignition-named-query/83781/2)

---

<div class="post-metadata">

**Author:** ![Kfoldesi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kfoldesi/32/11327_2.png) [@Kfoldesi](https://forum.inductiveautomation.com/u/Kfoldesi)\
**Post date:** [January 5, 2024, 3:16pm UTC](https://forum.inductiveautomation.com/t/suggestion-required-in-name-query-multiple-date-range/83831/4 "2024-01-05T15:16:34Z")

</div>

I think your looking for 'Group By'. Include a Date column in your select statement and 0 out the time. to group by the day. or create a column that renders to a shift name/number. and group by that. Here is something I wrote to do something similar.

```auto
/*Cross Tab from RunTime table by the day*/
SELECT sum(BoilerRT) as BoilerRT,
  sum(DrumRT) as DrumRT,
  sum(FluidRT) as FluidRT, /* add up values taken for each day */
  format(datetimefromparts(
  datepart(year,TStamp),
  datepart(month,TStamp),
  datepart(day,TStamp),0,0,0,0),'MM/dd/yy') as TStamp	/* put in normal date format 0 out time*/
FROM TblDataLog
WHERE (BoilerRT + DrumRT + FluidRT) > 0 /* Not empty weekend days */
group by datetimefromparts( /* Group Each day, could be month */
  datepart(year,TStamp),
  datepart(month,TStamp),
  datepart(day,TStamp),0,0,0,0)

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 5, 2024, 3:30pm UTC](https://forum.inductiveautomation.com/t/suggestion-required-in-name-query-multiple-date-range/83831/5 "2024-01-05T15:30:49Z")

</div>

You really should change the design of your table to use actual Date and Time types.

That aside why are you declaring variables? Just use named query parameters as you already have for BG, Plant, Line, and Machine? Why would the dates be any different?
