久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='Y6vpk'></bdo><ul id='Y6vpk'></ul>

<small id='Y6vpk'></small><noframes id='Y6vpk'>

      <legend id='Y6vpk'><style id='Y6vpk'><dir id='Y6vpk'><q id='Y6vpk'></q></dir></style></legend><tfoot id='Y6vpk'></tfoot>

        <i id='Y6vpk'><tr id='Y6vpk'><dt id='Y6vpk'><q id='Y6vpk'><span id='Y6vpk'><b id='Y6vpk'><form id='Y6vpk'><ins id='Y6vpk'></ins><ul id='Y6vpk'></ul><sub id='Y6vpk'></sub></form><legend id='Y6vpk'></legend><bdo id='Y6vpk'><pre id='Y6vpk'><center id='Y6vpk'></center></pre></bdo></b><th id='Y6vpk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Y6vpk'><tfoot id='Y6vpk'></tfoot><dl id='Y6vpk'><fieldset id='Y6vpk'></fieldset></dl></div>
      1. MySQL - 行到列

        MySQL - Rows to Columns(MySQL - 行到列)
        <legend id='PcHFG'><style id='PcHFG'><dir id='PcHFG'><q id='PcHFG'></q></dir></style></legend>
          <tbody id='PcHFG'></tbody>
      2. <i id='PcHFG'><tr id='PcHFG'><dt id='PcHFG'><q id='PcHFG'><span id='PcHFG'><b id='PcHFG'><form id='PcHFG'><ins id='PcHFG'></ins><ul id='PcHFG'></ul><sub id='PcHFG'></sub></form><legend id='PcHFG'></legend><bdo id='PcHFG'><pre id='PcHFG'><center id='PcHFG'></center></pre></bdo></b><th id='PcHFG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='PcHFG'><tfoot id='PcHFG'></tfoot><dl id='PcHFG'><fieldset id='PcHFG'></fieldset></dl></div>
              <bdo id='PcHFG'></bdo><ul id='PcHFG'></ul>

              <small id='PcHFG'></small><noframes id='PcHFG'>

              • <tfoot id='PcHFG'></tfoot>
                  本文介紹了MySQL - 行到列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我嘗試搜索帖子,但我只找到了 SQL Server/Access 的解決方案.我需要 MySQL (5.X) 中的解決方案.

                  I tried to search posts, but I only found solutions for SQL Server/Access. I need a solution in MySQL (5.X).

                  我有一個包含 3 列的表(稱為歷史記錄):hostid、itemname、itemvalue.
                  如果我做了一個選擇(select * from history),它會返回

                  I have a table (called history) with 3 columns: hostid, itemname, itemvalue.
                  If I do a select (select * from history), it will return

                     +--------+----------+-----------+
                     | hostid | itemname | itemvalue |
                     +--------+----------+-----------+
                     |   1    |    A     |    10     |
                     +--------+----------+-----------+
                     |   1    |    B     |     3     |
                     +--------+----------+-----------+
                     |   2    |    A     |     9     |
                     +--------+----------+-----------+
                     |   2    |    c     |    40     |
                     +--------+----------+-----------+
                  

                  如何查詢數據庫以返回類似的內容

                  How do I query the database to return something like

                     +--------+------+-----+-----+
                     | hostid |   A  |  B  |  C  |
                     +--------+------+-----+-----+
                     |   1    |  10  |  3  |  0  |
                     +--------+------+-----+-----+
                     |   2    |   9  |  0  |  40 |
                     +--------+------+-----+-----+
                  

                  推薦答案

                  我將添加更長更詳細的說明,說明解決此問題的步驟.如果太長,我深表歉意.

                  I'm going to add a somewhat longer and more detailed explanation of the steps to take to solve this problem. I apologize if it's too long.

                  我將從您提供的基礎開始,并使用它來定義我將在本文的其余部分使用的幾個術語.這將是基表:

                  I'll start out with the base you've given and use it to define a couple of terms that I'll use for the rest of this post. This will be the base table:

                  select * from history;
                  
                  +--------+----------+-----------+
                  | hostid | itemname | itemvalue |
                  +--------+----------+-----------+
                  |      1 | A        |        10 |
                  |      1 | B        |         3 |
                  |      2 | A        |         9 |
                  |      2 | C        |        40 |
                  +--------+----------+-----------+
                  

                  這將是我們的目標,漂亮的數據透視表:

                  select * from history_itemvalue_pivot;
                  
                  +--------+------+------+------+
                  | hostid | A    | B    | C    |
                  +--------+------+------+------+
                  |      1 |   10 |    3 |    0 |
                  |      2 |    9 |    0 |   40 |
                  +--------+------+------+------+
                  

                  history.hostid 列中的值將成為數據透視表中的 y 值.history.itemname 列中的值將變為 x 值(原因很明顯).

                  Values in the history.hostid column will become y-values in the pivot table. Values in the history.itemname column will become x-values (for obvious reasons).

                  當我必須解決創建數據透視表的問題時,我會使用三步流程(可選的第四步)來解決它:

                  When I have to solve the problem of creating a pivot table, I tackle it using a three-step process (with an optional fourth step):

                  1. 選擇感興趣的列,即y值x值
                  2. 使用額外的列擴展基表 - 每個 x 值
                  3. 對擴展表進行分組和聚合 -- 每個 y 值
                  4. (可選)美化聚合表

                  讓我們將這些步驟應用于您的問題,看看我們得到了什么:

                  Let's apply these steps to your problem and see what we get:

                  第 1 步:選擇感興趣的列.在所需的結果中,hostid 提供 y 值itemname 提供 x 值.

                  Step 1: select columns of interest. In the desired result, hostid provides the y-values and itemname provides the x-values.

                  第 2 步:使用額外的列擴展基表.我們通常每個 x 值需要一列.回想一下我們的 x 值列是 itemname:

                  Step 2: extend the base table with extra columns. We typically need one column per x-value. Recall that our x-value column is itemname:

                  create view history_extended as (
                    select
                      history.*,
                      case when itemname = "A" then itemvalue end as A,
                      case when itemname = "B" then itemvalue end as B,
                      case when itemname = "C" then itemvalue end as C
                    from history
                  );
                  
                  select * from history_extended;
                  
                  +--------+----------+-----------+------+------+------+
                  | hostid | itemname | itemvalue | A    | B    | C    |
                  +--------+----------+-----------+------+------+------+
                  |      1 | A        |        10 |   10 | NULL | NULL |
                  |      1 | B        |         3 | NULL |    3 | NULL |
                  |      2 | A        |         9 |    9 | NULL | NULL |
                  |      2 | C        |        40 | NULL | NULL |   40 |
                  +--------+----------+-----------+------+------+------+
                  

                  請注意,我們沒有更改行數——我們只是添加了額外的列.還要注意 NULLs 的模式——itemname = "A" 的行對于新列 A 具有非空值,并且其他新列的空值.

                  Note that we didn't change the number of rows -- we just added extra columns. Also note the pattern of NULLs -- a row with itemname = "A" has a non-null value for new column A, and null values for the other new columns.

                  步驟 3:對擴展表進行分組和聚合.我們需要group by hostid,因為它提供了y值:

                  Step 3: group and aggregate the extended table. We need to group by hostid, since it provides the y-values:

                  create view history_itemvalue_pivot as (
                    select
                      hostid,
                      sum(A) as A,
                      sum(B) as B,
                      sum(C) as C
                    from history_extended
                    group by hostid
                  );
                  
                  select * from history_itemvalue_pivot;
                  
                  +--------+------+------+------+
                  | hostid | A    | B    | C    |
                  +--------+------+------+------+
                  |      1 |   10 |    3 | NULL |
                  |      2 |    9 | NULL |   40 |
                  +--------+------+------+------+
                  

                  (請注意,我們現在每個 y 值有一行.) 好的,我們快到了!我們只需要擺脫那些丑陋的NULLs.

                  (Note that we now have one row per y-value.) Okay, we're almost there! We just need to get rid of those ugly NULLs.

                  第 4 步:美化.我們將用零替換任何空值,以便結果集更好看:

                  Step 4: prettify. We're just going to replace any null values with zeroes so the result set is nicer to look at:

                  create view history_itemvalue_pivot_pretty as (
                    select 
                      hostid, 
                      coalesce(A, 0) as A, 
                      coalesce(B, 0) as B, 
                      coalesce(C, 0) as C 
                    from history_itemvalue_pivot 
                  );
                  
                  select * from history_itemvalue_pivot_pretty;
                  
                  +--------+------+------+------+
                  | hostid | A    | B    | C    |
                  +--------+------+------+------+
                  |      1 |   10 |    3 |    0 |
                  |      2 |    9 |    0 |   40 |
                  +--------+------+------+------+
                  

                  我們已經完成了——我們已經使用 MySQL 構建了一個漂亮、漂亮的數據透視表.

                  And we're done -- we've built a nice, pretty pivot table using MySQL.

                  應用此程序時的注意事項:

                  Considerations when applying this procedure:

                  • 在額外的列中使用什么值.我在這個例子中使用了 itemvalue
                  • 在額外的列中使用什么中性"值.我使用了 NULL,但也可以是 0"",具體取決于您的具體情況
                  • 分組時使用什么聚合函數.我用的是sum,但是countmax 也經常用到(max 常用于構建單行分散在多行中的對象")
                  • 對 y 值使用多列.此解決方案不限于對 y 值使用單列——只需將額外的列插入 group by 子句(不要忘記 select他們)
                  • what value to use in the extra columns. I used itemvalue in this example
                  • what "neutral" value to use in the extra columns. I used NULL, but it could also be 0 or "", depending on your exact situation
                  • what aggregate function to use when grouping. I used sum, but count and max are also often used (max is often used when building one-row "objects" that had been spread across many rows)
                  • using multiple columns for y-values. This solution isn't limited to using a single column for the y-values -- just plug the extra columns into the group by clause (and don't forget to select them)

                  已知限制:

                  • 該解決方案不允許數據透視表中有 n 列——在擴展基表時需要手動添加每個數據透視列.所以對于 5 或?? 10 個 x 值,這個解決方案很好.100,不太好.有一些存儲過程生成查詢的解決方案,但它們很丑陋且難以正確處理.當數據透視表需要有很多列時,我目前不知道有什么好的方法可以解決這個問題.

                  這篇關于MySQL - 行到列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行)
                  reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結果;條款?)
                  Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數的 ignore 選項是忽略整個事務還是只是有問題的行?) - IT屋-程序員軟件開發技
                  Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環數組)
                  pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調用 o23.load 時發生錯誤 沒有合適的驅動程序)
                  How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數據庫表作為 Spark 數據幀讀取?)

                  • <i id='wHp6Z'><tr id='wHp6Z'><dt id='wHp6Z'><q id='wHp6Z'><span id='wHp6Z'><b id='wHp6Z'><form id='wHp6Z'><ins id='wHp6Z'></ins><ul id='wHp6Z'></ul><sub id='wHp6Z'></sub></form><legend id='wHp6Z'></legend><bdo id='wHp6Z'><pre id='wHp6Z'><center id='wHp6Z'></center></pre></bdo></b><th id='wHp6Z'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='wHp6Z'><tfoot id='wHp6Z'></tfoot><dl id='wHp6Z'><fieldset id='wHp6Z'></fieldset></dl></div>

                  • <tfoot id='wHp6Z'></tfoot>
                        <legend id='wHp6Z'><style id='wHp6Z'><dir id='wHp6Z'><q id='wHp6Z'></q></dir></style></legend>

                            <tbody id='wHp6Z'></tbody>
                            <bdo id='wHp6Z'></bdo><ul id='wHp6Z'></ul>

                            <small id='wHp6Z'></small><noframes id='wHp6Z'>

                            主站蜘蛛池模板: 亚洲成人精品 | 亚洲精品自在在线观看 | 亚洲免费观看视频 | 天天曰夜夜操 | eeuss国产一区二区三区四区 | 精品一区二区视频 | 国产一级片久久久 | 紧缚调教一区二区三区视频 | 亚洲成人第一页 | 四虎永久免费黄色影片 | 精品成人一区二区 | 网站一区二区三区 | 成人av鲁丝片一区二区小说 | 中国人pornoxxx麻豆 | 亚洲天堂成人在线视频 | 亚洲一区二区在线播放 | 国产99热| 成人精品一区二区三区 | 久草成人| 国产精品欧美一区二区三区不卡 | 日产精品久久久一区二区福利 | 国产精品久久久久久 | 自拍偷拍第一页 | 欧美一级二级视频 | 亚洲精品一区二区 | 成人精品一区 | 国产成人99久久亚洲综合精品 | 亚洲精品久久久久久久久久久 | 国产片侵犯亲女视频播放 | 中文字幕欧美一区 | 亚洲精品久久久久久久久久久久久 | 啪啪精品 | 日日夜夜天天 | 亚洲午夜精品一区二区三区他趣 | yeyeav| 国产影音先锋 | 韩日在线视频 | 亚洲成av| 国产无人区一区二区三区 | 亚洲最色网站 | 国产一区不卡在线观看 |