在连接后将两行合并为一行

时间:2015-07-27 07:21:21

标签: sql sql-server

我从外部程序给出了以下表格的数据,因此我无法修改它代表元素的方式。这个想法是单个元素由(xy)组成,并且当程序输出度量时,将某些(模糊)原因转换为两个不同的行。我想把它变成一行,结果是度量的总和。以下是数据格式。

+------------+---------+---------+---------+
|    Manager |    x    |    y    | Measure |
+------------+---------+---------+---------+
| A          | Left    | Right   | 10      |
| A          | Right   | Left    | -9      |
| A          | Venstre | Hojre   | 4       |
| A          | Hojre   | Venstre | -1      |

                     ...
+------------+---------+---------+---------+

这就是我想要的:

+------------+---------+---------+---------+
|    Manager |    x    |    y    | Measure |
+------------+---------+---------+---------+
| A          | Left    | Right   | 1       |
| A          | Venstre | Hojre   | 3       |
                    ...
+------------+---------+---------+---------+

我当前的简单连接,它复制了行:

SELECT * FROM _table s1
JOIN _table s2
ON s1.manager = s2.manager 
AND s1.x = s2.y
AND s2.y = s1.x

请不要犹豫,要求澄清。

编辑:看起来像是一致的'在命名左派'并且'对#39;没有。

2 个答案:

答案 0 :(得分:7)

使用 public int row; public int column; public byte[] bmp; public byte[,] data; public double width; public double height; private void button1_Click(object sender, RoutedEventArgs e) { // Create OpenFileDialog Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dlg.FileName = ""; //dlg.DefaultExt = ".jpg"; dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"; // Display OpenFileDialog by calling ShowDialog method Nullable<bool> result = dlg.ShowDialog(); if (result == true) { string filename = dlg.FileName; bmp = readfile(filename); int x = bmp.Length; data = new byte[(int)Math.Sqrt(x), (int)Math.Sqrt(x)]; row = 0; column = 0; for (int i = 0; i < x; i++) { row = i % (int)Math.Sqrt(x); column = i /(int)Math.Sqrt(x); data[row, column] = bmp[i]; } } } public byte[] readfile(String filename) { Image img = new Image(); BitmapImage bitmapImage = new BitmapImage(); Uri uri = new Uri(filename); bitmapImage.UriSource = uri; img.Source = bitmapImage; MemoryStream memStream = new MemoryStream(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapImage)); encoder.Save(memStream); return memStream.GetBuffer(); width = bitmapImage.Width; height = bitmapImage.Height;

我们的想法是重新排列列(GROUP BYx),在这种情况下按字母顺序排列,然后使用排列的列(yFirstColLastCol

SQL Fiddle

GROUP BY

<强>结果

WITH Cte AS(
    SELECT *,
        FirstCol = CASE WHEN x <= y THEN x ELSE y END,
        LastCol = CASE WHEN x <= y THEN y ELSE x END
    FROM tbl
)
SELECT
    Manager,
    x = FirstCol,
    y = LastCol,
    Measure = SUM(Measure)
FROM Cte
GROUP BY Manager, FirstCol, LastCol

编辑:如果只有一个| Manager | x | y | Measure | |---------|-------|--------|---------| | A | Left | Right | 1 | | A | Left2 | Right2 | 3 | 组合(没有对),此解决方案将保留原始列顺序。此外,它将保留具有较高xy值的行的顺序:

SQL Fiddle

Measure

答案 1 :(得分:3)

你可以试试这个:

DECLARE @DataSource TABLE
(
    [Manager] CHAR(1)
   ,[x] CHAR(8)
   ,[y] CHAR(8)
   ,[Measure] SMALLINT
);

INSERT INTO @DataSource ([Manager], [x], [y], [Measure])
VALUES ('A', 'Left', 'Right', 10)
      ,('A', 'Right', 'Left', -9)
      ,('A', 'Left2', 'Right2', 4)
      ,('A', 'Right2', 'Left2', -1);

SELECT DS1.[Manager]
      ,DS1.[x] AS [x]
      ,DS2.[x] AS [y]
      ,DS1.[Measure] + DS2.[Measure] AS [Measure]
FROM @DataSource DS1
INNER JOIN @DataSource DS2
    ON DS1.[x] = DS2.[y]
    AND DS1.[y] = DS2.[x]
    AND DS1.[Manager] = DS2.[Manager]
WHERE DS1.[Measure] > DS2.[Measure];

enter image description here