how t use start with -connect by nocycle in postgresql - postgresql

SELECT coalesce(RPAD('IN',16,' ') || RPAD( M.CASETYPE||'/' || M.CASENUMBER || '/' || M.CASEYEAR,16, ' '),' ') as inmatter
FROM (
select level as LEV, l1.LINKCASECCIN as CCI ,l1.linkcategorycode as lcode
from hclive.LINKEDMATTERS l1
start with l1.MAINCASECCIN ='001003201400100' and l1.linkcategorycode='I'
connect by nocycle prior l1.LINKCASECCIN = l1.MAINCASECCIN and l1.linkcategorycode = 'I'
) s1,
hclive.MAIN M
where M.CCIN=CCI

connect by is done with a recursive common table expression in Postgres.
with recursive tree as (
select 1 as level, l1.linkcaseccin as cci, l1.linkcategorycode as lcode
from hclive.linkedmatters l1
where l1.maincaseccin ='001003201400100'
and l1.linkcategorycode='I'
union all
select p.level + 1, c1.linkcaseccin as cci, c1.linkcategorycode as lcode
from hclive.linkedmatters c1
join tree p on p.maincaseccin = c1.linkcaseccin
where c1.linkcategorycode='I'
)
select coalesce(rpad('IN',16,' ') || rpad( m.casetype||'/' || m.casenumber || '/' || m.caseyear,16, ' '),' ') as inmatter
from tree t
join hclive.main M on m.ccin = t.cci;
The level column does not seem to be necessary as you don't use it at all in your query, but I left in there as an example to get the same information in Postgres.

Related

Need help combining the results of two select statements

My first select statements is:
SELECT cRefNum, cName, cProgram || '-' || cCode || '-' || cSection as CourseDesc, cTimeStart, cTimeEnd, cDay, ct.cCampus, cBuildingSection || cRoom AS Room, cSchedType
from coursedetails cd inner join coursetimes ct
using (cRefNum)
where cRefNum = 3816;
Which results in:
My second select statements is:
select cRefNum, LISTAGG(fname|| ' ' || lname, ', ') within group (order by cRefNum) as Teachers
from teachers
where cRefNum = 3816
group by cRefNum;
Which results in:
What I'm trying to achieve is:
select a.cRefNum, cName, CourseDesc, cTimeStart, cTimeEnd, cDay, Campus, Room, cSchedType, Teachers
from
(select cRefNum, cName, cProgram || '-' || cCode || '-' || cSection as CourseDesc, cTimeStart, cTimeEnd, cDay, ct.cCampus as Campus, cBuildingSection || cRoom AS Room, cSchedType from coursedetails cd inner join coursetimes ct using (cRefNum) where cRefNum = 3816) a,
(select cRefNum, LISTAGG(fname|| ' ' || lname, ', ') within group (order by cRefNum) as Teachers from teachers where cRefNum = 3816 group by cRefNum) b
where a.cRefNum = b.cRefNum;
This fixes it, but is really long and I don't know enough to shorten it.

Extract string of dynamic length when the indicator of completion exists in multiple instances. Postgres

So if I have a varchar length string column let's call ID(samples below):
97.128.39.256.1460854333288493
25.365.49.12.13454154815132
346.45.156.354.1523425161233
I want to grab, like a left in excel, everything to the left of the 4th period. How do i create a dynamic string to find the fourth instance of a period?
I know substring is a start but not sure how to write in the dynmic length that exists
This is probably the easiest for someone else to read:
select split_part(i, '.', 1) || '.' ||
split_part(i, '.', 2) || '.' ||
split_part(i, '.', 3) || '.' ||
split_part(i, '.', 4)
from (select '97.128.39.256.1460854333288493' as i) as sub;
Or if you don't like split_part and prefer to use arrays:
select array_to_string((string_to_array(i, '.'))[1:4], '.')
from (select '97.128.39.256.1460854333288493' as i) as sub;
I think the array example is a bit harder to grasp at first glance but both work.
Updated answer based on revised question to also convert the Unix timestamp to a Greenplum timestamp:
select 'epoch'::timestamp + '1 second'::interval *
(split_part(i, '.', 5)::numeric/1000000) as event_time,
array_to_string((string_to_array(i, '.'))[1:4], '.') as ip_address
from (
select '97.128.39.256.1460854333288493' as i
) as sub;
You could also try this:
mydb=> select regexp_replace('97.128.39.256.1460854333288493', E'^((?:\\d+\\.){3}\\d+).+$', E'\\1');
regexp_replace
----------------
97.128.39.256
(1 row)
Time: 0.634 ms
with t (s) as ( values
('97.128.39.256.1460854333288493'),
('25.365.49.12.13454154815132'),
('346.45.156.354.1523425161233')
)
select a[1] || '.' || a[2] || '.' || a[3] || '.' || a[4]
from (
select regexp_split_to_array(s, '\.')
from t
) t (a)
;
?column?
----------------
97.128.39.256
25.365.49.12
346.45.156.354

Why am I getting Error Code: -122 SQL State: 42803?

Using ODBC connection to a DB2 instance with the select statement below it returns the following error:
Error Code: -122 SQL State: 42803
Can someone help me identify which part of the code below is raising the error
SELECT
SAB089.ZCREPRE0.CREPREDOS as ContractCode,
SAB089.ZCLIENA0.CLIENACLI as ConsentCode,
SAB089.ZCREPRE0.CREPREAGE as Branch,
(case
when SAB089.ZCREPRE0.CREPRECTA='7' then 'Open'
when SAB089.ZCREPRE0.CREPRECTA='8' then 'Closed'
end) as PhaseOfContract,
(case
when SAB089.ZCREPRE0.CREPRECTA='6' then 'GrantedButNotActivated'
when SAB089.ZCREPRE0.CREPRECTA='7' then 'GrantedAndActivated'
when SAB089.ZCREPRE0.CREPRECTA='8' then 'SettledOnTime'
end) as ContractStatus,
'Installment' as TypeOfContract,
(case
when SAB089.ZCREPRE0.CREPRENAT = '757' then 'Vehicule'
when SAB089.ZCREPRE0.CREPRENAT = '756' then 'Equipement'
when SAB089.ZCREPRE0.CREPRENAT = '758' then 'Immobilier'
end) as PurposeOfFinancing,
SAB089.ZCREDOS0.CREDOSDEV as CurrencyOfContract,
Cast(SAB089.ZCREDOS0.CREDOSMNT as DECIMAL(19,2)) as TotalAmount,
SAB089.ZCREAVI0.CREAVIMON as InstallmentAmount,
SAB089.ZCREPLA0.CREPLANPC as NumberOfInstallments,
Cast(SAB089.ZCREPRE0.CREPRECAP as DECIMAL(19,2)) as OutstandingAmount,
Cast(SAB089.ZIMPECH0.IMPECHMON as DECIMAL(19,2)) as PastDueAmount,
SAB089.ZIMPECH0.IMPECHDTI as PastDueDays,
SAB089.ZIMPDOS0.IMPDOSECH as NumberOfDueInstallments,
Cast(SUM(SAB089.ZCREAVI0.CREAVIMON + SAB089.ZCREAVI0.CREAVIMIN + SAB089.ZCREAVI0.CREAVITVA + SAB089.ZCREAVI0.CREAVIAM1)as DECIMAL(19,2)) as TotalMonthlyPayment,
(case
when SAB089.ZCREPLA0.CREPLAPCA = 'U' then 'FinalDay'
when SAB089.ZCREPLA0.CREPLAPCA = 'M' then 'Days30'
when SAB089.ZCREPLA0.CREPLAPCA = 'T' then 'Days90'
when SAB089.ZCREPLA0.CREPLAPCA = 'S' then 'Days120'
when SAB089.ZCREPLA0.CREPLAPCA = 'A' then 'Days360'
end)
as PaymentPeriodicity,
{fn substring(SAB089.ZCREMAD0.CREMADDMD,5,2)} || '-' || {fn substring(SAB089.ZCREMAD0.CREMADDMD,3,2)} || '-' || {fn substring(SAB089.ZCREMAD0.CREMADDMD,1,2)} as StartDate,
{fn substring(SAB089.ZCREAVI0.CREAVIECH,5,2)} || '-' || {fn substring(SAB089.ZCREAVI0.CREAVIECH,3,2)} || '-' || {fn substring(SAB089.ZCREAVI0.CREAVIECH,1,2)} as RealEndDate
FROM
SAB089.ZCREAVI0, SAB089.ZIMPDOS0, SAB089.ZIMPECH0, SAB089.ZCREMAD0
INNER JOIN SAB089.ZCREPRE0 ON (SAB089.ZCREPRE0.CREPREETA=SAB089.ZCREMAD0.CREMADETA and SAB089.ZCREPRE0.CREPREAGE=SAB089.ZCREMAD0.CREMADAGE and SAB089.ZCREPRE0.CREPRESCE=SAB089.ZCREMAD0.CREMADSER and SAB089.ZCREPRE0.CREPRESSE=SAB089.ZCREMAD0.CREMADSSE and SAB089.ZCREPRE0.CREPREDOS=SAB089.ZCREMAD0.CREMADDOS and SAB089.ZCREPRE0.CREPREPRE=SAB089.ZCREMAD0.CREMADPRE)
INNER JOIN SAB089.ZCREPLA0 ON (SAB089.ZCREPLA0.CREPLAETA=SAB089.ZCREPRE0.CREPREETA and SAB089.ZCREPLA0.CREPLAAGE=SAB089.ZCREPRE0.CREPREAGE and SAB089.ZCREPLA0.CREPLASER=SAB089.ZCREPRE0.CREPRESCE and SAB089.ZCREPLA0.CREPLASSE=SAB089.ZCREPRE0.CREPRESSE and SAB089.ZCREPLA0.CREPLADOS=SAB089.ZCREPRE0.CREPREDOS and SAB089.ZCREPLA0.CREPLAPRE=SAB089.ZCREPRE0.CREPREPRE)
INNER JOIN SAB089.ZCREDOS0 ON (SAB089.ZCREDOS0.CREDOSETA=SAB089.ZCREPRE0.CREPREETA and SAB089.ZCREDOS0.CREDOSAGE=SAB089.ZCREPRE0.CREPREAGE and SAB089.ZCREDOS0.CREDOSSER=SAB089.ZCREPRE0.CREPRESCE and SAB089.ZCREDOS0.CREDOSSSE=SAB089.ZCREPRE0.CREPRESSE and SAB089.ZCREDOS0.CREDOSDOS=SAB089.ZCREPRE0.CREPREDOS)
INNER JOIN SAB089.ZCREEMP0 ON (SAB089.ZCREDOS0.CREDOSETA=SAB089.ZCREEMP0.CREEMPETA and SAB089.ZCREDOS0.CREDOSAGE=SAB089.ZCREEMP0.CREEMPAGE and SAB089.ZCREDOS0.CREDOSSER=SAB089.ZCREEMP0.CREEMPSER and SAB089.ZCREDOS0.CREDOSSSE=SAB089.ZCREEMP0.CREEMPSSE and SAB089.ZCREDOS0.CREDOSDOS=SAB089.ZCREEMP0.CREEMPDOS)
INNER JOIN SAB089.ZCLIENA0 ON (SAB089.ZCLIENA0.CLIENACLI=SAB089.ZCREEMP0.CREEMPNCL)
LEFT OUTER JOIN SAB089.X89ADRESSE ON (SAB089.X89ADRESSE.ADRESCLI=SAB089.ZCLIENA0.CLIENACLI)
INNER JOIN SAB089.ZCLIENB0 ON (SAB089.ZCLIENA0.CLIENAETB=SAB089.ZCLIENB0.CLIENBETB and SAB089.ZCLIENA0.CLIENACLI=SAB089.ZCLIENB0.CLIENBCLI)
INNER JOIN SAB089.ZCLIENC0 ON (SAB089.ZCLIENA0.CLIENAETB=SAB089.ZCLIENC0.CLIENCETB and SAB089.ZCLIENA0.CLIENACLI=SAB089.ZCLIENC0.CLIENCCLI)
LEFT OUTER JOIN SAB089.ZCLILIB0 ON (SAB089.ZCLILIB0.CLILIBCLI=SAB089.ZCLIENA0.CLIENACLI)
WHERE
(
{fn substring(SAB089.ZCLILIB0.CLILIBVAL,13,1)} = 'O'
AND SAB089.ZCREMAD0.CREMADDMD >= '1120101'
AND SAB089.ZCREPRE0.CREPRECTA IN (6, 7, 8)
AND SAB089.ZCLIENA0.CLIENADOU = 'N'
)

Checking empty column values in case when and ignoring them

I wrote the following query to check duplicates.
select s.*,m.IsMember_Ind,
case when REPLACE(s.[ Owner Mobile],' ','')
in (select cd.Mobile
from Company_Detail cd
inner join MEMBERSHIP m
on cd.Company_ID = m.Company_ID
where m.IsMember_Ind = 1)
then 'Match'
else ' ' end as OwnerMobileMatch,
case when REPLACE(s.[ Contact Number],' ','')
in (select cd.Mobile
from Company_Detail cd
inner join MEMBERSHIP m
on cd.Company_ID = m.Company_ID
where m.IsMember_Ind = 1)
then 'Match' else ' ' end as ContactMobileMatch
from COMPANY c
inner join surreynonmembers s
on c.TTR_ID = s.[Company ID]
left outer join MEMBERSHIP m
on m.Company_ID = c.Company_ID
My problem is, the case statement returns as a match when finds an empty value which I do not need. How can I modify this query to return if there is only a value?
Have you tried this?
GO
SELECT s.*
,m.IsMember_Ind
,CASE
WHEN REPLACE(s.[ Owner Mobile], ' ', '') IN (
SELECT cd.Mobile
FROM Company_Detail cd
INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID
WHERE m.IsMember_Ind = 1
AND ISNULL(s.[ Owner Mobile], '') <> ''
)
THEN 'Match'
ELSE ' '
END AS OwnerMobileMatch
,CASE
WHEN REPLACE(s.[ Contact Number], ' ', '') IN (
SELECT cd.Mobile
FROM Company_Detail cd
INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID
WHERE m.IsMember_Ind = 1
AND ISNULL(s.[ Contact Number], '')<> ''
)
THEN 'Match'
ELSE ' '
END AS ContactMobileMatch
FROM COMPANY c
INNER JOIN surreynonmembers s ON c.TTR_ID = s.[Company ID]
LEFT JOIN MEMBERSHIP m ON m.Company_ID = c.Company_ID
GO

openx adserver statistics tab error with postgres db version 9.x

I installed latest openX ad server(publisher server) with postgres 9.x version.
After successful installation and configuration i login and click at "Statistics" tab i see the following error.
i don't see this error when i installed openX server with mysql any quick fix ?
PEAR Error
MDB2 Error: unknown error
_doQuery: [Error message: Could not execute statement]
[Last executed query: SELECT m.clientid AS advertiser_id,d.campaignid AS placement_id,s.ad_id AS ad_id,SUM(s.impressions) AS sum_views,SUM(s.clicks) AS sum_clicks,SUM(s.revenue) AS sum_revenue, m.campaignid || IF( LENGTH(market_advertiser_id) > 0, ('_' || market_advertiser_id || ''), '') || ad_width || ' x ' || ad_height AS ad_id,( m.campaignid || IF( LENGTH(market_advertiser_id) > 0, ('_' || market_advertiser_id || ''), '') || ad_width || ' x ' || ad_height ) AS pkey FROM "ox_ext_market_stats" AS s INNER JOIN "ox_banners" AS d ON (d.bannerid=s.ad_id) INNER JOIN "ox_zones" AS z ON (z.zoneid=s.zone_id) INNER JOIN "ox_campaigns" AS m ON (m.campaignid=d.campaignid) INNER JOIN "ox_affiliates" AS p ON (p.affiliateid=z.affiliateid) INNER JOIN "ox_clients" AS a ON (a.clientid=m.clientid) WHERE s.ad_id IN (1,2) AND a.type = 1 AND s.zone_id <> 0 AND s.date_time>='2011-02-22 00:00:00' AND s.date_time<='2011-02-22 23:59:59' GROUP BY advertiser_id,placement_id,pkey]
[Native message: ERROR: column "s.ad_id" must appear in the GROUP BY clause or be used in an aggregate function at character 65]
PEAR Error
MDB2 Error: unknown error
_doQuery: [Error message: Could not execute statement]
[Last executed query: SELECT m.clientid AS advertiser_id,d.campaignid AS placement_id,s.ad_id AS ad_id,SUM(s.impressions) AS sum_views,SUM(s.clicks) AS sum_clicks,SUM(s.revenue) AS sum_revenue, m.campaignid || IF( LENGTH(market_advertiser_id) > 0, ('_' || market_advertiser_id || ''), '') || ad_width || ' x ' || ad_height AS ad_id,( m.campaignid || IF( LENGTH(market_advertiser_id) > 0, ('_' || market_advertiser_id || ''), '') || ad_width || ' x ' || ad_height ) AS pkey FROM "ox_ext_market_stats" AS s INNER JOIN "ox_banners" AS d ON (d.bannerid=s.ad_id) INNER JOIN "ox_campaigns" AS m ON (m.campaignid=d.campaignid) INNER JOIN "ox_clients" AS a ON (a.clientid=m.clientid) WHERE s.ad_id IN (1,2) AND s.zone_id = 0 AND a.type = 1 AND s.date_time>='2011-02-22 00:00:00' AND s.date_time<='2011-02-22 23:59:59' AND s.zone_id = 0 GROUP BY advertiser_id,placement_id,pkey]
[Native message: ERROR: column "s.ad_id" must appear in the GROUP BY clause or be used in an aggregate function at character 65]
Ok, i have found a solution. To fix the problem, must add this code after line 1031 in lib/max/SqlBuilder.php .
$aGroupColumns[] = "s.ad_id";