多张表中具有相同编号的记录合成一条记录。要如何实现啊???

时间:2021-07-30 21:40:18
A表结构如下:
编号        品名           重量     价格  
01          凤凰菜         3.6      19.85
02          粉丝肠         2.99     12.00

B表结构如下:
编号      配料        重量        
01        鸡          2.22 
01        鸭          0.88  
01        佐料        0.5  
02        猪肉        2.10  
02        大肠        0.89  


我要把以上两张表相同编号的记录合并成一条记录如下所示(C表):
编号  品名   重量  价格  配料1  重量  配料2   重量  配料3    重量   
01    凤凰菜  3.6  19.85  鸡    2.22   鸭     0.88  佐料      0.5 
02    粉丝肠  2.99 12.00  猪肉  2.10   大肠   0.89

说明:编号在A表中是唯一的,但在B表中的记录数不是确定的,换句话说C表的列数是不确定的(但编号也只能是唯一的).请问要实现以上的功能SQL语句要如何书写啊?

在以前的贴子中是这样子说的,可我没有明白是如何一回事.http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=182746
还请各位高手帮忙。

14 个解决方案

#1


?????/

#2


这个用SQL语句可能比较难,不如分几次查出结果再显示到网格中

#3


top

#4


你肯定每种菜最多只有3种配料吗?

#5


行转列(case),同时需要子查询

#6


在CSDN上有相关的内容:

【交叉数据报表】
有时候需要旋转结果以便在水平方向显示列,而在垂直方向显示行。这就是所谓的创建 PivotTable®、创建交叉数据报表或旋转数据。

假定有一个表 Pivot,其中每季度占一行。对 Pivot 的 SELECT 操作在垂直方向上列出这些季度:

Year      Quarter      Amount
----      -------      ------
1990      1           1.1
1990      2           1.2
1990      3           1.3
1990      4           1.4
1991      1           2.1
1991      2           2.2
1991      3           2.3
1991      4           2.4

生成报表的表必须是这样的,其中每年占一行,每个季度的数值显示在一个单独的列中,如:

Year
 Q1
 Q2
 Q3
 Q4
 
1990
 1.1
 1.2
 1.3
 1.4
 
1991
 2.1
 2.2
 2.3
 2.4
 


下面的语句用于创建 Pivot 表并在其中填入第一个表中的数据:

USE Northwind
GO

CREATE TABLE Pivot
( Year      SMALLINT,
  Quarter   TINYINT, 
  Amount      DECIMAL(2,1) )
GO
INSERT INTO Pivot VALUES (1990, 1, 1.1)
INSERT INTO Pivot VALUES (1990, 2, 1.2)
INSERT INTO Pivot VALUES (1990, 3, 1.3)
INSERT INTO Pivot VALUES (1990, 4, 1.4)
INSERT INTO Pivot VALUES (1991, 1, 2.1)
INSERT INTO Pivot VALUES (1991, 2, 2.2)
INSERT INTO Pivot VALUES (1991, 3, 2.3)
INSERT INTO Pivot VALUES (1991, 4, 2.4)
GO

下面是用于创建旋转结果的 SELECT 语句:

SELECT Year, 
    SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1,
    SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2,
    SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3,
    SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4
FROM Northwind.dbo.Pivot
GROUP BY Year
GO

该 SELECT 语句还处理其中每个季度占多行的表。GROUP BY 语句将 Pivot 中一年的所有行合并成一行输出。当执行分组操作时,SUM 聚合中的 CASE 函数的应用方式是这样的:将每季度的 Amount 值添加到结果集的适当列中,在其它季度的结果集列中添加 0。

如果该 SELECT 语句的结果用作电子表格的输入,那么电子表格将很容易计算每年的合计。当从应用程序使用 SELECT 时,可能更易于增强 SELECT 语句来计算每年的合计。例如:

SELECT P1.*, (P1.Q1 + P1.Q2 + P1.Q3 + P1.Q4) AS YearTotal
FROM (SELECT Year,
             SUM(CASE P.Quarter WHEN 1 THEN P.Amount ELSE 0 END) AS Q1,
             SUM(CASE P.Quarter WHEN 2 THEN P.Amount ELSE 0 END) AS Q2,
             SUM(CASE P.Quarter WHEN 3 THEN P.Amount ELSE 0 END) AS Q3,
             SUM(CASE P.Quarter WHEN 4 THEN P.Amount ELSE 0 END) AS Q4
     FROM Pivot AS P
     GROUP BY P.Year) AS P1
GO

带有 CUBE 的 GROUP BY 和带有 ROLLUP 的 GROUP BY 都计算与本例显示相同的信息种类,但格式稍有不同。

#7


看在sql语句中用do试试,不过不好意思,我没有试过

#8


谢谢各位。但我还是不很理解。我现在把问题缩小一点:
B表结构如下:
编号      配料        重量        
01        鸡          2.22 
01        鸭          0.88  
01        佐料        0.5  
02        猪肉        2.10  
02        大肠        0.89  

我要把B表相同编号的记录合并成一条记录如下所示(C表):
编号    配料1  重量  配料2   重量  配料3    重量   
01      鸡    2.22   鸭     0.88  佐料      0.5 
02      猪肉  2.10   大肠   0.89
要如何做呢?能否请将针对我这个写个SQL语句好吗我没有用过CASE?
说明一下:在B表中的记录是不确定的。也就是说在生成的新表C中列数是也是不确定的


#9


我问题解决马上结贴感谢各位

#10


http://www.study888.com/computer/data/sqlsl/200506/42597.html

#11


top

#12


嗬嗬,是不是bee?我是RainFly,帮你顶!

#13


谢了,我是BEE。

#14


/*
多条记录如何合并成一条记录A表结构如下: 
 店名       地区         级别  
d1          北京          1  
d2          北京          2 
d3          天津          2  
d4          河肥          3  
d5          杭州          4  
d6          杭州          3   

想得到如下的结果:   
地区    级别1的店数    级别2的店数    级别3的店数  级别4的店数  合计  
北京        1          1              0            0              2  
杭州        0          0              1            1              2  
河肥        0          0              1            0              1  
天津        0          1              0            0              1   
合计        1          2              2            1              6   

*/

--制作新查询表;
set nocount on
If exists (select * from dbo.sysobjects where name='temp_table' and type='U')
   Drop table temp_table
Go
Select distinct stord_zone into temp_table from table1  --插入地名;
Go
declare @aulcom char(20)                        --新增列名;
declare @SqlString varchar(500)
declare mycursor_1 cursor for                   --记录等级的游标
  Select distinct stord_level from table1             --功能:按等级内容插入新列;
Open mycursor_1
Fetch next from mycursor_1 into @aulcom
While (@@fetch_status<>-1)
Begin
  set @sqlstring='alter table temp_table add'+' '+'等级'+rtrim(@aulcom)+'的店数'+' '+'int null'
  execute (@sqlstring)
  Fetch next from mycursor_1 into @aulcom         --加入新列名;
End
Close mycursor_1
Deallocate mycursor_1

--新查询表制作完成;



--以下对新表增加记录数据;
Declare @stord_zone_1 char (20)   --记录地区名,用作查询条件;
Declare @stord_level_1 char (20)  --记录等级别,用作查询条件;
Declare @count_1 int              --记录一等级别的个数;
Declare @sqlstring_1 varchar(500)
Declare mycursor_2 cursor for     --记录地区名的游标;
  Select stord_zone from temp_table
Open mycursor_2
Fetch next from mycursor_2 into @stord_zone_1
While (@@fetch_status<>-1)
Begin
  Declare mycursor_3 cursor for     --记录等级别的游标;
  Select distinct stord_level from table1
  Open mycursor_3
  Fetch next from mycursor_3 into @stord_level_1
    While (@@fetch_status<>-1)
      Begin
        Select @count_1=count(*) from table1 where stord_zone=@stord_zone_1 and stord_level=@stord_level_1
        set @sqlstring_1='update temp_table set'+' '+'等级'+rtrim(@stord_level_1)+'的店数'+'='+rtrim(@count_1)+' '+'where stord_zone='+''''+rtrim(@stord_zone_1)+''''
        execute (@sqlstring_1)
      Fetch next from mycursor_3 into @stord_level_1
      End
  Close mycursor_3
  Deallocate mycursor_3
  Fetch next from mycursor_2 into @stord_zone_1
End
Close mycursor_2
Deallocate mycursor_2

select * from table1
select * from temp_table      
drop temp_table
go

还有两个合计的..自己看看..我不写了..

#1


?????/

#2


这个用SQL语句可能比较难,不如分几次查出结果再显示到网格中

#3


top

#4


你肯定每种菜最多只有3种配料吗?

#5


行转列(case),同时需要子查询

#6


在CSDN上有相关的内容:

【交叉数据报表】
有时候需要旋转结果以便在水平方向显示列,而在垂直方向显示行。这就是所谓的创建 PivotTable&reg;、创建交叉数据报表或旋转数据。

假定有一个表 Pivot,其中每季度占一行。对 Pivot 的 SELECT 操作在垂直方向上列出这些季度:

Year      Quarter      Amount
----      -------      ------
1990      1           1.1
1990      2           1.2
1990      3           1.3
1990      4           1.4
1991      1           2.1
1991      2           2.2
1991      3           2.3
1991      4           2.4

生成报表的表必须是这样的,其中每年占一行,每个季度的数值显示在一个单独的列中,如:

Year
 Q1
 Q2
 Q3
 Q4
 
1990
 1.1
 1.2
 1.3
 1.4
 
1991
 2.1
 2.2
 2.3
 2.4
 


下面的语句用于创建 Pivot 表并在其中填入第一个表中的数据:

USE Northwind
GO

CREATE TABLE Pivot
( Year      SMALLINT,
  Quarter   TINYINT, 
  Amount      DECIMAL(2,1) )
GO
INSERT INTO Pivot VALUES (1990, 1, 1.1)
INSERT INTO Pivot VALUES (1990, 2, 1.2)
INSERT INTO Pivot VALUES (1990, 3, 1.3)
INSERT INTO Pivot VALUES (1990, 4, 1.4)
INSERT INTO Pivot VALUES (1991, 1, 2.1)
INSERT INTO Pivot VALUES (1991, 2, 2.2)
INSERT INTO Pivot VALUES (1991, 3, 2.3)
INSERT INTO Pivot VALUES (1991, 4, 2.4)
GO

下面是用于创建旋转结果的 SELECT 语句:

SELECT Year, 
    SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1,
    SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2,
    SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3,
    SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4
FROM Northwind.dbo.Pivot
GROUP BY Year
GO

该 SELECT 语句还处理其中每个季度占多行的表。GROUP BY 语句将 Pivot 中一年的所有行合并成一行输出。当执行分组操作时,SUM 聚合中的 CASE 函数的应用方式是这样的:将每季度的 Amount 值添加到结果集的适当列中,在其它季度的结果集列中添加 0。

如果该 SELECT 语句的结果用作电子表格的输入,那么电子表格将很容易计算每年的合计。当从应用程序使用 SELECT 时,可能更易于增强 SELECT 语句来计算每年的合计。例如:

SELECT P1.*, (P1.Q1 + P1.Q2 + P1.Q3 + P1.Q4) AS YearTotal
FROM (SELECT Year,
             SUM(CASE P.Quarter WHEN 1 THEN P.Amount ELSE 0 END) AS Q1,
             SUM(CASE P.Quarter WHEN 2 THEN P.Amount ELSE 0 END) AS Q2,
             SUM(CASE P.Quarter WHEN 3 THEN P.Amount ELSE 0 END) AS Q3,
             SUM(CASE P.Quarter WHEN 4 THEN P.Amount ELSE 0 END) AS Q4
     FROM Pivot AS P
     GROUP BY P.Year) AS P1
GO

带有 CUBE 的 GROUP BY 和带有 ROLLUP 的 GROUP BY 都计算与本例显示相同的信息种类,但格式稍有不同。

#7


看在sql语句中用do试试,不过不好意思,我没有试过

#8


谢谢各位。但我还是不很理解。我现在把问题缩小一点:
B表结构如下:
编号      配料        重量        
01        鸡          2.22 
01        鸭          0.88  
01        佐料        0.5  
02        猪肉        2.10  
02        大肠        0.89  

我要把B表相同编号的记录合并成一条记录如下所示(C表):
编号    配料1  重量  配料2   重量  配料3    重量   
01      鸡    2.22   鸭     0.88  佐料      0.5 
02      猪肉  2.10   大肠   0.89
要如何做呢?能否请将针对我这个写个SQL语句好吗我没有用过CASE?
说明一下:在B表中的记录是不确定的。也就是说在生成的新表C中列数是也是不确定的


#9


我问题解决马上结贴感谢各位

#10


http://www.study888.com/computer/data/sqlsl/200506/42597.html

#11


top

#12


嗬嗬,是不是bee?我是RainFly,帮你顶!

#13


谢了,我是BEE。

#14


/*
多条记录如何合并成一条记录A表结构如下: 
 店名       地区         级别  
d1          北京          1  
d2          北京          2 
d3          天津          2  
d4          河肥          3  
d5          杭州          4  
d6          杭州          3   

想得到如下的结果:   
地区    级别1的店数    级别2的店数    级别3的店数  级别4的店数  合计  
北京        1          1              0            0              2  
杭州        0          0              1            1              2  
河肥        0          0              1            0              1  
天津        0          1              0            0              1   
合计        1          2              2            1              6   

*/

--制作新查询表;
set nocount on
If exists (select * from dbo.sysobjects where name='temp_table' and type='U')
   Drop table temp_table
Go
Select distinct stord_zone into temp_table from table1  --插入地名;
Go
declare @aulcom char(20)                        --新增列名;
declare @SqlString varchar(500)
declare mycursor_1 cursor for                   --记录等级的游标
  Select distinct stord_level from table1             --功能:按等级内容插入新列;
Open mycursor_1
Fetch next from mycursor_1 into @aulcom
While (@@fetch_status<>-1)
Begin
  set @sqlstring='alter table temp_table add'+' '+'等级'+rtrim(@aulcom)+'的店数'+' '+'int null'
  execute (@sqlstring)
  Fetch next from mycursor_1 into @aulcom         --加入新列名;
End
Close mycursor_1
Deallocate mycursor_1

--新查询表制作完成;



--以下对新表增加记录数据;
Declare @stord_zone_1 char (20)   --记录地区名,用作查询条件;
Declare @stord_level_1 char (20)  --记录等级别,用作查询条件;
Declare @count_1 int              --记录一等级别的个数;
Declare @sqlstring_1 varchar(500)
Declare mycursor_2 cursor for     --记录地区名的游标;
  Select stord_zone from temp_table
Open mycursor_2
Fetch next from mycursor_2 into @stord_zone_1
While (@@fetch_status<>-1)
Begin
  Declare mycursor_3 cursor for     --记录等级别的游标;
  Select distinct stord_level from table1
  Open mycursor_3
  Fetch next from mycursor_3 into @stord_level_1
    While (@@fetch_status<>-1)
      Begin
        Select @count_1=count(*) from table1 where stord_zone=@stord_zone_1 and stord_level=@stord_level_1
        set @sqlstring_1='update temp_table set'+' '+'等级'+rtrim(@stord_level_1)+'的店数'+'='+rtrim(@count_1)+' '+'where stord_zone='+''''+rtrim(@stord_zone_1)+''''
        execute (@sqlstring_1)
      Fetch next from mycursor_3 into @stord_level_1
      End
  Close mycursor_3
  Deallocate mycursor_3
  Fetch next from mycursor_2 into @stord_zone_1
End
Close mycursor_2
Deallocate mycursor_2

select * from table1
select * from temp_table      
drop temp_table
go

还有两个合计的..自己看看..我不写了..