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

  • SEARCH
  • Home
  • 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 7783899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:58:39+00:00 2026-06-01T19:58:39+00:00

Here’s my question: how do I maintain record integrity using aggregate functions with a

  • 0

Here’s my question: how do I maintain record integrity using aggregate functions with a group by?

To explain further, here’s an example.

I have a table with the following columns: (Think of it as an “order” table)

Customer_Summary (first 10 char of name + first 10 char of address)
Customer_Name
Customer_Address
Customer_Postal Code
Order_weekday

There is one row per “order”, so many rows with the same customer name, address, and summary.

What I want to do is show the customer’s name, address, and postal code, as well as the number of orders they’ve placed on each weekday, grouped by the customer’s summary.

So the data should look like:

Summary             | Name        | Address    | PCode | Monday | Tuesday | Wednesday | Thursday | Friday

test custntest addre|test custname|test address|123456 | 1      | 1       | 1         | 1        | 1

I only want to group records of similar customer summary together, but obviously I want one name, address, and postal code to show. I’m using min() at the moment, so my query looks like:

SELECT Customer_Summary, min(customer_name), min(customer_address), min(customer_postal_code) 
FROM Order
Group by customer_summary

I’ve omitted my weekday logic as I didn’t think it was necessary.

My issue is this – some of these customers with the same customer summary have different addresses and postal codes.

So I might have two customers, looking like:

test custntest addre|test custname |test address |323456|

test custntest addre|test custname2|test address2|123456|

Using the group by, my query will return the following:

test custntest addre|test custname |test address |123456|

Since I’m using min, it’s going to give me the minimum value for all of the fields, but not necessarily from the same record. So I’ve lost my record integrity here – the address and name returned by the query do not correctly match the postal code.

So how do I maintain data integrity on non-grouped fields when using a group by clause?

Hopefully I explained it clearly enough, and thanks in advance for the help.

EDIT: Solved. Thanks everyone!

  • 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-01T19:58:40+00:00Added an answer on June 1, 2026 at 7:58 pm

    You can always use ROW_NUMBER instead of GROUP BY

    WITH A AS (
        SELECT Customer_Summary, customer_name, customer_address, customer_postal_code,
            ROW_NUMBER() OVER (PARTITION BY Customer_Summary ORDER BY customer_name, customer_address) AS rn
        FROM Order
    )
    SELECT Customer_Summary, customer_name, customer_address, customer_postal_code
    FROM A
    WHERE rn = 1
    

    Then you are free to order which customer to use in the ORDER BY clause. Currently I am order them by name and then address.

    Edit:

    My solution does what you asked for. But I surely agree with the others: If you are allowed to change the database structure, this would be a good idea… which you are not (saw your comment). Well, then ROW_NUMBER() is a good way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an example: I have a file 1.js, which has some functions. I
Here is a complete example. I want to forbid using A::set from objects casted
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is an example: I have the generic type called Account. I wish to
Here is my situation: I am using telerik with winform. I have a dataset
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is an example: I write html code inside of textarea, then I swap

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.