如何总结不同的物业权重?

时间:2013-11-24 00:52:00

标签: neo4j sum cypher where-clause

我试图获得两个属性总和(带有权重)并最终将它们组合成一个组合权重。

因此,首先是单独的查询,因此您可以看到正确的总和权重

  1. Language = French,熟练程度超过6(1-10级):

    MATCH (User)-[Speaks]->(Language) 
    WHERE Language.language = 'French' AND Speaks.proficiency >=6 
    RETURN User.name, SUM(
    CASE
        WHEN Language.language='French'  THEN Speaks.proficiency*3  
        END )*0.1 AS TOTAL_Language
    ORDER BY TOTAL_Language DESC
    

    User.name TOTAL_Language    大卫3    Marion 1.8

  2. Skill = Finance,Banking,IT,Marketing Weight =1 (of 1)

    MATCH (User)-[IsSkilledIn]->(Skill)
    WHERE  Skill.skill IN ['Finance','Banking','IT','Marketing']
    RETURN User.name, SUM(
    CASE
        WHEN Skill.skill='Finance' THEN IsSkilledIn.weight*10
        WHEN Skill.skill='Banking' THEN IsSkilledIn.weight*5        
        WHEN Skill.skill='IT' THEN IsSkilledIn.weight*8
    WHEN Skill.skill='Marketing' THEN IsSkilledIn.weight*1
    END )*0.1 AS TOTAL_Skill
    ORDER BY TOTAL_Skill DESC
    
    User.name   TOTAL_Skill
    Eric            1.5
    Marion          0.9
    David           0.8
    Hans            0.8
    
  3. 所以现在,我尝试将这些查询组合成:

    1. French >=6 AND Skills Finance,Banking,IT,Marketing

      MATCH (User)-[Speaks]->(Language) , (User)-[IsSkilledIn]->(Skill)
      WHERE Language.language = 'French' AND Speaks.proficiency >=6 AND Skill.skill IN ['Finance','Banking','IT','Marketing']
      RETURN User.name, SUM(
      CASE
          WHEN Language.language='French'  THEN Speaks.proficiency*3   
          END )*0.1 AS TOTAL_Language,
      SUM(
      CASE
          WHEN Skill.skill='Finance' THEN IsSkilledIn.weight*10
          WHEN Skill.skill='Banking' THEN IsSkilledIn.weight*5        
          WHEN Skill.skill='IT' THEN IsSkilledIn.weight*8
          WHEN Skill.skill='Marketing' THEN IsSkilledIn.weight*1
      END )*0.1 AS TOTAL_Skill
      ORDER BY TOTAL_Skill DESC
      
    2. 现在我得到了这些结果:

       User.name  TOTAL_Language  TOTAL_Skill
       Marion          3.6            0.9
       David       3                  0.8
      

      马里昂的TOTAL_Language是第一个答案的两倍。

      所以我检查了什么可能是错的,它必须在WHERE-Clause中,但我不知道什么是错的。

      我怎样才能总结两个结果?

2 个答案:

答案 0 :(得分:1)

啊,我看到了问题。你最终得到两个结果而不是一个结果,因为你的匹配匹配两个技能记录。您需要做的是将两个查询放在一起,因此总和将单独计算。

MATCH (User)-[Speaks]->(Language) 
WHERE Language.language = 'French' AND Speaks.proficiency >=6 
WITH User, SUM(
CASE
    WHEN Language.language='French'  THEN Speaks.proficiency*3  
    END )*0.1 AS TOTAL_Language
MATCH (User)-[IsSkilledIn]->(Skill)
WHERE  Skill.skill IN ['Finance','Banking','IT','Marketing']
RETURN User.name, TOTAL_Language, SUM(
CASE
    WHEN Skill.skill='Finance' THEN IsSkilledIn.weight*10
    WHEN Skill.skill='Banking' THEN IsSkilledIn.weight*5        
    WHEN Skill.skill='IT' THEN IsSkilledIn.weight*8
    WHEN Skill.skill='Marketing' THEN IsSkilledIn.weight*1
END )*0.1 AS TOTAL_Skill
ORDER BY TOTAL_Skill DESC

答案 1 :(得分:1)

另一个选择是仍然使用一个查询,但收集一个人的技能,并总结技能权重的集合,而不是对行进行求和,

MATCH (User)-[Speaks]->(Language), (User)-[IsSkilledIn]->(Skill) 
WHERE Language.language = 'French' 
 AND Speaks.proficiency >=6 
 AND Skill.skill IN ['Finance','Banking','IT','Marketing']
WITH User.name as userName, 
     (CASE WHEN Language.language='French' 
           THEN Speaks.proficiency*3 END) as languageProficiency, 
      collect(CASE 
              WHEN Skill.skill='Finance' THEN IsSkilledIn.weight*10 
              WHEN Skill.skill='Banking' THEN IsSkilledIn.weight*5 
              WHEN Skill.skill='IT' THEN IsSkilledIn.weight*8 
              WHEN Skill.skill='Marketing' THEN IsSkilledIn.weight*1 
              END) as skillWeightCollection
RETURN userName, 
       sum(languageProficiency)*0.1 as TOTAL_Language, 
       reduce(totalSkill = 0, skillWeight in skillWeightCollection | 
              totalSkill + skillWeight) * 0.1 as TOTAL_SKILL