Solved

View with Count Distinct based on Case statement

Posted on 2007-11-18
4
4,009 Views
Last Modified: 2010-04-21
Hi Experts,
I have the view below that is working OK, ie it executes fine and gives me a result. I want to add another column that is the count of distinct jobs that meet one of the values calculated in a case statement.
I am not sure of the thought process to achieve this.

Specifically, I want to repeat the "NoOfJobs" (Count(distinct(jo1.jobnumber)), but want to only count jobs that have a "QuotedAmount" (Sum((case when Jo.SubLedger= 'Quoting' then Jo.Amount else 0 end))  of greater than 0.
Basically how many jobs have a quoted amount.

As you can see my Grouping is by Period. My function converts the starttime into a month.

I am not sure as to the best way to achieve this. Perhaps a temp table that has 2 columns, Number of Jobs with a quoted amount and Period.  Or declaring a variable and somehow getting the Number of Jobs quoted by period?

Any way forward would be appreciated.

Thanks

Tony
CREATE VIEW dbo.U_QuoteAnalysisSummary (NoOfJobs,StartTime,QuotesProspect, QuotesInProgress, QuotesLost, QuotesCompleted, CostsCompleted, 
InvoiceCompleted,  GrossProfit,  JobPrice, QuotedAmount) 
AS
SELECT Count(distinct(jo1.jobnumber)),
dbo.nf_DateToPeriod (Jo1.StartTime),
Sum((case when jo1.closed=0 and Jo.SubLedger= 'Quoting' and jo1.status = 'QS' then Jo.Amount else 0 end)),
Sum((case when jo1.closed=0 and Jo.SubLedger= 'Quoting' and jo1.status not in ('XQ','QR','QS','QT','P') then Jo.Amount else 0 end)),
Sum((case when Jo.SubLedger= 'Quoting' and jo1.status = 'XQ' then Jo.Amount else 0 end)),
Sum((case when jo1.closed=1 and Jo.SubLedger= 'Quoting' and jo1.status <> 'XQ' then Jo.Amount else 0 end)),
Sum((case when jo1.closed=1 and Jo.SubLedger= 'Costing' and jo1.status <> 'XQ' then Jo.Amount else 0 end)),
Sum((case when jo1.closed=1 and Jo.SubLedger= 'Invoice' and jo1.status <> 'XQ' then -Jo.Amount else 0 end)),
Sum((case when jo1.closed=1 and (Jo.SubLedger= 'Invoice' or Jo.SubLedger= 'Costing') and jo1.status <> 'XQ' then -Jo.Amount else 0 end)),
--Added Gross Profit below 21 August 2007 Tony Knowling
--Sum((case when Jo.SubLedger= 'Invoice' and jo1.status <> 'XQ'  and jo1.closed = 1 then -Jo.Amount else 0 end))-Sum((case when Jo.SubLedger= 'Costing' and jo1.status <> 'XQ' and jo1.closed = 1 then Jo.Cost else 0 end))),
Max(Jo1.U_JobPrice),
Sum((case when Jo.SubLedger= 'Quoting' then Jo.Amount else 0 end)) 
FROM JobBalances Jo INNER JOIN Jobs Jo1 
ON Jo.JobID = Jo1.JobID INNER JOIN Debtors De 
ON Jo1.DebtorID = De.DebtorID 
WHERE Jo.SubLedger <> 'INV GST' And De.DebtorIndex <> 'zAIE' And Jo1.Status Not In ('QWT','XQA')  
GROUP BY dbo.nf_DateToPeriod (Jo1.StartTime)

Open in new window

0
Comment
Question by:Tony303
  • 2
4 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20309484
SELECT Count(distinct(jo1.jobnumber)),
Sum(case when Jo.SubLedger= 'Quoting' then 1 else 0 end) as QuotedJobsCount,
..etc..
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 300 total points
ID: 20316683
SELECT ...,
    COUNT(DISTINCT CASE WHEN Jo.SubLedger= 'Quoting' THEN jo1.jobnumber ELSE NULL END) AS DistinctJobsWithQuotedAmt
...

Note that DISTINCT is not a traditional function in SQL Server: you don't need the parentheses around its argument.
0
 
LVL 12

Author Closing Comment

by:Tony303
ID: 31409846
Hi Scott, Thanks for that, your answer is providing an result that is very close to what I was expecting. 47 jobs vs 46 expected. I shall check my workings, I suspect my date selection is not quite correct as the underlying data has date and time, so those ones that fall on the last day of the month may not be included. Thanks again.
0
 
LVL 12

Author Comment

by:Tony303
ID: 20317020
Imitchie, the result your line gave was 119 as compared to Scotts which gave a result of 47 (I was expecting 46). I shall investigate further the date inclusions, but Scotts is the way to go FYI.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

790 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