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.
Related
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
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
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.
I am using postgresql 9.0
I am wonder if its possible to concatenate three attributes together.
this is how I concatenate two attributes (book & the comma):
SELECT string_agg(book, ',') FROM authors where id = 1;
| book1,book2,book3|
--------------------
how can I do something like below:
SELECT string_agg(name, ':', book, ',') FROM authors where id = 1;
| Ahmad: book1,book2,book3|
----------------
can some one help? thanks.
Just concatenate the fields like this:
SELECT name || ':' || string_agg(book, ',') FROM authors where id = 1;
Edit:
If your SQL returns multiple names you need to group by name (if you have multiple authors with the same name it gets a bit more complicated. I won't cover that case in this answer):
SELECT name || ':' || string_agg(book, ',')
FROM authors where id = 1
GROUP BY name;
If you want the books in alphabetical order you can add an ORDER BY for the books:
SELECT name || ':' || string_agg(book, ',') WITHIN GROUP ORDER BY book
FROM authors where id = 1
GROUP BY name;
SELECT name || ': ' || string_agg(book, ',') FROM authors where id = 1 group by name ;
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";