Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Syntax for Duration, Max Date, Min Date

Posted on 2013-01-24
7
768 Views
Last Modified: 2013-01-28
I am wrestling with a query and I need some expert help. I am trying to create four new fields in my select statement:

  ,MIN(SERVICE_DELIVERIES.SERVICE_PERIOD_START) AS SVCSTARTDT
  ,MAX(SERVICE_DELIVERIES.[SERVICE_PERIOD_START2]) AS SVCENDDR
  ,DATEDIFF(d, SVCSTARTDT), SVCENDDT)
  ,AVG(DATEDIFF)
 
FROM
tablename.fieldname INNER JOIN ON tablename.fieldname
WHERE
blah-blah-blah
GROUP BY
??????

I can't get Reporting Services to accept any of several variations on this theme.
What I need is to identify the earliest/latest service date (so I can explore a cohort of people getting certain services) and I need a new data element that gives me DURATION as an expression or function of Max - Min.  In a perfect world, I would be able to get for the DURATION data element a specific format of YY.YYY or YY.MM where the expression rounds up to the next month if even one day in the max month is a service date between or on Min/Max. (I'm looking for how many years and months a person got services in a program).  

Taking that one step further, I'd like to be able to get AVG_DURATION so I can use this in evaluating program tenure from date A to date B.

SO - to boil it all down, I need syntax/convention in T-SQL for Report Builder 3.0:

1.  MIN(SERVICE_DELIVERIES.SERVICE_PERIOD_START_DT) AS SVCSTARTDT
2.  MAX(SERVICE_DELIVERIES.SERVICE_PERIOD_START_DT) AS SVCENDDT
--Report Builder does NOT like using the same data element twice and I can't figure out how to differentiate the two--
3.  DURATION  (Difference between min and max date as YY,MM or YY.YYY)
4.  AVG_DURATION (self-evident)

Finally, I am using these in a query that has grouping applied, so which if any, and how, should these elements be represented in the GROUP BY clause?

Assistance VERY gratefully appreciated.
0
Comment
Question by:gberkeley
  • 4
  • 2
7 Comments
 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 150 total points
ID: 38816236
"SO - to boil it all down, I need syntax/convention in T-SQL for Report Builder 3.0:"

Why not to use SQL Stored procedure in the report instead? That's what I would do to simplify things (and SQL syntax) as you have way more posibilities like that and just use the SQL SP in your SSRS report
0
 

Author Comment

by:gberkeley
ID: 38816272
Hi lcohan,
Thanks for the fast response!
Perhaps a SPROC would be the better way to go. Unfortunately, that's WAY beyond my skill set.  If that's the right answer, then I'll need to wait for one of our DBAs to free up.  

Much appreciated,
j
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 350 total points
ID: 38825899
in the GROUP BY, you don't put the alias: AS SVCSTARTDT

anyhow, wouldn't this work
DURATION(  MIN( SERVICE_DELIVERIES.SERVICE_PERIOD_START_DT ) 
   , MAX(SERVICE_DELIVERIES.SERVICE_PERIOD_START_DT)  
  ) 

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:gberkeley
ID: 38827411
I think we're getting closer.... it seems it won't recognize Duration and it wants the DATEDIFF function. I just haven't been able to get the syntax straight that will let me use Min/max in same query.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38827449
the date difference in seconds, maybe?

DATEDIFF( second, MIN ( ... ) , MAX ( ... ) )
0
 

Author Comment

by:gberkeley
ID: 38827506
Report Builder still not cooperating.  I'm going to close out the question and refer it to the DBAs on my end; maybe they can figure it out.

Thanks, lcohan and angelIII!
0
 

Author Closing Comment

by:gberkeley
ID: 38827515
I really appreciated the fast responses and multiple attempts to assist.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by Valentino Vranken. Introduction: The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source.  The data…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question