query2 = "SELECT k.key_tag, k.key_avail
FROM keyinfo k
JOIN keyingroup kig ON k.key_id = kig.key_id
WHERE kig.group_id = (
SELECT uig.group_id
FROM useringroup uig
JOIN users u ON uig.user_id = u.user_id
WHERE u.user_csn = '" & GV.userCSN & "')"
keyingroup table
useringroup table
Other than the 2 tables shown in the images, there is a keyinfo table, a accessgroup table and a users table. The idea here is that the 2 images is made as a 'many to many' relation table. Where in the table in image 1 is to show that a key can be accessed by multiple access groups while in the second image users can have multiple access groups and as long as a key falls within that access group, the user has permission to draw/loan it. The SQL statement above is what I have right now but it is not returning any rows at all and I have no idea if the way I'm querying is even right. Can someone provide some pointers as to what could be wrong and what I should have done?
Related
I have created a method for fetching data from tables.
My importing parameters are IV_RCP_NO, IV_VERS, IV_ALT and exporting parameter is ET_TABLE.
Now I am using INNER JOIN for joining 4 tables but the requirement says it should be done using FOR ALL ENTRIES clause.
Can someone please help me out to proceed with it?
Here is my code:
SELECT RCP_TBL~SEARCH_TERM PLNT_TBL~PLANT DESCR_TBL~DESCR RCP_STATUS~DESCR
INTO TABLE ET_TABLE
FROM ESTRH AS ESTRH_TBL
INNER JOIN /PLMB/RCP_RECIPE AS RCP_TBL
ON ESTRH_TBL~RECN = RCP_TBL~SUBRECN
INNER JOIN /PLMB/SAM_NODE_T AS RCP_STATUS
ON RCP_STATUS~STATUS_ID = RCP_TBL~STATUS
INNER JOIN /PLMB/RCP_PLNT AS PLNT_TBL
ON RCP_TBL~RCP_GUID = PLNT_TBL~RCP_GUID
INNER JOIN /PLMB/RCP_DESCR AS DESCR_TBL
ON PLNT_TBL~RCP_GUID = DESCR_TBL~OBJECT_GUID
AND RCP_TBL~RCP_GUID = DESCR_TBL~OBJECT_GUID
WHERE SUBID = IV_RCP_NO AND ALT_NO = IV_ALT AND VERS_NO = IV_VERS.
If the join can be done without for all entries then do that. This will yield best performance.
For all entries is a tool to simplify queries where you already have part of the data in an internal table, from some previously executed code or pre-computation. It effectively results in a series of independent selects whose results are merged after individual completion - meaning this results in multiple database roundtrips, which can drastically worsen performance.
If you have specific requirements to apply the for all entries, you should clarify which part of the data is already there and needs to be joined that way. Otherwise any suggestion from StackOverflowers will remain inefficient guesswork.
You can refer the below code-
* Local structure
TYPES: BEGIN OF ty_estrth,
subid TYPE esesubid,
recn TYPE eserecn,
END OF ty_estrth.
* Internal table
DATA: lt_estrh TYPE STANDARD TABLE OF ty_estrth.
SELECT subid recn FROM estrh
INTO TABLE lt_estrh
WHERE subid = iv_rcp_no.
IF lt_estrh IS NOT INITIAL.
SELECT * FROM /plmb/rcp_recipe
INTO TABLE lt_rcp_recipe "Internal table of type /PLMB/RCP_RECIPE
FOR ALL ENTRIES IN lt_estrh
WHERE subrecn = lt_estrh-recn
AND alt_no = iv_alt AND vers_no = iv_vers.
IF lt_rcp_recipe IS NOT INITIAL.
SELECT * FROM /plmb/sam_node_t
INTO TABLE lt_sam_node_t "Internal table of type /PLMB/SAM_NODE_T
FOR ALL ENTRIES IN lt_rcp_recipe
WHERE status_id = lt_rcp_recipe-status.
SELECT * FROM /plmb/rcp_plnt
INTO TABLE lt_rcp_plnt " Internal table of type /PLMB/RCP_PLNT
FOR ALL ENTRIES IN lt_rcp_recipe
WHERE rcp_guid = lt_rcp_recipe-rcp_guid.
SELECT * FROM /plmb/rcp_descr
INTO TABLE lt_rcp_descr " Internal table of type /PLMB/RCP_DESCR
FOR ALL ENTRIES IN lt_rcp_recipe
WHERE rcp_guid = lt_rcp_recipe-rcp_guid.
ENDIF.
ENDIF.
You'll get your data in below internal tables
lt_rcp_recipe
lt_sam_node_t
lt_rcp_plnt
lt_rcp_descr
Better declare a local structure with specific fields which you want to read, as i declared above. After that you have to read and fill data in exporting table ET_TABLE.
I would like to transform this query in a view that I can call passing the UserId as a parameter to select all groups (including subtrees of those groups) related to that user.
In fact I have 2 tables:
"EVA_Roles" which contains the roles tree, defined through a materialized path in the RoleHid field as varchar(8000)
"EVA_UsersInRoles" which related users to roles through the field UserId
Problem here is that only some roles may be related to the user in the EVA_UsersInRoles table, but maybe those roles are parents to other roles in the tree hierarchy so I have to retrieve multiple subtrees for each user.
Finally I came up with this query which seems to work fine, but I would like to transform it in a View. The problem I'm facing of course is that the UserId parameter, which is the one I would use to filter the view results, is inside the subquery.
Any hint to refactor this into a view?
SELECT A.RoleId, E.EndDate FROM EVA_Roles A INNER JOIN EVA_Roles B ON
A.RoleHid LIKE B.RoleHid + '%' AND B.RoleHid IN (SELECT RoleHid FROM EVA_Roles C
LEFT JOIN EVA_UsersInRoles D ON C.RoleId = D.RoleId WHERE
(D.Userid = #0 OR C.RoleId = #1) AND C.ApplicationId = #2)
LEFT JOIN EVA_UsersInRoles E ON A.RoleId = E.RoleId AND E.UserId = #0 WHERE
A.ApplicationId = #2 ORDER BY A.RoleId
I left parameters where I should pass values to the view. I think it may be impossible to refactor in a view. It was just to exploit my micro-ORM (PetaPoco) in a more friendly way, otherwise I have to use the SQL in my code but it's ok, don't loose your mind on this.
About the tables definition:
EVA_Roles
RoleId INT - Primary Key
RoleHid VARCHAR(800) - Here I store the materialized path of the tree using nodes
ids... An example on this later.
RoleLevel INT - Security level of the role
RoleName INT - Name of the role (admin, guest, ...)
ApplicationID INT - Id of the application (in a multi app scenario)
EVA_UsersInRoles
RoleId - INT
UserId - INT
The materialized path in RoleHid follows this logic. Consider this data where RoleId 2 is child of RoleId 1:
RoleId 1
RoleHid "1."
RoleId 2
RoleHid "1.2."
With the query above I'm able to retrieve all subtrees tied to a specific user and application.
I have a very simple database. It contains 3 tables. The first is the primary input table where values go in. The 2nd and 3rd are there purely for translating values to names for a view. When I view the rows in table 1, I can see that column businessUnit contains valid values in all rows. When I add the Business_Units table (The 3rd table in this DB), all but 2 rows go away and despite the businessUnit value both being 1 in the 1st table, the view gives them different names.
I created a DB diagram and uploaded a screenshot to imgur. Link: http://imgur.com/jXF7L1R
I only have 2 relationships in the table. One from equipType on New_Equipment_User to id in Equipment_Type and one from businessUnit in New_Equipment_User to id in Business_Units. The weird thing is that the Equipment Type works perfectly, yet when I replicate the table, relationship and view information exactly, it doesn't work. Instead of 6 rows appearing, there are only 2 which share the same value in businessUnit, but gives 2 different names for it.
In case it matters, here is my view Query:
SELECT dbo.New_Equipment_User.id, dbo.Equipment_Type.name AS equipType, dbo.New_Equipment_User.jobNumber, dbo.New_Equipment_User.costCode,
dbo.New_Equipment_User.reason, dbo.New_Equipment_User.mobile, dbo.New_Equipment_User.mobileQty, dbo.New_Equipment_User.mobileComment,
dbo.New_Equipment_User.laptop, dbo.New_Equipment_User.laptopQty, dbo.New_Equipment_User.laptopComment, dbo.New_Equipment_User.desktop,
dbo.New_Equipment_User.desktopQty, dbo.New_Equipment_User.desktopComment, dbo.New_Equipment_User.modem, dbo.New_Equipment_User.modemQty,
dbo.New_Equipment_User.modemComment, dbo.New_Equipment_User.printer, dbo.New_Equipment_User.printerQty, dbo.New_Equipment_User.printerComment,
dbo.New_Equipment_User.camera, dbo.New_Equipment_User.cameraQty, dbo.New_Equipment_User.cameraComment, dbo.New_Equipment_User.dateRequired,
dbo.New_Equipment_User.requestedBy, dbo.New_Equipment_User.dateRequested, dbo.New_Equipment_User.approvalStatus,
dbo.Business_Units.name AS businessUnit
FROM dbo.New_Equipment_User
JOIN dbo.Equipment_Type ON dbo.New_Equipment_User.equipType = dbo.Equipment_Type.id
JOIN dbo.Business_Units ON dbo.New_Equipment_User.id = dbo.Business_Units.id
WHERE (dbo.New_Equipment_User.approvalStatus = '0')
And here is an image of the view since it is easier to read: http://imgur.com/pZ97ehQ
Is anyone able to assist with why this might be happening?
Try using a LEFT JOIN
SELECT ...
FROM dbo.New_Equipment_User
JOIN dbo.Equipment_Type ON dbo.New_Equipment_User.equipType = dbo.Equipment_Type.id
LEFT JOIN dbo.Business_Units ON dbo.New_Equipment_User.id = dbo.Business_Units.id
This will ensure that all dbo.New_Equipment_User and all dbo.Equipment_Type is present
I have the following DB Tables with SQL Server
Booking(bookingID,customerName,branchID,RefNumber,...)
Trip(TripID,vehicleID,...)
BookingToTripMap(TripID,bookingID)
Branch(branchID, branchName)
Vehicle(vehicleID,vehicleNumber)
There is a one to one relationship between (Booking,Branch) and (Trip, Vehicle) and Many to many relationship between Booking, Trip which is saved in the table BookingToTripMap.
Now I want to extract a query that would return the following
Booking.RefNumber Booking.CustomerName Vehicle.VehicleNumber
(All vehicle numbers in one cell)
Here is your query
SELECT B.RefNumber, B.CustomerName, V.VehicleNumber
FROM ((Booking AS B INNER JOIN BookingToTripMap AS BT
ON B.bookingID = BT.bookingID) INNER JOIN TRIP as T
ON T.TripID = BT.TripID) INNER JOIN Vehicle as V
ON V.vehicleID = T.vehicleID
I would add the field bookingID to the table Trip, it seems that the table BookingToTripMap doesn't add any value to your database.
Also, if your vehicle's numbers are unique, you could change the primary key in the Vehicle table to vehicleNumber, and change the same columns in the Trip table. Thus you could retrieve the vehicleNumber directly from the Trip table.
I'm just guessing in that, based on the given information.
Regards,
Using SQL Server 2008, I am trying to figure out how to create a query that returns a pivot table with a standard many-to-many relationship. This relationship defines which users belong to which roles and I want the table to list the user name along the side and the role names across the top. The ultimate goal is to have this output in SQL Server Reporting Services, so it doesn't matter if SQL server generates the pivot results, or SSRS generates the results (is one method better than the other?). Here is my sample schema:
Users Table:
UserID
UserName
Rights Table:
RightID
RightName
RightsMembership Table:
UserID
RightID
I want the following output as a report in SSRS. Any help is appreciated.
RightOne RightTwo RightThree RightFour
jdoe X X
mjane X X
ssmith X X X
FYI: Roles can be added, so I would prefer not to have to hard code the role name or count in the query.
I Don't Know about the Pivot Tables but you can achieve this in the SSRS as below
Select UserName,
RightName
From users u INNER JOIN RightMembership rm on rm.UserID = u.UserID
INNER JOIN Rights r on rm.RightID = r.RightID
Use this query as the Stored Procedure or Query and the Order By Accordingly and
Create the Dataset and Datasource
Insert matrix in the report
In the rows select the UserName field From the Created Dataset
In the columns select the RightName field from the created Dataset
In the Data use this Expression below then you will get the desired output
=IIF(Fields!UserName.Value = nothing,nothing,"X") As the Data in the matrix