首页 > 其他分享 >行转列,值转换成列

行转列,值转换成列

时间:2024-09-02 20:26:33浏览次数:4  
标签:转换成 product store3 store1 price 转列 id store

 

 

1. 值转换成列操作。值转列操作:[1777题库]

 

表:Products

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_id  | int     |
| store       | enum    |
| price       | int     |
+-------------+---------+
在 SQL 中,(product_id,store) 是这个表的主键。
store 字段是枚举类型,它的取值为以下三种 ('store1', 'store2', 'store3') 。
price 是该商品在这家商店中的价格。
 

找出每种产品在各个商店中的价格。

可以以 任何顺序 输出结果。

返回结果格式如下例所示。

 

示例 1:

输入:
Products 表:
+-------------+--------+-------+
| product_id  | store  | price |
+-------------+--------+-------+
| 0           | store1 | 95    |
| 0           | store3 | 105   |
| 0           | store2 | 100   |
| 1           | store1 | 70    |
| 1           | store3 | 80    |
+-------------+--------+-------+
输出:
+-------------+--------+--------+--------+
| product_id  | store1 | store2 | store3 |
+-------------+--------+--------+--------+
| 0           | 95     | 100    | 105    |
| 1           | 70     | null   | 80     |
+-------------+--------+--------+--------+
解释:
产品 0 的价格在商店 1 为 95 ,商店 2 为 100 ,商店 3 为 105 。
产品 1 的价格在商店 1 为 70 ,商店 3 的产品 1 价格为 80 ,但在商店 2 中没有销售。
View Code
====方法1======
SELECT 
product_id  ,
min( case when store = 'store1' then price else null end ) store1,
min( case when store = 'store2' then price else null end ) store2,
min( case when store = 'store3' then price else null end ) store3
  
FROM Products 
group by product_id


====方法2=====
 
 SELECT 
product_id  ,
nullif( sum( case when store = 'store1' then price else 0 end ) ,0) store1,
nullif( sum( case when store = 'store2' then price else 0 end ) ,0) store2,
nullif( sum( case when store = 'store3' then price else 0 end ) ,0) store3
  
 FROM Products 
 group by product_id

===
体会如上两种方法, 结果是一样的。 其实就是 null 和0 的关系,最终为0 的结果让展示为null,这个地方是重点。

 

标签:转换成,product,store3,store1,price,转列,id,store
From: https://www.cnblogs.com/mengbin0546/p/18393459

相关文章

  • jeecg 月份产值行转列(备份)
    @RequestMapping(params="list")publicModelAndViewlist(HttpServletRequestrequest){List<Map<String,Object>>list=newArrayList<>();for(inti=1;i<13;i++){Map<String,Object>m=......
  • SciTech-BigDataAIML-CV+CG-Digital Image/Signal Processing- RGB图片转换成 RGBA格
    RGBA与RGBRGB是Color(颜色)数值化为R(红色)、G(绿色)、B(蓝色)**三Channel(分量),每分量数值的取值范围为0-255。通过组合这三个ColorChannel(颜色分量)的不同数值,可以得到各种各样的颜色。RGBA是RGB颜色模型的一种扩展,只增加了一个表示透明度(Alpha)的透明分量(A)。A代......
  • Android开发 - LayoutInflater 类将 XML 布局文件转换成对应的 View 对象解析
    LayoutInflater是什么LayoutInflater用于将XML布局文件转换成对应的View对象。它可以理解为一个“布局解析器”,帮助我们将静态的XML文件转换为可以动态操作的Java对象(View及其子类)LayoutInflater的主要作用在Android开发中,我们通常会在res/layout文件夹中......
  • 如何将视频转换成音频mp3格式?大家都在用的7个视频转音频方法,点进来看看!
    如何将视频转换成音频mp3格式?首先,将视频转换成音频mp3格式的原因有很多:视频文件相较于mp3音频文件,通常体积更大。如果希望在手机等便携设备上收听音乐,转换视频为mp3会更加合适。此外,播放视频会消耗更多的电池,这使得手机的电量消耗更加快速。而mp3音频则可以在设备处于待机状态......
  • Java 字符串转换成罗马数字
    键盘录入一个字符串要求1:长度为小于等于9要求2:只能是数字将内容变成罗马数字下面是阿拉伯数字跟罗马数字的对比关系Ⅰ-1,Ⅱ-2,Ⅲ-3,Ⅳ-4,Ⅴ-5,Ⅵ-6,Ⅶ-7,Ⅷ-8,Ⅸ-9注意点:罗马数字里面是没有0的如果键盘录入的数字包含0,可以变成""(长度为0的字符串)packagetest;......
  • 1479 leetcode, 将值转换成列的问题
    最终结果#WriteyourMySQLquerystatementbelowselectdistinctb.item_categoryasCategory,ifnull(sum(casewhendayofweek(a.order_date)=2thena.quantityend),0)Monday,ifnull(sum(casewhendayofweek(a.order_date)=3thena.quantityend),0)Tuesday,......
  • springboot配置文件0开头的数字自动被转换成为其他值/与原值不匹配: 000000转为0
    在springboot配置文件需要使用字符串类型的数值时候,有时候直接写一个0开头的数值,例如001。配置生效之后,打印配置参数时候发现不对,输出的是数值1。而yml中配置的:000000,输出的是:0publicclassConstructYamlIntextendsAbstractConstruct{publicConstructYamlInt(){......
  • js 将十进制字符串转换成4字节的字节数组
    函数functionconvertToHexArrays(input){//通过制表符分割输入字符串constnumbers=input.split('\t');//用于存储结果的数组constresult=[];for(letnumofnumbers){//将字符串转换为数字constvalue=parseInt(num)......
  • pdf转换成excel有没有免费软件?6款pdf转excel软件大公开!
    如今pdf格式已成为我们日常生活中最常见的文件格式之一。尽管pdf非常适合存储大型文档和表格,但在需要对内容进行编辑时却显得有些不便。这正是为什么许多人希望将pdf转换成excel表格,以便更方便地进行数据修改和分析。然而,对于很多用户来说,如何高效地实现pdf转excel仍然是个难题......
  • 我应该如何将这个密码机转换成一个可以工作的网络密码破解器?
    我对一般代码尤其是Python非常陌生。我创建了下面的代码作为基本测试(归功于youtuberhttps://www.youtube.com/watch?v=sI5zo1ZElCc),并且想知道如何制作它以便我可以用它获取wifi密码?这是我尝试过的:importrandomimportpyautoguiimportplatformchars='ab......