I am querying my table with the following query;
SELECT DISTINCT [RoleId], [RoleName], [RoleDescription], [PermissionId]
FROM [Role]
ORDER BY [RoleName]
And I get the following results;
RoleId RoleName RoleDescription PermissionId
1 Admin CanEdit 1
2 Admin asdf;lkj 2
3 Admin al;dskfj;l 3
4 Admin fa;ldkfjas;d 4
17 SuperAdmin aslkdfja; 1
18 SuperAdmin asldkfa;f 2
15 Users alskdfj;alk 1
16 Users aslkdfja;sl 2
However, I want to get the unique values of RoleName i-e RoleName shouldn't be repeated which is already in my query results. I want to get the RoleName once.
How do I get unique RoleName?
Updated:
I want to populate the combobox with the following results;
Admin
SuperAdmin
Users
but not not repeating (which I get already using my query
Admin
Admin
Admin
Admin
SuperAdmin
SuperAdmin
Users
Users
If you want to have only Distinct RoleName then you can query the table like this.
SELECT DISTINCT [RoleName]
FROM [Role]
ORDER BY [RoleName]
The answer you are watching is little bit complicated as you are going. if you want such type of result then you might have to change your process to "add Roles". While you are inserting Roles from front end then there you have to check whether the rolename is being entering doesn't exists in database already. Then you can easily go the result you are seeing for with the following Query.
Select Distinct rolename,roleid from tblroles
You say you want RoleId as well as RoleName. Well, you have to tell the system which RoleId to pick:
SELECT MIN(RoleId) as RoleId, RoleName
FROM Role
GROUP BY RoleName
ORDER BY RoleName
In this case, I've chosen somewhat arbitrarily to use the lowest RoleId for each name. If there's a different rule you want to use, then you a) need to tell us what that rule is, and/or b) need to tell SQL Server what that rule is.
Related
I have two tables in SQL Server DB. I have a dropdownmenu on my c# page where staff should be able to choose the relevant plan and it should show all users who do not have a current plan setup
Table 1 contains all users
Table 2 only contains people who have a plan. They also can have different plan_id's in table 2 from 1-10.
I need a SQL join which returns all people who do not have a plan at all or the plan id does not match the one selected from the dropdownlist
E.G a user could exist on table 2 with 3 entries with plan_id's 2,3 and 4.
E.G If a user selects plan 7 from the downdownlist, the user would appear in the list as they currently do not have a plan matching the id selected.
Table 2 plans also have an expiry_date and "active" field.
The plan should also not be valid if the expiry date is in the past or active is "0"
Write a not in Query, Where user does not exist in Table 2 with that plan id.
Suppose
Table_1 -> Userid, Username
Table_2 -> UserId, PlanId
SELECT UserId, Username FROM Table_1
WHERE UserId NOT IN ( SELECT UserId FROM Table_2 WHERE PlanId = 1 )
This query will return all the users who are not linked with Plan 1.
I have an sqlite database with a table Users that looks as follow:
Username | InsertDateTime
User101 4/22/2013 11:44
User102 3/22/2013 12:43
User103 4/22/2013 15:20
User104 1/21/2012 16:31
I want to select the Username with the highest datetime.
User103 in this case.
I tried some queries like:
Select Username from Users where InsertDateTime = MAX(datetime);
but this isn't a valid sqlite query.
Can somebody help me out? Thanks in advance!
How about:
SELECT Username FROM Users ORDER BY InsertDateTime DESC LIMIT 1
Hopefully Sqlite will be smart enough not to actually perform a complete ordering :)
Try this:
select Username
from Users
order by InsertDateTime desc
limit 1
I am developing a web-based training management system that provides the employees with weekly short quizzes. In profile section in the webiste, the employee should be able to see a list of the quizzes that he did not participate in them. I could be able to let the system to show them but not after improving the system, it doesn't show.
First of all, I have the following database design:
Quiz Table: QuizID, Title, Description, IsSent
UserQuiz Table: ID, QuizID, Score, DateTimeComplete, Username
IsSent is a flag that refers to the quizzes that have been sent or not
I put IsSent in the Quiz table because I want the Admin to be able to insert around 50 quizzes in a day. Then, the system will deal with sending them on a weekly basis. Now, when the user wants to see his profile and see the quizzes that he did not participate in them, he should see the the list of quizzes that have been sent and he did not participate in them.
The problem that I am facing it right now is that the system shows the quizzes that have not been sent as a list of quizzes that the employee did not participate in them and this is wrong. So how I can fix this problem?
My Query:
SELECT Title, Description
FROM dbo.Quiz
WHERE (NOT EXISTS
(SELECT QuizID
FROM dbo.UserQuiz
WHERE (dbo.Quiz.QuizID = QuizID) AND (Username = #Username)))
Add folllowing line in the end of the query
AND IsSent = 1
P.S. i'm assuming ur isSent column is tinyint and 1 = true
If I understand properly then you are looking for this query
select QuizID, Title, Description, from quiz
INNER JOIN userquiz ON quiz.QuizID= userquiz.QuizID
where quiz.issent = 1
I Have a User table, where it has userFirstname, emailid, organisation as columns. I have to filter the users based on the user that have logged into the system. I have the Email ID of the user who logs into the system. I need a sql statement so that the users are filtered based on the organisation which is same as the logged in user.
something like this,
(get the logged on users organisation, then find all users in that organisation):
SELECT *
FROM USER
WHERE organisation IN (SELECT organisation
FROM USER
WHERE emailid = loggedon_emailid)
I don't think it's a big deal here, because this sounds like a homework question. But, in an application, you will most likely need to frequently reference the data associated with a given emailid. Thus, you would probably want to run
SELECT * FROM User WHERE emailid = loggedon_emailid
at the beginning of your codepath and save that data in memory. Then, to execute the query you need, you could simply run:
SELECT * FROM User WHERE organisation = $loggedon_organisation
Then, when you need to print the user's name or organisation on the screen, you will not need an additional SQL query. And, if you have other tables, you could run more queries without repeatedly looking up the user's row in the User table. For example (this is purely hypothetical):
SELECT organisation_address FROM Organisation WHERE organisation = $loggedon_organisation
How would you design a database to manage multi organisation?
(ie 1 user can own/manage more than 1 organisation)
Example
Xero (www.xero.com), you can login to xero then select the company on the list that you want to manage. I think freshbook has something simular.
USER_ACCESS
Id
CompanyUserId (UserId of company)
UserId (UserId that will manages company)
USER_PERMISSION
Id
UserAccessId
CanViewM
CanEditM
....
CanViewN
CanEditN
You should not mix users and logins. They should be kept treated as seperate tables/objects. As ones role in one company might not be the same role as in the other company.
Also do not create a permission table with one column for each possible permission. Instead you should create one row per allowed permission. (and if needed one table defining all permissions)
Hence you should have tables like:
USER_ACCOUNT (used to define logins)
Id
UserName
Password
USER
Id
AccountId (account used for login)
CompanyId (company that the user belongs to)
PERMISSIONS
Id
Name
USER_ALLOWED_PERMISSIONS
UserId
PermissionId
When logging in, simply check the USER table if more than one row is returned for the account and display a select user form if needed.