LEFT JOIN question - Pervasive SQL - combining multiple records from one table into a single column - pervasive-sql

I am having some trouble with getting data into a column from a combination of two tables.
I'm using pervasive SQL and don't have access to pivot tables. I did a left join and tried union but am not getting the results I want.
I have a primary table - table A - That for a given contract has 10 summary 'colors'.
I am using a select statement and a case clause to string the 10 unique colors together with commas to properly determine the number of commas I am checking for values in those color fields.
This works well - I get a single row with all of the colors listed (10 fields into 1)
Table B contains records for the same contract and can have almost up to 2000 colors (detail colors).
If table B has colors then Table A doesn't.
Is there a way to select all of the colors in Table B into a single row and column and show all of the other column data in Table A?
What I want :
Contract +---------+ PHONENO +----------+ Color +------+------+
TEST 555-555-5555 BLUE,RED,GREEN,YELLOW
Instead of what I'm currently getting:
Contract+---------+ PHONENO +----------+ Color +------+------+
TEST 555-555-5555 BLUE
TEST 555-555-5555 RED
TEST 555-555-5555 GREEN
TEST 555-555-5555 YELLOW
SELECT TableA.ContractNo AS Contract,
TableA.PhoneNo,
CASE
WHEN TableA.Color10 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7 + ',' + TableA.Color8 + ',' + TableA.Color9 + ',' + TableA.Color10)
WHEN TableA.Color9 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7 + ',' + TableA.Color8 + ',' + TableA.Color9)
WHEN TableA.Color8 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7 + ',' + TableA.Color8)
WHEN TableA.Color7 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6 + ',' + TableA.Color7)
WHEN TableA.Color6 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5 + ',' + TableA.Color6)
WHEN TableA.Color5 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4 + ',' + TableA.Color5)
WHEN TableA.Color4 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3 + ',' + TableA.Color4)
WHEN TableA.Color3 <> '' then (TableA.Color1 + ',' + TableA.Color2 + ',' + TableA.Color3)
WHEN TableA.Color2 <> '' then (TableA.Color1 + ',' + TableA.Color2)
WHEN TableA.Color1 <> '' then (TableA.Color1)
WHEN TableB.Color <> '' then TableB.Color
ELSE 'n/a'
end as Color
FROM TableA
LEFT JOIN TableB ON (TableA.ContractNo = TableB.ContractNo)
ORDER BY TableA.ContractNo
Can anyone help out with this?

Related

STRING_AGG Issue

I was working with SQL Server 2017 and I used the following query,
select
'select s.' + string_agg (c.source_column + ', t.' + c.target_column, ', ') +
' from ' + t.source_table + ' s' +
' join ' + t.target_table + ' t' +
' on ' + string_agg('t.' + c.target_column + ' = s.' + c.source_column, ' and ') +
';' as query
from configuration_tables t
join configuration_columns c on c.id_configuration_tables = t.id_configuration_tables
group by t.source_table, t.target_table
order by t.source_table, t.target_table;
The query is used to get the list of columns in a string for each table name.
I have to move to 2016 due to some issues, I modified the query as below,
select
distinct 'select ' + STUFF((select ',' + 's.[' + c1.source_column + '],t.[' + c1.target_column + ']'
from recon_configuration_columns c1 where c1.id_recon_configuration_table = c.id_recon_configuration_table FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
+ ' from ' + t.source_table + ' s' +
' join ' + t.target_table + ' t' +
' on ' + STUFF((select ' and ' + 's.[' + c1.source_column + '] = t.[' + c1.target_column + '] ' from recon_configuration_columns c1
where c1.id_recon_configuration_table = c.id_recon_configuration_table FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,5,'')
from
recon_configuration_tables t
join recon_configuration_columns c on c.id_recon_configuration_table = t.id_recon_configuration_table
I would like to know whether the query I changed is a standard one, also it wont create any problems in long run and always both queries should return the same results.
Thanks for your support

Eliminating multiple delimiters in single column in oracle

My table has single column info
Info
+aname + + + + + + +ano+ + + + + + + + + +agender+ + + + + + + + + + + + +arace
I should get output like
aname+ano+agender+arace
I have to eliminate multiple delimiters and replace with single +
I tried with regexp_replace and trim and working as below
select trim(REGEXP_REPLACE('+aname + + + + + + +
+ano + + + + + + + +
+agender+ + + + + + + + + + +
+arace', '\ + + ', '+'),'+') from dual;
i get output as
aname+++++++ano+++++++agender++++++++++arace
This regexp does the trick: '\++'
select REGEXP_REPLACE('+aname++++++ano+++++++++agender++++++++++++arace', '\++', '+') from dual;
To get rid of the leading + (like in your example), use ltrim (or trim to remove trailing + too).
select ltrim(REGEXP_REPLACE('+aname++++++ano+++++++++agender++++++++++++arace', '\++', '+'),'+') from dual;

using sql - Is not null in a select statement

I can't seem to figure out how to use the opposite of isnull or ifnull statements in sql. I need to say if a.Error1 is not null -- then print the ' - ' and the + CHAR(13)+CHAR(10). Basically There should be no dash or no new line break if the a.Error1 comes back null. So print the information if the field isn't null.
select a. ....
' - ' + a.Error1 + CHAR(13)+CHAR(10) +
' - ' + a.Error2 + CHAR(13)+CHAR(10) +
' - ' + a.Error3 + CHAR(13)+CHAR(10) +
' - ' + a.Error4 + CHAR(13)+CHAR(10) +
' - ' + a.Error5 + CHAR(13)+CHAR(10) +
' - ' + a.Error6 as 'error_message'
...
from table1 a
For example if for a given record error1, 2 and 5 returned output I would like the output to be as follows:
- Error1: There was a ...
- Error2: ....
- Error5: The data was ...
If no errors existed for that row it should simply be an empty/null field.
You can use CASE:
SELECT a. ....
(CASE WHEN a.Error1 IS NOT NULL
THEN ' - ' + a.Error1 + CHAR(13)+CHAR(10)
ELSE ''
END) +
(CASE WHEN a.Error2 IS NOT NULL
THEN ' - ' + a.Error2 + CHAR(13)+CHAR(10)
ELSE ''
END) +
(CASE WHEN a.Error3 IS NOT NULL
THEN ' - ' + a.Error3 + CHAR(13)+CHAR(10)
ELSE ''
END) +
...etc
Yes! i know i'm like 5 years too late but i too enountered this problem.
It's weird how it doesn't exist some kind of !ISNULL() but whatever.
Try this for a cleaner code:
select a. ....
IIF(a.Error1 IS NOT NULL, ' - ' + a.Error1 + CHAR(13)+CHAR(10) , '') as Error1,
IIF(a.Error1 IS NOT NULL, ' - ' + a.Error1 + CHAR(13)+CHAR(10) , '') as Error2
from table1 a
Learn more about IIF() function : SQL Server IIF Function
The COALESCE function does what you want here. The result of COALESCE is the first NOT NULL value it is passed. Below we use '', which is distinct from NULL so that the outer + is always applied to NOT NULL strings.
e.g.
select a. ....
COALESCE( ' - ' + a.Error1 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error2 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error3 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error4 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error5 + CHAR(13)+CHAR(10), '' ) +
COALESCE( ' - ' + a.Error6 , '' ) as 'error_message'
...
from table1 a
SELECT (CASE WHEN a.Error1 IS NOT NULL
THEN ' - ' + a.Error1 + CHAR(13)+CHAR(10) +
ELSE a.Error1
END) +
(CASE WHEN a.Error2 IS NOT NULL
THEN ' - ' + a.Error2 + CHAR(13)+CHAR(10) +
ELSE a.Error2
END) +
.....etc

SQL Server 2012 ORDER BY works differently?

I have used the following code in SQL Server 2005 for years and recently upgraded to SQL Server 2012 and it has seemingly broken the ORDER BY clause. The code is supposed to display like so:
A
A1
B
B1
B2
B3
C
But is grouping all the lines of the same type together. Any ideas why?
DECLARE #PeriodStart DATETIME
DECLARE #PeriodEnd DATETIME
SELECT #PeriodEnd = Getdate(),
#PeriodStart = Dateadd(hour, -96, Getdate());
WITH outpq
AS (SELECT 1 AS grpOrd,
Cast(NULL AS VARCHAR(255)) AS posInGrp,
'A' AS Ord,
casenumberkey,
'A|' + clientskey + '|'
+ Cast(brtnumber AS VARCHAR(11)) + '|'
+ Isnull(Replace(CONVERT(CHAR(10), coverdate, 101), '/', ''), ''
)
+ '|'
+ Isnull(Replace(CONVERT(CHAR(10), coverdate, 101), '/', ''), ''
)
+ '|' + Isnull(parcelnumber, '') + '|'
+ Isnull(assessedbeg, '') + '|'
+ Isnull(assesseddim, '') + '|'
+ Isnull(abbrlegal, '') + '|'
+ Isnull(waterfrom, '') + '|'
+ Isnull(waterto, '') + '|'
+ Isnull(Cast(wateropen AS VARCHAR(50)), '')
+ '|' + Isnull(taxfrom, '') + '|' + Isnull(taxto, '')
+ '|'
+ Isnull(Cast(taxopen AS VARCHAR(50)), '') AS Extract
FROM newcitycollection.dbo.propertyinformation
WHERE datefinished BETWEEN #PeriodStart AND #PeriodEnd
AND clientkey = 2
UNION ALL
SELECT 1 AS grpOrd,
NULL AS posInGrp,
'A1',
A.casenumberkey,
'A1|' + '|' + '|' + B.liennumber + '|'
+ Isnull(Cast(B.lienamt AS VARCHAR(50)), '')
+ '|' + Isnull(Replace(liendate, '/', ''), '')
+ '|' + Isnull(B.lienreason, '') AS Extract
FROM newcitycollection.dbo.propertyinformation A
JOIN newcitycollection.dbo.muniliens B
ON B.casenumberkey = A.casenumberkey
WHERE A.datefinished BETWEEN #PeriodStart AND #PeriodEnd
AND clientkey = 2
UNION ALL
SELECT 2 AS grpOrd,
Cast(C.interestskey AS VARCHAR(11)) AS posInGrp,
'B',
A.casenumberkey,
'B|' + '|' + Isnull(C.first, '') + '|'
+ Isnull(C.middle, '') + '|' + Isnull(C.last, '')
+ '|' + Isnull(C.alias, '') + '|'
+ Isnull(C.comname, '') + '|'
+ Isnull(C.docrel, '') + '|'
+ Cast(C.interestskey AS VARCHAR(11)) AS Extract
FROM newcitycollection.dbo.propertyinformation A
JOIN newcitycollection.dbo.interests C
ON C.casenumberkey = A.casenumberkey
WHERE A.datefinished BETWEEN #PeriodStart AND #PeriodEnd
AND clientkey = 2
UNION ALL
SELECT 2 AS grpOrd,
Cast(C.interestskey AS VARCHAR(11)) AS posInGrp,
'B1',
A.casenumberkey,
'B1|' + Isnull(fulladd, '') + '|'
+ Isnull(D.city, '') + '|' + Isnull(D.state, '')
+ '|' + Isnull(D.zip, '') AS Extract
FROM newcitycollection.dbo.propertyinformation A
JOIN newcitycollection.dbo.interests C
ON C.casenumberkey = A.casenumberkey
JOIN newcitycollection.dbo.interestadd D
ON D.casenumberkey = A.casenumberkey
AND D.interestskey = C.interestskey
WHERE A.datefinished BETWEEN #PeriodStart AND #PeriodEnd
AND clientkey = 2
UNION ALL
SELECT 2 AS grpOrd,
Cast(C.interestskey AS VARCHAR(11)) AS posInGrp,
'B2',
A.casenumberkey,
'B2|' + '|' + Isnull(E.suitnumber, '') + '|'
+ Cast(E.bdate AS VARCHAR(11)) + '|'
+ Isnull(E.chapter, '') + '|' + Isnull(E.vs, '') AS Extract
FROM newcitycollection.dbo.propertyinformation A
JOIN newcitycollection.dbo.interests C
ON C.casenumberkey = A.casenumberkey
JOIN newcitycollection.dbo.banks E
ON E.casenumberkey = A.casenumberkey
AND E.interestskey = C.interestskey
WHERE A.datefinished BETWEEN #PeriodStart AND #PeriodEnd
AND clientkey = 2
UNION ALL
SELECT 3 AS grpOrd3,
NULL AS posInGrp,
'B3',
A.casenumberkey,
'B3|' + '|' + F.doctype + '|'
+ Isnull(Cast(F.docamt AS VARCHAR(50)), '')
+ '|'
+ Isnull(Replace(CONVERT(CHAR(10), docdate, 101), '/', ''), '')
+ '|'
+ Isnull(Replace(CONVERT(CHAR(10), recdate, 101), '/', ''), '')
+ '|' + Isnull(F.docid, '') + '|'
+ Isnull(F.grantee, '') + '|'
+ Isnull(F.grantor, '')
+ Cast(F.docidkey AS VARCHAR(11)) AS Extract
FROM newcitycollection.dbo.propertyinformation A
JOIN newcitycollection.dbo.documents F
ON F.casenumberkey = A.casenumberkey
WHERE A.datefinished BETWEEN #PeriodStart AND #PeriodEnd
AND clientkey = 2
UNION ALL
SELECT 4
AS grpOrd
,
NULL
AS posInGrp,
'C',
A.casenumberkey,
'C|' + Isnull(J.ctype, '') + '|'
+ Isnull(J.plaintiffname, '') + '|'
+ Isnull(J.plaintiffadd1, '') + '|'
+ Isnull(J.plaintiffcity, '') + '|'
+ Isnull(J.plaintiffstate, '') + '|'
+ Isnull(J.plaintiffzip, '') + '|' + '|'
+ Isnull(J.defendantname, '') + '|'
+ Isnull(J.defendantadd1, '') + '|'
+ Isnull(J.defcity, '') + '|'
+ Isnull(J.defstate, '') + '|'
+ Isnull(J.defzip, '') + '|' + '|'
+ Isnull(J.court, '') + '|' + Isnull(J.caseid, '')
+ '|' + Isnull(J.jamt, '') + '|'
+ Isnull(Replace(CONVERT(VARCHAR(10), jdate, 101), '/', ''), '')
+ '|'
+ Isnull(Replace(CONVERT(VARCHAR(10), reviveddate, 101), '/', ''
), ''
)
AS
Extract
FROM newcitycollection.dbo.propertyinformation A
JOIN acme.new_judgment_system.dbo.selected_compiled_clean J
ON J.casenumber = A.casenumberkey
WHERE A.datefinished BETWEEN #PeriodStart AND #PeriodEnd
AND clientkey = 2
AND J.plaintiffname NOT IN (SELECT plaintiff
FROM
newcitycollection.dbo.excluded_plaintiffs))
--Extract data set into a table -- dump table in .txt with current date as part of name then delete that table
SELECT extract
INTO datadump
FROM outpq
ORDER BY casenumberkey,
grpord,
posingrp,
ord
DECLARE #FileName VARCHAR(50),
#bcpCommand VARCHAR(2000)
SET #FileName = Replace('D:\LDExport\Argosy_import_'
+ CONVERT(CHAR(8), Getdate(), 1) + '_0001.txt', '/', '')
SET #bcpCommand = 'bcp "SELECT Extract FROM datadump" QUERYOUT "'
SET #bcpCommand = #bcpCommand + #FileName
+ '" -U sa -P $%^&*() -T -c'
EXEC master..Xp_cmdshell
#bcpCommand
DROP TABLE datadump
Your final query...
SELECT Extract FROM datadump
...doesn't have an ORDER BY. What do you expect?
Add an ORDER BY there. What you ordered by when you inserted has no bearing on future queries.

SQL results to Outlook VCard?

I need to export SQL records to outlook vcards, so 1 vcard per result row returned. Does anyone know how to do this?
Maybe something like this:
DECLARE #crlf char(2)
SET #crlf = CHAR(13) + CHAR(10)
SELECT
'BEGIN:VCARD' + #crlf
+ COALESCE ('N:' + COALESCE (Last, '') + ';'
+ COALESCE (First, '') + ';'
+ COALESCE (Mi, '') + #crlf, '')
+ COALESCE ('FN:' + FullName + #crlf, '')
+ COALESCE ('TITLE:' + Title + #crlf, '')
+ COALESCE ('ORG:' + Company + #crlf, '')
+ COALESCE ('TEL;WORK;VOICE:' + PhoneWork + #crlf, '')
+ COALESCE ('TEL;WORK;FAX:' + FaxWork + #crlf, '')
+ COALESCE ('TEL;HOME;VOICE:' + [PhoneHome] + #crlf, '')
+ COALESCE ('TEL;HOME;FAX:' + [FaxHome] + #crlf, '')
+ COALESCE ('TEL;CELL;VOICE:' + [PhoneMobile] + #crlf, '')
+ COALESCE ('TEL;PAGER;VOICE:' + [Pager] + #crlf, '')
+ 'ADR;WORK:;;' + COALESCE ([Address], '') + ';'
+ COALESCE ([City], '') + ';' + COALESCE ([State], '') + ';'
+ COALESCE ([Zip], '') + #crlf
+ COALESCE ('EMAIL;PREF;INTERNET:' + [email] + #crlf, '')
+ 'REV:' + { fn REPLACE({ fn REPLACE(
{ fn REPLACE(CONVERT(varchar, GETDATE(), 120), '-', '')
}, ':', '') }, ' ', 'T') } + 'Z'+ #crlf
+ 'END:VCARD' + #crlf AS vcard
FROM
yourTable