Solved

View with Count Distinct based on Case statement

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

717 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