473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Figuring out the right query

I have a table (user)

ip, ssn, date,name

Each time the user does something, I record that information in the table.
I'd like get a list of the last 4 entries for distinct ssn.

I started with this ... but cannot seem to get only the top 4 distinct SSNs.

select top 4,ssn,name from user where ip='<ip>' order by date desc

Any insight you can provide would be most appreciated.

BBB
Feb 8 '06 #1
5 1736
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

There shouldn't be a comma after the 4 in the SELECT clause.

How about an example of the data you are querying and your expected
results?

Is the ip address set up like this in the WHERE clause:

WHERE ip = '192.0.0.168'

Depending on your data the same ssn,name could be showing up on the same
date. Does the date column also contain the time?

Have you tried the DISTINCTROW predicate?

SELECT DISTINCTROW top 4 ssn, ...

Some design details: "date" and "name" are bad column names. Date is a
VBA reserved word. Anyway, "Date for what?" Name is an SQL reserved
word. Again, "Name of what?"
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+ucf4echKqOuFEgEQJMxwCgwLq+pxWZ6HN7ByDWVYrHdm UT69oAn1O7
NKtg9hC3U++ALXmMSqkZVMwv
=wslw
-----END PGP SIGNATURE-----

booner wrote:
I have a table (user)

ip, ssn, date,name

Each time the user does something, I record that information in the table.
I'd like get a list of the last 4 entries for distinct ssn.

I started with this ... but cannot seem to get only the top 4 distinct SSNs.

select top 4,ssn,name from user where ip='<ip>' order by date desc

Any insight you can provide would be most appreciated.

Feb 9 '06 #2
I had tried Distinct ... was not familiar with DistinctRow.

Understand on the names - was just trying to make the example more generic.

Example data:

IP,Date,LastName,SSN
1.1.1.2, 2/8/2006 3:54:41 PM,Smith,123456789
1.1.1.2, 2/8/2006 1:23:40 PM,Smith,123456789
1.1.1.2, 2/6/2006 3:23:21 PM,Jones,987654321
1.1.1.2,2/6/2006 5:23:34 PM,Jones,987654321
1.1.1.2,2/3/2006 9:23:21 AM,Public,111223333
1.1.1.2,2/3/2006 10:24:28 AM,Public,111223333
1.1.1.2,2/3/2006 8:17:39 AM,Johnson,321549876
1.1.1.2,2/3/2006 9:16:39 AM,Jones,231539676

So what I'm looking to get is
123456789,Smith
987654321,Jones
111223333,Public
231539676,Jones

Get the latest 4 unique SSN based on the Date.
"MGFoster" <me@privacy.com> wrote in message
news:_%**************@newsread2.news.pas.earthlink .net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

There shouldn't be a comma after the 4 in the SELECT clause.

How about an example of the data you are querying and your expected
results?

Is the ip address set up like this in the WHERE clause:

WHERE ip = '192.0.0.168'

Depending on your data the same ssn,name could be showing up on the same
date. Does the date column also contain the time?

Have you tried the DISTINCTROW predicate?

SELECT DISTINCTROW top 4 ssn, ...

Some design details: "date" and "name" are bad column names. Date is a
VBA reserved word. Anyway, "Date for what?" Name is an SQL reserved
word. Again, "Name of what?"
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+ucf4echKqOuFEgEQJMxwCgwLq+pxWZ6HN7ByDWVYrHdm UT69oAn1O7
NKtg9hC3U++ALXmMSqkZVMwv
=wslw
-----END PGP SIGNATURE-----

booner wrote:
I have a table (user)

ip, ssn, date,name

Each time the user does something, I record that information in the table. I'd like get a list of the last 4 entries for distinct ssn.

I started with this ... but cannot seem to get only the top 4 distinct SSNs.
select top 4,ssn,name from user where ip='<ip>' order by date desc

Any insight you can provide would be most appreciated.

Feb 9 '06 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Something like this:

SELECT TOP 4 ssn, last_name
FROM table_name As T
WHERE [date] = (SELECT MAX([date]) FROM table_name
WHERE ssn = T.ssn)
ORDER BY [date] DESC
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+uzv4echKqOuFEgEQIlHwCg6T0LmI9tTqiPzeLIvkDK0Z e8P8sAn0Gs
WJmvQAE38iTf7ZgBvLJntYy7
=UJY/
-----END PGP SIGNATURE-----

booner wrote:
I had tried Distinct ... was not familiar with DistinctRow.

Understand on the names - was just trying to make the example more generic.

Example data:

IP,Date,LastName,SSN
1.1.1.2, 2/8/2006 3:54:41 PM,Smith,123456789
1.1.1.2, 2/8/2006 1:23:40 PM,Smith,123456789
1.1.1.2, 2/6/2006 3:23:21 PM,Jones,987654321
1.1.1.2,2/6/2006 5:23:34 PM,Jones,987654321
1.1.1.2,2/3/2006 9:23:21 AM,Public,111223333
1.1.1.2,2/3/2006 10:24:28 AM,Public,111223333
1.1.1.2,2/3/2006 8:17:39 AM,Johnson,321549876
1.1.1.2,2/3/2006 9:16:39 AM,Jones,231539676

So what I'm looking to get is
123456789,Smith
987654321,Jones
111223333,Public
231539676,Jones

Get the latest 4 unique SSN based on the Date.
"MGFoster" <me@privacy.com> wrote in message
news:_%**************@newsread2.news.pas.earthlink .net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

There shouldn't be a comma after the 4 in the SELECT clause.

How about an example of the data you are querying and your expected
results?

Is the ip address set up like this in the WHERE clause:

WHERE ip = '192.0.0.168'

Depending on your data the same ssn,name could be showing up on the same
date. Does the date column also contain the time?

Have you tried the DISTINCTROW predicate?

SELECT DISTINCTROW top 4 ssn, ...

Some design details: "date" and "name" are bad column names. Date is a
VBA reserved word. Anyway, "Date for what?" Name is an SQL reserved
word. Again, "Name of what?"
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+ucf4echKqOuFEgEQJMxwCgwLq+pxWZ6HN7ByDWVYrHdm UT69oAn1O7
NKtg9hC3U++ALXmMSqkZVMwv
=wslw
-----END PGP SIGNATURE-----

booner wrote:
I have a table (user)

ip, ssn, date,name

Each time the user does something, I record that information in the
table.
I'd like get a list of the last 4 entries for distinct ssn.

I started with this ... but cannot seem to get only the top 4 distinct
SSNs.
select top 4,ssn,name from user where ip='<ip>' order by date desc

Any insight you can provide would be most appreciated.


Feb 9 '06 #4
I tried that. In access it prompts me to enter a parameter value for
last_name. I'm looking to perform this query programmatically - and when I
do it just fails:

ERROR: [Microsoft][ODBC Microsoft Access Driver] Too few parameters.
Expected 1.
"MGFoster" <me@privacy.com> wrote in message
news:%s**************@newsread2.news.pas.earthlink .net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Something like this:

SELECT TOP 4 ssn, last_name
FROM table_name As T
WHERE [date] = (SELECT MAX([date]) FROM table_name
WHERE ssn = T.ssn)
ORDER BY [date] DESC
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+uzv4echKqOuFEgEQIlHwCg6T0LmI9tTqiPzeLIvkDK0Z e8P8sAn0Gs
WJmvQAE38iTf7ZgBvLJntYy7
=UJY/
-----END PGP SIGNATURE-----

booner wrote:
I had tried Distinct ... was not familiar with DistinctRow.

Understand on the names - was just trying to make the example more generic.
Example data:

IP,Date,LastName,SSN
1.1.1.2, 2/8/2006 3:54:41 PM,Smith,123456789
1.1.1.2, 2/8/2006 1:23:40 PM,Smith,123456789
1.1.1.2, 2/6/2006 3:23:21 PM,Jones,987654321
1.1.1.2,2/6/2006 5:23:34 PM,Jones,987654321
1.1.1.2,2/3/2006 9:23:21 AM,Public,111223333
1.1.1.2,2/3/2006 10:24:28 AM,Public,111223333
1.1.1.2,2/3/2006 8:17:39 AM,Johnson,321549876
1.1.1.2,2/3/2006 9:16:39 AM,Jones,231539676

So what I'm looking to get is
123456789,Smith
987654321,Jones
111223333,Public
231539676,Jones

Get the latest 4 unique SSN based on the Date.
"MGFoster" <me@privacy.com> wrote in message
news:_%**************@newsread2.news.pas.earthlink .net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

There shouldn't be a comma after the 4 in the SELECT clause.

How about an example of the data you are querying and your expected
results?

Is the ip address set up like this in the WHERE clause:

WHERE ip = '192.0.0.168'

Depending on your data the same ssn,name could be showing up on the same
date. Does the date column also contain the time?

Have you tried the DISTINCTROW predicate?

SELECT DISTINCTROW top 4 ssn, ...

Some design details: "date" and "name" are bad column names. Date is a
VBA reserved word. Anyway, "Date for what?" Name is an SQL reserved
word. Again, "Name of what?"
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+ucf4echKqOuFEgEQJMxwCgwLq+pxWZ6HN7ByDWVYrHdm UT69oAn1O7
NKtg9hC3U++ALXmMSqkZVMwv
=wslw
-----END PGP SIGNATURE-----

booner wrote:

I have a table (user)

ip, ssn, date,name

Each time the user does something, I record that information in the


table.
I'd like get a list of the last 4 entries for distinct ssn.

I started with this ... but cannot seem to get only the top 4 distinct


SSNs.
select top 4,ssn,name from user where ip='<ip>' order by date desc

Any insight you can provide would be most appreciated.


Feb 10 '06 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Change the names of the columns & tables I used in my example to the
actual names in your database.

The table_name in the main query and the subquery should be the same
table.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+zZG4echKqOuFEgEQI9oQCfeuKOpiI1C+GAF3h/7qF8lAPthuEAoMDr
oxZRP1m4NeDfL9qJjMeUif57
=l3zY
-----END PGP SIGNATURE-----

booner wrote:
I tried that. In access it prompts me to enter a parameter value for
last_name. I'm looking to perform this query programmatically - and when I
do it just fails:

ERROR: [Microsoft][ODBC Microsoft Access Driver] Too few parameters.
Expected 1.
"MGFoster" <me@privacy.com> wrote in message
news:%s**************@newsread2.news.pas.earthlink .net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Something like this:

SELECT TOP 4 ssn, last_name
FROM table_name As T
WHERE [date] = (SELECT MAX([date]) FROM table_name
WHERE ssn = T.ssn)
ORDER BY [date] DESC
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+uzv4echKqOuFEgEQIlHwCg6T0LmI9tTqiPzeLIvkDK0Z e8P8sAn0Gs
WJmvQAE38iTf7ZgBvLJntYy7
=UJY/
-----END PGP SIGNATURE-----

booner wrote:
I had tried Distinct ... was not familiar with DistinctRow.

Understand on the names - was just trying to make the example more
generic.
Example data:

IP,Date,LastName,SSN
1.1.1.2, 2/8/2006 3:54:41 PM,Smith,123456789
1.1.1.2, 2/8/2006 1:23:40 PM,Smith,123456789
1.1.1.2, 2/6/2006 3:23:21 PM,Jones,987654321
1.1.1.2,2/6/2006 5:23:34 PM,Jones,987654321
1.1.1.2,2/3/2006 9:23:21 AM,Public,111223333
1.1.1.2,2/3/2006 10:24:28 AM,Public,111223333
1.1.1.2,2/3/2006 8:17:39 AM,Johnson,321549876
1.1.1.2,2/3/2006 9:16:39 AM,Jones,231539676

So what I'm looking to get is
123456789,Smith
987654321,Jones
111223333,Public
231539676,Jones

Get the latest 4 unique SSN based on the Date.

Feb 10 '06 #6

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

Similar topics

8
by: Nathan Pinno | last post by:
Hi all, I need help figuring out how to fix my code. I'm using Python 2.2.3, and it keeps telling me invalid syntax in the if name == "Nathan" line. Here is the code if you need it. #This...
4
by: Omavlana | last post by:
Hi, I need your help to resolve this problem. I have written a right outer join query between 2 indipendent tables as follows. select b.Account_desc, b.Account, a.CSPL_CSPL from...
2
by: Roberto Leibman | last post by:
I haven't been able to figure this out, or find any resource for this. I'd like to be able to know, in javascript, if the media that's rendering is screen or print. I have no nefarious purpose...
1
by: Darin Browne | last post by:
We figure budgets in 26 14 days periods in a year. I currently can take a date and calculate which of those 26 periods that date falls in. Now I have to figure out what is beginning and ending...
11
by: Jordan Bowness | last post by:
I'm trying to develop a bunch of classes and want to do it the right way. Say I have 5 classes: City, Neighborhood, NeighborhoodCollection, House, HouseCollection
3
by: Kundham Saare | last post by:
Hi, I would like to be able to figure out if a table has been updated in this connection from within a C trigger. I have already tried to the use a query with currval on the autoincremented...
20
by: Terry Olsen | last post by:
I'm writing an app that communicates with computers both inside and outside my router. So I need to determine by the remote host's IP address if I need to send them my LAN IP or my Internet IP. ...
1
by: aaron.reese | last post by:
Guys, this is my problem:- I have records which are linked by two fields on a left inner join (there may be 0,1 or more records in the right hand table) The relationship is not unique (it's...
10
by: bobf | last post by:
I am using a program 'My Contact Table' which is a code generator program. It allows you to easily create a PHP/MySQL web application without writing any code. I am trying to create an additional...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.