Skip to main content
Notice removed Insufficient justification by Jamal
added 2 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Using overOVER, you can do it in one pass:

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;

Using over you can do it in one pass

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;

Using OVER, you can do it in one pass:

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;
added 28 characters in body
Source Link
paparazzo
  • 6k
  • 3
  • 19
  • 41

Using over you can do it in one pass

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;

Using over

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;

Using over you can do it in one pass

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;
added 86 characters in body
Source Link
paparazzo
  • 6k
  • 3
  • 19
  • 41

Using over

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;
With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;

Using over

With Cte1 (ZoneId, QuestionId, Count1, sum1, rn)
AS
(
    Select 
        tz.ZoneId, 
        qn.QuestionId 
      , COUNT(1) OVER(PARTITION BY tz.ZoneId, qn.QuestionId) AS "Count1"  
      , COUNT(1) OVER(PARTITION BY tz.ZoneId) AS "Sum1"
      , row_number() OVER(PARTITION BY tz.ZoneId, qn.QuestionId order by ShopCode) AS "rn"
    From 
        Customers cust 
        Inner Join Shops td on td.ShopCode = cust.ShopCode
        Inner Join Zones tz on tz.Id = td.SlsZone
        Inner Join Responses res on res.SampleId = cust.SampleId
        Inner Join QstNodes qn on qn.QuestionId = res.QstNodeId
    Where 
        Event >= 201201 And Event <= 201212
)
SELECT *
FROM    
    Cte1 outercte 
where 
    rn = 1;
Notice added Insufficient justification by Jamal
Source Link
paparazzo
  • 6k
  • 3
  • 19
  • 41
Loading