Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQL-如何实现类别的评分和匹配?_Python_Sql - Fatal编程技术网

Python SQL-如何实现类别的评分和匹配?

Python SQL-如何实现类别的评分和匹配?,python,sql,Python,Sql,我试图在MySQL中实现评分和匹配。我有一个表,它有三个级别:级别1(最终父级)、级别2(父级)和级别3(子级)。 我试图为从外部接收到的每个新数据类别分配一个分数,并将其分配给我的表中的特定类别IDscore+=r.score*weight +----+------------------------------------+-----------------------------+--------------------+ | ID | LEVEL1

我试图在MySQL中实现评分和匹配。我有一个表,它有三个级别:级别1(最终父级)、级别2(父级)和级别3(子级)。 我试图为从外部接收到的每个新数据类别分配一个分数,并将其分配给我的表中的特定类别ID
score+=r.score*weight

+----+------------------------------------+-----------------------------+--------------------+
| ID | LEVEL1                             | LEVEL2                      | LEVEL3             |
+----+------------------------------------+-----------------------------+--------------------+
| 1  |  Arts and Entertainment Businesses |  Casinos                    |  NULL              |
| 1  |  Arts and Entertainment Businesses |  Performing Arts Businesses |  Radio Stations    |
| 2  |  Auto Sales Businesses             |  Motorcycle Dealers         |  Motorcycle Parts  |
| 2  |  Auto Sales Businesses             |  RVs and Motor Home Dealers |  NULL              |
| 2  |  Auto Sales Businesses             |  Car Dealers                |  Used Cars Dealers |
| 3  |  Bars and Lounges                  |  Pubs and Dive Bars         |  Pubs              |
| 3  |  Bars and Lounges                  |  Wine Bars                  |  NULL              |
| 4  |  Restaurants                       |  American Restaurants       |  Barbeque          |
+----+------------------------------------+-----------------------------+--------------------+
上面是我的主表,它有类别

我想做什么:

If input = 'Radio',
   Then match to LEVEL3 'Radio Station' with score less than 1.0
If LEVEL3 is NULL, Move up to LEVEL2
   Then Match to LEVEL2 
IF LEVEL2 is NULL, Move up to LEVEl1
   Then Match to LEVEL1

scores: 0.0(No Match) to 1.00 (Exact match)
        0.8 - 0.99 (Very good Match)
Input = Used Cars
Output = [ID: 2 ,LEVEL1 : Auto Sales Business]
我试图计算所有输入变量的接近度得分,然后为它们分配ID。如果Level 3和Level 2中没有数据,则每个Level 1都有一行,其中Level 2和Level 3为
Other
,这将是最低得分匹配

我真的在尝试包装这是SQL,而不必使用Python ML/AI并对其进行过度设计。(如果在SQL中不可能,那么我将转到Python) 任何想法都会有帮助

预期结果:

If input = 'Radio',
   Then match to LEVEL3 'Radio Station' with score less than 1.0
If LEVEL3 is NULL, Move up to LEVEL2
   Then Match to LEVEL2 
IF LEVEL2 is NULL, Move up to LEVEl1
   Then Match to LEVEL1

scores: 0.0(No Match) to 1.00 (Exact match)
        0.8 - 0.99 (Very good Match)
Input = Used Cars
Output = [ID: 2 ,LEVEL1 : Auto Sales Business]

请参阅:我知道这可能不完全属于技术问题/代码错误。我确实理解不发布讨论并专注于获得答案的规则。任何指针/SQL代码/Python脚本都会对我有很大帮助。谢谢。

这里是tsql中的一个选项,使用字符长度分配分数。-

DECLARE @imput varchar(300) = 'Radio';

WITH Data AS (
SELECT  1  as id,  'Arts and Entertainment Businesses' AS Level1,  'Casinos'                    AS Level2,  NULL               AS Level3 Union
SELECT  1  as id,  'Arts and Entertainment Businesses' AS Level1,  'Performing Arts Businesses' AS Level2,  'Radio Stations'   AS Level3 Union
SELECT  2  as id,  'Auto Sales Businesses'             AS Level1,  'Motorcycle Dealers'         AS Level2,  'Motorcycle Parts' AS Level3 Union
SELECT  2  as id,  'Auto Sales Businesses'             AS Level1,  'RVs and Motor Home Dealers' AS Level2,  NULL               AS Level3 Union
SELECT  2  as id,  'Auto Sales Businesses'             AS Level1,  'Car Dealers'                AS Level2,  'Used Cars Dealers'AS Level3 Union
SELECT  3  as id,  'Bars and Lounges'                  AS Level1,  'Pubs and Dive Bars'         AS Level2,  'Pubs'             AS Level3 Union
SELECT  3  as id,  'Bars and Lounges'                  AS Level1,  'Wine Bars'                  AS Level2,  NULL               AS Level3 Union
SELECT  4  as id,  'Restaurants'                       AS Level1,  'American Restaurants'       AS Level2,  'Barbeque'         AS Level3 
)

SELECT * 
    ,CAST(Len(@imput) AS numeric(18,2))/Len(COALESCE(Level3,Level2,Level1)) AS Score 
FROM data
WHERE COALESCE(Level3,Level2,Level1) LIKE '%'+ @imput + '%'

添加适当的数据样本,预期结果与预期结果一致。数据示例是表(主表的子集)。谢谢,我将此作为起点