如何根据另一列的值在 SQL 选择查询中创建/添加列作为返回输出?

如何解决如何根据另一列的值在 SQL 选择查询中创建/添加列作为返回输出?

我已使用 Case 语句向 SQL 视图动态添加一列 (industry_type)。现在我想使用相同的列名/值添加另一个带有 industry_group 输出的动态列。我尝试使用相同的方法创建 industry_group 列。但我无法这样做,因为在视图中创建的列在“主表”中不可用。

我是否需要在源代码处创建一个带有 industry_group 的新列?或者有没有办法在同一个视图中动态创建它并返回 industry_group 值?

如果你能帮我解决这个问题,那将是一个很大的帮助。

示例代码:

select 
    column 1,column 2,date,case
        when (column 1 like '%12321%' or column 2 like'%poultry%' 
           then 'Poultry Facilities'
        when (column 1  like '%43434%') or column 2 like'%Hospi%' 
           then 'Hospitals'
        else null
    end as [industry_type],from table

输出:

Date        Industry Type
------------------------------
7/02/2021   Poultry facilities
7/02/2021   Poultry facilities
7/02/2021   Poultry facilities
10/02/2021  Poultry facilities
11/01/2021  Hospitals
10/01/2021  Hospitals
5/02/2021   Hospitals

试过这个脚本1:

select 
    column 1,case
       when (column 1 like '%12321%' or column 2 like'%poultry%' 
          then 'Poultry Facilities'
       when (column 1  like '%43434%') or column 2 like'%Hospi%' 
          then 'Hospitals'
       else null
    end as [industry_type],'PF' as industry_group,from table

输出:

Date        Industry Type     Industry Group
----------------------------------------------
7/02/2021   Poultry facilities  PF
7/02/2021   Poultry facilities  PF
7/02/2021   Poultry facilities  PF
10/02/2021  Poultry facilities  PF
11/01/2021  Hospitals           PF
10/01/2021  Hospitals           PF
5/02/2021   Hospitals           PF

试过脚本 2

select 
    column 1,case
       when (column 1 like '%12321%' or column 2 like'%poultry%' 
          then 'Poultry Facilities'
    end as [industry_type],case
       when (column 1  like '%43434%') or column 2 like'%Hospi%' 
          then 'Hospitals'
          else null
    end as [industry_type],'HOS' as industry_group,from table

不幸的是,“脚本 2”不起作用。

所需的输出:

Date        Industry Type   Industry Group
---------------------------------------
7/02/2021   Poultry facilities  PF
7/02/2021   Poultry facilities  PF
7/02/2021   Poultry facilities  PF
10/02/2021  Poultry facilities  PF
11/01/2021  Hospitals          HOS
10/01/2021  Hospitals          HOS
5/02/2021   Hospitals          HOS

我有超过 10 个搜索条件,我需要根据这些条件返回正确的行业组

请注意:industry_type 在“主表”中不可用

谢谢大家!

解决方法

您的输出似乎与查询不匹配:查询未显示任何列日期,我希望输出包含 3 列:

<div class="judicual-scale">
        <svg class="judicial-scale-svg" width="560" height="560" viewBox="0 0 560 560" fill="none" xmlns="http://www.w3.org/2000/svg">
          <g id="justice-scale 1">
          <g id="part-to-anim">
          <g id="Group">
          <g id="Group_2">
          <g id="Group_3">
          <path id="Vector" d="M255.999 99.724H216.236C212.373 99.724 208.67 101.273 205.808 104.085C198.008 111.747 183.646 120.881 159.517 120.881C131.004 120.881 108.937 105.94 97.381 78.812C95.094 73.444 89.816 69.976 83.934 69.976H48.661C41.549 69.976 35.784 64.211 35.784 57.099C35.784 49.987 41.55 44.222 48.661 44.222H83.934C100.157 44.222 114.736 53.838 121.075 68.719C128.54 86.243 141.473 95.127 159.517 95.127C174.94 95.127 183.388 90.008 187.759 85.714C195.466 78.142 205.58 73.971 216.236 73.971H255.999V99.724Z" fill="#5B5555"/>
          </g>
          </g>
          <g id="Group_4">
          <path id="Vector_2" d="M68.278 110.183C64.011 110.183 60.552 106.724 60.552 102.457V37.067C60.552 32.8 64.011 29.341 68.278 29.341C72.545 29.341 76.004 32.8 76.004 37.067V102.456C76.005 106.723 72.545 110.183 68.278 110.183Z" fill="#FFD597"/>
          </g>
          </g>
          <g id="Group_5">
          <g id="Group_6">
          <g id="Group_7">
          <path id="Vector_3" d="M256 99.724H295.763C299.626 99.724 303.329 101.273 306.191 104.085C313.991 111.747 328.353 120.881 352.482 120.881C380.995 120.881 403.062 105.94 414.618 78.812C416.905 73.444 422.183 69.976 428.065 69.976H463.338C470.45 69.976 476.215 64.211 476.215 57.099C476.215 49.987 470.449 44.222 463.338 44.222H428.065C411.842 44.222 397.263 53.838 390.924 68.719C383.459 86.243 370.526 95.127 352.482 95.127C337.059 95.127 328.611 90.008 324.24 85.714C316.533 78.142 306.419 73.971 295.763 73.971H256V99.724Z" fill="#5B5555"/>
          </g>
          </g>
          <g id="Group_8">
          <path id="Vector_4" d="M443.72 110.183C439.452 110.183 435.994 106.724 435.994 102.457L435.995 37.068C435.995 32.801 439.455 29.342 443.721 29.342C447.989 29.342 451.447 32.801 451.447 37.068L451.446 102.457C451.447 106.723 447.987 110.183 443.72 110.183Z" fill="#FFD597"/>
          </g>
          </g>
          <g id="Group_9">
          <g id="Group_10">
          <path id="Vector_5" d="M120.841 349.578C117.405 349.578 114.27 347.27 113.368 343.789L68.279 169.924L23.19 343.79C22.119 347.92 17.901 350.398 13.771 349.329C9.641 348.258 7.16 344.041 8.232 339.91L60.8 137.205C61.684 133.798 64.759 131.418 68.279 131.418C71.799 131.418 74.874 133.797 75.758 137.205L128.326 339.91C129.397 344.041 126.917 348.258 122.787 349.329C122.136 349.497 121.483 349.578 120.841 349.578Z" fill="#FFD597"/>
          </g>
          <g id="Group_11">
          <g id="Group_12">
          <path id="Vector_6" d="M93.12 397.46H43.437C19.447 397.46 0 378.012 0 354.023V349.021C0 342.216 5.517 336.699 12.322 336.699H124.234C131.039 336.699 136.556 342.216 136.556 349.021V354.023C136.556 378.012 117.109 397.46 93.12 397.46Z" fill="#FCB44D"/>
          <g id="Group_13">
          <path id="Vector_7" d="M136.509 356.134C136.251 361.615 134.974 366.827 132.872 371.587H3.68799C1.58599 366.828 0.308995 361.615 0.0509949 356.134H136.509Z" fill="#FB9927"/>
          </g>
          <path id="Vector_8" d="M136.557 349.027V354.017C136.557 378.008 117.113 397.464 93.122 397.464H58.399C82.39 397.464 101.834 378.008 101.834 354.017V349.027C101.834 342.213 96.314 336.705 89.512 336.705H124.235C131.037 336.705 136.557 342.213 136.557 349.027Z" fill="#FB9927"/>
          </g>
          </g>
          <g id="Group_14">
          <path id="Vector_9" d="M68.278 139.144C78.978 139.144 87.652 130.47 87.652 119.77C87.652 109.07 78.978 100.396 68.278 100.396C57.578 100.396 48.904 109.07 48.904 119.77C48.904 130.47 57.578 139.144 68.278 139.144Z" fill="#FCB44D"/>
          <path id="Vector_10" d="M87.65 119.771C87.65 130.473 78.976 139.147 68.274 139.147C64.041 139.147 60.133 137.788 56.945 135.489C63.634 132.437 68.274 125.686 68.274 117.858C68.274 111.389 65.107 105.662 60.237 102.15C62.683 101.021 65.41 100.394 68.274 100.394C78.976 100.395 87.65 109.069 87.65 119.771Z" fill="#FB9927"/>
          </g>
          <g id="Group_15">
          <path id="Vector_11" d="M496.285 349.578C492.849 349.578 489.714 347.27 488.811 343.789L443.721 169.923L398.633 343.79C397.562 347.92 393.35 350.4 389.215 349.329C385.084 348.258 382.604 344.041 383.676 339.91L436.243 137.205C437.126 133.798 440.202 131.418 443.722 131.418C447.242 131.418 450.318 133.797 451.201 137.205L503.769 339.91C504.84 344.041 502.361 348.258 498.23 349.329C497.579 349.497 496.927 349.578 496.285 349.578V349.578Z" fill="#FFD597"/>
          </g>
          <g id="Group_16">
          <g id="Group_17">
          <path id="Vector_12" d="M468.563 397.46H418.88C394.891 397.46 375.443 378.013 375.443 354.023V349.021C375.443 342.216 380.96 336.699 387.765 336.699H499.677C506.482 336.699 511.999 342.216 511.999 349.021V354.023C511.999 378.012 492.552 397.46 468.563 397.46V397.46Z" fill="#FCB44D"/>
          <g id="Group_18">
          <path id="Vector_13" d="M511.948 356.134C511.69 361.615 510.413 366.827 508.311 371.587H379.127C377.025 366.828 375.748 361.615 375.49 356.134H511.948Z" fill="#FB9927"/>
          </g>
          <path id="Vector_14" d="M512 349.027V354.017C512 378.008 492.556 397.464 468.565 397.464H433.842C457.833 397.464 477.277 378.008 477.277 354.017V349.027C477.277 342.213 471.757 336.705 464.955 336.705H499.678C506.48 336.705 512 342.213 512 349.027Z" fill="#FB9927"/>
          </g>
          </g>
          <g id="Group_19">
          <path id="Vector_15" d="M136.509 356.134C136.251 361.615 134.974 366.827 132.872 371.587H98.135C100.247 366.828 101.524 361.615 101.782 356.134H136.509Z" fill="#F98824"/>
          </g>
          <g id="Group_20">
          <path id="Vector_16" d="M511.948 356.134C511.69 361.615 510.413 366.827 508.311 371.587H473.573C475.685 366.828 476.962 361.615 477.22 356.134H511.948Z" fill="#F98824"/>
          </g>
          <g id="Group_21">
          <path id="Vector_17" d="M443.722 139.144C454.422 139.144 463.096 130.47 463.096 119.77C463.096 109.07 454.422 100.396 443.722 100.396C433.022 100.396 424.348 109.07 424.348 119.77C424.348 130.47 433.022 139.144 443.722 139.144Z" fill="#FCB44D"/>
          <path id="Vector_18" d="M463.094 119.771C463.094 130.473 454.42 139.147 443.718 139.147C439.485 139.147 435.577 137.788 432.389 135.489C439.078 132.437 443.718 125.686 443.718 117.858C443.718 111.389 440.551 105.662 435.681 102.15C438.127 101.021 440.854 100.394 443.718 100.394C454.42 100.395 463.094 109.069 463.094 119.771Z" fill="#FB9927"/>
          </g>
          </g>
          </g>
          <g id="background-parts">
          <g id="Group_22">
          <path id="Vector_19" d="M281.573 492.639H230.427L242.92 43.859H269.08L281.573 492.639Z" fill="#FCB44D"/>
          <g id="Group_23">
          <path id="Vector_20" d="M270.083 79.699C265.694 81.327 260.945 82.213 256 82.213C251.055 82.213 246.306 81.327 241.917 79.699L242.916 43.859H269.083L270.083 79.699Z" fill="#FB9927"/>
          <path id="Vector_21" d="M234.698 339.234L233.534 381.05H278.466L277.302 339.234H234.698Z" fill="#FB9927"/>
          </g>
          </g>
          <path id="Vector_22" d="M256 337.903C274.603 337.903 289.683 322.823 289.683 304.22C289.683 285.617 274.603 270.537 256 270.537C237.397 270.537 222.317 285.617 222.317 304.22C222.317 322.823 237.397 337.903 256 337.903Z" fill="#FB9927"/>
          <path id="Vector_23" d="M289.687 304.217C289.687 322.822 274.605 337.904 256 337.904C251.477 337.904 247.161 337.008 243.215 335.39C255.474 330.352 264.107 318.289 264.107 304.217C264.107 290.145 255.474 278.092 243.215 273.054C247.161 271.437 251.477 270.54 256 270.54C274.605 270.54 289.687 285.622 289.687 304.217V304.217Z" fill="#F98824"/>
          <g id="Group_24">
          <path id="Vector_24" d="M255.999 62.116C269.833 62.116 281.048 50.9012 281.048 37.067C281.048 23.2328 269.833 12.018 255.999 12.018C242.165 12.018 230.95 23.2328 230.95 37.067C230.95 50.9012 242.165 62.116 255.999 62.116Z" fill="#FFD597"/>
          <path id="Vector_25" d="M281.053 37.072C281.053 50.904 269.831 62.113 255.999 62.113C252.065 62.113 248.35 61.213 245.034 59.594C253.364 55.519 259.11 46.971 259.11 37.073C259.11 27.175 253.364 18.614 245.034 14.539C248.351 12.919 252.066 12.019 255.999 12.019C269.831 12.018 281.053 23.227 281.053 37.072Z" fill="#FCC16D"/>
          </g>
          <path id="Vector_26" d="M358.506 471.355H153.494V444.473C153.494 435.939 160.412 429.02 168.947 429.02H343.053C351.587 429.02 358.506 435.938 358.506 444.473V471.355Z" fill="#756F6F"/>
          <path id="Vector_27" d="M358.503 444.476V471.353H332.934V444.476C332.934 435.936 326.011 429.023 317.481 429.023H343.05C351.591 429.023 358.503 435.936 358.503 444.476Z" fill="#5B5555"/>
          <g id="Group_25">
          <path id="Vector_28" d="M392.228 499.982H119.772C114.082 499.982 109.47 495.37 109.47 489.68V472.978C109.47 467.288 114.082 462.676 119.772 462.676H392.227C397.917 462.676 402.529 467.288 402.529 472.978V489.68C402.529 495.37 397.917 499.982 392.228 499.982Z" fill="#FFD597"/>
          <path id="Vector_29" d="M402.533 472.981V489.68C402.533 495.367 397.918 499.982 392.231 499.982H366.652C372.349 499.982 376.954 495.367 376.954 489.68V472.981C376.954 467.284 372.349 462.679 366.652 462.679H392.231C397.918 462.679 402.533 467.284 402.533 472.981Z" fill="#FCC16D"/>
          </g>
          <path id="Vector_30" d="M291.621 358.355H220.38C214.69 358.355 210.078 353.743 210.078 348.053V334.843C210.078 329.153 214.69 324.541 220.38 324.541H291.621C297.311 324.541 301.923 329.153 301.923 334.843V348.053C301.922 353.743 297.31 358.355 291.621 358.355Z" fill="#FFD597"/>
          <path id="Vector_31" d="M301.925 334.844V348.051C301.925 353.738 297.31 358.353 291.623 358.353H266.044C271.741 358.353 276.346 353.738 276.346 348.051V334.844C276.346 329.157 271.741 324.542 266.044 324.542H291.623C297.31 324.542 301.925 329.157 301.925 334.844Z" fill="#FCC16D"/>
          </g>
          </g>
        </svg>
      </div>

相反,它只有 2 个

如果您粘贴实际代码而不是 excel 中的图片会有所帮助

无论如何要在选择中添加一列,只需将其添加到字段中即可:

Column1,column2,industry_type

问题增强后更新

每一列本身就是一个规则,如果你想让它依赖于前一个,你应该重写规则两次,或者进行嵌套查询

第一个例子

SELECT column1,case
...
END AS industry_type,"PF" as industry_group
FROM table;

第二个例子

select 
    column 1,column 2,date,case
       when (column 1 like '%12321%' or column 2 like'%poultry%' 
          then 'Poultry Facilities'
       when (column 1  like '%43434%') or column 2 like'%Hospi%' 
          then 'Hospitals'
       else null
    end as [industry_type],case
       when (column 1 like '%12321%' or column 2 like'%poultry%' 
          then 'PF'
       when (column 1  like '%43434%') or column 2 like'%Hospi%' 
          then 'HOS'
       else null
    end as [industry_group],from table

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res