Find list of additional urls in Sitefinity database - content-management-system

Does anyone know what SQL I could use to find all of the Additional URLs for a site in the Sitefinity database?

I was able to create a list of urls and the associated page and parent page (using the html_title of both) with the following sql.
SELECT ud.url,
pd.html_title_, ppd.html_title_ AS ParentHtmlTitle
FROM sf_url_data ud INNER JOIN sf_page_node pn ON ud.id2 = pn.id
INNER JOIN sf_page_data pd ON pn.content_id = pd.content_id
INNER JOIN sf_page_node ppn ON pn.parent_id = ppn.id
INNER JOIN sf_page_data ppd ON ppn.content_id = ppd.content_id
ORDER BY ud.url
The piece that was missing for me was that while the sf_url_data has a content_id field, it doesn't always correspond to the appropriate page. However, the field id2 in that table does correspond to the sf_page_node table which contains the corresponding content_id for the sf_page_data table.

This is what I ultimately figured out for our scenario. The query by wbg34 wasn't giving me the real result url's that users were actually navigating to, as that is determined by the page hierarchy you setup in sitefinity. So what I did was build the FinalUrl in the below query by going up a max of 8 depth levels, if you need to go more just add more levels to the query.
This query removes the special backend pages, and removes the preceeding "Pages" from the url in FinalUrl. It lists all redirect rows (IsRedirect = 1), or if you see multiple urls on a page with (IsRedirect = 0), that means it's an alternate URL to that page and not a redirect.
select right(
stuff(FinalUrl,len(finalurl),1,''), len(finalurl)-6) --Remove trailing slash and Pages from beginning of URL
, url RedirectUrl, redirect IsRedirect, title_ as Title from (
select
concat(ppn8.url_name_ + '/',ppn7.url_name_ + '/',ppn6.url_name_ + '/',ppn5.url_name_ + '/', ppn4.url_name_+ '/', ppn3.url_name_+ '/', ppn2.url_name_+ '/', ppn.url_name_+ '/', pn.url_name_+ '/')
FinalUrl, ud.url, ud.redirect, pn.title_ from sf_url_data ud
INNER JOIN sf_page_node pn ON ud.id2 = pn.id
left outer join sf_page_node ppn ON pn.parent_id = ppn.id
left outer join sf_page_node ppn2 ON ppn.parent_id = ppn2.id
left outer join sf_page_node ppn3 ON ppn2.parent_id = ppn3.id
left outer join sf_page_node ppn4 ON ppn3.parent_id = ppn4.id
left outer join sf_page_node ppn5 ON ppn4.parent_id = ppn5.id
left outer join sf_page_node ppn6 ON ppn5.parent_id = ppn6.id
left outer join sf_page_node ppn7 ON ppn6.parent_id = ppn7.id
left outer join sf_page_node ppn8 ON ppn7.parent_id = ppn8.id
) a
where FinalUrl not like 'Backend%'
order by FinalUrl
Progresses knowledge base articles give these additional pointers, but don't provide the final url of the page.
--PageName, PageUrl, EnableMultipleUrl, SearchEngineTitle
SELECT pn.[title_] AS PageName,
pn.[url_name_] AS PageUrl,
pn.[allow_multiple_urls] AS EnableMultipleUrl,
(SELECT Count(*)
FROM (SELECT url,
Row_number()
OVER(
partition BY url
ORDER BY url DESC) rn
FROM [dbo].[sf_url_data]
WHERE id2 = pn.id) a
WHERE rn = 1) AS AdditionalUrlsCount,
pd.[html_title_] AS SearchEngineTitle
FROM [dbo].[sf_page_node] AS pn
JOIN [dbo].[sf_page_data] AS pd
ON pn.content_id = pd.content_id
-- if only frontend pages
WHERE pn.root_id = 'F669D9A7-009D-4D83-DDAA-000000000002' -- default frontend root id
--PageName, PageUrl, EnableMultipleUrl, SearchEngineTitle
SELECT pn.[title_] AS PageName,
pn.[url_name_] AS PageUrl,
pn.[allow_multiple_urls] AS EnableMultipleUrl,
(SELECT Count(*)
FROM (SELECT url,
Row_number()
OVER(
partition BY url
ORDER BY url DESC) rn
FROM [dbo].[sf_url_data]
WHERE id2 = pn.id) a
WHERE rn = 1) AS AdditionalUrlsCount,
(SELECT TOP 1 Substring((SELECT ',' + ST1.url AS [text()]
FROM (SELECT url
FROM (SELECT url,
Row_number()
OVER(
partition BY url
ORDER BY url DESC) rn
FROM [dbo].[sf_url_data]
WHERE id2 = pn.id) a
WHERE rn = 1) ST1
FOR xml path ('')), 2, 1000) AS [Urls]
FROM (SELECT *
FROM (SELECT url,
Row_number()
OVER(
partition BY url
ORDER BY url DESC) rn
FROM [dbo].[sf_url_data]
WHERE id2 = pn.id) a
WHERE rn = 1) ST2) AS Urls,
pd.[html_title_] AS SearchEngineTitle
FROM [dbo].[sf_page_node] AS pn
JOIN [dbo].[sf_page_data] AS pd
ON pn.content_id = pd.content_id
-- if only frontend pages
WHERE pn.root_id = 'F669D9A7-009D-4D83-DDAA-000000000002' -- default frontend root id

Related

How to use DISTINCT ON in ARRAY_AGG()?

I have the following query:
SELECT array_agg(DISTINCT p.id) AS price_ids,
array_agg(p.name) AS price_names
FROM items
LEFT JOIN prices p on p.item_id = id
LEFT JOIN third_table t3 on third_table.item_id = id
WHERE id = 1;
When I LEFT JOIN the third_table all my prices are duplicated.
I'm using DISTINCT inside ARRAY_AGG() to get the ids without dups, but I want the names without dups aswell.
If I use array_agg(DISTINCT p.name) AS price_names, it will return distinct values based on the name, not the id.
I want to do something similar to array_agg(DISTINCT ON (p.id) p.name) AS price_names, but it is invalid.
How can I use DISTINCT ON inside ARRAY_AGG()?
Aggregate first, then join:
SELECT p.price_ids,
p.price_names,
t3.*
FROM items
LEFT JOIN (
SELECT pr.item_id,
array_agg(pr.id) AS price_ids,
array_agg(pr.name) AS price_names
FROM prices pr
GROUP BY pr.item_id
) p on p.item_id = items.id
LEFT JOIN third_table t3 on third_table.item_id = id
WHERE items.id = 1;
Using a lateral join might be faster if you only pick a single item:
SELECT p.price_ids,
p.price_names,
t3.*
FROM items
LEFT JOIN LATERAL (
SELECT array_agg(pr.id) AS price_ids,
array_agg(pr.name) AS price_names
FROM prices pr
WHERE pr.item_id = items.id
) p on true
LEFT JOIN third_table t3 on third_table.item_id = id
WHERE items.id = 1;

Strange Behaviour on Postgresql query

We created a view in Postgres and I am getting strange result.
View Name: event_puchase_product_overview
When I try to get records with *, I get the correct result. but when I try to get specific fields, I get wrong values.
I hope the screens attached here can explain the problem well.
select *
from event_purchase_product_overview
where id = 15065;
select id, departure_id
from event_puchase_product_overview
where id = 15065;
VIEW definition:
CREATE OR REPLACE VIEW public.event_puchase_product_overview AS
SELECT row_number() OVER () AS id,
e.id AS departure_id,
e.type AS event_type,
e.name,
p.id AS product_id,
pc.name AS product_type,
product_date.attribute AS option,
p.upcomming_date AS supply_date,
pr.date_end AS bid_deadline,
CASE
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_hotel'::text) tt)) THEN e.maximum_rooms
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_flight'::text) tt)) THEN e.maximum_seats
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_bike'::text) tt)) THEN e.maximum_bikes
ELSE e.maximum_seats
END AS departure_qty,
CASE
WHEN now()::date > pr.date_end AND po.state::text = 'draft'::text THEN true
ELSE false
END AS is_deadline,
pl.product_qty::integer AS purchased_qty,
pl.comments,
pl.price_unit AS unit_price,
rp.id AS supplier,
po.id AS po_ref,
po.state AS po_state,
po.date_order AS po_date,
po.user_id AS operator,
pl.po_state_line AS line_status
FROM event_event e
LEFT JOIN product_product p ON p.related_departure = e.id
LEFT JOIN product_template pt ON pt.id = p.product_tmpl_id
LEFT JOIN product_category pc ON pc.id = pt.categ_id
LEFT JOIN purchase_order_line pl ON pl.product_id = p.id
LEFT JOIN purchase_order po ON po.id = pl.order_id
LEFT JOIN purchase_order_purchase_requisition_rel prr ON prr.purchase_order_id = po.id
LEFT JOIN purchase_requisition pr ON pr.id = prr.purchase_requisition_id
LEFT JOIN res_partner rp ON rp.id = po.partner_id
LEFT JOIN ( SELECT p_1.id AS product_id,
pav.name AS attribute
FROM product_product p_1
LEFT JOIN product_attribute_value_product_product_rel pa ON pa.prod_id = p_1.id
LEFT JOIN product_attribute_value pav ON pav.id = pa.att_id
LEFT JOIN product_attribute pat ON pat.id = pav.attribute_id
WHERE pat.name::text <> ALL (ARRAY['Date'::character varying, 'Departure'::character varying]::text[])) product_date ON product_date.product_id = p.id
WHERE (p.id IN ( SELECT DISTINCT mrp_bom_line.product_id
FROM mrp_bom_line)) AND p.active
ORDER BY e.id, pt.categ_id, p.id;
If I add new event_event or new product_product I'll get a new definition of row_number in my view, then the column ID of my view is not stable.
at least you can't use row_number as Id of the view,
If you insist to use row_number, you can use the Order By "creation DATE" by this way all new records will be as last lines in the view and this will not change the correspondency between ID (row_number) and other columns.
Hope that helps !
Very likely the execution plan of your query depends on the columns you select. Compare the execution plans!
Your id is generated using the row_number window function. Now window functions are executed before the ORDER BY clause, so the order will depend on the execution plan and hence on the columns you select.
Using row_number without an explicit ordering doesn't make any sense.
To fix that, don't use
row_number() OVER ()
but
row_number() OVER (ORDER BY e.id, pt.categ_id, p.id)
so that you have a reliable ordering.
In addition, you should omit the ORDER BY clause at the end.

sql, group a query with no aggregations and multiple tables

I need to group the query below by dda.LA and need to display all the columns listed in the select but almost none of them are aggregated. i don't know what the syntax to get around this is and i can not find a post that shows this syntax (most examples only have one table, two at tops).
Select dda.a,
dda.b,
dda.c,
dda.d,
dda.e,
dda.f,
dda.g,
dda.h,
dda.i,
dda.j,
dda.k,
dda.l,
dda.m,
dda.n,
dda.o,
dda.p,
dda.r,
dda.u,
dda.LA,
dd.aa,
coalesce(apn.apn,Pt.z) as abc,
coalesce(apn.v,Pt.y) as def,
'RFN' RowFocusIndicator ,
'SRI' SelectRowIndicator ,
'Y' Expanded ,
Convert(Int, Null) SortColumn
From dda (NoLock)
Inner Join dd (NoLock) On dda.d = dd.q and dda.e = dd.e
Left Outer Join apn (nolock) on dda.r = apn.r
Left Outer Join Pt (nolock) on dda.s = Pt.t
Where 1 = 1
And dda.u = (Select Min(c.w)
From c (NoLock)
Where c.x = dda.s)
Thanks!
Just add this to any column that you want to aggregate by:
AggregatedColumnName = Aggregation(fieldToAggregate) Over (Partition By dda.LA)
ex.
aCount = Count(dda.a) Over (Partition By dda.LA)

sql joins The multi-part identifier could not be found

here`s my query
SELECT cont.FILTER_VALUE as filter,
o.[OBJECT_ID] as Id, o.[OBJECT_NAME] as Name, o.DESCRIPTION as Description, o.CREATED as Created,
o.MODIFIED as Modified, u.[LOGIN] as LastModifiedByLogin, o.[OBJECT_NAME] as ObjectName, t.[TEMPLATE_NAME] as TemplateName--,p.[PAGE_NAME] as PageName
FROM
[OBJECT] AS o
LEFT OUTER JOIN [CONTAINER] as cont
on cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov ON
ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p ON o.[PAGE_ID] = p.[PAGE_ID]
INNER JOIN [USERS] as u on u.[USER_ID] = o.LAST_MODIFIED_BY INNER JOIN [PAGE_TEMPLATE] as t
on o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID] INNER JOIN [site] as s on t.SITE_ID = s.SITE_ID
WHERE
s.SITE_ID = '34' --AND сont.[FILTER_VALUE] is null--like '%fff%'
And it works nice, until I remove the comment.
Here's a mess of joins, still it has sense. I inner join main table with couple of others, and left join with optional, so, that I have a column, that contains cont.FILTER_VALUE as filter, its null in some records, I can get it, but I cant filter by this field.
I get The multi-part identifier "сont.FILTER_VALUE" could not be bound.
I've looked through similar topics, but found no useful information. I don't use any old SQL dialects: everywhere I use INNER/LEFT joins, tried group by and order by, tried to re-order joins - nothing helped. I guess I just don't understand something important about joins, could you tell me, please.
Thanx.
if you wrote it like this:
s.SITE_ID = '34' AND сont.[FILTER_VALUE] is null like '%fff%'
its just wrong syntax. you're missing a column for the LIKE function (in which column does the query suppose to look for the pattern?). if you didnt write it like that, please post what you did write
I don't know why you're getting your error, but try this just in case, while we wait for a better answer:
SELECT FILTER_VALUE as filter
, o.[OBJECT_ID] as Id
, o.[OBJECT_NAME] as Name
, o.DESCRIPTION as Description
, o.CREATED as Created
, o.MODIFIED as Modified
, u.[LOGIN] as LastModifiedByLogin
, o.[OBJECT_NAME] as ObjectName
, t.[TEMPLATE_NAME] as TemplateName
FROM [OBJECT] AS o
INNER JOIN [USERS] as u
ON u.[USER_ID] = o.LAST_MODIFIED_BY
INNER JOIN [PAGE_TEMPLATE] as t
ON o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID]
INNER JOIN [site] as s
ON t.SITE_ID = s.SITE_ID
LEFT JOIN [CONTAINER] as cont
ON cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov
ON ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p
ON o.[PAGE_ID] = p.[PAGE_ID]
WHERE s.SITE_ID = '34' AND FILTER_VALUE IS NULL
Well, I solved this problem, using CTE, still I wonder, why did I have problems without cte.
This way it works good
with query_CTE
AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY o.[OBJECT_ID] asc) as rowNum,
o.[OBJECT_ID] as Id, o.[OBJECT_NAME] as Name, o.DESCRIPTION as Description, o.CREATED as Created,
o.MODIFIED as Modified, u.[LOGIN] as LastModifiedByLogin, o.[OBJECT_NAME] as ObjectName, t.[TEMPLATE_NAME] as TemplateName,p.[PAGE_NAME] as PageName,
s.SITE_ID, t.PAGE_TEMPLATE_ID, p.PAGE_ID, ov.VARIABLE_NAME, ov.VARIABLE_VALUE, cont.FILTER_VALUE, cont.DYNAMIC_CONTENT_VARIABLE, cont.SELECT_START,
cont.SELECT_TOTAL
FROM [OBJECT] AS o
INNER JOIN [USERS] as u
ON u.[USER_ID] = o.LAST_MODIFIED_BY
INNER JOIN [PAGE_TEMPLATE] as t
ON o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID]
INNER JOIN [site] as s
ON t.SITE_ID = s.SITE_ID
LEFT JOIN [CONTAINER] as cont
ON cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov
ON ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p
ON o.[PAGE_ID] = p.[PAGE_ID]
)
select rowNum,
Id, Name,[Description], Created, Modified, LastModifiedByLogin, ObjectName, TemplateName,PageName
from query_CTE

T-SQL Subquery on Latest Date with InnerJoin

I want to do a similiar thing like this guy:
T-SQL Subquery Max(Date) and Joins
I have to do this with an n:m relation.
So the layout is:
tbl_Opportunity
tbl_Opportunity_tbl_OpportunityData
tbl_OpportunityData
So as you see there is an intersection table which connects opportunity with opportunitydata.
For every opportunity there are multiple opportunity datas. In my view i only want a list with all opportunites and the data from the latest opportunity datas.
I tried something like this:
SELECT
dbo.tbl_Opportunity.Id, dbo.tbl_Opportunity.Subject,
dbo.tbl_User.UserName AS Responsible, dbo.tbl_Contact.Name AS Customer,
dbo.tbl_Opportunity.CreationDate, dbo.tbl_Opportunity.ActionDate AS [Planned Closure],
dbo.tbl_OpportunityData.Volume,
dbo.tbl_OpportunityData.ChangeDate, dbo.tbl_OpportunityData.Chance
FROM
dbo.tbl_Opportunity
INNER JOIN
dbo.tbl_User ON dbo.tbl_Opportunity.Creator = dbo.tbl_User.Id
INNER JOIN
dbo.tbl_Contact ON dbo.tbl_Opportunity.Customer = dbo.tbl_Contact.Id
INNER JOIN
dbo.tbl_Opprtnty_tbl_OpprtnityData ON dbo.tbl_Opportunity.Id = dbo.tbl_Opprtnty_tbl_OpprtnityData.Id
INNER JOIN
dbo.tbl_OpportunityData ON dbo.tbl_Opprtnty_tbl_OpprtnityData.Id2 = dbo.tbl_OpportunityData.Id
The problem is my view now includes a row for every opportunity data, since I don't know how to filter that I only want the latest data.
Can you help me? is my problem description clear enough?
thank you in advance :-)
best wishes,
laurin
; WITH Base AS (
SELECT dbo.tbl_Opportunity.Id, dbo.tbl_Opportunity.Subject, dbo.tbl_User.UserName AS Responsible, dbo.tbl_Contact.Name AS Customer,
dbo.tbl_Opportunity.CreationDate, dbo.tbl_Opportunity.ActionDate AS [Planned Closure], dbo.tbl_OpportunityData.Volume,
dbo.tbl_OpportunityData.ChangeDate, dbo.tbl_OpportunityData.Chance
FROM dbo.tbl_Opportunity INNER JOIN
dbo.tbl_User ON dbo.tbl_Opportunity.Creator = dbo.tbl_User.Id INNER JOIN
dbo.tbl_Contact ON dbo.tbl_Opportunity.Customer = dbo.tbl_Contact.Id INNER JOIN
dbo.tbl_Opprtnty_tbl_OpprtnityData ON dbo.tbl_Opportunity.Id = dbo.tbl_Opprtnty_tbl_OpprtnityData.Id INNER JOIN
dbo.tbl_OpportunityData ON dbo.tbl_Opprtnty_tbl_OpprtnityData.Id2 = dbo.tbl_OpportunityData.Id
)
, OrderedByDate AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY ChangeDate DESC) RN FROM Base
)
SELECT * FROM OrderedByDate WHERE RN = 1
To make it more readable I'm using CTE (the WITH part). In the end the real "trick" is doing a ROW_NUMBER() partitioning the data by tbl_Opportunity.Id and ordering the partitions by ChangeDate DESC (and I call it RN). Clearly the maximum date in each partition will be RN = 1 and then we filter it by RN.
Without using CTE it will be something like this:
SELECT * FROM (
SELECT dbo.tbl_Opportunity.Id, dbo.tbl_Opportunity.Subject, dbo.tbl_User.UserName AS Responsible, dbo.tbl_Contact.Name AS Customer,
dbo.tbl_Opportunity.CreationDate, dbo.tbl_Opportunity.ActionDate AS [Planned Closure], dbo.tbl_OpportunityData.Volume,
dbo.tbl_OpportunityData.ChangeDate, dbo.tbl_OpportunityData.Chance,
ROW_NUMBER() OVER (PARTITION BY dbo.tbl_Opportunity.Id ORDER BY dbo.tbl_OpportunityData.ChangeDate DESC) RN
FROM dbo.tbl_Opportunity INNER JOIN
dbo.tbl_User ON dbo.tbl_Opportunity.Creator = dbo.tbl_User.Id INNER JOIN
dbo.tbl_Contact ON dbo.tbl_Opportunity.Customer = dbo.tbl_Contact.Id INNER JOIN
dbo.tbl_Opprtnty_tbl_OpprtnityData ON dbo.tbl_Opportunity.Id = dbo.tbl_Opprtnty_tbl_OpprtnityData.Id INNER JOIN
dbo.tbl_OpportunityData ON dbo.tbl_Opprtnty_tbl_OpprtnityData.Id2 = dbo.tbl_OpportunityData.Id
) AS Base WHERE RN = 1
The statement can be simplified for one more step further:
SELECT TOP 1 WITH TIES
dbo.tbl_Opportunity.Id, dbo.tbl_Opportunity.Subject, dbo.tbl_User.UserName AS Responsible,
dbo.tbl_Contact.Name AS Customer, dbo.tbl_Opportunity.CreationDate,
dbo.tbl_Opportunity.ActionDate AS [Planned Closure], dbo.tbl_OpportunityData.Volume,
dbo.tbl_OpportunityData.ChangeDate, dbo.tbl_OpportunityData.Chance
FROM
dbo.tbl_Opportunity INNER JOIN
dbo.tbl_User ON dbo.tbl_Opportunity.Creator = dbo.tbl_User.Id INNER JOIN
dbo.tbl_Contact ON dbo.tbl_Opportunity.Customer = dbo.tbl_Contact.Id INNER JOIN
dbo.tbl_Opprtnty_tbl_OpprtnityData ON dbo.tbl_Opportunity.Id = dbo.tbl_Opprtnty_tbl_OpprtnityData.Id INNER JOIN
dbo.tbl_OpportunityData ON dbo.tbl_Opprtnty_tbl_OpprtnityData.Id2 = dbo.tbl_OpportunityData.Id
ORDER BY
ROW_NUMBER() OVER (PARTITION BY dbo.tbl_Opportunity.Id ORDER BY dbo.tbl_OpportunityData.ChangeDate DESC);