Solved

View with Count Distinct based on Case statement

Posted on 2007-11-18
4
4,004 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:
ScottPletcher 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 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

23 Experts available now in Live!

Get 1:1 Help Now