Showing posts with label specific. Show all posts
Showing posts with label specific. Show all posts

Monday, March 12, 2012

I think there is a better way.

It seems like im missing something, what i want to do is select the last row
from a table that relates to a specific person. My table and select
statement follow, the select statement works fine, works great as a matter
of fact, but i feel as if i am missing something (well something in addition
to a better understanding of sql but im still working on that).

CREATE TABLE Logins
(
ID IDENTITY(1,1) PRIMARY KEY
Username VARCHAR(100),
LoginTime DATETIME
)

-- Now i want to select the last login time for a specific user, so this is
what i have been doing.
SELECT TOP 1 FROM Logins WHERE Username = 'x' ORDER BY ID DESC

Best,
Muhd.Ok i just realized this might not be the best example because i think you
could probably just compare the date value of logintime and select the one
thats the highest (although im not sure how to do that either). So maybe
this is a better example, where what i want to do is select the most recent
comment made by someone.

CREATE TABLE Comments
(
ID IDENTITY(1,1) PRIMARY KEY,
UserName VARCHAR(100),
Comments VARCHAR(1000)
)

And then as in the previous example i would simply select the top 1 row
sorted desc by id. Of course maybe i touched on the solution, if i date
stamped each entry i could then select the most recent entry (its that most
recent entry thats giving me problems).

"Muhd" <muhd@.binarydemon.com> wrote in message
news:nLDNb.113982$JQ1.55283@.pd7tw1no...
> It seems like im missing something, what i want to do is select the last
row
> from a table that relates to a specific person. My table and select
> statement follow, the select statement works fine, works great as a matter
> of fact, but i feel as if i am missing something (well something in
addition
> to a better understanding of sql but im still working on that).
> CREATE TABLE Logins
> (
> ID IDENTITY(1,1) PRIMARY KEY
> Username VARCHAR(100),
> LoginTime DATETIME
> )
> -- Now i want to select the last login time for a specific user, so this
is
> what i have been doing.
> SELECT TOP 1 FROM Logins WHERE Username = 'x' ORDER BY ID DESC
> Best,
> Muhd.|||"Muhd" <muhd@.binarydemon.com> wrote in message news:nLDNb.113982$JQ1.55283@.pd7tw1no...
> It seems like im missing something, what i want to do is select the last row
> from a table that relates to a specific person. My table and select
> statement follow, the select statement works fine, works great as a matter
> of fact, but i feel as if i am missing something (well something in addition
> to a better understanding of sql but im still working on that).
> CREATE TABLE Logins
> (
> ID IDENTITY(1,1) PRIMARY KEY
> Username VARCHAR(100),
> LoginTime DATETIME
> )
> -- Now i want to select the last login time for a specific user, so this is
> what i have been doing.
> SELECT TOP 1 FROM Logins WHERE Username = 'x' ORDER BY ID DESC
> Best,
> Muhd.

CREATE VIEW LatestLogins (username, login_time)
AS
SELECT Username, LoginTime
FROM Logins AS L1
WHERE NOT EXISTS (SELECT *
FROM Logins AS L2
WHERE L2.Username = L1.Username AND
L2.LoginTime > L1.LoginTime)

SELECT username, login_time
FROM LatestLogins
WHERE username = 'x'

Regards,
jag|||Ok one last comment, by "last row" i actually mean the last entry that
someone made. I know enough to understand there isn't a "last row" in a
relational database. Just thought i would clarify.

"Muhd" <muhd@.binarydemon.com> wrote in message
news:nLDNb.113982$JQ1.55283@.pd7tw1no...
> It seems like im missing something, what i want to do is select the last
row
> from a table that relates to a specific person. My table and select
> statement follow, the select statement works fine, works great as a matter
> of fact, but i feel as if i am missing something (well something in
addition
> to a better understanding of sql but im still working on that).
> CREATE TABLE Logins
> (
> ID IDENTITY(1,1) PRIMARY KEY
> Username VARCHAR(100),
> LoginTime DATETIME
> )
> -- Now i want to select the last login time for a specific user, so this
is
> what i have been doing.
> SELECT TOP 1 FROM Logins WHERE Username = 'x' ORDER BY ID DESC
> Best,
> Muhd.|||> if i date
> stamped each entry i could then select the most recent entry (its that
most
> recent entry thats giving me problems).

Exactly. If you don't put that date/time stamp in your table then you
haven't recorded the information you need for your query. I suggest you
don't rely on the sequence of an identity column since identity isn't a
"real" attribute of your entity and it will cause you problems if you want
re-seed the value or merge it with data from another table.

SELECT username, comments
FROM Comments AS C
WHERE date_created =
(SELECT MAX(date_created)
FROM Comments
WHERE username = C.username)

--
David Portas
----
Please reply only to the newsgroup
--

Friday, March 9, 2012

I Need to rise to the next level of SQL Programming, Recommendations?

This really isn't a SQL Server specific question, but more tword SQL in general. I am pretty good at SQL, being able to perform joins on several tables at one time. I am looking for more challenges in SQL though as I want to learn more and to rise to the next level. Can anyone recommend some good resources to me?

It would be challenging to me to learn how to do more complex queries involving three or more tables.

Ralph

This forums is a good place to start. Keep checking for questions being asked here and see if you can help out. This way you benefit from learning while the poster benefits from getting his issue resolved.

|||

Subscribe to SQL Server Magazine and read Itzik Ben-Gan's articles religiously. And get his books: very deep T-SQL information. Also, get the Inside SQL Server series of books; Kalen Delaney is the series editor.

These are the best resources available, hands down.

Let us know what you think of them, if you get any, okay?

Don

Sunday, February 19, 2012

I need help performing a date query where the output is all related records to one specific date

Hi Everybody,

I need help with a query where the output is all records related to one specific date. Like take for ex. all records which have been entered on 5/26/07. I would really appreciate the help. Thanks in advance. . .

I've pasted the code which I've been using but just gives me a reserved error 3646.

SELECT Escal_Tracker.Escal_Type, Escal_Tracker.Cross_func, Escal_Tracker.cas_no, Escal_Tracker.Inc_no, Escal_Tracker.esc_com, Escal_Tracker.nt_login, Escal_Tracker.Date
FROM Escal_Tracker
WHERE date = '5/26/07';

Regards,

Inspired_One

Hi inspired_one,

Move the thread from Visual Basic Forum in order to get better answers, since this issue is related to Transact-SQL.
Thanks for your understanding!

|||

Your code is 'mostly' ok. I recommend using the ISO date format which for SQL Server, is unabiguous. Also, if any of the Date field values include a time component, they will not match. The best way to accomplish a filter criteria for a complete day is to bracket all values between midnight the desired date, and just before midnight the next date. Also when you use reserved words as your table or column names, you MUST then always enclose them in square brackets to let the server know that you made a mistake and now must type extra keystrokes to compensate. ( [Date] is a reserved word.) Refer to Books Online, Topic: 'Reserved Words'

Perhaps this will work for you...

Code Snippet


SELECT
e.Escal_Type,
e.Cross_func,
e.cas_no,
e.Inc_no,
e.esc_com,
e.nt_login,
e.[Date]
FROM Escal_Tracker e
WHERE ( e.[Date] >= '2007/05/26'
AND e.[Date] < '2007/05/27'

|||

Arnie,

Thanks a lot for the help bro. that actually led me to the answer to this problem which was .

SELECT
e.Escal_Type,
e.Cross_func,
e.cas_no,
e.Inc_no,
e.esc_com,
e.nt_login,
e.[Date]
FROM Escal_Tracker e
WHERE ( e.[Date] >= # " & varfirstdate & " # AND e.[Date] < # " & varseconddate & " #

The difference is I'm using vba variables for this code. . .

Regards,

Inspired one

I need help performing a date query where the output is all related records to one specific date

Hi Everybody,

I need help with a query where the output is all records related to one specific date. Like take for ex. all records which have been entered on 5/26/07. I would really appreciate the help. Thanks in advance. . .

I've pasted the code which I've been using but just gives me a reserved error 3646.

SELECT Escal_Tracker.Escal_Type, Escal_Tracker.Cross_func, Escal_Tracker.cas_no, Escal_Tracker.Inc_no, Escal_Tracker.esc_com, Escal_Tracker.nt_login, Escal_Tracker.Date
FROM Escal_Tracker
WHERE date = '5/26/07';

Regards,

Inspired_One

Hi inspired_one,

Move the thread from Visual Basic Forum in order to get better answers, since this issue is related to Transact-SQL.
Thanks for your understanding!

|||

Your code is 'mostly' ok. I recommend using the ISO date format which for SQL Server, is unabiguous. Also, if any of the Date field values include a time component, they will not match. The best way to accomplish a filter criteria for a complete day is to bracket all values between midnight the desired date, and just before midnight the next date. Also when you use reserved words as your table or column names, you MUST then always enclose them in square brackets to let the server know that you made a mistake and now must type extra keystrokes to compensate. ( [Date] is a reserved word.) Refer to Books Online, Topic: 'Reserved Words'

Perhaps this will work for you...

Code Snippet


SELECT
e.Escal_Type,
e.Cross_func,
e.cas_no,
e.Inc_no,
e.esc_com,
e.nt_login,
e.[Date]
FROM Escal_Tracker e
WHERE ( e.[Date] >= '2007/05/26'
AND e.[Date] < '2007/05/27'

|||

Arnie,

Thanks a lot for the help bro. that actually led me to the answer to this problem which was .

SELECT
e.Escal_Type,
e.Cross_func,
e.cas_no,
e.Inc_no,
e.esc_com,
e.nt_login,
e.[Date]
FROM Escal_Tracker e
WHERE ( e.[Date] >= # " & varfirstdate & " # AND e.[Date] < # " & varseconddate & " #

The difference is I'm using vba variables for this code. . .

Regards,

Inspired one