Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Syntax for Duration, Max Date, Min Date

Posted on 2013-01-24
7
Medium Priority
?
784 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
[X]
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
  • 4
  • 2
7 Comments
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 600 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 1400 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

604 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