Solved

t-sql: group by statement

Posted on 2012-03-20
2
263 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Backup & Restore 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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 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