SQL Server 2005 error when grouping using subquery

前端 未结 2 1014
长情又很酷
长情又很酷 2021-01-28 13:06

Using SQL Server 2005 I\'m trying to group based on a case statement with a subquery, but I\'m getting an error (\"Each GROUP BY expression must contain at least one column refe

2条回答
  •  太阳男子
    2021-01-28 13:20

    You are telling it to group by 1 or 0 when you need to give it an actual column to group by (header), not value.

    So if I'm understanding right you are wanting a list of the headers and a count of their detail records?

    This may work for you?

    SELECT DISTINCT h.header, COUNT(d.detail) AS detail_count
    FROM #header AS h
    LEFT JOIN #detail AS d ON d.header = h.header
    GROUP BY h.header, d.detail
    

    With results like...

    header   detail_count
    1       1
    2       1
    3       0
    

提交回复
热议问题