May 9, 2011 at 3:18 am
Dear all,
I'm fairly new to SQL.
I have created a new view (nominal_narratives), which contains three columns (dbo.nominal_narratives.narr, dbo.nominal_narratives.nt_seqnum and dbo.nominal_narratives.nt_accode).
Within my database I also have a table (axxia01.dbo.nmnomtrn) which contains the following columns (axxia01.dbo.nmnomtrn.nt_addesc, axxia01.dbo.nmnomtrn.nt_trdesc and axxia01.dbo.nmnomtrn.nt_seqnum).
What I have been struggling to do is create a new View which needs to combine the 'narr' column in the nominal_narratives table with the 'nt_trdesc' column in the axxia01.dbo.nmnomtrn table. Both these columns are text and are linked by the 'nt_seqnum' columns in both tables.
The problem I have faced is that some transactions have a seqnum in the axxia01.dbo.nmnomtrn table but not in the nominal_narratives View. This is highlighted by the axxia01.dbo.nmnomtrn.nt_addesc column which contains Y (for yes) and N (for no) and this indicates whether or not there is additional text in dbo.nominal_narratives.narr.
All transactions will have a seqnum in axxia01.dbo.nmnomtrn.nt_seqnum and text in axxia01.dbo.nmnomtrn.nt_trdesc but many will also have a matching seqnum in dbo.nominal_narratives.nt_seqnum along with text in dbo.nominal_narratives.narr where axxia01.dbo.nmnomtrn.nt_addesc column = 'Y'.
When combining the two tables myself I have found that if the axxia01.dbo.nmnomtrn.nt_addesc column = N then the data is excluded all together when joining the axxia01.dbo.nmnomtrn.nt_trdesc and dbo.nominal_narratives.narr columns.
In very basic terms (I told you I was new to SQL!) I think I need a statement which does the following:
IF axxia01.dbo.nmnomtrn.nt_addesc = 'N' THEN just show axxia01.dbo.nmnomtrn.nt_trdesc, BUT IF axxia01.dbo.nmnomtrn.nt_addesc = 'Y' THEN show RTRIM(axxia01.dbo.nmnomtrn.nt_trdesc) + NEW LINE + dbo.nominal_narratives.narr WHERE axxia01.dbo.nmnomtrn.nt_seqnum = dbo.nominal_narratives.nt_seqnum, AS Description.
This new column can be called 'Description'.
Also, this data will be placed into Excel (and wrapped) when created. Is it possible to add a 'new line' function between axxia01.dbo.nmnomtrn.nt_trdesc and dbo.nominal_narratives.narr IF axxia01.dbo.nmnomtrn.nt_addesc = 'Y'?
When created I am hoping to have a new View which will contain nt_seqnum from axxia01.dbo.nmnomtrn and Description.
Hope this makes sense as I have started to confuse myself.
Thanking you all in advanced and please feel free to ask any questions.
May 9, 2011 at 5:09 am
good job explaining what you wanted;
try this SQL below adn see if this gets the data you were looking for:
SELECT
dbo.nominal_narratives.narr,
dbo.nominal_narratives.nt_seqnum,
dbo.nominal_narratives.nt_accode,
axxia01.dbo.nmnomtrn.nt_addesc,
axxia01.dbo.nmnomtrn.nt_trdesc,
CASE
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'N'
THEN axxia01.dbo.nmnomtrn.nt_trdesc
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'Y'
THEN RTRIM(axxia01.dbo.nmnomtrn.nt_trdesc) + CHAR(13) + CHAR(10) + dbo.nominal_narratives.narr
END AS Description
FROM dbo.nominal_narratives
LEFT OUTER JOIN axxia01.dbo.nmnomtrn
ON dbo.nominal_narratives.nt_seqnum = axxia01.dbo.nmnomtrn.nt_seqnum
Lowell
May 9, 2011 at 5:38 am
Thanks Lowell.
I'm currently getting an error message with this statement and can't place my finger on why:
"Error in SELECT clause: expression near ','.
Missing FROM clause.
Unable to parse query text."
Followed by:
"SQL Execution Error.
Executed SQL statement: SELECT
dbo.nominal_narratives.narr,
dbo.nominal_narratives.nt_seqnum,
dbo.nominal_narratives.nt_accode,
axxia01.dbo.nmnomtrn.nt_addesc,
axxia01.dbo.nmnomtrn.nt_trdesc,
CASE
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'N'
TH...
Error Source: .NetSqlClient Data Provider
Error Message: Incorrect syntax near ','."
Do you have any ideas?
Thanks again
May 9, 2011 at 6:31 am
CASE
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'N'
THEN axxia01.dbo.nmnomtrn.nt_trdesc,
Remove the comma at the end of that last line
May 9, 2011 at 6:33 am
Ninja's_RGR'us (5/9/2011)
CASEWHEN axxia01.dbo.nmnomtrn.nt_addesc = 'N'
THEN axxia01.dbo.nmnomtrn.nt_trdesc,
Remove the comma at the end of that last line
yep i created the SQL without testing it for syntax;
here it is corrected i think:
SELECT
dbo.nominal_narratives.narr,
dbo.nominal_narratives.nt_seqnum,
dbo.nominal_narratives.nt_accode,
axxia01.dbo.nmnomtrn.nt_addesc,
axxia01.dbo.nmnomtrn.nt_trdesc,
CASE
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'N'
THEN axxia01.dbo.nmnomtrn.nt_trdesc
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'Y'
THEN RTRIM(axxia01.dbo.nmnomtrn.nt_trdesc) + CHAR(13) + CHAR(10) + dbo.nominal_narratives.narr
END AS Description
FROM dbo.nominal_narratives
LEFT OUTER JOIN axxia01.dbo.nmnomtrn
ON dbo.nominal_narratives.nt_seqnum = axxia01.dbo.nmnomtrn.nt_seqnum
Lowell
May 9, 2011 at 6:38 am
Works for me now...
Lowell (5/9/2011)
Ninja's_RGR'us (5/9/2011)
CASEWHEN axxia01.dbo.nmnomtrn.nt_addesc = 'N'
THEN axxia01.dbo.nmnomtrn.nt_trdesc,
Remove the comma at the end of that last line
yep i created the SQL without testing it for syntax;
here it is corrected i think:
SELECT
dbo.nominal_narratives.narr,
dbo.nominal_narratives.nt_seqnum,
dbo.nominal_narratives.nt_accode,
axxia01.dbo.nmnomtrn.nt_addesc,
axxia01.dbo.nmnomtrn.nt_trdesc,
CASE
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'N'
THEN axxia01.dbo.nmnomtrn.nt_trdesc
WHEN axxia01.dbo.nmnomtrn.nt_addesc = 'Y'
THEN RTRIM(axxia01.dbo.nmnomtrn.nt_trdesc) + CHAR(13) + CHAR(10) + dbo.nominal_narratives.narr
END AS Description
FROM dbo.nominal_narratives
LEFT OUTER JOIN axxia01.dbo.nmnomtrn
ON dbo.nominal_narratives.nt_seqnum = axxia01.dbo.nmnomtrn.nt_seqnum
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply