从单个表中拆分列,并在另一个列上作为多个列连接

时间:2018-05-01 15:14:05

标签: sql-server split sql-server-2014 multiple-columns

我想将1个表中的列拆分为另一个表中的多个列

假设我想将FoodAttributeTable加入FoodTable:

FoodTable

+------------+-------+
|    Food    | Price |
+------------+-------+
| Strawberry |    10 |
| Broccoli   |    25 |
+------------+-------+

FoodAttributeTable

+------------+---------------+----------------+
|    Food    | AttributeName | AttributeValue |
+------------+---------------+----------------+
| Strawberry | Vitamin       | C              |
| Strawberry | Weight        | 15g            |
| Strawberry | Color         | Red            |
| Broccoli   | Vitamin       | B              |
| Broccoli   | Weight        | 70g            |
| Broccoli   | Color         | Green          |
+------------+---------------+----------------+

表格为:

FoodTable

+------------+-------+---------+--------+-------+
|    Food    | Price | Vitamin | Weight | Color |
+------------+-------+---------+--------+-------+
| Strawberry |    10 | C       | 15g    | Red   |
| Broccoli   |    25 | B       | 70g    | Green |
+------------+-------+---------+--------+-------+

2 个答案:

答案 0 :(得分:1)

你可以使用如下的简单PIVOT查询。 查看官方 MSDN documentation on PIVOT

select 
Food,Price,Vitamin,Weight,Color
from
(
    select f.Food,f.Price,
     AttributeName  ,AttributeValue
    from
    FoodTable f join
    FoodAttributeTable fat on
    f.Food=fat.Food
)s
pivot
(max(AttributeValue) for AttributeName in (Vitamin,Weight,Color))
p

此处还有 live demo

答案 1 :(得分:0)

这是动态数据透视查询。因此,如果您的import React, { Component } from 'react'; import { Dimensions, StyleSheet, Text, View } from 'react-native'; import { Table, Row, Rows} from 'react-native-table-component'; const {width, height} = Dimensions.get('window'); export default class TableReactNative extends Component { constructor(props) { super(props); this.state = { tableHead: ['Head1', 'Head2', 'Head3'], tableData: [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ] } } renderTable(){ return this.state.tableData.map((tableDataRow)=>{ return( <View key={tableDataRow[0]}> <Table> <Row data={this.state.tableHead}/> <Row data={tableDataRow}/> </Table> </View> ) }) } render() { return ( <View style={{width: width, height:height }}> {this.renderTable()} </View> ) } } 属性是动态的,我建议您这样做。

创建表格和插入数据

FoodAttributeTable

查询

CREATE TABLE FoodAttributeTable(
    Food nvarchar(50) NULL,
    AttributeName nvarchar(50) NULL,
    AttributeValue nvarchar(50) NULL
) 

INSERT FoodAttributeTable (Food, AttributeName, AttributeValue) VALUES (N'Strawberry', N'Vitamin', N'C')    
INSERT FoodAttributeTable (Food, AttributeName, AttributeValue) VALUES (N'Strawberry', N'Weight', N'15g')    
INSERT FoodAttributeTable (Food, AttributeName, AttributeValue) VALUES (N'Strawberry', N'Color', N'Red')    
INSERT FoodAttributeTable (Food, AttributeName, AttributeValue) VALUES (N'Broccoli', N'Vitamin', N'B')    
INSERT FoodAttributeTable (Food, AttributeName, AttributeValue) VALUES (N'Broccoli', N'Weight', N'70g')    
INSERT FoodAttributeTable (Food, AttributeName, AttributeValue) VALUES (N'Broccoli', N'Color', N'Green')


CREATE TABLE FoodTable(
    Food nvarchar(50) NULL,
    Price decimal(18, 0) NULL
)

INSERT FoodTable (Food, Price) VALUES (N'Strawberry', N'10')    
INSERT FoodTable (Food, Price) VALUES (N'Broccoli', N'25')

输出

DECLARE @colsValues AS NVARCHAR(max) = Stuff((SELECT DISTINCT ',' + Quotename(fat.attributename)
         FROM   foodattributetable fat       
         FOR xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, ''); 

DECLARE @query AS NVARCHAR(max) =  'SELECT     * 
                                    FROM       (SELECT * 
                                                FROM   foodattributetable 
                                                PIVOT(Max(attributevalue) 
                                                FOR attributename IN ('+ @colsValues +')) piv) fat
                                    INNER JOIN foodtable tb 
                                    ON         tb.food = fat.food';

EXECUTE(@query) 

演示:http://rextester.com/ATZF46215