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 |
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2011-01-02 : 13:57:01
|
| HI Perhaps you see some reason here why I am not getting data back for all customers?GO/****** Object: StoredProcedure [dbo].[GetTransactions] Script Date: 01/02/2011 13:54:36 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- drop procedure GetTransactionsALTER PROCEDURE [dbo].[GetTransactions] @CustomerId INT = NULL AS BEGIN SET NOCOUNT ON; SELECT LTRIM(RTRIM(ISNULL(d.DepartmentName, ''))) AS DepartmentName ,d.DepartmentId AS DepartmentId ,ISNULL(t.Cost, 0) AS Expense ,ISNULL(t.Quantity , 0)AS Quantity ,LTRIM(RTRIM(ISNULL(c.CustomerName, ''))) AS CUSTNAME ,LTRIM(RTRIM(ISNULL(i.ItemName, ''))) AS Description ,t.DateCreated AS DateCreated ,LTRIM(RTRIM(ISNULL(ct.CategoryLongName, ''))) AS CategoryName ,LTRIM(RTRIM(ISNULL(ct.CategoryName, ''))) AS USCATVAL --,'00000' AS Budget ,LTRIM(RTRIM(ISNULL(t.InvoiceNumber,''))) as InvoiceNumFROM Transactions tLEFT JOIN Departments d ON t.DepartmentId = d.DepartmentIdLEFT JOIN Customers c ON t.CustomerId = c.CustomerIdLEFT JOIN Categories ct ON t.CategoryId = ct.CategoryIdLEFT JOIN Items i ON t.ItemId = i.ItemIdWHERE DateCreated >= DATEADD(YYYY, - 2, GETDATE()) AND DateCreated <= GETDATE()AND t.CustomerId = @CustomerIdORDER BY DateCreated DESC END |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-01-02 : 17:22:35
|
| will oly give data for customers with transactions (and only for @CustomerId).(I assume that datecreated is on the transaction table.Maybe you wantFROM Customers cLEFT JOIN Transactions tON t.CustomerId = c.CustomerIdAND DateCreated >= DATEADD(YYYY, - 2, GETDATE()) AND DateCreated <= GETDATE()LEFT JOIN Departments d ON t.DepartmentId = d.DepartmentIdLEFT JOIN Categories ct ON t.CategoryId = ct.CategoryIdLEFT JOIN Items i ON t.ItemId = i.ItemIdWHERE c.CustomerId = @CustomerIdORDER BY DateCreated DESC ==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-01-02 : 18:26:45
|
| "LEFT JOIN Customers c ON t.CustomerId = c.CustomerId"Not the answer to your question, but can you really have transactions for customers that don't exist? or, perhaps more likely, transactions for NO customer?If I was doing the design I'd be a bit anxious about Transactions that have no matching Category too ... |
 |
|
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2011-01-02 : 18:53:28
|
Thank you both, I didn't write this, it was modified a while back so I can pass this along to the DBA.quote: Originally posted by Kristen "LEFT JOIN Customers c ON t.CustomerId = c.CustomerId"Not the answer to your question, but can you really have transactions for customers that don't exist? or, perhaps more likely, transactions for NO customer?If I was doing the design I'd be a bit anxious about Transactions that have no matching Category too ...
|
 |
|
|
|
|
|
|
|