SQL group by将第二组结合起来并得到不同的第一组

时间:2019-03-01 21:06:48

标签: sql sql-server group-by

查询当前返回

Bob    President     5
Joe    Programmer    3
Greg   Programmer    2
Joe    Janitor       6
Liz    Programmer    6

但是我希望这样的每个人只有一行

Bob    President     5
Joe    Programmer (or Janitor)  9
Greg   Programmer    2
Liz    Programmer    6

我现在按名称和角色分组。我如何也只能获得唯一的名称并合并该值?

SELECT
   ...
FROM 
    vw_QuickbaseAudit v
    INNER JOIN [staging].[QuickbaseSystemUsers] qsu ON v.[user] = qsu.UserId
WHERE 
    Date_Modified >= @StartDate
    AND Date_Modified < DATEADD(dd, 1, @EndDate)
GROUP BY
    qsu.UserFirstName + ' ' + qsu.UserLastName, 
    qsu.RoleName
ORDER BY 
    RoleName, 
    NumberOfEditTotal DESC;

3 个答案:

答案 0 :(得分:2)

{
    "id": 259609,
    "parent_id": 0,
    "number": "CW-104-270219",
    "order_key": "xxxxxxxxxxxx",
    "created_via": "checkout",
    "version": "3.5.5",
    "status": "completed",
    "currency": "USD",
    "date_created": "2019-02-27T15:11:58",
    "date_created_gmt": "2019-02-27T20:11:58",
    "date_modified": "2019-02-27T15:11:59",
    "date_modified_gmt": "2019-02-27T20:11:59",
    "discount_total": "0.00",
    "discount_tax": "0.00",
    "shipping_total": "0.00",
    "shipping_tax": "0.00",
    "cart_tax": "0.00",
    "total": "9.68",
    "total_tax": "0.00",
    "prices_include_tax": false,
    "customer_id": 2,
    "customer_ip_address": "xxxxxxxxxxxx",
    "customer_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36",
    "customer_note": "",
    "billing": {
        "first_name": "Valerie",
        "last_name": "Criswell",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "postcode": "",
        "country": "",
        "email": "xxxxxxxxxx",
        "phone": ""
    },
    "shipping": {
        "first_name": "",
        "last_name": "",
        "company": "",
        "address_1": "",
        "address_2": "",
        "city": "",
        "state": "",
        "postcode": "",
        "country": ""
    },
    "payment_method": "authorize_net_aim",
    "payment_method_title": "Credit Card",
    "transaction_id": "xxxxxxxxxxxxx",
    "date_paid": "2019-02-27T15:11:59",
    "date_paid_gmt": "2019-02-27T20:11:59",
    "date_completed": "2019-02-27T15:11:59",
    "date_completed_gmt": "2019-02-27T20:11:59",
    "cart_hash": "xxxxxxxxxxxxx",
    "meta_data": [
        {
            "id": 8785641,
            "key": "_wcson_order_number",
            "value": "CWD-259609-022719-108"
        },
        {
            "id": 8785642,
            "key": "_order_number",
            "value": "104"
        },
        {
            "id": 8785643,
            "key": "_order_number_formatted",
            "value": "CW-104-270219"
        },
        {
            "id": 8785644,
            "key": "_order_number_meta",
            "value": {
                "prefix": "CW-",
                "suffix": "-{DD}{MM}{YY}",
                "length": "2"
            }
        },
        {
            "id": 8785645,
            "key": "_wc_authorize_net_aim_retry_count",
            "value": "0"
        },
        {
            "id": 8785646,
            "key": "_wc_authorize_net_aim_trans_id",
            "value": "xxxxxxxx"
        },
        {
            "id": 8785647,
            "key": "_wc_authorize_net_aim_trans_date",
            "value": "2019-02-27 15:11:59"
        },
        {
            "id": 8785648,
            "key": "_wc_authorize_net_aim_environment",
            "value": "production"
        },
        {
            "id": 8785649,
            "key": "_wc_authorize_net_aim_account_four",
            "value": "xxxxxxxx"
        },
        {
            "id": 8785650,
            "key": "_wc_authorize_net_aim_authorization_amount",
            "value": "xxxxxxx"
        },
        {
            "id": 8785651,
            "key": "_wc_authorize_net_aim_authorization_code",
            "value": "xxxxxxx"
        },
        {
            "id": 8785652,
            "key": "_wc_authorize_net_aim_charge_captured",
            "value": "yes"
        },
        {
            "id": 8785653,
            "key": "_wc_authorize_net_aim_card_expiry_date",
            "value": "21-08"
        },
        {
            "id": 8785654,
            "key": "_wc_authorize_net_aim_card_type",
            "value": "mastercard"
        },
        {
            "id": 8785659,
            "key": "_pip_invoice_number",
            "value": "CW-104-270219"
        },
        {
            "id": 8785660,
            "key": "_wc_pip_invoice_email_count",
            "value": "1"
        },
        {
            "id": 8785661,
            "key": "_wc_pip_packing_list_email_count",
            "value": "1"
        }
    ],
    "line_items": [
        {
            "id": 17,
            "name": "I Give Myself Away -   MP3 Download - 30 Day Access",
            "product_id": 192127,
            "variation_id": 192128,
            "quantity": 1,
            "tax_class": "",
            "subtotal": "9.68",
            "subtotal_tax": "0.00",
            "total": "9.68",
            "total_tax": "0.00",
            "taxes": [],
            "meta_data": [
                {
                    "id": 182,
                    "key": "pa_format",
                    "value": "download-30-day"
                }
            ],
            "sku": "131599-DL30DAY",
            "price": 9.68
        }
    ],
    "tax_lines": [],
    "shipping_lines": [],
    "fee_lines": [],
    "coupon_lines": [],
    "refunds": []
}

那个的一些变体 (编辑:错过了分组依据中的其他STUFF命令)

答案 1 :(得分:1)

基本上,您需要从RoleName子句中删除GROUP BY。您可以使用STRING_AGG()进行字符串聚合:

SELECT
   qsu.UserFirstName + ' ' + qsu.UserLastName,
   STRING_AGG(RoleName, ' or ') RoleNames,
   ...
FROM 
    vw_QuickbaseAudit v
    INNER JOIN [staging].[QuickbaseSystemUsers] qsu ON v.[user] = qsu.UserId
WHERE 
    Date_Modified >= @StartDate
    AND Date_Modified < DATEADD(dd, 1, @EndDate)
GROUP BY
    qsu.UserFirstName,
    qsu.UserLastName
ORDER BY 
    NumberOfEditTotal DESC;

答案 2 :(得分:1)

我被解释为:

Joe    Programmer (or Janitor)  9

说这两个都可以:

Joe    Programmer  9
Joe    Janitor     9

查询如下:

SELECT qsu.UserFirstName + ' ' + qsu.UserLastName, 
       MIN(qsu.RoleName) as RoleName
       SUM(NumberOfEditTotal) as NumberOfEditTotal
FROM vw_QuickbaseAudit v JOIN
     [staging].[QuickbaseSystemUsers] qsu 
     ON v.[user] = qsu.UserId
WHERE Date_Modified >= @StartDate AND
      Date_Modified < DATEADD(day, 1, @EndDate)
GROUP BY qsu.UserFirstName + ' ' + qsu.UserLastName
ORDER BY RoleName, NumberOfEditTotal DESC;

目前尚不清楚NumberOfEditTotal的计算方式。

编辑:

棘手地追加所有行。在最新版本的SQL Server中,可以将STRING_AGG()用作字符串聚合函数。那很简单。

我在早期版本中的建议是一个折衷方案。因此值可能是:

Janitor
Janitor and Programmer 
Janitor, Programmer, and others

您可以执行以下操作:

(case when min(rolename) = max(rolename)
      then min(rolename)
      when count(distinct rolename) = 2
      then min(rolename) + ' and ' + max(rolename)
      else min(rolename) + ', ' + max(rolename) + ' others'
 end)