SQL Query returns multiple rows of the same record when View includes one-to-many table

Rufyan

In MS-SQL, I have a View 'ListingResult' which contains rows from tables 'ListingCategoryXref' and 'Listing'. This is the View statement:

SELECT        
    dbo.Listing.ListingName,  
    dbo.Listing.ListingId,  
    dbo.ListingCategoryXref.CategoryId
FROM    dbo.Listing INNER JOIN
        dbo.ListingCategoryXref ON dbo.Listing.ListingId = dbo.ListingCategoryXref.ListingId

GROUP BY 
    dbo.Listing.ListingName, 
    dbo.Listing.ListingId, 
        dbo.ListingCategoryXref.CategoryId

Listings can have many rows in ListingCategoryXref, thus.

ListingResult (View)
Listing (table)
ListingId   ListingName     StateId
1           Toms bar        3
2           French place    5

ListingCategoryXref (table)
ListingId   CategoryId
1           10
1           15

The query below returns a row per Listing per ListingCategoryXref.

SELECT TOP(26) 
        [ListingResult].[ListingId],
        [ListingResult].[ListingName]
FROM    [ListingResult]     
WHERE   [ListingResult].[StateId] = 3

So 'Tom's Bar' is returned twice as it has two categories. I figure I can either change the query above, or change the ListingResult View in SQL. I still need to return 26 results which I can't dictate if I use a wrapped select statement with ROW_NUMBER() OVER(PARTITION BY ListingId. (Is that true?) I'm using LLBLGen to access the DB so I'd prefer to change the view, if that is possible? Apologies for my newness to SQL being that obvious.

Spock

From the query above, the following result will be returned...

ListingName | ListingId | CategoryId
Toms bar | 1 | 10
Toms bar |1 | 15

If you only want Toms bar to be returned once, you'll need to remove the CategoryId column from the result set, and the group by clause, or add CategoryId to an agrgate function, and remove it from the group by clause i.e.

SELECT
    dbo.Listing.ListingName,
    dbo.Listing.ListingId,
    COUNT(dbo.ListingCategoryXref.CategoryId) as Categories
FROM dbo.Listing 
    INNER JOIN dbo.ListingCategoryXref ON dbo.Listing.ListingId = dbo.ListingCategoryXref.ListingId
GROUP BY dbo.Listing.ListingName, dbo.Listing.ListingId

Which will return...

ListingName | ListingId | Categories
Toms bar | 1 | 2

Can you give an example of what you would like to see?

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 returns multiple rows of the same record when View includes one-to-many table

From Dev

How to join these rows into one record in the same table

From Dev

sql query in many to many relation returns single record

From Dev

SQL Query to count multiple values from one table into specific view

From Dev

SQL query which returns variable number of columns for each record combining multiple rows

From Dev

Optimize one to many relation in same table query

From Dev

How to display one row when table has multiple rows related to Single record in other table?

From Dev

Eloquent model with multiple one to many to same table

From Dev

SQL query returns 0 rows for multiple AND conditions

From Dev

SQL Query - Join multiple fields to one record

From Dev

Combine multiple rows in one from same table

From Dev

Combine multiple rows in one from same table

From Dev

Join multiple rows of one table on one record (Codeigniter 3)

From Dev

Reuse of same layouts inside one layout using multiple includes - TabLayout + ViewPager + RecyclerView (multiple viewpagers in one View)

From Dev

Merging multiple rows from one table into multiple colums in a SQL Query Result Set

From Dev

Get last record by date for multiple table with many-to-one relationship

From Dev

Multiple joins on the same table with counting in one query

From Dev

Access query distinct record when all rows same

From Dev

Many SQL rows into one

From Dev

SQL: query one column in same table

From Dev

Laravel: Show multiple rows one by one under same id in a table

From Dev

SQL Query to compare record rows between each other in single table

From Dev

SQL Query to compare record rows between each other in single table

From Dev

SQL Query: Multiple select and sums on the same table

From Dev

Sqlalchemy single query for multiple rows from one column in one table

From Dev

Too many rows when Joining to same table twice

From Dev

how to iterate on a table in sql server for the same id in many rows?

From Dev

MySQL query - single row in one table with multiple rows in another table

From Dev

How to delete record for multiple table in one query using oracle

Related Related

  1. 1

    SQL Query returns multiple rows of the same record when View includes one-to-many table

  2. 2

    How to join these rows into one record in the same table

  3. 3

    sql query in many to many relation returns single record

  4. 4

    SQL Query to count multiple values from one table into specific view

  5. 5

    SQL query which returns variable number of columns for each record combining multiple rows

  6. 6

    Optimize one to many relation in same table query

  7. 7

    How to display one row when table has multiple rows related to Single record in other table?

  8. 8

    Eloquent model with multiple one to many to same table

  9. 9

    SQL query returns 0 rows for multiple AND conditions

  10. 10

    SQL Query - Join multiple fields to one record

  11. 11

    Combine multiple rows in one from same table

  12. 12

    Combine multiple rows in one from same table

  13. 13

    Join multiple rows of one table on one record (Codeigniter 3)

  14. 14

    Reuse of same layouts inside one layout using multiple includes - TabLayout + ViewPager + RecyclerView (multiple viewpagers in one View)

  15. 15

    Merging multiple rows from one table into multiple colums in a SQL Query Result Set

  16. 16

    Get last record by date for multiple table with many-to-one relationship

  17. 17

    Multiple joins on the same table with counting in one query

  18. 18

    Access query distinct record when all rows same

  19. 19

    Many SQL rows into one

  20. 20

    SQL: query one column in same table

  21. 21

    Laravel: Show multiple rows one by one under same id in a table

  22. 22

    SQL Query to compare record rows between each other in single table

  23. 23

    SQL Query to compare record rows between each other in single table

  24. 24

    SQL Query: Multiple select and sums on the same table

  25. 25

    Sqlalchemy single query for multiple rows from one column in one table

  26. 26

    Too many rows when Joining to same table twice

  27. 27

    how to iterate on a table in sql server for the same id in many rows?

  28. 28

    MySQL query - single row in one table with multiple rows in another table

  29. 29

    How to delete record for multiple table in one query using oracle

HotTag

Archive