How to write a query(may be a recursive one) to fetch parent-child hierarchy in Postgresql 8.4? - postgresql

I have a parent child hierarchy that is depicted as in the picture. There is one head office at the top which acts as the parent branch. The other nodes are child branches.
There can be any number of Regional zones and any number of divisional zones can be added to a Regional zone. Similarly, any number of child branches can be added to a divisional zone. Here, User login is provided at all the levels and there I'm facing a difficulty in showing only the branches that is child to the currently logged in users branch. The data base that I'm using is 'Postgresql8.4'. By Googling I found that recursion can be done. But quite frankly I didn't understand the steps followed in most of them. So can someone please help me in solving this piece of puzzle with explanation on the steps followed?
User Table
============
usr_id; //Unique id of user
usr_branch_id; //Id from the branch table
Branch Table
============
branch_id; //Unique id of branch
branch_text_id;
branch_name;
branch_parent;

you would want something like:
WITH RECURSIVE branch_tree AS (
select branch_id, branch_text_id, branch_name, branch_parent,
branch_id::text as path
from branch
where branch_parent is null
union all
select b.branch_id, b.branc_text_id, b.branch_name, b.branch_parent,
t.path || ',' || b.branch_id::text
FROM branch b
JOIN branch_tree t ON b.parent_id = t.branch_id
)
SELECT * FROM branch_tree ORDER BY string_to_array(path, ',')::int[];

Related

uSync Dropdown DataType Issue

I have 2 sites, dev branch and production. On my dev branch I'm using a couple of archetype DataType which are able to be configured by a simple dropdown with couple color names (like Green, Blue etc). I have like 300 published nodes which uses that datatype (required field). After I've exported "Full Export" in uSync dashboard and then copy/pasted the whole uSync folder to my production branch and clicked "Full Import". After import complete I check my nodes. All fields are there except that dropdown.
There are some old issues on the our.umbraco.com website stating the same issue, but they seem to been fixed.
I've posted there this issue, but has been 6 days, and the topic still is on manual approval.
Any idea what must be causing this issue?
I am using (uSync.BackOffice 4.0.16.0) (uSync.Core 6.0.15.0) [uSync.Content: 4.1.9.1]
Umbraco version 7.15.4
Thank you in advance.
Best regards,
Artur
PS. I am unable to create the USync label. Would appreciate if someone would edit with it!
I'd open up the database and look at the values in the cmsDataTypePreValues table. I suspect that uSync has changed the prevalues out from under you. Prevalues are really tricky and uSync does its best given the circumstances. You'll notice that the uSync .def files in Umbraco 7 don't record any unique ids on those prevalues. They just get inserted into the database and are assigned an id automatically. This means if the prevalues are deleted and recreated, they will have different ids. If those dropdown prevalues are being picked by the id field on the cmsDataTypePreValues record, that is probably what is going wrong.
Use the following query to see your prevalues for the datatype:
DECLARE #dataTypeNodeId AS INT = <dataTypeNodeId, INT, 0>
SELECT *
FROM cmsDataType dt
INNER JOIN cmsDataTypePreValues pv
ON pv.datatypeNodeId = dt.nodeId
WHERE dt.nodeId = #dataTypeNodeId
Compare the values you see here between the dev database and the production database. To help give you a better picture of what the data looks like, you could also run this query to see what the actual picked values look like. The query is so long because it is filtering out results from old versions. This might not work very well for you because your datatype is inside of Archetype. Hopefully it can still give you an idea about what selected dropdown values actually look like in the database.
DECLARE #dataTypeNodeId AS INT = <dataTypeNodeId, INT, 0>
SELECT TOP 1000 n.id
,n.[text]
,n.[path]
,pt.[alias] AS 'property alias'
,dtn.id AS 'Datatype Id'
,dtn.[text] AS 'Datatype Name'
,dtn.uniqueID AS 'Datatype Guid'
,d.templateId
,t.[alias] AS 'template alias'
,pd.dataInt
,pd.dataDate
,pd.DataNvarchar
,pd.dataNtext
,cv.VersionDate
FROM umbracoNode n
INNER JOIN cmsDocument d
ON d.nodeId = n.id
INNER JOIN cmsContentVersion cv
ON cv.VersionId = d.versionId
INNER JOIN cmsPropertyData pd
ON pd.contentNodeId = n.id AND pd.versionId = d.versionId
INNER JOIN cmsPropertyType pt
ON pt.id = pd.propertytypeid
INNER JOIN cmsDataType dt
ON dt.nodeId = pt.dataTypeId
INNER JOIN umbracoNode dtn
ON dtn.id = dt.nodeId
LEFT OUTER JOIN cmsTemplate t
ON t.nodeId = d.templateId
WHERE d.newest = 1
AND dtn.id = #dataTypeNodeId
You can try to manually fix the prevalue entries in the database. I've run into enough prevalue trouble with Umbraco dropdowns that I try to always use something like nuPickers to avoid picking prevalues by id.

How to make a mysqli statement between two tables

I have two tables in one MySQLI database. One is called 'actions' and one is called 'links'. But recently I have noticed that many links which I have transfered from my old system don't have any actions attached to it, and I want to count how many they are. So I want to count the amount of links whose id hasn't any entries in the actions table. How can I formulate the statement. There is one id in the links table and one link_id in the actions table.
You are looking for a outer join. without a lot of details, it would look something like:
SELECT * FROM links
LEFT JOIN actions on links.field = actions.field
WHERE actions.ID is NULL;
This will give you all the links records that don't have a corresponding record in actions.

TFS 2013 Kanban board Done column (multiple complete states)

In TFS 2013 Microsoft "fixed" a bug which allowed to map a WorkItem's state to the "Done" state in the Kanban board.
This feature was heavily used in our company. There is a petition to bring it back back but I don't think it will make it:
http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5589316-allow-multiple-complete-meta-state-mapping-in-tfs
In order to migrate TFS2012 to TFS2013 I would like to know where the customized "Done" state columns in TFS 2012 is stored in the database to create a report which shows which team used which WorkItem state as their "Done" state.
TFS2012 Kanban Board looked like that (note the dropdown):
TFS2013 Kanban Board looks like that (note NO dropdown):
I do have access to the TFS Collection database and I would like to create a SQL query which shows me all the customization of this column in TFS 2012.
How can I get for "My WorkItem" the for every Team Project and every Team the customized "Done" state in TFS2012 database?
What other tables do I need to link to in order to get those states?
So far I could only get the TeamId, Name, ColumnType ProjectId but not the effective WorkItem and the "Done" column customization. How can I do that?
SELECT
tbl_Board.TeamId,
tbl_Board.Revision,
tbl_BoardColumn.Name,
tbl_BoardColumn.ColumnType,
tbl_WorkItemTypeExtensions.Description,
tbl_BoardColumn.[Order],
tbl_WorkItemTypeExtensions.ProjectId
FROM
tbl_WorkItemTypeExtensions
RIGHT OUTER JOIN tbl_Board ON
tbl_WorkItemTypeExtensions.Id = tbl_Board.ExtensionId
LEFT OUTER JOIN tbl_BoardColumn ON
tbl_Board.Id = tbl_BoardColumn.BoardId
Experts do not recommend accessing TFS DB but you can use Tfs_WarehouseDatabase if Reporting is configured and Data from all project collections is collected and stored in tables that are optimized for reporting.
I do not have a knowledge about the db structures of TFS but going through few important online articles I managed understood quiet a few about it and as I understood the information that is required for you is in WorkItemsAretable.
Team Foundation Server Databases
Work item field and database schema reference
Stack overflow Question Access the Kanban Column (a Team-Specific Field) for a Work Item
With those queries below you can get the state of a certain work item on the Kanban board:
USE Tfs_DefaultCollection
SELECT TOP(10)
MarkerField + 1 as FieldId,
*
FROM tbl_WorkItemTypeExtensions with(nolock)
JOIN tbl_projects on tbl_WorkItemTypeExtensions.ProjectId = tbl_projects.project_id
WHERE tbl_projects.project_name LIKE '%ProjectName%
Copy the result from "FieldId" column to below's query at position XXXXXXXX
SELECT TOP 1000
wid.Id,
wia.State,
wid.StringValue as Kanban,
wia.[Work Item Type],
wia.Title,
tn.Name as Iteration
FROM tbl_WorkItemData wid with(nolock)
JOIN WorkItemsAre wia on wia.ID = wid.Id
JOIN TreeNodes tn on wia.IterationID = tn.ID
WHERE FieldId = XXXXXXXX and RevisedDate = '9999-01-01 00:00:00.000'
ORDER BY Id
Create a Detailed Report using Report Designer
Hope the sources that I have provided above will help your problem!
I contacted the Microsoft Support and they provided me the following answer to my question:
SELECT
board.TeamId,
boardColumn.Name,
workItemTypeExtensions.Rules
FROM
tbl_Board board JOIN
tbl_WorkItemTypeExtensions workItemTypeExtensions ON board.ExtensionId = workItemTypeExtensions.Id JOIN
tbl_projects projects ON workItemTypeExtensions.ProjectId = projects.project_id JOIN
tbl_BoardColumn boardColumn ON board.Id = boardColumn.BoardId
WHERE
projects.project_name LIKE '%< ENTER YOUR PROJECT NAME HERE >%' AND
boardColumn.ColumnType = 2
ORDER BY
board.TeamId,
boardColumn.[Order]
When I check XML in the "Rules" column there I can find exactly what I was looking for.

changing a record to a different area

I hope yo can point me in the right direction.
I have a SSRS report organized by Continent/Country/Customer and I need to change (force?) some of the customers to appear in a different region/country from the DB. ie:
I have a local NZ customer in the right Region/Country (australasia/New Zealand) but I want this one to show up in a different Region/Country namely (Asia/China) as this customer buys locally but exports all to China.
There are some others customers that needs to be manually organized, following the same criteria, of course.
any idea on how can I do this?
and will be the best option to do it through SQL-Server or SSRS?
Thanks in advance.
Eric
I would create a new table called something like AreaOverride with the following columns:
CustomerId
Continent
Country
Then link to this in your query using a left outer join and replace the Continent and Country with the overridden values if they exist:
Select CustomerId,
Case when AreaOverride.Continent is not null then AreaOverride.Continent else Customer.Continent end as Continent,
Case when AreaOverride.Country is not null then AreaOverride.Country else Customer.Country end as Country
From Customer
Left outer join AreaOverride On AreaOverride.CustomerId = Customer.CustomerId
You might want to make this a view if you are going to use it in several reports or other places.
Then whenever you need to override a customer's details you simply add a row for them in this table and the values will be overridden in your reports.
Note that if you are dealing with a vendor database rather than your own you don't want to be messing with their database structure but you can still do this by creating a new database and put the AreaOverride table in it. Then you add the database name to your join. For example if your database was called MyStuff then your join looks like this:
Left outer join MyStuff.dbo.AreaOveride On ...

T-SQL Trigger After Delete

Situation: Let's say you have a database of organizations, students, and course purchases, and an administrator wants to get a refund for a course they assigned out. The software doesn't support refunds by default. You don't have access to the source code, but you can set up triggers in the database.
Database Structure:
organizationsorganization_idaccount_balance
studentsstudent_idorganization_id
purchasesstudent_idtime_of_purchaseamount_to_refund
General idea is that, when an entry (or more than one!) is removed from the purchases table, a trigger runs to update the account balance of the appropriate organization. amount_to_refund can be null or zero, so no refund is given in that case. Refunds should also not be given if time_of_purchase was more than 30 days ago.
Any ideas how to pull this off? I've been modeling it off of another trigger, but am getting thrown off by the UPDATE ... FROM ... syntax, which I can't say I've used before. I have looked at MSDN, but I'm a bit overwhelmed.
Optionally, I'd also like to insert rows into another table (not documented here) containing the refund amount and organization ID. I just need a general idea where this fits in and can probably handle the rest myself.
CREATE TRIGGER [dbo].[TrgPurchasesDelete] ON [dbo].[Purchases] FOR DELETE
AS
BEGIN
UPDATE
[dbo].[Organizations]
SET
account_balance = account_balance + DLTD.ammount_to_refund
FROM
[dbo].[Organizations] ORGA
INNER JOIN [dbo].[Students] STDT ON
STDT.organization_id = ORGA.organization_id
INNER JOIN DELETED DLTD ON
DLTD.student_id = STDT.student_id
WHERE
DLTD.ammount_to_refund > 0
AND DLTD.time_of_purchase > DATEADD(DAY, -30, SYSDATE)
END
GO