如何在SQL Server中合并相同的列数据

时间:2017-12-27 10:15:02

标签: sql sql-server

我不知道如何合并相同的列数据,请帮助我

类别

   cid  cname
   ---------------
    1   Carat
    2   Metal Type
    3   Color

子类别

  subid cid name
  -----------------
   1    1   14k
   2    2   Gold
   3    3   White
   4    3   Yellow
   5    3   Rose

我需要这个输出:

id     name 
-----------------------
 1    14k_Gold_White
 2    14k_Gold_Yellow
 3    14k_Gold_Rose

请帮帮我

4 个答案:

答案 0 :(得分:1)

试试这个:

select A1.name + '_' + A2.name + '_' + A3.name from
(select name from sub_category where cid = 1) as A1
cross join
(select name from sub_category where cid = 2) as A2
cross join
(select name from sub_category where cid = 3) as A3

答案 1 :(得分:1)

请试试这个

数据生成和表创建 -

CREATE TABLE SubCategory
(
   subid INT
  ,cid INT
  ,name VARCHAR(10)
)
GO

INSERT INTO SubCategory VALUES
(1,1,'14k'),
(2,2,'Gold'),
(6,2,'Silver'),
(3,3,'White'),
(4,3,'Yellow'),
(5,3,'Rose')
GO

SELECT 
    DISTINCT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) id, CONCAT(sc.name,'-',sc1.name,'-',sc2.name) 
FROM SubCategory sc
INNER JOIN SubCategory sc1 on sc1.cid > sc.cid
INNER JOIN SubCategory sc2 on sc2.cid > sc1.cid

输出

/*------------------------
SELECT 
    DISTINCT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) id, CONCAT(sc.name,'-',sc1.name,'-',sc2.name) 
FROM SubCategory sc
INNER JOIN SubCategory sc1 on sc1.cid > sc.cid
INNER JOIN SubCategory sc2 on sc2.cid > sc1.cid
------------------------*/
id                   
-------------------- --------------------------------
1                    14k-Gold-White
2                    14k-Silver-White
3                    14k-Gold-Yellow
4                    14k-Silver-Yellow
5                    14k-Gold-Rose
6                    14k-Silver-Rose

(6 row(s) affected)

答案 2 :(得分:0)

//使用以下内容我希望它有用..

select Convert(nvarchar(50),cid)+'-'+Convert(nvarchar(50),name) as cid_name from tablename

答案 3 :(得分:0)

你有一个排列项目。通常,您将使用递归算法来解决排列问题。在这种情况下,表类别确定最终结果中的项目的顺序和数量。我使用类来模拟数据库中的表。见下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static List<string> finalOutputs = new List<string>();
        static List<Category> category = null;
        static List<SubCategory> subcategory = null;
        static int[] cids;
        static void Main(string[] args)
        {
            category = new List<Category>() {
                new Category() { cid = 1, cname = "Carat"},
                new Category() { cid = 2, cname = "Metal Type"},
                new Category() { cid = 3, cname = "Color"}
            };

            subcategory = new List<SubCategory>() {
                new SubCategory() { subidid = 1, cid = 1, name = "14k"},
                new SubCategory() { subidid = 2, cid = 2, name = "Gold"},
                new SubCategory() { subidid = 3, cid = 3, name = "White"},
                new SubCategory() { subidid = 4, cid = 3, name = "Yellow"},
                new SubCategory() { subidid = 5, cid = 3, name = "Rose"}
            };

            cids = category.Select(x => x.cid).ToArray();

            Recursive(0, new List<string>());

            foreach(string output in finalOutputs)
            {
                Console.WriteLine(output);
            }
            Console.ReadLine();
        }
        static void Recursive(int index, List<string> outputs)
        {
            if (index == cids.Length)
            {
                finalOutputs.Add(string.Join("_", outputs));
            }
            else
            {
                foreach (string name in subcategory.Where(x => x.cid == cids[index]).Select(x => x.name))
                {
                    List<string> newOutput = new List<string>();
                    newOutput.AddRange(outputs);
                    newOutput.Add(name);
                    Recursive(index + 1, newOutput);
                }
            }
        }

    }
    public class Category
    {
        public int cid { get; set; }
        public string cname { get; set; }
    }
    public class SubCategory
    {
        public int cid { get; set; }
        public int subidid { get; set; }
        public string name { get; set; }
    }
}