Each GROUP BY expression must contain at least one column that is not an outer reference.

Nan 1 Reputation point
2022-03-22T13:32:38.617+00:00

WITH transport AS (
SELECT --top 5
date_id,booking_code ,city_id,taxi_type_id,driver_id
FROM dwh.f_transport
WHERE date_id = 20220321
),
rating AS (
SELECT date_id, driver_id,booking_code,response ,dax_rating ,response_type
FROM dwh.f_ratings
WHERE date_id = 20220321
)
SELECT
c.city_name
,tt.taxi_type_simple as vertical_name
,tt.taxi_type_name
,r.dax_rating
,r.response
,d.activated as driver_activated
,COUNT(DISTINCT t.booking_code) as total_responses
,r.driver_id
FROM transport t
INNER JOIN rating r
ON t.booking_code = r.booking_code
INNER JOIN dwh.d_city c
ON c.city_id = t.city_id
INNER JOIN dwh.d_taxi_type tt
ON t.taxi_type_id = tt.taxi_type_id
INNER JOIN dwh.d_driver d
ON d.driver_id = t.driver_id
WHERE r.date_id= 20220321
AND r.response_type in ('NEW_IMPROVEMENT' ,'IMPROVEMENT')
AND r.dax_rating < 5
AND c.country_id = 9
GROUP BY 1,2,3,4,5,6,8 ;

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Naomi Nosonovsky 8,431 Reputation points
    2022-03-23T04:11:50.047+00:00

    Instead of using numbers in the GROUP BY list every column by name. I also suggest to move r conditions inside the rating CTE, No need to repeat date condition the second time either.

    0 comments No comments

  2. Bert Zhou-msft 3,436 Reputation points
    2022-03-23T08:50:54.48+00:00

    Hi,@Nan

    Welcome to Microsoft T-SQL Q&A Forum!

    I can't understand why you're using two unrelated cte's, I'm guessing what you're trying to achieve is simply to link two tables, try the code below, and if something goes wrong please comment your expected result.

    SELECT c.city_name,tt.taxi_type_simple as vertical_name,tt.taxi_type_name,  
           r.dax_rating,r.response,d.activated as driver_activated,  
    	   COUNT(DISTINCT t.booking_code) as total_responses,r.driver_id  
    FROM   dwh.f_transport t INNER JOIN dwh.f_ratings r  
    ON     r.booking_code=t.booking_code   
           INNER JOIN dwh.d_city c  
    ON     c.city_id = t.city_id  
           INNER JOIN dwh.d_taxi_type tt  
    ON     tt.taxi_type_id=t.taxi_type_id   
           INNER JOIN dwh.d_driver d  
    ON     d.driver_id = t.driver_id  
    WHERE  r.date_id= 20220321 AND r.response_type in ('NEW_IMPROVEMENT' ,'IMPROVEMENT')  
           AND r.dax_rating < 5  
           AND c.country_id = 9  
    GROUP BY c.city_name,tt.taxi_type_simple ,tt.taxi_type_name,  
           r.dax_rating,r.response,d.activated ,r.driver_id  
    

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.