473,387 Members | 1,844 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,387 software developers and data experts.

SQL join query

Hi,
I have a question regarding join query syntax:
In my DB there are Users table, and Transactions table (which has
'TransactionDate' and 'UserName' fields). I would like to display a
list of all users (each user will appear only once), and for each user
- his last transaction date.
Please advice.

Thanks,
Yaron
Jul 20 '05 #1
4 12795
"Yaron Avior" <ya****@mercury.co.il> wrote in message
news:1b*************************@posting.google.co m...
Hi,
I have a question regarding join query syntax:
In my DB there are Users table, and Transactions table (which has
'TransactionDate' and 'UserName' fields). I would like to display a
list of all users (each user will appear only once), and for each user
- his last transaction date.
Please advice.

Thanks,
Yaron


CREATE TABLE Users
(
user_name VARCHAR(25) NOT NULL PRIMARY KEY
)

CREATE TABLE UserTransactions
(
user_name VARCHAR(25) NOT NULL REFERENCES Users (user_name),
transaction_date DATETIME NOT NULL
CHECK (transaction_date <= CURRENT_TIMESTAMP),
PRIMARY KEY (user_name, transaction_date)
)

SELECT user_name, MAX(transaction_date) AS latest_transaction_date
FROM UserTransactions
GROUP BY user_name

If you want every user, regardless of whether he has a transaction entry,
then do the following:

SELECT U.user_name, MAX(T.transaction_date) AS latest_transaction_date
FROM Users AS U
LEFT OUTER JOIN
UserTransactions AS T
ON U.user_name = T.user_name
GROUP BY U.user_name

Regards,
jag
Jul 20 '05 #2
"John Gilson" <ja*@acm.org> wrote in message news:<dF*********************@twister.nyc.rr.com>. ..
"Yaron Avior" <ya****@mercury.co.il> wrote in message
news:1b*************************@posting.google.co m...
Hi,
I have a question regarding join query syntax:
In my DB there are Users table, and Transactions table (which has
'TransactionDate' and 'UserName' fields). I would like to display a
list of all users (each user will appear only once), and for each user
- his last transaction date.
Please advice.

Thanks,
Yaron


CREATE TABLE Users
(
user_name VARCHAR(25) NOT NULL PRIMARY KEY
)

CREATE TABLE UserTransactions
(
user_name VARCHAR(25) NOT NULL REFERENCES Users (user_name),
transaction_date DATETIME NOT NULL
CHECK (transaction_date <= CURRENT_TIMESTAMP),
PRIMARY KEY (user_name, transaction_date)
)

SELECT user_name, MAX(transaction_date) AS latest_transaction_date
FROM UserTransactions
GROUP BY user_name

If you want every user, regardless of whether he has a transaction entry,
then do the following:

SELECT U.user_name, MAX(T.transaction_date) AS latest_transaction_date
FROM Users AS U
LEFT OUTER JOIN
UserTransactions AS T
ON U.user_name = T.user_name
GROUP BY U.user_name

Regards,
jag


That worked great, thanks. though - when I wanted to add the
Transaction name from Transaction table (join by TransactionID to
UserTransactions table), I got multiplied recordset without the
ability to get only one record per user. Any ideas?
Jul 20 '05 #3
Yaron Avior (ya****@mercury.co.il) writes:
That worked great, thanks. though - when I wanted to add the
Transaction name from Transaction table (join by TransactionID to
UserTransactions table), I got multiplied recordset without the
ability to get only one record per user. Any ideas?


Something like:
SELECT ut.username, t.transactionname, ut.transactiondate
FROM (SELECT ut.username, transactiondate = MAX(ut.transactiondate)
FROM usertransactions
GROUP BY ut.username) AS ut
JOIN usertransactions ut2 ON ut.username = ut2.username
AND ut.transactiondate = ut2.transactiondate
JOIN transactions t ON ut.transactionid = t.transactionid

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
hasdy
1
I also have the same requirement although with a minor difference.

I have a field of date and another field of time.

So how can we modify the query so that it will pick out the max date with the cooresponding max time? aka latest transaction?

Thanks,
hasdy
Jun 26 '06 #5

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

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
12
by: Phil Powell | last post by:
<cfquery name="getAll" datasource="#request.dsn#"> SELECT U.userID, U.fname, U.lname, U.phone, U.lastLoggedIn, U.choiceId, U.experience, T.label AS teamLabel, R.label AS roleLabel FROM User U...
5
by: jason.evans | last post by:
Hi there. I am having an intrigueing problem. I have a query which left joins another query to itself twice. The original query is derived from a linked table in SQLServer 2000. When I run...
14
by: Jim Andersen | last post by:
I have a problem with this standard employee-supervisor scenario For pictures: http://www.databasedev.co.uk/self-join_query.html I want to show all employees "belonging" to a specific...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
0
by: mlarson | last post by:
I have a program that worked fine then they needed to be able to also see the empty cells (inmate cells) on a housing unit when they ran the query. So what I had to do was take two tables and...
3
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so...
12
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
3
by: nico3334 | last post by:
I currently have a query that Joins 2 Tables (Table1 and Table2) using LEFT OUTER JOIN. Here is an example of that query: SELECT a.supply, a.state, b.cost FROM Table1 a LEFT...
2
theGeek
by: theGeek | last post by:
I always wonder which one of join or subquery should I be using to solve a particuar problem so that I get the correct result set in minimum time. It usually happens that I can write a query quickly...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.