Solved

t-sql: group by statement

Posted on 2012-03-20
2
261 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:ditallop
2 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 500 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:ditallop
ID: 37743641
Awesome!:-)
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now