[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

View with Count Distinct based on Case statement

Posted on 2007-11-18
4
Medium Priority
?
4,018 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

Accepted Solution

by:
Scott Pletcher earned 1200 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

656 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