0

Suppose I have a table called cnms_table in my PostgresSQL database that is equivalent to a pandas dataframe called cnms_df that I created in a Python script. In the Python dataframe, I was able to use groupby and agg to summarize and aggregate the dataframe based on specific columns/fields to create a new summarized dataframe called sum_df.

 sum_df_prelim = cnms_df.groupby(['Region', 'State',  'CO_FIPS', 'Tiermetric_Prelim',
                              'Mod_unMod_Prelim', 'Val_Combined_Prelim', 'Det_Approx_Prelim',
                              'Decay_Date_Prelim'], as_index=False).agg({'Actual_Miles_Prelim': 'sum'})

How would I do this in a Postgres SQL query or function?

1 Answer 1

1
SELECT
  SUM(Actual_Miles_Prelim)
FROM
  cnms_table
GROUP BY
  'Region',
  'State',
  'CO_FIPS',
  'Tiermetric_Prelim',
  'Mod_unMod_Prelim',
  'Val_Combined_Prelim', 
  'Det_Approx_Prelim',
  'Decay_Date_Prelim'
Sign up to request clarification or add additional context in comments.

1 Comment

This worked but only after I removed the ' 's from the column names. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.