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
Related
I have a very large, complex, and undocumented database. I have a task to provide a document which would show which tables and columns have been used for all stored procedures, functions, etc.
According to my research majority of queries will have the similar format to this:
SELECT u.FirstName , u.LastName, a.AccountNumber
FROM Username u
LEFT JOIN Account a
ON a.UserID = u.UserID
~90% of tables and columns will have aliases.
Further, I do have a table with 2 columns - function/sproc name, and its SQL code.
I am looking for a method (preferably SQL, but can be C#) which would output the following results for the above SQL code:
Username - FirstName
Username - LastName
Username - UserID
Account - UserID
Account - AccountNumber
What would be the best approach to achieve this? I have tried to join each SQL code cell with INFORMATION_SCHEMA.COLUMNS but I get inaccurate results, say when column name appears in the COLUMNS table, but was not actually used for that specific table in the SQL code cell.
Thanks
Probably you want to look at the dependencies on the stored procedure/function that you are looking at?
Take a look at https://www.mssqltips.com/sqlservertip/1768/identifying-object-dependencies-in-sql-server/
e.g. if the procedure name is dbo.myproc, something like the below
SELECT '* ' + referenced_entity_name + ' - ' + referenced_minor_name
FROM sys.dm_sql_referenced_entities ('dbo.myproc', 'OBJECT')
WHERE referenced_minor_name is not null;
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 one table which can store employee leave start date and end date
like so:
EmployeeID ManagerId leaveStartDate leaveEndDate isApproved
1 14 10/02/2014 15/02/2014 approved
4 17 10/02/2014 12/02/2014 rejected
I need check when manager login is its any employee is on leave? if yes then manager can assign that employees work to other employee.For that I have to check wheather current system time is lies between leaveStartDate and leaveEndDate.How can I do that ?
If you wanna filter with IsApprved as well try this.
SELECT * FROM tblEmployeeLeaves WHERE Date(Getdate()) between leaveStartDate and leaveEndDate AND isApproved='approved'
try this :
this can solve this:
*abc is your table name
select * from abc where isApproved="approved" and Date(Getdate()) between leaveStartDate and leaveEndDate
Worked fine thanks
select EmployeeId from tblName where Getdate() between leaveStartDate and leaveEndDate and isApproved='Approve' and ManagerId=#mgrId
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.
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