Selecting users,groups and site from alfresco database - postgresql

I would like to select users,in which groups they belong to, and their sites. I want it with a single postgre sql query.
I've found these queries, but i want them combined with join if possible.
select * from alf_permission
select * from ALF_AUTHORITY
select * from ALF_CHILD_ASSOC where CHILD_NODE_NAME like ‘group%’
select * from ALF_CHILD_ASSOC where QNAME_LOCALNAME like ‘GROUP%’
select
node_id,
string_agg(string_value, ',')
from (
select
node_id,
qname_id,
(select local_name from alf_qname where id = qname_id) as qname_type,
string_value
from alf_node_properties
where node_id in (
select id from alf_node
where type_qname_id = (
select id from alf_qname where local_name = 'person'
)
and qname_id in (
select id
from alf_qname
where local_name in (
'username',
'firstName',
'lastName',
'email'
)
)
)
) alf_users
group by node_id;

I would advise to create webscript possibly in Java that will do this fast and efficient!

I found a solution!
I put the script in Company Home > Data Dictionary > Web Scripts > org > alfresco > test
As you can see i created the "test" folder in "alfresco" folder, and in it i put these three files.
hello.get.html.ftl file
<table border>
<tr>
<th>Username</th>
<th>Groups</th>
<th>Sites</th>
</tr>
<#list test as child>
<tr>
<td>${child['username']}</td>
<td>${child['groups']}</td>
<td>${child['sites']}</td>
</tr>
</#list>
</table>
hello.get.desc.xml
<webscript>
<shortname>Hello</shortname>
<description>Polite greeting</description>
<url>/test/hello</url>
<authentication>user</authentication>
</webscript>
hello.get.js
var gens = search.luceneSearch("TYPE:\"{http://www.alfresco.org/model/content/1.0}person\"");
var a = [];
for (var i=0; i<gens.length;i++) {
var username = gens[i].properties["cm:userName"];
var b = [];
var groups = people.getContainerGroups(gens[i]);
for(var j=0; j<groups.length; j++) {
b.push(groups[j].properties['authorityDisplayName']);
}
var sites = siteService.listUserSites(username);
var g=[]
for(var j=0; j<sites.length; j++) {
g.push(sites[j]['shortName']);
}
//a.push('\n\n\n'+username+'\groups--> '+ b.join(', ') + '\nsites--> '+g.join(', '));
a.push({
'username' : username,
'groups' : b.join(', '),
'sites' : g.join(', ')
})
}
model.test = a;
you can access the result in your_domain_name/alfresco/service/test/hello

Try this query to get persons and groups
select
ca.id,
ca.parent_node_id,
ca.child_node_id,
ca.qname_localname,
np.string_value
from alf_child_assoc as ca
join alf_node as child on child.id = ca.child_node_id
join alf_node as parent on parent.id = ca.parent_node_id
join alf_qname as q1 on q1.id = parent.type_qname_id
join alf_qname as q2 on q2.id = child.type_qname_id
join alf_node_properties as np on np.node_id = parent.id
where q1.local_name = 'authorityContainer'
and q2.local_name = 'person'
and np.qname_id = (select id from alf_qname where local_name =
'authorityDisplayName')
order by ca.qname_localname;`

Related

How to correlate update with select query

I want to update every row in the column SalesDifference20132015 in the table Stockitems based on the values in different columns in the same table. I know the SELECT query work however when I plug it into my update query I get the subquery returned more than 1 value error. How do i correlate my select query with my update?
UPDATE warehouse.StockItems
SET SalesDifference20132015 =
(SELECT (SELECT COUNT(*)
FROM Sales.Orders o
join sales.OrderLines ol ON o.OrderID = ol.OrderID
WHERE ol.StockItemID = si.StockItemID AND YEAR(o.OrderDate) = 2013
Group by ol.StockItemID)
-
(SELECT COUNT(*)
FROM Sales.Orders o
join sales.OrderLines ol ON o.OrderID = ol.OrderID
WHERE ol.StockItemID = si.StockItemID AND YEAR(o.OrderDate) = 2015
Group by ol.StockItemID)
FROM Warehouse.StockItems si
)
Something like this should work:
UPDATE si
SET SalesDifference20132015 = DIFF.Diff
FROM
warehouse.StockItems si
JOIN
(
SELECT DISTINCT
StockItemID
,
(
SELECT COUNT(*)
FROM
Sales.Orders o
JOIN sales.OrderLines ol ON o.OrderID = ol.OrderID
WHERE ol.StockItemID = si.StockItemID AND YEAR(o.OrderDate) = 2013
GROUP BY ol.StockItemID
)
-
(
SELECT COUNT(*)
FROM
Sales.Orders o
JOIN sales.OrderLines ol ON o.OrderID = ol.OrderID
WHERE ol.StockItemID = si.StockItemID AND YEAR(o.OrderDate) = 2015
GROUP BY ol.StockItemID
) Diff
FROM Warehouse.StockItems si
) DIFF
ON si.StockItemID = DIFF.StockItemID

How can I do calculations with previous defined columns in PostgreSQL?

I'm currently trying to calculate a column using previous declared columns. I think that would work in MySQL, but I'm unsure how do do that in PostgreSQL.
My Statement looks like this:
SELECT customerNumber, customerName, (
SELECT COALESCE(SUM(payments.amount), 0)
FROM payments
WHERE customers.customerNumber = payments.customerNumber
) as totalOfPaymentsMade,(
SELECT COALESCE(SUM(orderdetails.priceeach * orderdetails.quantityordered), 0)
FROM orders
LEFT JOIN orderdetails ON orderdetails.ordernumber = orders.ordernumber
WHERE customers.customerNumber = orders.customerNumber
) as totalValueOfAllOrdersMade, creditLimit, creditLimit + totalOfPaymentsMade - totalValueOfAllOrdersMade as amountOfAvailableCredit
FROM customers
ORDER BY customerNumber
I get this error message: column "totalofpaymentsmade" does not exist
LINE 11: ...lValueOfAllOrdersMade, creditLimit, creditLimit + totalOfPay...
Try this:
SELECT
*,
creditLimit + totalOfPaymentsMade - totalValueOfAllOrdersMade
as amountOfAvailableCredit
from
(
SELECT
customerNumber,
customerName,
creditLimit,
(
SELECT COALESCE(SUM(payments.amount), 0)
FROM payments
WHERE customers.customerNumber = payments.customerNumber
)
as totalOfPaymentsMade,
(
SELECT COALESCE(SUM(orderdetails.priceeach * orderdetails.quantityordered), 0)
FROM orders
LEFT JOIN orderdetails ON orderdetails.ordernumber = orders.ordernumber
WHERE customers.customerNumber = orders.customerNumber
)
as totalValueOfAllOrdersMade
FROM customers
) a
ORDER BY customerNumber

TSQL Combine Multiple rows and columns

I have a sql server 2008 stored proc with joins which returns multiple rows:
default ~/ / NULL NULL NULL Lorem
default ~/ / NULL NULL NULL Ipsum
I would like to return these two rows as one with the last column combined:
default ~/ / NULL NULL NULL Lorem, Ipsum
My Procedure is below
BEGIN
SET NOCOUNT ON;
SELECT
p.display_name AS Name,
p.url AS Url,
ISNULL(REPLACE(parentPage.url,'~','') + parentPage.alias,REPLACE(p.url,'~','')) as ParentPageURL,
p.page_image AS PageImage,
p.synopsis AS Synopsis,
p.metatitle AS Metatitle,
t.tag_name as tag
FROM page p
LEFT JOIN page parentPage on parentPage.id = p.parentid
LEFT JOIN page_features pf on p.id = pf.pageid and (pf.feature_type = 'TEXT' OR pf.feature_type = 'BLOG')
JOIN dbo.tag_collection_tag tc on tc.tag_collection_id = p.tag_collection_id
JOIN dbo.tag t on t.id = tc.tag_id
JOIN dbo.[Split](UPPER(#tags),',') Split on UPPER(t.tag_name) like '%' + split.Data + '%'
WHERE p.status = 'PUBLISHED'
AND p.include_in_nav = 1
AND (p.pagelevel = #level OR #level IS NULL)
AND (p.section_id = #sectionId OR #sectionId IS NULL)
ORDER BY ISNULL(REPLACE(parentPage.url,'~','') + parentPage.alias,REPLACE(p.url,'~',''))
END
Thank you.
Note if you have a column id in table page. It should be used instead of display_name
;WITH a as
(
SELECT
/*p.id,*/
p.display_name AS Name,
p.url AS Url,
ISNULL(REPLACE(parentPage.url,'~','') + parentPage.alias,REPLACE(p.url,'~','')) as ParentPageURL,
p.page_image AS PageImage,
p.synopsis AS Synopsis,
p.metatitle AS Metatitle,
t.tag_name as tag,
ISNULL(REPLACE(parentPage.url,'~','')
+ parentPage.alias,REPLACE(p.url,'~','')) XXX
FROM page p
LEFT JOIN page parentPage on parentPage.id = p.parentid
LEFT JOIN page_features pf on p.id = pf.pageid and (pf.feature_type = 'TEXT' OR pf.feature_type = 'BLOG')
JOIN dbo.tag_collection_tag tc on tc.tag_collection_id = p.tag_collection_id
JOIN dbo.tag t on t.id = tc.tag_id
JOIN dbo.[Split](UPPER(#tags),',') Split on UPPER(t.tag_name) like '%' + split.Data + '%'
WHERE p.status = 'PUBLISHED'
AND p.include_in_nav = 1
AND (p.pagelevel = #level OR #level IS NULL)
AND (p.section_id = #sectionId OR #sectionId IS NULL)
)
SELECT Name, Url, ParentPageURL, PageImage, Synopsis, Metatitle
,STUFF((
select ',' + [tag]
from a t1
-- I assume display_name is unique. I would use page.id,
-- but I am not sure you have that column
-- t1.id = t.id
where t1.display_name = t.display_name
for xml path(''), type
).value('.', 'varchar(max)'), 1, 1, '') [tags]
from a t
group by Name, Url, ParentPageURL, PageImage, Synopsis, Metatitle
ORDER BY XXX

The multi-part identifier "..." could not be bound

I get error (The multi-part identifier "f.FormID" could not be bound.) running this query:
select f.FormID, f.Title, fv.UserName
from Forms f join (
SELECT FormID
FROM Reports
WHERE (ReportID = #ReportID)
UNION
SELECT FormRelations.ForigenFormID
FROM FormRelations INNER JOIN
Forms ON FormRelations.ForigenFormID = Forms.FormID
WHERE (FormRelations.PrimaryFormID =
(SELECT FormID
FROM Reports
WHERE (ReportID = #ReportID)))
) ids
on f.FormID = ids.FormID
LEFT OUTER JOIN (select top 1 UserName, FormID from FormValues where FormID = f.FormID and UserName = #UserName) fv
ON f.FormID = fv.FormID
Please someone help me :(
#bluefeet:
I want such a result:
01304636-FABE-4A3E-9487-A14B012F9A61 item_1 1234567890
C0455E97-788A-4305-876A-A15000CFE928 item_2 1234567890
7719F37E-7021-4ABD-91ED-A15301830324 item_3 1234567890
If you need to use your alias inside of your subquery like that, you might want to look at using the APPLY operator:
select f.FormID, f.Title, fv.UserName
from Forms f
join
(
SELECT FormID
FROM Reports
WHERE (ReportID = #ReportID)
UNION
SELECT FormRelations.ForigenFormID
FROM FormRelations
INNER JOIN Forms
ON FormRelations.ForigenFormID = Forms.FormID
WHERE (FormRelations.PrimaryFormID = (SELECT FormID
FROM Reports
WHERE (ReportID = #ReportID)))
) ids
on f.FormID = ids.FormID
CROSS APPLY
(
select top 1 UserName, FormID
from FormValues
where FormID = f.FormID
and UserName = #UserName
) fv
Or you can use row_number():
select f.FormID, f.Title, fv.UserName
from Forms f
join
(
SELECT FormID
FROM Reports
WHERE (ReportID = #ReportID)
UNION
SELECT FormRelations.ForigenFormID
FROM FormRelations
INNER JOIN Forms
ON FormRelations.ForigenFormID = Forms.FormID
WHERE (FormRelations.PrimaryFormID = (SELECT FormID
FROM Reports
WHERE (ReportID = #ReportID)))
) ids
on f.FormID = ids.FormID
LEFT JOIN
(
select UserName, FormID,
ROW_NUMBER() over(PARTITION by FormID, UserName order by FormID) rn
from FormValues
where UserName = #UserName
) fv
on f.FormID = fv.FormID
and fv.rn = 1

sql server update many columns with a select from a join of a table variable and a db table

I have the classical person -> person attributes scheme.
So, like this: person(PK) <- person_attribute(FK)
What I need is a query to get one row where a person is joined with her attributes.
For example, to transform:
Person:
{ ID = 123456, Name = 'John Smith', Age = 25 }
Attributes:
1) { PersonID = 123456, AttributeTypeID = 'Height', AttributeValue = '6'6'''}
2) { PersonID = 123456, AttributeTypeID = 'Weight', AttributeValue = '220lbs'}
3) { PersonID = 123456, AttributeTypeID = 'EyeColor', AttributeValue = 'Blue'}
To:
PersonWithAttributes
{
ID = 123456, Name = 'John Smith', Age = 25, Height = '6'6''', Weight = '220lbs', EyeColor = 'Blue'
}
To make things worse my persons are in a table variable.
So, I have (in a sp with a parameter of person_id):
--result table
declare #people_info table
(
person_id int,
name nvarchar(max),
age int,
height nvarchar(10) null,
weight nvarchar(10) null,
eye_color nvarchar(16) null
)
insert into #people_info
select person_id, name, age, null, null, null
from dbo.HR.people where person_id = #person_id
update pi
set
pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Height'),
pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Weight'),
pi.eye_color = (select pa.attribute_value where pa.attribute_type_id = 'EyeColor')
from
#people_info pi
inner join dbo.HR.person_attributes pa on pi.person_id = pa.person_id
select * from #people_info
Which of course does not work for some reason.
If I query the two joined tables and select "pa.attribute_value where pa.attribute_type_id = 'someval'" I get the correct value. But the update does not work.
Of course, I can write this as three updates, but I am thinking that it will be faster to do one join and then to filter in the update clause.
Also, please keep in mind that my attributes are spread over three tables, not just the attributes table. So, this is why I have the table variable.
Any help is very welcome. Maybe I am going about this the wrong way. Performance matters. What is the most performant way to accomplish this?
Thank you very much.
Try this code for update with pivot:
update
pi
set
pi.height = pa.Height
pi.weight = pa.Weight
pi.eye_color = pa.EyeColor
from
#people_info pi
inner join
(
SELECT
person_id
,[Height] Height
,[Weight] Weight
,[EyeColor] EyeColor
FROM
(
SELECT
attribute_type_id
, attribute_value
, person_id
FROM
dbo.HR.person_attributes pa
) pa
PIVOT
(
MAX(attribute_value) FOR attribute_type_id IN ([Height],[Weight],[EyeColor])
)pvt
) pa
on
pi.person_id = pa.person_id
Maybe you want something like:
update pi
set
pi.height = paH.attribute_value,
pi.weight = paW.attribute_value,
pi.eye_color = paE.attribute_value
from
#people_info pi
inner join dbo.HR.person_attributes paH on pi.person_id = paH.person_id
and paH.attribute_type_id = 'Height'
inner join dbo.HR.person_attributes paW on pi.person_id = paW.person_id
and paW.attribute_type_id = 'Weight'
inner join dbo.HR.person_attributes paE on pi.person_id = paE.person_id
and paE.attribute_type_id = 'EyeColor'