• SQL合并数据


    --CREATE TABLE TMaterial (PMaterial INT,FName NVARCHAR(32))
    --INSERT INTO TMaterial
    --SELECT 1,'A' UNION ALL
    --SELECT 2,'B' UNION ALL
    --SELECT 3,'C'

    --CREATE TABLE TOrderIN (FCode NVARCHAR(32),FAmount INT,KMaterial INT)
    --INSERT INTO TOrderIN
    --SELECT 'i001',10,1 UNION ALL
    --SELECT 'i002',10,1 UNION ALL
    --SELECT 'i003',10,2

    --CREATE TABLE TOrderOut(FCode NVARCHAR(32),FAmount INT,KMaterial INT)
    --INSERT INTO TOrderOut
    --SELECT 't001',20,1 UNION ALL
    --SELECT 't002',20,2 UNION ALL
    --SELECT 't003',20,2 UNION ALL
    --SELECT 't004',20,3
    --GO

    SELECT PMaterial,FName,MAX(inCode) AS inCode,MAX(inAmount) AS inAmount,MAX(outCode) AS outCode,MAX(outAmount) AS outAmount FROM
    (
    SELECT ROW_NUMBER() OVER(PARTITION BY PMaterial ORDER BY PMaterial) AS GB,PMaterial,FName,FCode AS inCode,FAmount AS inAmount,NULL AS outCode,NULL AS outAmount FROM dbo.TMaterial
    LEFT JOIN dbo.TOrderIN ON PMaterial = KMaterial
    UNION ALL
    SELECT ROW_NUMBER() OVER(PARTITION BY PMaterial ORDER BY PMaterial) AS GB,PMaterial,FName,NULL AS inCode,NULL AS inAmount,FCode AS outCode,FAmount AS outAmount FROM dbo.TMaterial
    LEFT JOIN dbo.TOrderOut ON PMaterial = dbo.TOrderOut.KMaterial
    ) AS T GROUP BY GB,T.PMaterial,T.FName ORDER BY T.PMaterial

  • 相关阅读:
    Spring 学习笔记(4)依赖注入 DI
    05Spring之AOP
    01Spring核心概念
    06Spring之循环依赖
    04Spring之依赖注入
    09Spring 设计模式总结
    08Spring之事务
    01Servlet 回顾
    03Spring之Bean生命周期
    02Spring启动过程
  • 原文地址:https://www.cnblogs.com/linyijia/p/3473637.html
Copyright © 2020-2023  润新知