Postgresql 如何正确分组Postgres数据

Postgresql 如何正确分组Postgres数据,postgresql,go,go-gorm,Postgresql,Go,Go Gorm,在PostgreSQL中,我有这样的表: |问题|文本|类别|同意|百分比|不同意|百分比| |----------------------------------------|----------|---------------|------------------| |你支持总统的政策吗政策| 50 | 50| |你支持民主党吗政策| 32 | 68| |你支持兰尼斯特一家吗电影院| 45 | 55| |你支持斯皮尔伯格的工作吗电影院| 60 | 40| 在我的Go应用程序中,在的帮助下,

在PostgreSQL中,我有这样的表:

|问题|文本|类别|同意|百分比|不同意|百分比|
|----------------------------------------|----------|---------------|------------------|
|你支持总统的政策吗政策| 50 | 50|
|你支持民主党吗政策| 32 | 68|
|你支持兰尼斯特一家吗电影院| 45 | 55|
|你支持斯皮尔伯格的工作吗电影院| 60 | 40|
在我的Go应用程序中,在的帮助下,我向PostgreSQL数据库发出如下SQL请求:

type Entry struct {
     QuestionText string `json:"question_text"`
     Category string `json:"category"`
     AgreePercent float64 `json:"agree_percent"`
     DisagreePercent float64 `json:"disagree_percent"`
}

rows, _ := database.DBGORM.Raw("SELECT * FROM SPECIFICATION").Rows()

for rows.Next() {
     entry := &Entry{}

     if err = rows.Scan(&entry.QuestionText, & entry.Category,  &entry.AgreePercent, &entry.DisagreePercent); err != nil {
          utils.Logger().Println(err)   
     }
}
如何得到类似的结果?如您所见,数组中的每个对象都按
类别
列中的值分组:

[
     {
          category: "Policy",
          questions: ["Do you support the President's policy?", "Do you support Democrats?"],
          series: [
               {
                    name: "Agree, %",
                    data: [50, 32]
               },
               {
                    name: "Disagree, %",
                    data: [50, 68]
               },
          ] 
     },
     {
          category: "Cinema",
          questions: ["Do you support the Lannisters?", "Do you support Spielberg's work?"],
          series: [
               {
                    name: "Agree, %",
                    data: [45, 60]
               },
               {
                    name: "Disagree, %",
                    data: [55, 40]
               },
          ] 
     },
]

嗯,我不能说这是一种优雅的方式,但最终我解决了我的任务:

// Create struct called "Series".
type Series struct {
    Name string `json:"name"`
    Data []float64 `json:"data"`
}

// Create struct called "Specification".
type Specification struct {
    Category string `json:"category"`
    Questions []string `json:"questions"`
    Series []Series `json:"series"`
}

// Initialize an array of struct.
var specifications []Specification

// Initialize several variables.
var catogoryName string
var arrayQuestionText []string
var arrayAgreePercent []float64
var arrayDisagreePercent []float64


for rows.Next() {
    // Check the change in the name of the category.
    if entry.Category == catogoryName {
        // Add new elements to arrays.
        arrayQuestionText = append(arrayQuestionText, entry.QuestionText)
        arrayDisagreePercent = append(arrayDisagreePercent, entry.DisagreePercent)
        arrayAgreePercent = append(arrayAgreePercent, entry.AgreePercent)
    } else {
        if len(catogoryName) > 0 {
            // Fill the struct with data.
            specification := Specification{
                Category: catogoryName,
                Questions: arrayQuestionText,
                Series: []Series{
                    {
                        Name: "Agree, %",
                        Data: arrayAgreePercent,
                    },
                    {
                        Name: "Disagree, %",
                        Data: arrayDisagreePercent,
                    },
                },
            }

            // Add new struct to array.
            specifications = append(specifications, specification)
        }
    }
    // Update data in arrays.
    catogoryName = entry.Category
    arrayQuestionText = nil
    arrayQuestionText = append(arrayQuestionText, entry.QuestionText)
    arrayDisagreePercent = nil
    arrayDisagreePercent = append(arrayDisagreePercent, entry.DisagreePercent)
    arrayAgreePercent = nil
    arrayAgreePercent = append(arrayAgreePercent, entry.AgreePercent)
}

最后一个片段是您想要得到的还是您实际得到的?“如何获得类似的结果?”–类似于什么?您没有显示此输出是从何处生成的,因此很难知道分组的作用是什么。我的猜测是,稍后会有一些逻辑进行分组,并打印分组后的输出。如果在从数据库读取扫描行时打印出每一行,则应看到pg的原始近似值table@EliBendersky目前没有分组。我想要上面例子中的输出。你知道我怎么做吗?@mkrieger1你好!在文章的最后,您可以注意到我希望在golang应用程序中生成的JSON输出。如何得到这样的结果?你有什么想法吗?