从3个表创建视图

时间:2015-11-27 08:41:56

标签: sql oracle views

我有3张桌子。

表1

id info1 info2
1  a     b
2  a     b
3  a     b
4  a     b

表2

id table1_id column_id value
1  1         1      10
2  1         2      20
3  1         3      30
4  2         1      40
5  2         2      50
6  2         3      60
7  3         1      70
8  3         2      80
9  3         3      90
10  4         1      100
11  4         2      110
12  4         3      120

表3

column_id column
1         column1
2         column2
3         column3

基于以上所述,我需要创建一个视图,它将以下列方式将行转换为列:

column1 column2 column3
10      20      30
40      50      60
70      80      90
100     110     120

是否可以创建如上所述的视图?

2 个答案:

答案 0 :(得分:1)

如果您使用oracle 11g或更高版本,可以使用以下版本,也可以使用pivot。

SELECT max(CASE 
                WHEN column1 = 'column1'
                    THEN value1
                END) column1
        ,max(CASE 
                WHEN column1 = 'column2'
                    THEN value1
                END) column2
        ,max(CASE 
                WHEN column1 = 'column3'
                    THEN value1
                END) column3
    FROM (
        SELECT t2.table1_id
            ,t3.column1
            ,to_char(t2.value1) AS value1
        FROM table2 t2
        JOIN table1 t1 ON t1.id1 = t2.table1_id
        JOIN table3 t3 ON t3.column_id = t2.column_id
        )
    GROUP BY table1_id

答案 1 :(得分:0)

问题的简短答案是肯定的,问题的解决方案可能是加入表格然后转动它们。

我已经写了一些代码作为建议来帮助你,这是未经测试的代码,你将不得不稍微调整一下

   $scope.height = window.innerHeight - 110;
    /**
    * d3 code for the thermometer! d3 is a library for javascript and injected in the index.html in the www map.
    */

    //this is an array for the chart is using for the rectangle in the chart itself. it gets it data form the ChallengeService
var bardata = [$scope.challenge.amount];

    //this code is used to correctly show the axes of the chart.
var margin = {top: 30 , right: 30 , bottom: 40 , left: 50 };

    // these are values needed to drawn the chart correctly $scope.height is the ion-view - header bar - footer bar.
var height = $scope.height - margin.top - margin.bottom,
    width = 200 -  margin.left - margin.right,
    barWidth = 50,
    barOffset = 5,
    border = 5,
    bordercolor = '#F5F5F5';

    //used for the axis in scaling
var yScale = d3.scale.linear()
    .domain([0, 2184])
    .range([0, height]);

var xScale = d3.scale.ordinal()
    .domain(d3.range(0, bardata.length))
    .rangeBands([0, width])

    //this is the actual chart that gets drawn.
var myChart = d3.select('#chart').append('svg')
    .attr('width', width + margin.left + margin.right)
    .attr('height', height + margin.top + margin.bottom)
    .attr('id' , 'mijnsvg')
    .append('g')
    .attr('transform', 'translate(' +margin.left +','+margin.right +')')
    .selectAll('rect').data(bardata)
    .enter().append('rect')
    .style('fill', '#d0e4db')
    .attr('width', xScale.rangeBand())
    .attr('x', function(d,i) {
        return xScale(i);
    })
    //.attr('ry', 75)
    .attr('height', 0)
    .attr('y', height)
        //if they want a border this is the way keep in mind that it will shrink the graph and you need to adjust it when drawing!
    //.style("stroke", bordercolor)
    //.style("stroke-width", border)

    // used for how the chart is rendered this allows me to delay the drawing of the chart till the moment the users is on the tab.
myChart.transition()
    .attr('height', function(d) {
        return yScale(d);
    })
    .attr('y', function(d) {
        return height - yScale(d);
    })
    .delay(function(d, i) {
        return i * 50;
    })
    .duration(1300)
    .ease('exp')
    // theese are both axes being drawn
var vGuideScale1 = d3.scale.linear()
    .domain([0, 2184])
    .range([height, 0])

var vGuideScale2 = d3.scale.linear()
    .domain([0, 2184])
    .range([height, 0])

var vAxisLeft = d3.svg.axis()
    .scale(vGuideScale1)
    .orient('right')
    .ticks(5)
    .tickFormat(function(d){
      return '€ ' + parseInt(d);
    })

var vAxisRight = d3.svg.axis()
    .scale(vGuideScale2)
    .orient('right')
    // i use this in d3 to draw the right line otherwise it will fill in values.
    .ticks("")

var vGuide = d3.select('#mijnsvg').append('g')
    vAxisLeft(vGuide)
    vGuide.attr('transform', 'translate('+ margin.left+',' + margin.top+ ')')
    vGuide.attr('rx', 50)
    vGuide.selectAll('path')
        .style({ fill: '#368169', stroke: "#368169"})
    vGuide.selectAll('line')
        .style({ stroke: "#368169"})

var vGuideRight = d3.select('#mijnsvg').append('g')
    vAxisRight(vGuideRight)
    vGuideRight.attr('transform', 'translate('+ 164.5 + ',' + margin.top +')' )
    vGuideRight.selectAll('path')
        .style({ fill: '#368169', stroke: "#368169"})
    vGuideRight.selectAll('line')
        .style({ stroke: "#368169"})

var hAxis = d3.svg.axis()
    .scale(xScale)
    .orient('top')
    .tickValues('')

var hGuide = d3.select('#mijnsvg').append('g')
        hAxis(hGuide)
    hGuide.attr('transform', 'translate('+ margin.left+','  + margin.top + ')')
    hGuide.selectAll('path')
        .style({ fill: '#368169', stroke: "#368169"})
    hGuide.selectAll('line')
        .style({ stroke: "#368169"})
        .style('xr', 100)