How can I stop SQL Server query from returning multile rows for the same item ID in this query

James

First let me say this: I cannot use the Distinct keyword. There are column types in the returned data that it cannot compare.

Here is my query:

SELECT
    tblTemp.maxBid, tblAbsentee.abs_bid_price, 
    tblAbsentee.abs_date, tblSale.sale_name, tblSale.sale_date,
    tblItem.*,
    tblTemp.bid_count
FROM 
    tblAbsentee 
INNER JOIN 
    tblItem ON tblAbsentee.abs_item_id = tblItem.item_id AND tblAbsentee.abs_sale_id = tblItem.item_sale_id 
INNER JOIN 
    tblBidder ON tblAbsentee.abs_bidder_id = tblBidder.bidder_number AND tblAbsentee.abs_sale_id = tblBidder.bidder_sale_id 
LEFT JOIN 
    tblSale ON tblAbsentee.abs_sale_id = tblSale.sale_id 
INNER JOIN 
    (SELECT 
        COUNT(tblAbsentee.abs_id) As bid_count, 
        CASE 
            WHEN NOT (MAX(tblAbsentee.abs_bid_price) IS NULL) THEN MAX(tblAbsentee.abs_bid_price) ELSE 0 
        END As maxBid, 
        tblItem.item_id 
     FROM 
        tblAbsentee 
     INNER JOIN 
        tblItem ON tblAbsentee.abs_item_id = tblItem.item_id AND tblAbsentee.abs_sale_id = tblItem.item_sale_id 
     INNER JOIN  
        tblBidder ON tblAbsentee.abs_bidder_id = tblBidder.bidder_number AND tblAbsentee.abs_sale_id = tblBidder.bidder_sale_id 
     LEFT JOIN 
        tblSale ON tblAbsentee.abs_sale_id = tblSale.sale_id 
     WHERE 
        DATEADD(day, 7, tblSale.sale_date) >= GETDATE() 
     GROUP BY
        tblItem.item_id) As tblTemp On tblItem.item_id = tblTemp.item_id AND tblAbsentee.abs_bid_price = tblTemp.maxBid 
WHERE 
    DATEADD(day, 7, tblSale.sale_date) >= GETDATE() 
    AND tblBidder.bidder_mail_id = 13096 
ORDER BY 
    tblItem.item_lot ASC

So long as the bidder only leaves one bid, this works fine. But as soon as the bidder leaves another bid I get the error

System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Due to using a data table for the results.

Here's some short sample data:

+--------+-------------+--------------------+-------+
|Max Bid |Abs_bid_price|abs_date            |item_id|
+--------+-------------+--------------------+-------+
|600.0000|600.0000     |1/15/2014 2:40:16 PM|135827 |
+--------+-------------+--------------------+-------+
|600.0000|600.0000     |1/15/2014 2:40:16 PM|135827 |
+--------+-------------+--------------------+-------+
|600.0000|600.0000     |1/15/2014 2:40:16 PM|135827 |
+--------+-------------+--------------------+-------+
|600.0000|600.0000     |1/15/2014 2:40:16 PM|135827 |
+--------+-------------+--------------------+-------+
|600.0000|600.0000     |1/15/2014 2:40:16 PM|135827 |
+--------+-------------+--------------------+-------+
|600.0000|600.0000     |1/15/2014 2:40:16 PM|135827 |
+--------+-------------+--------------------+-------+
|20.0000 |20.0000      |1/15/2014 2:42:16 PM|123562 |
+--------+-------------+--------------------+-------+

I'm not going to bother putting in all of the columns. Just trust me when I say that they all contain the same data. In most cases, the abs_date column would NOT be the exact same. This was just a fluke. In either case, I would like to just group it by item_id and sort it by the abs_date field asc when grouping so that the first max bid left will be the one shown. If this was MySQL it'd be easy. I just don't know enough about SQL Server to figure this out, unfortunately, and cannot find a similar issue. Any help is much appreciated.

EDIT

It may not have been clear in my question, but I could validly get multiple rows returned, I just want to only get one of each item ID.

Benjamin Beaulieu

You can use a Common Table Expression. This will return the first occuring item_id.

;WITH CTE AS
(
    SELECT *, ROW_NUMBER() OVER(PARTITION BY item_id ORDER BY abs_date) Rank
    FROM tblAbsentee
    ...
)
SELECT *
FROM CTE
WHERE Rank = 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I return rows from an SQL query where one unique ID satisfies conditions in different rows?

From Dev

PHP SQL query not returning same rows as in phpMyAdmin

From Dev

How can i query item from list?

From Dev

How can I correctly implement an Hibernate SQL query starting from an SQL query that count the number of rows?

From Dev

SQL Server query is not returning any rows

From Dev

SQL Server Query returning duplicate rows

From Dev

SQL query for returning rows from two tables

From Dev

How can I write this SQL Server query?

From Dev

Can I create a view that will query a table from another sql server on another server but same domain

From Dev

SQL Server - Query. How do I split a column into 2 where the ID of rows are duplicated?

From Dev

SQL Server - Query. How do I split a column into 2 where the ID of rows are duplicated?

From Dev

How can I extend this sql query to delete all rows which match the ID of its results?

From Dev

How can I get the id, min and max in the same My SQL query?

From Dev

Merge MY SQL query result with multiple rows for same item id into single row for json response

From Dev

SQL query: same rows

From Dev

SQL query: same rows

From Dev

How can I get the average of the top N rows of a SQL query?

From Dev

How can I update multiple rows in a table with SQL query?

From Dev

How Can I Flatten the Results of a SQL Query - Transposing Rows to Columns?

From Dev

How can I create an SQL query from a List<string> contained in a Session["id"]?

From Java

How can I show the table structure in SQL Server query?

From Dev

How can I make this query recursive Sql Server?

From Dev

How can I exclude weekends and holidays in SQL Server query

From Dev

How I can group the results by day in this query with SQL Server?

From Dev

How can I convert this query result to date format (SQL Server)

From Dev

How can I write a query in SQL Server on XML column

From Dev

How can I display tablenames in resultheader of SQL Server 2008 query

From Dev

How can I secure this sql query from SQL Injection in Laravel?

From Dev

How can I query wikidata with a freebase id from a youtube video?

Related Related

  1. 1

    How can I return rows from an SQL query where one unique ID satisfies conditions in different rows?

  2. 2

    PHP SQL query not returning same rows as in phpMyAdmin

  3. 3

    How can i query item from list?

  4. 4

    How can I correctly implement an Hibernate SQL query starting from an SQL query that count the number of rows?

  5. 5

    SQL Server query is not returning any rows

  6. 6

    SQL Server Query returning duplicate rows

  7. 7

    SQL query for returning rows from two tables

  8. 8

    How can I write this SQL Server query?

  9. 9

    Can I create a view that will query a table from another sql server on another server but same domain

  10. 10

    SQL Server - Query. How do I split a column into 2 where the ID of rows are duplicated?

  11. 11

    SQL Server - Query. How do I split a column into 2 where the ID of rows are duplicated?

  12. 12

    How can I extend this sql query to delete all rows which match the ID of its results?

  13. 13

    How can I get the id, min and max in the same My SQL query?

  14. 14

    Merge MY SQL query result with multiple rows for same item id into single row for json response

  15. 15

    SQL query: same rows

  16. 16

    SQL query: same rows

  17. 17

    How can I get the average of the top N rows of a SQL query?

  18. 18

    How can I update multiple rows in a table with SQL query?

  19. 19

    How Can I Flatten the Results of a SQL Query - Transposing Rows to Columns?

  20. 20

    How can I create an SQL query from a List<string> contained in a Session["id"]?

  21. 21

    How can I show the table structure in SQL Server query?

  22. 22

    How can I make this query recursive Sql Server?

  23. 23

    How can I exclude weekends and holidays in SQL Server query

  24. 24

    How I can group the results by day in this query with SQL Server?

  25. 25

    How can I convert this query result to date format (SQL Server)

  26. 26

    How can I write a query in SQL Server on XML column

  27. 27

    How can I display tablenames in resultheader of SQL Server 2008 query

  28. 28

    How can I secure this sql query from SQL Injection in Laravel?

  29. 29

    How can I query wikidata with a freebase id from a youtube video?

HotTag

Archive