Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

t-sql: group by statement

Posted on 2012-03-20
2
Medium Priority
?
274 Views
Last Modified: 2012-03-20
Techies--
 This statement ALMOST works.  It works for conditions where the the max(BusinessDate) is the same for all the Facilities -- however, there may be a condition where the max(BusinessDate) for FacilityId  1 = '2012-03-20' , but for FacilityId 2 = '2012-02-28'.  How do I pick up the max(BusinessDate) for each facility?

select x.FacilityId,
       x.BusinessDate as StartOfPeriodDate,
       x.Value as ValueofActiveEmpl,
       y.BusinessDate as EndOfPeriodDate,
       y.Value as ValueofActiveEmpl
  from aggregated.DailyReportCategoryDataByFacility x
    inner join aggregated.DailyReportCategoryDataByFacility y on x.FacilityId = y.FacilityId
 where x.reportcategoryid = 239
   and x.BusinessDate = '2011-12-21'
   and y.reportcategoryid = 239
   and y.BusinessDate =
     (select max(z.BusinessDate)
        from aggregated.DailyReportCategoryDataByFacility z
         where z.reportcategoryid = 239
      )
   order by x.FacilityId

Open in new window

0
Comment
Question by:Paula DiTallo
2 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 2000 total points
ID: 37743239
select x.FacilityId,
       x.BusinessDate as StartOfPeriodDate,
       x.Value as ValueofActiveEmpl,
       y.BusinessDate as EndOfPeriodDate,
       y.Value as ValueofActiveEmpl
  from aggregated.DailyReportCategoryDataByFacility x
    inner join aggregated.DailyReportCategoryDataByFacility y on x.FacilityId = y.FacilityId
    inner join (select z.FacitlityId, max(z.BusinessDate) MaxBusinessDate
        from aggregated.DailyReportCategoryDataByFacility z
         where z.reportcategoryid = 239
         group by z.FacitlityId
      ) T on x.FacitlityId = T.FacitlityId and y.BusinessDate = T.MaxBusinessDate
 where x.reportcategoryid = 239
   and x.BusinessDate = '2011-12-21'
   and y.reportcategoryid = 239
   order by x.FacilityId
                                  

Open in new window

0
 

Author Closing Comment

by:Paula DiTallo
ID: 37743641
Awesome!:-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

971 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