Returning duplicates based on one value in SQL

user3638575

So I've been working with SQL Server 2014 Express for the first time and I've encountered a problem.

Basically, what I've been asked to do (as part of an assignment in school) is to return values from a regular "customer-table", based entirely on the customer's stored location.

My query looks like this atm:

SELECT 
    City, 
    COUNT(Clients.ClientNo) AS AmountOfClients, 
    Orders.Pieces * products.Price TotalPrice
FROM 
    Clients
INNER JOIN 
    ClientOrder ON Clients.ClientNo = ClientOrder.ClientNo
INNER JOIN 
    Orders ON ClientOrder.OrderNo = Orders.OrderNo
INNER JOIN 
    products ON Orders.ProductNo = products.ProductNo
GROUP BY 
    City, ClientOrders.Pieces, products.Price
HAVING 
    COUNT(Clients.ClientNo) > 1

Now to explain it - I've been trying to find all customers where the contents of the "City" column re-occurs and return only those - along with the total amount of their orders (seen here as currency) based on the contents of three other tables (which is why the JOINs are there). The latter part is working, but the wrong clients get returned and instead of returning the actual "ClientNo", only the amount of clients found get returned. I can't seem to find the right course of action here.

Ideally, the returned information/output would look somewhat like this:

City, ClientNo, TotalCost     -- for each client that lives in the same City as another

Any input would be appreciated.

Piyush Parashar

You should use only City in the group by clause and also use

SUM(Orders.Pieces * products.Price) TotalPrice

in the SELECT clause to get the total sum from each city order (Sum of Products).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL query returning duplicates

From Dev

Laravel SQL Query Returning Duplicates

From Dev

SQL Statement to eliminate duplicates based on value in another column

From Dev

SQL Server : duplicates value

From Dev

SQL Server stored procedure only returning one value

From Dev

remove duplicates based on a value in a row

From Dev

Delete duplicates based on Group By - SQL

From Java

Returning a value based on matching one lists values to another list based on order

From Dev

R - Finding minimum values based on multiple conditions and returning one or multiple created strings based on the minimum value

From Dev

SQL Count duplicates in one column but condition for duplicate is not only one fixed value

From Dev

How Can I Merge All Duplicates In Array Based On One Key's Value?

From Dev

SQL: SELECT value for all rows based on a value in one of the rows and a condition

From Dev

Advanced SQL Query Design Help (Duplicates across two tables, multiple fields, possible exclusions based on one field)

From Dev

MS Access - Remove duplicates based on three columns while keeping one result without using SQL Min/Max

From Dev

Returning a value based on multiple criteria

From Dev

How to delete one of the duplicates based on another column?

From Dev

Excel duplicates in one column based on another

From Dev

Remove duplicates in one column based on another column

From Dev

SQL, only returning rows where at least one more entry with the same value in one column is found

From Dev

SQL, only returning rows where at least one more entry with the same value in one column is found

From Dev

Avoid duplicates rows which have one different column value in SQL Server

From Dev

SQL Server - Select data from one table based on a string value

From Dev

SQL If-Else logic based on value of one table

From Dev

Filtering SQL query based on parameters with more than one value

From Dev

If duplicates exist, select the value based on another column

From Dev

deleting duplicates based on value of another column

From Dev

remove duplicates based on the value of another column

From Dev

Print only one value from duplicates

From Dev

Filter out the duplicates based on a group in SQL

Related Related

  1. 1

    SQL query returning duplicates

  2. 2

    Laravel SQL Query Returning Duplicates

  3. 3

    SQL Statement to eliminate duplicates based on value in another column

  4. 4

    SQL Server : duplicates value

  5. 5

    SQL Server stored procedure only returning one value

  6. 6

    remove duplicates based on a value in a row

  7. 7

    Delete duplicates based on Group By - SQL

  8. 8

    Returning a value based on matching one lists values to another list based on order

  9. 9

    R - Finding minimum values based on multiple conditions and returning one or multiple created strings based on the minimum value

  10. 10

    SQL Count duplicates in one column but condition for duplicate is not only one fixed value

  11. 11

    How Can I Merge All Duplicates In Array Based On One Key's Value?

  12. 12

    SQL: SELECT value for all rows based on a value in one of the rows and a condition

  13. 13

    Advanced SQL Query Design Help (Duplicates across two tables, multiple fields, possible exclusions based on one field)

  14. 14

    MS Access - Remove duplicates based on three columns while keeping one result without using SQL Min/Max

  15. 15

    Returning a value based on multiple criteria

  16. 16

    How to delete one of the duplicates based on another column?

  17. 17

    Excel duplicates in one column based on another

  18. 18

    Remove duplicates in one column based on another column

  19. 19

    SQL, only returning rows where at least one more entry with the same value in one column is found

  20. 20

    SQL, only returning rows where at least one more entry with the same value in one column is found

  21. 21

    Avoid duplicates rows which have one different column value in SQL Server

  22. 22

    SQL Server - Select data from one table based on a string value

  23. 23

    SQL If-Else logic based on value of one table

  24. 24

    Filtering SQL query based on parameters with more than one value

  25. 25

    If duplicates exist, select the value based on another column

  26. 26

    deleting duplicates based on value of another column

  27. 27

    remove duplicates based on the value of another column

  28. 28

    Print only one value from duplicates

  29. 29

    Filter out the duplicates based on a group in SQL

HotTag

Archive