【问题标题】:how do i count two values in single row in sql server我如何计算sql server中单行中的两个值
【发布时间】:2022-10-12 22:33:21
【问题描述】:

我有一个包含两列公司和描述的表格,其中包含以下详细信息

company         decription
a               welding,plumbing
b                 welding
c                plumbing
d                welding,plumbing
e                welding plumbing
f                 plumbing 

我如何计算具有焊接和管道描述的公司

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    您可以使用LIKE 来检查这两个术语。

    SELECT COUNT(1) 
    FROM your_table
    WHERE LOWER(decription) LIKE '%welding%'
      AND LOWER(decription) LIKE '%plumbing%'
    

    【讨论】:

    • 工作正常,只要没有焊接或水管出现。 (或类似的。)
    • 确实如此,@Richa 并没有对他的数据给出很多解释:)。
    【解决方案2】:

    Vvdl 确实提供了解决方案。

    另一种方法是计算数据中分隔符的数量。假设描述总是用逗号分隔。

    你可以这样做:

    SELECT 
    Company
    ,LEN(Description) - LEN(replace(Description,',',''))+1 AS CountDesc
    --Add one as the number of companies will always be number of commas plus 1
    FROM your_table
    

    您的结果将如下所示:

    【讨论】:

      猜你喜欢
      • 2020-06-30
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多