-1

I want to Group By CUSTOM_DRIVER in the query because I am getting over 1.3 million results and I just need to view them by Custom Driver for the month by store.

The query is returning me

enter image description here

I want something like this

enter image description here

I've tried using Distinct and Group By but I keep getting the error:

Attribute A.STORE_NUM must be GROUPed or used in an aggregate function

How do I get around this? The code I've been using is:

SELECT *
FROM(
SELECT
SE.STORE_NUM,
DM.MONTH_NAME_445 AS MO_445,
--DM.WEEK_START_DT_MON AS WK_BEGIN_MONDAY,
--SE.METRIC_DATE,
substring(SE.ROLLUP_NAME, 39, 20) as CUSTOM_DRIVER,
SE.DT_IMPORTED,
SE.METRIC_VALUE
FROM DNA_PUBLIC.ADMIN.SCHEDULE_EFFECTIVENESS SE
       JOIN DNA_PUBLIC.ADMIN.DAY_MAP DM ON SE.METRIC_DATE = DM.DATE_DT
       JOIN (
             SELECT DISTINCT
                METRIC_DATE,
                MAX(DATE(DT_IMPORTED)) AS MAX_DT
       FROM   DNA_PUBLIC.ADMIN.SCHEDULE_EFFECTIVENESS
       GROUP BY 1
       ) MX ON SE.METRIC_DATE = MX.METRIC_DATE
WHERE METRIC_NAME = 'Coverage Effectiveness'
AND SE.ROLLUP_NAME LIKE 'O%'
AND SE.METRIC_DATE between '07/27/2020' and '11/11/2020'
AND CAST(SE.DT_IMPORTED AS DATE) = MAX_DT
--AND CUSTOM_DRIVER = 'Truck/Truck'
AND SE.INDICATOR_NAME = 'Required'
--and STORE_NUM = 1
) AS A
Order by STORE_NUM
adura826
  • 103
  • 1
  • 1
  • 10
  • Also having a `GROUP BY` with a `DISTINCT` normally means the `DISTINCT` is redundant or your `GROUP BY` clause is wrong. It's also highly recommended you do *not* use ordinal positions in things like the `ORDER BY`/`GROUP BY` clauses and actually define your column properly. – Thom A Nov 12 '20 at 18:45
  • I was only trying them individually, I figured the two were redundant on each other. – adura826 Nov 12 '20 at 18:52

1 Answers1

0

A work around was to select the values and group by the order of the selected variables. Thanks for the all the input!

SELECT
                STORE_NUM,
                MO_445,
                
                CUSTOM_DRIVER,
                
                SUM(METRIC_VALUE) as Tot_Hrs
FROM(
SELECT
SE.STORE_NUM,
DM.MONTH_NAME_445 AS MO_445,
DM.WEEK_START_DT_MON AS WK_BEGIN_MONDAY,
SE.METRIC_DATE,
substring(SE.ROLLUP_NAME, 39, 20) as CUSTOM_DRIVER,
SE.DT_IMPORTED,
SE.METRIC_VALUE
FROM DNA_PUBLIC.ADMIN.SCHEDULE_EFFECTIVENESS SE
       JOIN DNA_PUBLIC.ADMIN.DAY_MAP DM ON SE.METRIC_DATE = DM.DATE_DT
       JOIN (
             SELECT DISTINCT
                METRIC_DATE,
                MAX(DATE(DT_IMPORTED)) AS MAX_DT
       FROM   DNA_PUBLIC.ADMIN.SCHEDULE_EFFECTIVENESS
       GROUP BY 1
       ) MX ON SE.METRIC_DATE = MX.METRIC_DATE
WHERE METRIC_NAME = 'Coverage Effectiveness'
AND SE.ROLLUP_NAME LIKE 'O%'
AND SE.METRIC_DATE between '07/27/2020' and '11/11/2020'
AND CAST(SE.DT_IMPORTED AS DATE) = MAX_DT
--AND CUSTOM_DRIVER = 'Truck/Truck'
AND SE.INDICATOR_NAME = 'Required'
and STORE_NUM = 1
) AS A
group by 1,2,3
Order by STORE_NUM
adura826
  • 103
  • 1
  • 1
  • 10