Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9013577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:18:13+00:00 2026-06-16T03:18:13+00:00

I have two tables describing users and their payments: CREATE TABLE test_users (id int

  • 0

I have two tables describing users and their payments:

CREATE TABLE test_users
(id int IDENTITY NOT NULL,
name varchar(25),
PRIMARY KEY (id));

CREATE TABLE test_payments
(id int IDENTITY NOT NULL,
user_id int NOT NULL,
money money NOT NULL,
date datetime NOT NULL,
PRIMARY KEY (id));

INSERT INTO test_users (name) 
VALUES ('john');

INSERT INTO test_users (name) 
VALUES ('peter');

INSERT INTO test_payments (user_id, money, date)
VALUES (1, $1, CONVERT(datetime, '15.12.2012'));

INSERT INTO test_payments (user_id, money, date)
VALUES (1, $2, CONVERT(datetime, '16.12.2012'));

INSERT INTO test_payments (user_id, money, date)
VALUES (2, $1, CONVERT(datetime, '16.12.2012'));

INSERT INTO test_payments (user_id, money, date)
VALUES (2, $3, CONVERT(datetime, '17.12.2012'));

INSERT INTO test_payments (user_id, money, date)
VALUES (1, $1, CONVERT(datetime, '19.12.2012'));

Table test_users:

id  name
-------------
1   john
2   peter

Table test_payments:

id  user_id money   last_activity
---------------------------------------
1   1   1.0000  2012-12-15
2   1   2.0000  2012-12-16
3   2   1.0000  2012-12-16
4   2   3.0000  2012-12-17
5   1   1.0000  2012-12-19

I need to make a users statistic which will show me :

  1. username
  2. total fee for a period of time
  3. the date of the last
    user’s activity (general, not for a time period).

For example taking the period 15-18.12.12 I expect the following results:

name    total    last_activity
--------------------------------
peter   $4       2012-12-17
john    $3       2012-12-19

I’ve tried the following query:

SELECT u.*, SUM(p.money) total, MAX(p.date) last_activity
FROM test_users u
JOIN test_payments p
ON u.id= p.user_id
WHERE p.date BETWEEN CONVERT(datetime, '15.12.2012') AND CONVERT(datetime, '18.12.2012')
GROUP BY u.id, u.name
ORDER BY total DESC;

but getting wrong result for last_activity as it is also in the date range:

id  name    total   last_activity
--------------------------------
2   peter   4.0000  2012-12-17
1   john    3.0000  2012-12-16

Please suggest a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:18:14+00:00Added an answer on June 16, 2026 at 3:18 am

    Looks like a couple of other answers popped up while I worked on mine, but here it is anyhow. There is a working sql fiddle here: http://sqlfiddle.com/#!3/14808/6

    Basically, you need a query to pull the max date regardless of the date range. I chose to do this as a correlated subquery.

    SELECT 
      u.id,
      u.name,
      SUM(IsNull(money,0)) as TotalMoneyInRange,
      (SELECT max(date) FROM test_payments where user_id = u.id) AS LastPaymentOverAll
    FROM test_users AS u
    LEFT JOIN test_payments AS p
    ON u.id = p.user_id
    WHERE 
      p.date IS NULL OR
      p.date between 
      CAST('12-11-2012' AS datetime) --range begin
      and 
      CAST('12-16-2012' as datetime) --range end
    GROUP BY u.id, u.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have two tables: CREATE TABLE IF NOT EXISTS `issue_details` ( `id` INT NOT NULL
I have two tables with a linking table describing their many-to-many relationship. TABLE buildings
I have two tables: CREATE TABLE tblEatables ( `EatId` int UNSIGNED PRIMARY AUTO_INCREMENT, `Fruits`
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables: 'movies' and 'users'. There's an n:m relationship between those, describing
I have two tables, A and B. The structure looks like this: CREATE TABLE
I have two tables, like these: Table People: VARCHAR Name INTEGER Age Table Message
I have two tables with the following definitions: CREATE TABLE [dbo].[Shows] ( [Id] UNIQUEIDENTIFIER
Have two tables with a linking table between them. USERS +-------+---------+ | userID| Username|

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.