如果字段具有相同的值并添加新的计数器字段,如何实现对输出进行分组的SQL查询?

时间:2021-04-02 07:44:31

I am not so into SQL and I have the following problem.

我不是很喜欢SQL,我有以下问题。

I have to implement a simple SELECT query that return the list of all the records into a table, and it is very simple:

我必须实现一个简单的SELECT查询,将所有记录的列表返回到表中,它非常简单:

SELECT
    MD_CD.id,
    MD_CD.market_details_id,
    MD_CD.commodity_details_id
FROM MarketDetails_CommodityDetails         AS MD_CD

that return something like:

返回类似于:

id                   market_details_id    commodity_details_id
--------------------------------------------------------------
177                  13                   41                  
178                  12                   43                  
179                  12                   13                  
180                  12                   11                  
181                  12                   38                  
182                  12                   39                  
183                  12                   40                  
184                  12                   42 
185                  12                   42

Ok my problem is that I want to group the rows having the same values into the market_details_id and the commodity_details_id. I want also add a counter of the number of these rows.

好吧,我的问题是我想将具有相同值的行分组到market_details_id和commodity_details_id中。我还想添加这些行数的计数器。

So for example, considering the last 2 rows I want that these rows are groupped in a single row and I want a new counter fiend containing the value 2 (because I have 2 rows with the same values of these 2 fields).

因此,例如,考虑到最后2行,我希望这些行被分组在一行中,我想要一个包含值2的新计数器恶魔(因为我有2行具有这两个字段的相同值)。

So I think that I want to obtain something like this:

所以我认为我想得到这样的东西:

                 market_details_id    commodity_details_id     counter
------------------------------------------------------------------------------
                  13                   41                       1                 
                  12                   43                       1                   
                  12                   13                       1                   
                  12                   11                       1                   
                  12                   38                       1                   
                  12                   39                       1                   
                  40                   40                        1                   
                  12                   42                       2 

How can I implement this behavior in my query?

如何在查询中实现此行为?

1 个解决方案

#1


2  

SELECT
    COUNT(MD_CD.id) As Counter,
    MD_CD.market_details_id,
    MD_CD.commodity_details_id
FROM MarketDetails_CommodityDetails  AS MD_CD
GROUP BY   MD_CD.market_details_id,  MD_CD.commodity_details_id

#1


2  

SELECT
    COUNT(MD_CD.id) As Counter,
    MD_CD.market_details_id,
    MD_CD.commodity_details_id
FROM MarketDetails_CommodityDetails  AS MD_CD
GROUP BY   MD_CD.market_details_id,  MD_CD.commodity_details_id