Compare Combinations in SQL Server - sql

I need to compare two attributes in a table as a single combination.
Basically I have this table in my database with these attributes and some sample data.
Rights => RoleID,SectionID,RightID
1 2 3
1 3 5
1 5 7
2 3 5
2 1 6
so I want to pass the sectionID and RightID information belonging to a certain roleID as input in my stored procedure and return the ROleID. For example when I pass the combinations (3,5) and (1,6) my stored procedure should return RoleID 2. If there is no exact match with the combinations I pass as my input, it shouldn't return anything.
Any help in this regard is highly appreciated.

I haven't tested this but you might able to use this or amend as appropriate. The idea is that you put the joined values into temp column then use that as basis for calling the procedure. If that fails you might want to add another column to your table with the joined roleid and sectioned?
CREATE PROCEDURE GETRIGHTS
(
#param varchar(10)
)
AS
SELECT * INTO #RIGHTS FROM TABLENAME
ALTER TABLE #RIGHTS ADD JOINEDVALUES INT
SELECT #RIGHTS.JOINEDVALUE = (SELECT ROLEID + ',' + SECTIONID FROM TABLENAME WHERE #RIGHTS.JOINEDVALUE = #PARAMS)

Related

union table, change serial primary key, postgresql

Postgresql:
I have two tables 'abc' and 'xyz' in postgresql. Both tables have same 'id' columns which type is 'serial primary key;'.
abc table id column values are 1,2,3 and also xyz id column containing same values 1,2,3,4
I want to union both tables with 'union all' constraint. But I want to change 'xyz' id column values to next value of 'abc' id column last value as 1,2,3,4,5,6,7
select id from abc
union all
select id from xyz
|id|
1
2
3
1
2
3
4
my wanted resuls as
|id|
1
2
3
4
5
6
7
BETTER - Thanks to #CaiusJard
This should do it for you
select id FROM abc
UNION ALL select x.id + a.maxid FROM xyz x,
(SELECT MAX(id) as maxid from abc) a
ORDER BY id
For anyone who's doing something like this:
I had a similar problem to this, I had table A and table B which had two different serials. My solution was to create a new table C which was identical to table B except it had an "oldid" column, and the id column was set to use the same sequence as table A. I then inserted all the data from table B into table C (putting the id in the oldid field). Once I fixed the refernces to point to from the oldid to the (new)id I was able to drop the oldid column.
In my case I needed to fix the old relations, and needed it to remain unique in the future (but I don't care that the ids from table A HAVE to all be before those from table C). Depending on what your trying to accomplish, this approach may be useful.
If anyone is going to use this approach, strictly speaking, there should be a trigger to prevent someone from manually setting an id in one table to match another. You should also alter the sequence to be owned by NONE so it's not dropped with table A, if table A is ever dropped.

SQL adding columns together

I have a PostgreSQL table of 7k records. each record has 3 a unique ID and 3 fields with it. childcares, shcools, hospitals. there are all integer fields. i want to add a new column and calculate the total amount of receptors (schools,childcares,hospitals) for each row. I thought this should be pretty straighforward with adding a column and doing an insert with a select but i am not getting the results i want
alter table site add total integer;
insert into site(total) select sum(schools+childcares+hospitals) as s from site;
i have also tried a group by id in the insert select statement
You are looking for Update not Insert
Update site
set total = COALESCE(schools,0)+COALESCE(childcares,0)+COALESCE(hospitals,0)
Added COALESCE to handle NULL values.
Ex :
1 + 2 + NULL = NULL so to replace NULL with 0 I have used COALESCE.
Now it will be 1 + 2 + 0(NULL) = 3

Update SQL column with sequenced number

I have a table SL_PROD which has the following columns, NUMBER, DEPTCODE, DISP_SEQ AND SL_PROD_ID.
SL_PROD_ID is an identity column which incrementally increases with each row.
I need to write a query which updates the DISP_SEQ column with sequential numbers (1-X) for the rows which have a DEPTCODE of '725'. I've tried several things with no luck, any ideas?
Try this:
A common table expression can be used in updates. This is extremely usefull, if you want to use the values of window functions (with OVER) as update values.
Attention: Look carefully what you are ordering for. I used NUMBER but you might need some other sort column (maybe your IDENTITY column)
CREATE TABLE #SL_PROD(NUMBER INT,DEPT_CODE INT,DISP_SEQ INT,SL_PROD_ID INT IDENTITY);
INSERT INTO #SL_PROD(NUMBER,DEPT_CODE,DISP_SEQ) VALUES
(1,123,0)
,(2,725,0)
,(3,725,0)
,(4,123,0)
,(5,725,0);
WITH UpdateableCTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY NUMBER) AS NewDispSeq
,DISP_SEQ
FROM #SL_PROD
WHERE DEPT_CODE=725
)
UPDATE UpdateableCTE SET DISP_SEQ=NewDispSeq;
SELECT * FROM #SL_PROD;
GO
--Clean up
--DROP TABLE #SL_PROD;
The result (look at the lines with 725)
1 123 0 1
2 725 1 2
3 725 2 3
4 123 0 4
5 725 3 5

Single SQL Query to validate input values

I have written a Stored procedure which has three argumets
#UserID1 BIGINT
#UserID2 BIGINT
#UserID3 BIGINT
What I want to achieve is to write a single SQL query against table dbo.aspnet_UsersInRoles column ID so that #UserID1, #UserID2, #UserID3 are present in ID column of dbo.aspnet_UsersInRoles.
For example, I have received three values in variables and I want to confirm they are part of Id column. i.e. ID column of table has values 1,2,3,4,5,6,7,8,9,10 and UserID1 is 2, UserID2 is 5 and UserID3 is 7. So it should give true else false.
I can implement using three different queries but I am not getting any clue to do it in a single query.
You should be able to use something like this:
SELECT CASE WHEN SUM( CASE
WHEN #UserID1 = ID THEN 1
WHEN #UserID2 = ID THEN 1
WHEN #UserID3 = ID THEN 1
ELSE 0
END ) = 3 THEN 1 ELSE 0 AS [AllThree]
FROM aspnet_UsersInRoles
(untested code)
...assuming that the ID column is unique. If all three IDs are in the table, then you should end up with a summed value of 3. This will allow you to process the table in a single pass, but you don't get the advantages of index lookups like you get in Upendra's answer.

get previous row id using stored procedure

My requirement like this:
I have to fetch a row from table "A". Now I have to loop through all the rows and get the values from the ID column. If I get ID = 5 then I want to execute some stored procedure through which I get the previous id row from some table "B". Like
select * from table b where ID = 4
Buy my question is: if someone deleted that row from the database (e.g. some user deleted the rows with id 3 and 4) - then how can I get the row with id = 2 from the database?
Please give me some sql stored procedure to get previous id record from table.
Hard to understand exactly what it is your looking for - something like this maybe??
CREATE PROCEDURE dbo.FetchPreviousRow #ID INT
AS BEGIN
SELECT TOP 1 ID, (other columns.....)
FROM dbo.TableB
WHERE ID < #ID
ORDER BY ID DESC
END
This will select the row with the biggest ID less than the #ID you pass in, so:
EXEC dbo.FetchPreviousRow #ID = 5
will return the row with ID = 4 - if it exists. Otherwise, it will return the row with ID = 3 - if it exists. Otherwise the row with ID = 2 - and so on....
The following query should get it for you:
select max(ID) from YourTable where ID < #YourID
#YourID is a variable containing your recently inserted ID
How about selecting all ID's less than 5 and sorting them by ID. The first result then would be the one you want.