nested query for same table oracle - oracle

We have a table where we maintain the menu and submenus for the main menu,
for ex, I have the main menu called Warehouse->and submenus Admin and USer and user-profile and edit profile are nested menus for each submenu.
we have a column called function id and parent id to differentiate menu and its submenus suppose say functionid for a warehouse is 5 admin parent id will be 5 and function id of admin will be parent id for user-profile and edit profile, where functionid is unique for all and parentid will act as foreign key in the same table.
now I need to update the column say URL for only user profile which is under admin->warehouse. first, I tried with a select query like
select * from TIFX_CM_MENU_MAIN t1 where function='User Profile' and t1.PARENTID in
(select t2.FUNCTIONID from TIFX_CM_MENU_MAIN t2 where t2.function='Admin' and t2.PARENTID in
(select t3.FUNCTIONID from TIFX_CM_MENU_MAIN t3 where t3.function='WareHouse Operator')) ;
but looks some issue in the query, can anyone help me to get the expected result.
Thank you

Might be easier to understand and maintain this query (though it's might be a bit slower):
select t1.*
from TIFX_CM_MENU_MAIN t1
left join TIFX_CM_MENU_MAIN t2 on t1.PARENTID = t2.FUNCTIONID
left join TIFX_CM_MENU_MAIN t3 on t2.PARENTID = t3.FUNCTIONID
where t1.function = 'User Profile' and
t2.function = 'Admin' and
t3.function = 'WareHouse Operator';

Related

can i set up an SSRS report where users input parameters to a table

I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id

Query to find all views/charts owned or shared with a user in Dynamics 2013

Is there a way to see all views / charts owned or shared with a user in Dynamics 2013? (DB query would be fine. I can access the base tables if needed.
#Ryan 's solution helped me quite a bit but was painful to transcribe. This is the solution in a more usable format (with minor edits):
DECLARE #userid varchar(100)
SELECT #userid = 'domain\user'
-- Get team membership for the user
IF OBJECT_ID(N'tempdb.dbo.#UserAndTeams') IS NOT NULL DROP TABLE #UserAndTeams
SELECT DISTINCT t.TeamId TeamOrUserId, t.[Name]
INTO #UserAndTeams
FROM
TeamBase t
INNER JOIN TeamMembership tm ON t.TeamId = tm.TeamId
INNER JOIN SystemUserBase su ON su.SystemUserId = tm.SystemUserId
WHERE
su.DomainName = #userid
INSERT INTO #UserAndTeams(TeamOrUserId, [Name])
SELECT su.SystemUserId, su.Firstname + ' ' + su.Lastname [Name]
FROM SystemUserBase su WHERE su.DomainName = #userid
-- Get a union of all teams and this user
IF OBJECT_ID(N'tempdb.dbo.#AllUsersAndTeams') IS NOT NULL DROP TABLE #AllUsersAndTeams
SELECT t.TeamId TeamOrUserId, t.[Name]
INTO #AllUsersAndTeams
FROM TeamBase t
INSERT INTO #AllUsersAndTeams(TeamOrUserId, [Name])
SELECT su.SystemUserId, su.Firstname + ' ' + su.Lastname [Name]
FROM SystemUserBase su
-- Extract share info from POA for selected entity types
SELECT DISTINCT su.[Name] SharedWith, objectIds.[Type], objectIds.[Name] Objectname, aut.[Name] Ownername
FROM
PrincipalobjectAccess poa WITH (NOLOCK)
INNER JOIN #UserAndTeams su WITH (NOLOCK) ON poa.PrincipalId = su.TeamOrUserId
INNER JOIN
(
SELECT 'Chart' [Type], UserQueryVisualizationId id, [Name], OwnerId FROM UserQueryVisualizationBase WITH (NOLOCK)
UNION SELECT 'View', UserQueryId id, [Name], OwnerId FROM UserQueryBase WITH (NOLOCK)
UNION SELECT 'Report', ReportId, [Name], OwnerId FROM Report WITH (NOLOCK)
UNION SELECT 'Dashboard', uf.userFormId id, [Name], OwnerId FROM UserForm uf WITH (NOLOCK) WHERE uf.[Type] = 0
) objectIds ON poa.ObjectId = objectIds.id
INNER JOIN #AllUsersAndTeams aut ON objectIds.OwnerId = aut.TeamOrUserId
ORDER BY
objectIds.[Type], objectIds.[Name]
I came up with this procedure:
Which works well for views/dashboards and charts. If a user can access an item via team membership the team name is displayed; if it's been shared directly with the user, the users' name is displayed.
It needs to be run for a user with access to the base tables.
(I can't post the full SQL to Stack Overflow! It's containted in the attached image.)
This is actually trickier then you might think - The information about shared views is contained within the principalobjectaccess table in database.
This table contains the object that's being shared, who it's being shared with, and what permissions they have on that object.
The hard part is that the guid that contains the object being shared does not also specify what object type is being shared as well. It might be possible to create a query that joins the POA table to the views created, and then put in a where clause for that particular user... but be careful and don't run this against a production system (and if you have to make sure to use a (nolock) on the POA table.)
Sorry I couldn't be more help, but I'm not using On-prem and cant test a query.

How to JQuery PHP AJAX filter based on multiple checkbox product

I am working on a job site on which jobs will have categories titles like, salary, Sector, Location etc. The idea is to enable a visitor to filter out posts by selecting appropriate checkboxes for all these three categories and show the posts which contains category parameters chosen by the user. Here is an example of what i am looking for : http://underwearking.nl/heren/ (see the left sidebar)
I have gone through over 200 plugins and searched wordpress thoroughly but i am unable to find any plugin which fulfills this. Also, I don't know much about coding but i know that it involves something like integration of jQuery Ajax and checkboxes... Can someone please help me???
Try UNION ALL:::
select distinct wp_usermeta.user_id, wp_users.user_nicename
FROM wp_usermeta
LEFT JOIN wp_users
ON wp_usermeta.user_id=wp_users.ID
ORDER BY wp_usermeta.user_id
UNION ALL
select distinct t1.user_id ,t2.meta_value shop from wp_usermeta as t1, wp_usermeta as t2 where t1.user_id = t2.user_id and ( (t1.meta_key = 'category' and t1.meta_value like '%$catid%') ) and t2.meta_key='shop_name' order by t2.meta_value ASC
You can an extra JOIN to that table p_usermeta in the same query, then LEFT JOIN the wp_users the same way you did, something like this:
SELECT distinct
t1.user_id,
wp_users.user_nicename,
t2.meta_value shop
FROM wp_usermeta AS t1
INNER JOIN wp_usermeta as t2 ON t1.user_id = t2.user_id
LEFT JOIN wp_users ON t1.user_id = wp_users.ID
where ((t1.meta_key = 'category' and t1.meta_value like '%$catid%'))
and t2.meta_key='shop_name'
ORDER BY wp_usermeta.user_id, t2.meta_value
LIMIT 1,10"

For some products, in default scope, only root category is chooseable?

just stumbled across a weird Magento problem.
Some products in the backend only has "root" as a chooseable category in the categories tab... but all categories appear if i choose a storefront as scoope.
While some other products has the whole tree chooseable in the default scope.
Index & cache has been reset.
Any clues? Please :)
you can find the solution here:
http://zaclee.net/magento/errors-magento/magento-product-edit-only-shows-default-category
Greetz!
Theres another solution in comments there. I changed it to find the categories with inconsistent children count without modifying them in the first place:
select
entity_id,
children_count,
(select (SELECT count(*) from catalog_category_entity u1
where u1.path REGEXP CONCAT('^',u2.path,'/[[:digit:]]+$'))
from catalog_category_entity u2
where u2.path = w1.path)
as actual_children_count
from catalog_category_entity w1
having children_count != actual_children_count
After you examine the offending records, run the update by Tim Schmidt:
CREATE TABLE catalog_category_entity_test AS SELECT * FROM catalog_category_entity;
UPDATE catalog_category_entity w1
SET children_count = (
select (SELECT count(*) from catalog_category_entity_test u1
where u1.path REGEXP CONCAT('^',u2.path,'/[[:digit:]]+$'))
from catalog_category_entity_test u2 where u2.path = w1.path)
DROP TABLE catalog_category_entity_test;

Rownum in the join condition

Recently I fixed the some bug: there was rownum in the join condition.
Something like this: left join t1 on t1.id=t2.id and rownum<2. So it was supposed to return only one row regardless of the “left join”.
When I looked further into this, I realized that I don’t understand how Oracle evaluates rownum in the "left join" condition.
Let’s create two sampe tables: master and detail.
create table MASTER
(
ID NUMBER not null,
NAME VARCHAR2(100)
)
;
alter table MASTER
add constraint PK_MASTER primary key (ID);
prompt Creating DETAIL...
create table DETAIL
(
ID NUMBER not null,
REF_MASTER_ID NUMBER,
NAME VARCHAR2(100)
)
;
alter table DETAIL
add constraint PK_DETAIL primary key (ID);
alter table DETAIL
add constraint FK_DETAIL_MASTER foreign key (REF_MASTER_ID)
references MASTER (ID);
prompt Disabling foreign key constraints for DETAIL...
alter table DETAIL disable constraint FK_DETAIL_MASTER;
prompt Loading MASTER...
insert into MASTER (ID, NAME)
values (1, 'First');
insert into MASTER (ID, NAME)
values (2, 'Second');
commit;
prompt 2 records loaded
prompt Loading DETAIL...
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (1, 1, 'REF_FIRST1');
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (2, 1, 'REF_FIRST2');
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (3, 1, 'REF_FIRST3');
commit;
prompt 3 records loaded
prompt Enabling foreign key constraints for DETAIL...
alter table DETAIL enable constraint FK_DETAIL_MASTER;
set feedback on
set define on
prompt Done.
Then we have this query :
select * from master t
left join detail d on d.ref_master_id=t.id
The result set is predictable: we have all the rows from the master table and 3 rows from the detail table that matched this condition d.ref_master_id=t.id.
Result Set
Then I added “rownum=1” to the join condition and the result was the same
select * from master t
left join detail d on d.ref_master_id=t.id and rownum=1
The most interesting thing is that I set “rownum<-666” and got the same result again!
select * from master t
left join detail d on d.ref_master_id=t.id and rownum<-666.
Due to the result set we can say that this condition was evaluated as “True” for 3 rows in the detail table. But if I use “inner join” everything goes as supposed to be.
select * from master t
join detail d on d.ref_master_id=t.id and rownum<-666.
This query doesn’t return any row,because I can't imagine rownum to be less then -666 :-)
Moreover, if I use oracle syntax for outer join, using “(+)” everything goes well too.
select * from master m ,detail t
where m.id=t.ref_master_id(+) and rownum<-666.
This query doesn’t return any row too.
Can anyone tell me, what I misunderstand with outer join and rownum?
ROWNUM is a pseudo-attribute of result sets, not of base tables. ROWNUM is defined after rows are selected, but before they're sorted by an ORDER BY clause.
edit: I was mistaken in my previous writeup of ROWNUM, so here's new information:
You can use ROWNUM in a limited way in the WHERE clause, for testing if it's less than a positive integer only. See ROWNUM Pseudocolumn for more details.
SELECT ... WHERE ROWNUM < 10
It's not clear what value ROWNUM has in the context of a JOIN clause, so the results may be undefined. There seems to be some special-case handling of expressions with ROWNUM, for instance WHERE ROWNUM > 10 always returns false. I don't know how ROWNUM<-666 works in your JOIN clause, but it's not meaningful so I would not recommend using it.
In any case, this doesn't help you to fetch the first detail row for each given master row.
To solve this you can use analytic functions and PARTITION, and combine it with Common Table Expressions so you can access the row-number column in a further WHERE condition.
WITH numbered_cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY d.something) AS rn
FROM master t LEFT OUTER JOIN detail d ON d.ref_master_id = t.id
)
SELECT *
FROM numbered_cte
WHERE rn = 1;
if you want to get the first three values from the join condition change the select statement like this.
select *
from (select *
from master t left join detail d on d.ref_master_id=t.id)
where rownum<3;
You will get the required output. Take care on unambigiously defined column names when using *
Let me give an absolute answer which u can run directly with out making any changes to the code.
select *
from (select t.id,t.name,d.id,d.ref_master_id,d.name
from master t left join detail d on d.ref_master_id=t.id)
where rownum<3;
A ROWNUM filter doesn't make any sense in a join, but it isn't being rejected as invalid.
The explain plan will either include the ROWNUM filter or exclude it. If it includes it, it will apply the filter to the detail table after applying the other join condition(s). So if you put in ROWNUM=100 (which will never be satisfied) all the detail rows are excluded and then the outer join kicks in.
If you put in ROWNUM=1 it seems to drop the filter.
And if you query
with
a as (select rownum a_val from dual connect by level < 10),
b as (select rownum*2 b_val from dual connect by level < 10)
select * from a left join b on a_val < b_val and rownum in (1,3);
you get something totally weird.
It probably should be rejected as an error, so expect nonsensical things to happen

Resources