Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
velkymx
Starting Member
5 Posts |
Posted - 2005-12-13 : 16:29:56
|
| OK - basically I am trying to build a view that will count all of the records in a specific category. While it is returning lists with counts >0 it is just skipping the 0.dbo.Patient is what we are counting, and ReportStatus are the categories.HELP!<code>SELECT COUNT(dbo.Patient.PatientID) AS LeadCount, MONTH(dbo.Patient.CreateDate) AS CreateMonth, YEAR(dbo.Patient.CreateDate) AS CreateYear,dbo.SourceCodes.Name AS CategoryFROM dbo.Patient INNER JOINdbo.Status ON dbo.Patient.StatusID = dbo.Status.StatusID RIGHT OUTER JOINdbo.SourceCodes ON dbo.Patient.SourceID = dbo.SourceCodes.SourceIDWHERE (dbo.Status.ReportStatusID IN (4, 5, 6, 7))GROUP BY MONTH(dbo.Patient.CreateDate), YEAR(dbo.Patient.CreateDate), dbo.SourceCodes.Name</code>So it is returning:TV 19 3/2005-- 3 4/2005Internet 27 4/2005Other 6 4/2005Print 14 4/2005Radio 1 4/2005Referral 68 4/2005TV 32 4/2005Instead of-- 0 3/2005Internet 0 3/2005Other 0 3/2005Print 0 3/2005Radio 0 3/2005Referral 0 3/2005TV 19 3/2005-- 3 4/2005Internet 27 4/2005Other 6 4/2005Print 14 4/2005Radio 1 4/2005Referral 68 4/2005TV 32 4/2005 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-13 : 16:50:55
|
Put your sourceCodes table first then LEFT JOIN to the other tables:create table #data (rowid int identity(1,1), lookupid int)create table #lookup (lookupid int, lookupDesc varchar(20))goinsert #lookupselect 1, 'item 1' unionselect 2, 'item 2' unionselect 3, 'item 3'insert #data (lookupid)select 1 union allselect 1 union allselect 2 union allselect 2 union allselect 2goselect l.lookupDesc ,count(d.lookupid) [count]from #lookup lleft join #data d on d.lookupid = l.lookupidgroup by l.lookupDescorder by l.lookupDescgodrop table #lookupdrop table #data Be One with the OptimizerTG |
 |
|
|
velkymx
Starting Member
5 Posts |
Posted - 2005-12-13 : 16:57:33
|
| OK so I updated to this... still doesnt workSELECT dbo.SouceCategories.SourceCategory AS Category, COUNT(*) AS LeadCount, MONTH(dbo.tmpGLSource.CreateDate) AS CreateMonth, YEAR(dbo.tmpGLSource.CreateDate) AS CreateYearFROM dbo.SouceCategories LEFT OUTER JOIN dbo.tmpGLSource ON dbo.tmpGLSource.Name = dbo.SouceCategories.SourceCategoryGROUP BY dbo.SouceCategories.SourceCategory, MONTH(dbo.tmpGLSource.CreateDate), YEAR(dbo.tmpGLSource.CreateDate), dbo.tmpGLSource.ReportStatusIDHAVING (dbo.tmpGLSource.ReportStatusID IN (4, 5, 6, 7)) |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-13 : 17:10:43
|
Does this work? If not, post the DDL and inserts for some sample, and exptected results and we'll get it working for you.http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxSELECT dbo.SouceCategories.SourceCategory AS Category ,COUNT(dbo.tmpGLSource.Name) AS LeadCount ,MONTH(dbo.tmpGLSource.CreateDate) AS CreateMonth ,YEAR(dbo.tmpGLSource.CreateDate) AS CreateYearFROM dbo.SouceCategories LEFT JOIN dbo.tmpGLSource ON dbo.tmpGLSource.Name = dbo.SouceCategories.SourceCategory and dbo.tmpGLSource.ReportStatusID IN (4, 5, 6, 7)GROUP BY dbo.SouceCategories.SourceCategory ,MONTH(dbo.tmpGLSource.CreateDate) ,YEAR(dbo.tmpGLSource.CreateDate) ,dbo.tmpGLSource.ReportStatusID Be One with the OptimizerTG |
 |
|
|
velkymx
Starting Member
5 Posts |
Posted - 2005-12-13 : 17:14:00
|
| Naw - now I am getting duplicate SourceCategories. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
|
|
velkymx
Starting Member
5 Posts |
Posted - 2005-12-13 : 17:27:54
|
| All of the requested infor is in the first post. |
 |
|
|
|
|
|
|
|