SQL 按拆分数据分组
SQL group by splitted data
我有一个 table 看起来像这样:
Name | Temperament
----------------------------------------
"Husky" | "Smart, Loyal, Cute"
"Poodle"| "Smart, Cute"
"Golden"| "Cute, Loyal, Caring, Loving"
我想将这些数据投影为一组气质。
例如:
Temperament | Name | Count(Optional)
-----------------------------------------------------------
"Smart" | "Poodle", "Husky" | 2
"Loyal" | "Husky", "Golden" | 2
"Cute" | "Poodle", "Golden", "Husky" | 3
"Caring" | "Golden" | 1
"Loving" | "Golden" | 1
我的问题是我找不到一种方法来拆分 table 中的字符串并处理这些数据。
如果有人能帮我解决这个问题就太好了。
如果无法完成纯粹的 SQL,告诉我我正在使用 Entity Framework 可能会有所帮助,如果可以将解决方案写入其中,可能会更好。
谢谢大家
这完全可以做到 SQL。在 Oracle 中,您可以使用正则表达式函数和正则表达式来拆分分隔字符串,然后使用字符串聚合生成每个气质的名称列表:
with cte (name, temperament, temp, cnt, lvl) as (
select
name,
temperament,
regexp_substr (temperament, '[^, ]+', 1, 1) temp,
regexp_count(temperament, ',') cnt,
1 lvl
from mytable
union all
select
name,
temperament,
regexp_substr (temperament, '[^, ]+', 1, lvl + 1),
cnt,
lvl + 1
from cte
where lvl <= cnt
)
select
temp temperament,
listagg(name, ', ') within group(order by name) name,
count(*) cnt
from cte
group by temp
order by 1
TEMPERAMENT | NAME | CNT
:---------- | :-------------------- | --:
Caring | Golden | 1
Cute | Golden, Husky, Poodle | 3
Loving | Golden | 1
Loyal | Golden, Husky | 2
Smart | Husky, Poodle | 2
如果有人需要答案:
var result = (from t in ((from t1 in db.mytables select new {tmp= t1.TEMP1}).Concat(from t2 in db.mytables select new {tmp= t2.TEMP2}).Concat(from t3 in db.mytables select new {tmp= t3.TEMP3})) group t.tmp by t.tmp into g select new { tmp = g.Key, cnx=g.Count()}).ToList();
希望对大家有所帮助!
我有一个 table 看起来像这样:
Name | Temperament
----------------------------------------
"Husky" | "Smart, Loyal, Cute"
"Poodle"| "Smart, Cute"
"Golden"| "Cute, Loyal, Caring, Loving"
我想将这些数据投影为一组气质。 例如:
Temperament | Name | Count(Optional)
-----------------------------------------------------------
"Smart" | "Poodle", "Husky" | 2
"Loyal" | "Husky", "Golden" | 2
"Cute" | "Poodle", "Golden", "Husky" | 3
"Caring" | "Golden" | 1
"Loving" | "Golden" | 1
我的问题是我找不到一种方法来拆分 table 中的字符串并处理这些数据。 如果有人能帮我解决这个问题就太好了。
如果无法完成纯粹的 SQL,告诉我我正在使用 Entity Framework 可能会有所帮助,如果可以将解决方案写入其中,可能会更好。
谢谢大家
这完全可以做到 SQL。在 Oracle 中,您可以使用正则表达式函数和正则表达式来拆分分隔字符串,然后使用字符串聚合生成每个气质的名称列表:
with cte (name, temperament, temp, cnt, lvl) as (
select
name,
temperament,
regexp_substr (temperament, '[^, ]+', 1, 1) temp,
regexp_count(temperament, ',') cnt,
1 lvl
from mytable
union all
select
name,
temperament,
regexp_substr (temperament, '[^, ]+', 1, lvl + 1),
cnt,
lvl + 1
from cte
where lvl <= cnt
)
select
temp temperament,
listagg(name, ', ') within group(order by name) name,
count(*) cnt
from cte
group by temp
order by 1
TEMPERAMENT | NAME | CNT :---------- | :-------------------- | --: Caring | Golden | 1 Cute | Golden, Husky, Poodle | 3 Loving | Golden | 1 Loyal | Golden, Husky | 2 Smart | Husky, Poodle | 2
如果有人需要答案:
var result = (from t in ((from t1 in db.mytables select new {tmp= t1.TEMP1}).Concat(from t2 in db.mytables select new {tmp= t2.TEMP2}).Concat(from t3 in db.mytables select new {tmp= t3.TEMP3})) group t.tmp by t.tmp into g select new { tmp = g.Key, cnx=g.Count()}).ToList();
希望对大家有所帮助!