Solved

View with Count Distinct based on Case statement

Posted on 2007-11-18
4
4,010 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

733 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