在SQL中将多行和多列合并为单行

时间:2018-07-08 02:41:48

标签: sql sql-server tsql

我已经阅读了一些有关在SQL中合并行的主题,并且大多数涉及使用数据透视。我不确定枢纽是否会在这里有所帮助,因为我试图将多列合并为一行,而不是为同一列提取值。

这是桌子的样子:

OrderID   XboxLive   iTunes   XboxDate   iTunesDate   SerialNumber
9439      50.00      NULL     9/1/2018   NULL         12345
9439      NULL       82.00    NULL       9/2/2018     12345
9440      70.00      NULL     9/10/2018  NULL         12346

我希望结果看起来像这样

OrderID   XboxLive   iTunes   XboxDate   iTunesDate   SerialNumber
9439      50.00      82.00    9/1/2018   9/2/2018     12345
9440      70.00      NULL     9/10/2018  NULL         12346

我尝试了以下这段代码的不同变体,但无济于事:

SELECT
  ISNULL(Xbox.OrderID, Apple.OrderID) AS OrderID,
  Xbox.XboxLive,
  Apple.iTunes,
  Xbox.XboxDate,
  Apple.iTunesDate
  ISNULL(Xbox.Serial, Apple.Serial) AS Serial
FROM SampleTable AS Xbox
FULL JOIN SampleTable AS Apple
  ON Apple.OrderID = Xbox.OrderID
  AND Apple.Serial = Xbox.Serial
 WHERE Xbox.XboxLive > 0
 OR Apple.iTunes > 0

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

尝试以下操作:

var width, height;
    var camera, renderer, scene;

    var position = {
        earth: {
            x: 200,
            y: 1,
            z: 0,
            theta: 0,
            traceRadius: 200
        }
    };

    width = window.innerWidth;
    height = window.innerHeight;

    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(45,        // Field of view
        400 / 400,  // Aspect ratio
        .1,         // Near
        10000       // Far);
    );
    camera.lookAt(scene.position);
    camera.position.set(0, 0, 1000);
    scene.add(camera);

    renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(0x000000, 1);
    renderer.setSize(width, height);
    renderer.shadowMapEnabled = false;

    var sunGeo = new THREE.SphereGeometry(70, 128, 128);
    var sunMat = new THREE.MeshLambertMaterial({color: 0x00ff00});
    var sunMesh = new THREE.Mesh(sunGeo, sunMat);
    sunMesh.position.set(0, 0, 0);
    sunMesh.castShadow = true;
    sunMesh.receiveShadow = false;
    scene.add(sunMesh);

    var boxGeo = new THREE.BoxGeometry(50, 50, 50);
    var boxMat = new THREE.MeshLambertMaterial({color: 0xfff0f0});
    var boxMesh = new THREE.Mesh(boxGeo, boxMat);
    boxMesh.position.set(-100, 100, 0);
    boxMesh.castShadow = true;
    scene.add(boxMesh);

    var earthTraceGeo = new THREE.CircleGeometry(position.earth.traceRadius, 128, 128);
    var edges = new THREE.EdgesGeometry(earthTraceGeo);
    var line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({color: 0xffffff}));
    scene.add(line);

    var earthGeo = new THREE.SphereGeometry(30, 128, 128);
    var earthMat = new THREE.MeshLambertMaterial({color: 0x0000ff});
    var earthMesh = new THREE.Mesh(earthGeo, earthMat);
    earthMesh.position.set(position.earth.traceRadius, 0, 0);
    earthMesh.castShadow = true;
    earthMesh.receiveShadow = true;
    scene.add(earthMesh);

    var lightSize = 250;
    var pointLight = new THREE.PointLight(0xff0000, 1000, lightSize, 2);
    pointLight.position.set(110, 0, 110);
    pointLight.castShadow = true;
    scene.add(pointLight);

    var spotLight = new THREE.SpotLight(0xffffff, 1, 1000);
    spotLight.position.set(-200, 120, 200);
    spotLight.castShadow = true;
    scene.add(spotLight);

    var spotLightHelper = new THREE.SpotLightHelper( spotLight, 0xffbbaa);
    scene.add( spotLightHelper );

    var pointLightHelper = new THREE.PointLightHelper( pointLight, lightSize );
    scene.add( pointLightHelper );

    var ambientLight = new THREE.AmbientLight(0x404040);
//    scene.add(ambientLight);
    renderer.render(scene, camera);

    render();
    document.getElementById("WebGL-output").appendChild(renderer.domElement);

    function render() {
//        spotLightHelper.update();

        position.earth.theta += 1;
        position.earth.x = getX(0, position.earth.theta, position.earth.traceRadius);
        position.earth.y = getY(0, position.earth.theta, position.earth.traceRadius);
        earthMesh.position.set(position.earth.x, position.earth.y, 0);

        renderer.render(scene, camera);

        requestAnimationFrame(render);
    }

    function getX(x, theta, radius) {
        return x + Math.cos((Math.PI / 180) * theta) * radius;
    }

    function getY(y, theta, radius) {
        return y + Math.sin((Math.PI / 180) * theta) * radius;
    }

答案 1 :(得分:0)

GROUPING BY OrderID 足以使用AVG之类的适当聚合函数对于 SerialNumber

SELECT OrderID, max(XboxLive) as XboxLive,
                max(iTunes) as iTunes,
                max(XboxDate) as XboxDate,
                max(iTunesDate) as iTunesDate,
                avg(SerialNumber) as SerialNumber
  FROM tab
 GROUP BY OrderID;

SQL Fiddle Demo 1

通过使用样式合并已拆分的sampleTable,您将再次需要max聚合,如下所示:

SELECT ISNULL(Xbox.OrderID, Apple.OrderID) AS OrderID,
       max(Xbox.XboxLive) as XboxLive,  
       max(Apple.iTunes) as iTunes,  
       max(Xbox.XboxDate) as XboxDate,
       max(Apple.iTunesDate) as iTunesDate,  
       max(ISNULL(Xbox.SerialNumber, Apple.SerialNumber)) AS Serial
  FROM SampleTable Xbox
  LEFT OUTER JOIN SampleTable Apple
    ON ( Apple.OrderID = Xbox.OrderID AND Apple.SerialNumber = Xbox.SerialNumber )
 GROUP BY ISNULL(Xbox.OrderID, Apple.OrderID);

SQL Fiddle Demo 2