473,789 Members | 2,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 20 '05 #1
4 3130
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" <mu**@binarydem on.com> wrote in message
news:nLDNb.1139 82$JQ1.55283@pd 7tw1no...
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.

Jul 20 '05 #2
"Muhd" <mu**@binarydem on.com> wrote in message news:nLDNb.1139 82$JQ1.55283@pd 7tw1no...
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
Jul 20 '05 #3
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" <mu**@binarydem on.com> wrote in message
news:nLDNb.1139 82$JQ1.55283@pd 7tw1no...
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.

Jul 20 '05 #4
> 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_create d)
FROM Comments
WHERE username = C.username)

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
5127
by: Brandon J. Van Every | last post by:
What do you think of this Python logo? http://pythonology.org/logos Good, bad, indifferent, love it, hate it? -- Cheers, www.indiegamedesign.com Brandon Van Every Seattle, WA Brandon's Law (after Godwin's Law): "As a Usenet discussion grows longer, the probability of
72
4856
by: The Plankmeister | last post by:
Is doing this bad: <h3>Some title or other...<a href="#pagetop">back to top</a></h3> I have a feeling it is... My problem is I'm using CSS to style the H3 into a block that spans the whole containing element. I would like the <a> to appear next to the title, but I'm sure this is bad practice (for screen readers and heading-level navigation etc etc) So... is it acceptable to do this:
86
7804
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that: http://www.clagnut.com/blog/348/ I hope that's going to be a good discussion! Michael
11
1049
by: FrzzMan | last post by:
There's not much here, my question is the subject...
2
2926
by: Carsten H. Pedersen | last post by:
I apologize in advance for the length of the note, and the source code included. I posted earlier under the title "Double streams", where i wanted to use an RMI object as a sort of proxy for two clients to exchange data via streams. I kinda failed, but now i'm trying something new. This doesn't work either, but this time i think someone might be able to solve it, since i'm not adept at using threads and i think that's where the problem...
29
2592
by: GhostInAK | last post by:
I'm seeing a terribly distubing number of questions that have no purpose in existing. As an example: How do I change the position of a stream? Hmm.. Could it be some method on the stream itsself? Probably. My advice, pleading advice, is that you USE YOUR DAMN BRAINS before spewing your crap on here. Think about what you are doing. If your question is "How do I do X with class/object Y?" Then why not open up the Obejct Browser and...
10
2619
by: Marek Zawadzki | last post by:
Hi all. Is it possible to implement an accurate voting mechanism (think digg.com) that does not require users to sign in before voting? I think for many people registering is pain and thus they rarely give their votes online. Just look at digg and compare the number of votes to the number of people visiting their website. IOW what if somebody needs votes but cannot afford to bother users with registration process?
14
2243
by: colombianpimp28 | last post by:
please tell me what you think of my website any suggestions http://masterjuan0101.googlepages.com
0
2197
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.