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.
Related
My data has orderid, userid and userage along with a lot other data.
Now age is something that the user can provide if and whenever he/she wishes.
Sample data
orderid userid userage
1 1
2 2
3 1
4 1 18
5 3 25
Now, if I wish to find all orderids for userage=18, I can not do something as Select orderid from table where userage=18 as it will give me only orderid '4'.
I want a single query which should preferably work on any db (as though I'm using sql server, it might change any time) which will give me result which has orderid '1', '3' and '4'.
Thanks
I think you are asking for all orderid where the userid has a userage value of 18.
In which case the following should work:
SELECT orderid
FROM TABLE
WHERE userid IN (SELECT DISTINCT userid
FROM TABLE
WHERE userage = 18)
You haven't disclosed how you represent missing data. Many DBMS designs use NULL values for that. If that's your situation
SELECT orderid FROM table WHERE (userage = 18 OR userage IS NULL)
will work.
If you use -1 for missing data (you probably don't, but I'm trying to make a point here)
SELECT orderid FROM table WHERE (userage = 18 OR userage = -1)
will work.
Notice that when a column value is NULL, no test for equality or inequality ever comes back true. You must is IS NULL or IS NOT NULL to test NULLs.
In Oracle, zero length text strings are NULL. That's not so in other DBMSs.
If you're looking for all orders from a user who has ever given an age of 18, this will do the trick
SELECT orderid
FROM table
WHERE userid IN (SELECT userid FROM table where userage = 18)
Please try this:
Select orderid from table where orderid in (1,3,8)
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
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)
We are using audit tables for each operational table, which stores the previous value of its operational equivalent plus change date, change type (UPDATE or DELETE) and its own auto incremental Primary Key.
So, for a table Users with columns UserID, Name, Email there would be a table xUsers with columns ID, OpererationType, OperationDate, UserID, Name, Email.
See that the xTable contains every column that its 'parent' does with 3 extra fields. This pattern is repeated for all tables used by our system.
table Users:
UserID int
Name nvarchar
Email nvarchar
table xUsers:
xUserID int
OpererationType int
OperationDate datetime
UserID int
Name nvarchar
Email nvarchar
Now, my question:
If I have a certain UserID, for which there is 2 entries in the xUsers table when the email was changed twice,
how would I construct a query that identifies which columns (can be more than 1) differ between the two rows in the audit table?
If I'm understanding this correctly, you'd like to create a query passing in the UserID as a parameter, which I'll call #UserID for the following example.
This query will select all rows from xUsers joined onto itself where there is a difference in a non-UserID column, using a series of case statements (one per column) to pull out specifically which columns differ.
SELECT *
, CASE
WHEN a.OperationType <> b.OperationType
THEN 1
ELSE 0
END AS OperationTypeDiffers
, CASE
WHEN a.OperationDate <> b.OperationDate
THEN 1
ELSE 0
END AS OperationDateDiffers
FROM xUsers a
JOIN xUsers b
ON a.xUserID < b.xUserID
AND a.UserID = b.UserID
AND (a.OperationType <> b.OperationType
OR a.OperationDate <> b.OperationDate) -- etc.
WHERE a.UserID = #UserID
You can put the rows of xUsers in a temporary table and then make a while cycle to go for each one and compare the results.
OR
You can do some dynamic SQL and use sysobjects and syscolumns tables to compare each result. It would be more dynamic and then it would be easy to implement for other tables.
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.