如何从初始行创建多个行

时间:2017-01-12 17:08:35

标签: mysql sql

我使用 mysql db engine ,我不知道有可能将表中的数据一行转移到另一个表中,这个表将包含两列,id和value 每个传输的值将进入一行,而行看起来像ID,值,并且只要它具有传递给新行的值,只要它具有属于id的值,就保持id。转移的一行

初始表看起来像

id  |country_name   |city_1      |city_2      |city_3      |city_4
------------------------------------------------------------------------
1   |Some_country   |some_city1  |some_city2  |some_city3  |some_city4

通缉表看起来像

 id | city_name
 1  |  some_city1
 1  |  some_city2
 1  |  some_city3
 1  |  some_city4

2 个答案:

答案 0 :(得分:1)

将此用于特定ID

select id, city_name from(
    select id, city_1 as city_name from yourTable    
    union all
    select id, city_2 from yourTable    
    union all
    select id, city_3 from yourTable    
    union all
    select id, city_4 from yourTable
) as t where id= yourID

http://sqlfiddle.com/#!9/7ee1f/1

将此用于整个表格

 select id, city_name from(
    select id, city_1 as city_name from yourTable    
    union all
    select id, city_2 from yourTable    
    union all
    select id, city_3 from yourTable    
    union all
    select id, city_4 from yourTable
) as t
order by id

答案 1 :(得分:0)

您正在寻找的内容通常被称为垂直旋转:您希望将类似四个城市名称的数组(在表格定义中硬连线)转换为四个垂直行。

解决方案是一个带有临时表的交叉连接,其中包含从1开始的连续整数,因为您有要旋转的列,以及使用该系列整数的CASE-WHEN表达式。

见这里:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="participants" match="Eligibility_Detail_Record" use="PT_PARTICIPANT_FILE_IMP_ID"/>

  <xsl:template match="/*">
    <xsl:copy>
      <!--Strip all Eligibility_Detail_Records that have a blank PT_PARTICIPANT_FILE_IMP_ID AND The EN_PLAN_NAME node is not "Health Reimbursement Arrangement" or "Health Savings Account"-->
      <xsl:for-each select="Eligibility_Detail_Record[string(PT_PARTICIPANT_FILE_IMP_ID) and (EN_PLAN_NAME='Health Reimbursement Arrangement' or EN_PLAN_NAME='Health Savings Account')]
        [count(.|key('participants',PT_PARTICIPANT_FILE_IMP_ID)[1])=1]">
        <xsl:variable name="participantCount" select="count(key('participants',PT_PARTICIPANT_FILE_IMP_ID))"/>
        <Eligibility_Detail_Record>
          <xsl:copy-of select="*[starts-with(name(),'PT_')]|EN_PLAN_NAME"/>
          <xsl:choose>
            <!--Add an EN_PLAN_TYPE field for each Record that has an EN_PLAN_NAME of "Health Reimbursement Arrangement" that reads:-->
            <xsl:when test="EN_PLAN_NAME='Health Reimbursement Arrangement'">
              <xsl:call-template name="HRA">
                <xsl:with-param name="participantCount" select="$participantCount"/>
              </xsl:call-template>
            </xsl:when>
            <!--Add an EN_HDHP_COVERAGE_LEVEL field for each Record that has en EN_PLAN_NAME of "Health Savings Account" that reads:-->
            <xsl:when test="EN_PLAN_NAME='Health Savings Account'">
              <xsl:call-template name="HSA">
                <xsl:with-param name="participantCount" select="$participantCount"/>
              </xsl:call-template>
            </xsl:when>
          </xsl:choose>
          <xsl:copy-of select="*[not(self::EN_PLAN_NAME) and starts-with(name(),'EN_')]"/>
          <!--Create a new Dependent_Record that is a child record of Eligibility_Detail_Record for each DP_PARTICIPANT_FILE_IMP_ID that matches a PT_PARTICIPANT_FILE_IMP_ID-->
          <xsl:for-each select="key('participants',PT_PARTICIPANT_FILE_IMP_ID)[string(DP_PARTICIPANT_FILE_IMP_ID)]">
            <Dependent_Record>
              <xsl:copy-of select="*[starts-with(name(),'DP_')]"/>
            </Dependent_Record>
          </xsl:for-each>
        </Eligibility_Detail_Record>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="HRA">
    <xsl:param name="participantCount"/>
    <EN_PLAN_TYPE>
      <xsl:choose>
        <!--"Family" if more than one dependent-->
        <xsl:when test="$participantCount > 1">Family</xsl:when>
        <!--"IndChild" if one and only one dependent that has DP_RELATIONSHIP of Dependent-->
        <xsl:when test="$participantCount = 1 and DP_RELATIONSHIP = 'Dependent'">IndChild</xsl:when>
        <!--"IndSpouse" if one and only one dependent that has DP_RELATIONSHIP of Spouse-->
        <xsl:when test="$participantCount = 1 and DP_RELATIONSHIP = 'Spouse'">IndSpouse</xsl:when>
        <!--"Ind" if no dependents are found-->
        <xsl:when test="$participantCount = 1">Ind</xsl:when>
      </xsl:choose>
    </EN_PLAN_TYPE>
  </xsl:template>

  <xsl:template name="HSA">
    <xsl:param name="participantCount"/>
    <EN_HDHP_COVERAGE_LEVEL>
      <xsl:choose>
        <!--"Family" if one or more dependents are found-->
        <xsl:when test="$participantCount > 1 or
          $participantCount = 1 and string(DP_RELATIONSHIP)">Family</xsl:when>
        <!--"Ind" if no dependents are found-->
        <xsl:when test="$participantCount = 1">Ind</xsl:when>
      </xsl:choose>
    </EN_HDHP_COVERAGE_LEVEL>
  </xsl:template>

</xsl:stylesheet>

仅在前一天,我回答了一个问题,正在寻找扭转我们在这里执行的操作:水平旋转。 如果你好奇,请看这里...... How to go about a column with different values in a same row in sql?

快乐的游戏 -

Marco the Sane