May 28, 2014 at 6:51 am
Hi,
I would like to create a dataset from two tables, similar to
tableA.name, tableA.job, tableB.salary, tableB.startDate
tableA has an id column Aid and table b has an id column Bid.
I'd like to be able to join the tables so that if there is not a match between Aid and Bid then name and job detail is displayed but salary and startDate have a default message eg 'No Data' and if there is a match then all four columns have data in them.
eg
name job salary startdate
------ ---- ------ ----------
bob dd 99.0 1/2/2004 (has matching id)
tim ee ND ND (no matching id)
thanks
May 28, 2014 at 7:03 am
Check BOL (the SQL Server help system) for the ISNULL() function, and have a look at the related functions COALESCE() and NULLIF() too.
Note that your output columns can only be of one datatype - you can't mix and match, so if 'ND' is your replacement for NULL values, you will have to CAST non-null values to a character datatype.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
May 28, 2014 at 7:52 am
can you use those functions in the FROM section?
This is the working vers, not my sample from above and includes a ref table as well and there's a bit more too it.
SELECT
b.TransDate, b.TransType, b.SortCode, b.AccNum, b.TransDesc,
(ISNULL(b.Debit, 0) + ISNULL (b.Credit, 0)) as Amount,
ISNULL(r.OrthoID, 999) as OrthoID
ISNULL(t.PatientID, 0) as PatientID
FROM
EPSBankTransactions b
LEFT JOIN EPSBankRef r on SUBSTRING(b.TransDesc, 0, 26) = r.BankID
LEFT JOIN EPSTransactions t on r.OrthoID = t.PatientID
so when I DON'T include the sql relating to EPSTransactions t table the result row count is correct (278) and any null values in the r.OrthoID column display 999. Cool. But when I try and join the EPSTransactions table it only returns the rows where the r.OrthoID matches the t.PatientID (11)
What I'd like is to have (278) rows and where there isn't a match, t.PatientID has 0 in it.
thanks,
May 28, 2014 at 8:01 am
mattech06 (5/28/2014)
can you use those functions in the FROM section?This is the working vers, not my sample from above and includes a ref table as well and there's a bit more too it.
SELECT
b.TransDate, b.TransType, b.SortCode, b.AccNum, b.TransDesc,
(ISNULL(b.Debit, 0) + ISNULL (b.Credit, 0)) as Amount,
ISNULL(r.OrthoID, 999) as OrthoID
ISNULL(t.PatientID, 0) as PatientID
FROM
EPSBankTransactions b
LEFT JOIN EPSBankRef r on SUBSTRING(b.TransDesc, 0, 26) = r.BankID
LEFT JOIN EPSTransactions t on r.OrthoID = t.PatientID
so when I DON'T include the sql relating to EPSTransactions t table the result row count is correct (278) and any null values in the r.OrthoID column display 999. Cool. But when I try and join the EPSTransactions table it only returns the rows where the r.OrthoID matches the t.PatientID (11)
What I'd like is to have (278) rows and where there isn't a match, t.PatientID has 0 in it.
thanks,
I suspect you didn't post the entire query here. Do you also have a where clause on this query? Does it have any predicates referencing t?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 28, 2014 at 8:09 am
Hi, yes!
SELECT
b.TransDate, b.TransType, b.SortCode, b.AccNum, b.TransDesc,
(ISNULL(b.Debit, 0) + ISNULL (b.Credit, 0)) as Amount,
ISNULL(r.OrthoID, 999) as OrthoID
ISNULL(t.PatientID, 0) as PatientID
FROM
EPSBankTransactions b
LEFT JOIN EPSBankRef r on SUBSTRING(b.TransDesc, 0, 26) = r.BankID
LEFT JOIN EPSTransactions t on r.OrthoID = t.PatientID
WHERE
t.LedgerType in (26,30)
AND t.TransactionDate >= '2014-04-01 00:00.00' AND t.TransactionDate <= '2014-04-30 23:00.00'
May 28, 2014 at 8:14 am
Have you tried the following?
Change the left join here
LEFT JOIN EPSTransactions t on r.OrthoID = t.PatientID
to this
RIGHT JOIN EPSTransactions t on r.OrthoID = t.PatientID
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
May 28, 2014 at 8:17 am
mattech06 (5/28/2014)
Hi, yes!
SELECT
b.TransDate, b.TransType, b.SortCode, b.AccNum, b.TransDesc,
(ISNULL(b.Debit, 0) + ISNULL (b.Credit, 0)) as Amount,
ISNULL(r.OrthoID, 999) as OrthoID
ISNULL(t.PatientID, 0) as PatientID
FROM
EPSBankTransactions b
LEFT JOIN EPSBankRef r on SUBSTRING(b.TransDesc, 0, 26) = r.BankID
LEFT JOIN EPSTransactions t on r.OrthoID = t.PatientID
WHERE
t.LedgerType in (26,30)
AND t.TransactionDate >= '2014-04-01 00:00.00' AND t.TransactionDate <= '2014-04-30 23:00.00'
Much as I suspected. This effectively turns your left join into an inner join because it will only return those rows where there is a match. π
Moving those conditions to the join should work. Try this and let us know if this is what you were looking for.
SELECT
b.TransDate, b.TransType, b.SortCode, b.AccNum, b.TransDesc,
(ISNULL(b.Debit, 0) + ISNULL (b.Credit, 0)) as Amount,
ISNULL(r.OrthoID, 999) as OrthoID,
ISNULL(t.PatientID, 0) as PatientID
FROM
EPSBankTransactions b
LEFT JOIN EPSBankRef r on SUBSTRING(b.TransDesc, 0, 26) = r.BankID
LEFT JOIN EPSTransactions t on r.OrthoID = t.PatientID
AND t.LedgerType in (26,30)
AND t.TransactionDate >= '2014-04-01 00:00.00' AND t.TransactionDate <= '2014-04-30 23:00.00'
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 28, 2014 at 8:42 am
I'm not sure if it will work the same way. If r.BankID will always be 26 characters long this should be equivalent and SARGable. If not, there's a risk for wrong information returned.
SELECT
b.TransDate, b.TransType, b.SortCode, b.AccNum, b.TransDesc,
(ISNULL(b.Debit, 0) + ISNULL (b.Credit, 0)) as Amount,
ISNULL(r.OrthoID, 999) as OrthoID,
ISNULL(t.PatientID, 0) as PatientID
FROM
EPSBankTransactions b
LEFT JOIN EPSBankRef r on r.BankID LIKE b.TransDesc
LEFT JOIN EPSTransactions t on r.OrthoID = t.PatientID
AND t.LedgerType in (26,30)
AND t.TransactionDate >= '2014-04-01 00:00.00' AND t.TransactionDate <= '2014-04-30 23:00.00'
May 30, 2014 at 3:56 am
Thanks Sean, that did the trick...give yourself a 1000 points!
May 30, 2014 at 7:07 am
mattech06 (5/30/2014)
Thanks Sean, that did the trick...give yourself a 1000 points!
Glad that worked for you and thanks for letting us know.
(the points don't really help, I tried to sell them on ebay but nobody was interested. ;-))
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 30, 2014 at 7:34 am
Well, at least it is working.
I find it odd that nobody was interested in Sean's points on ebay. Seems very uneducated or crass of people - sheesh.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
May 30, 2014 at 7:36 am
SQLRNNR (5/30/2014)
Well, at least it is working.I find it odd that nobody was interested in Sean's points on ebay. Seems very uneducated or crass of people - sheesh.
I know!!! I am shocked. The same place where people spent thousands of dollars to buy a stale grilled cheese sandwich because the burn marks sort of look like a certain religious person...
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 30, 2014 at 7:53 am
Sean Lange (5/30/2014)
SQLRNNR (5/30/2014)
Well, at least it is working.I find it odd that nobody was interested in Sean's points on ebay. Seems very uneducated or crass of people - sheesh.
I know!!! I am shocked. The same place where people spent thousands of dollars to buy a stale grilled cheese sandwich because the burn marks sort of look like a certain religious person...
:w00t::hehe::-D
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply