Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,736 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,189 people online right now. Registration is fast and FREE... Join Now!




Counting items from a View

 
Reply to this topicStart new topic

Counting items from a View, Not returning correctly.

gymratz
post 10 Oct, 2008 - 12:57 PM
Post #1


D.I.C Head

**
Joined: 18 Oct, 2007
Posts: 65


My Contributions


I simplified the View, alone it now returns exactly what I expect it to.
For each client, for each unique Classification they have, it returns the Max YearQuarter (which is a concatenation of Year and Quarter).
Problem is, when I try to pull from that View in my main query, it totally messes stuff up.

View:
CODE
Select
    Client_ID,
    Classification,
    MAX(CAST(YEAR(End_Year)as varchar(50)) + End_Quarter) as YearQuarter
    FROM Engagements as E
WHERE Client_ID is not NULL and Classification is not NULL and CAST(YEAR(End_Year)as varchar(50)) + End_Quarter is not NULL and Classification like '%Ongoing%'
GROUP BY Client_ID, Classification
Order By YearQuarter


Main Query:
This is pulling items that is has no right pulling. It shows, for instance, an item called “Verification (Initial)” yet the view doesn’t have this at all! The numbers it is pulling also seems to be off (higher by at least one). I think I'm missing something that is causing it to pull from areas it isn't supposed to. Perhaps something in my join?
CODE
Select
    EmpMe.Status as MyStatus,
    EmpMe.Full_Name as TheName,
    E.Classification as Classification,
    Count(distinct case when OA.YearQuarter = '20073Q' then 1 else 0 end) as [3Q07],
    Count(distinct case when OA.YearQuarter = '20072Q' then 1 else 0 end) as [2Q07],
    Count(distinct case when OA.YearQuarter = '20071Q' then 1 else 0 end) as [1Q07]
From Engagements as E
    INNER JOIN Clients as C on C.ID = E.Client_ID
    INNER JOIN Rep_Client as RC on RC.Client_ID = C.ID and RC.Client_ID = E.Client_ID
    INNER JOIN Employees as EmpMe on EmpMe.ID = RC.Emp_ID
    INNER JOIN [Ongoing_A] as OA on C.ID = OA.Client_ID
WHERE  (EmpMe.Status = 'Partner' or EmpMe.Status = 'Sr. Manager' or EmpMe.Status = 'Manager' or EmpMe.Status = 'Lead Verifier') and RC.Eng_ID is Null
Group By
    EmpMe.Status,
    EmpMe.Full_Name,
    E.Classification



This post has been edited by gymratz: 10 Oct, 2008 - 02:02 PM
User is offlineProfile CardPM

Go to the top of the page

gymratz
post 10 Oct, 2008 - 03:00 PM
Post #2


D.I.C Head

**
Joined: 18 Oct, 2007
Posts: 65


My Contributions


Almost Got it!

View changed to:
CODE
Select
    Client_ID,
    Classification,
    MAX(CAST(YEAR(End_Year)as varchar(50)) + End_Quarter) as YearQuarter,
    (    SELECT MAX(Engagement_ID) as Expr1
        From Engagements as E2
        WHERE (E.Client_ID = Client_ID) and (MAX(CAST(YEAR(E.End_Year)as varchar(50)) + E.End_Quarter) = CAST(YEAR(End_Year)as varchar(50)) + End_Quarter)) as Engagement_ID
FROM Engagements as E
WHERE Client_ID is not NULL and Classification is not NULL and CAST(YEAR(End_Year)as varchar(50)) + End_Quarter is not NULL
GROUP BY Client_ID, Classification
Order By YearQuarter


Main Query changed to:
CODE
Select
    EmpMe.Status as MyStatus,
    EmpMe.Full_Name as TheName,
    E.Classification as Classification,
    (    SELECT
            count(OA.YearQuarter)
        WHERE OA.YearQuarter = '20081Q') as [1Q08],
    (    SELECT
            count(OA.YearQuarter)
        WHERE OA.YearQuarter = '20074Q') as [4Q07],
    (    SELECT
            count(OA.YearQuarter)
        WHERE OA.YearQuarter = '20073Q') as [3Q07],
    (    SELECT
            count(OA.YearQuarter)
        WHERE OA.YearQuarter = '20072Q') as [2Q07],
    (    SELECT
            count(OA.YearQuarter)
        WHERE OA.YearQuarter = '20071Q') as [1Q07]
From Engagements as E
    INNER JOIN Clients as C on C.ID = E.Client_ID
    INNER JOIN Rep_Client as RC on RC.Client_ID = C.ID and RC.Client_ID = E.Client_ID
    INNER JOIN Employees as EmpMe on EmpMe.ID = RC.Emp_ID
    INNER JOIN [Ongoing_A] as OA on E.Engagement_ID = OA.Engagement_ID
WHERE  (EmpMe.Status = 'Partner' or EmpMe.Status = 'Sr. Manager' or EmpMe.Status = 'Manager' or EmpMe.Status = 'Lead Verifier')
and RC.Eng_ID is Null
and E.Classification like '%Ongoing%'
Group By
    EmpMe.Status,
    EmpMe.Full_Name,
    E.Classification,
    OA.YearQuarter


However it's not grouping quite properly.
For instance the first five lines look like this:
CODE
Lead Verifier    Nicole Seeley    Model Exam (Ongoing)    NULL    NULL    NULL    NULL    2
Lead Verifier    Nicole Seeley    Model Exam (Ongoing)    1    NULL    NULL    NULL    NULL
Lead Verifier    Nicole Seeley    Verification (Ongoing)    NULL    NULL    NULL    1    NULL
Lead Verifier    Nicole Seeley    Verification (Ongoing)    NULL    NULL    1    NULL    NULL
Lead Verifier    Nicole Seeley    Verification (Ongoing)    NULL    1    NULL    NULL    NULL

That should be a SINGLE line for Lead Verifier - Nicole Seeley - (Don't care about classification) - 1Q08 (1) - 4Q07 (1) - 3Q07 (1) - 2Q07 (1) - 1Q07 (2)
User is offlineProfile CardPM

Go to the top of the page

gymratz
post 10 Oct, 2008 - 04:09 PM
Post #3


D.I.C Head

**
Joined: 18 Oct, 2007
Posts: 65


My Contributions


I think the problem is with my group by OA.YearQuarter; however, without it I am told Column 'Ongoing_A.YearQuarter' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 10:55AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month