无法根据我的要求输出编写SQL查询

时间:2018-05-09 03:55:22

标签: sql ms-access

我在MS Access数据库(Table1,Table2)中有两个表,它们具有相同的列IdPartNoNomenclatureQuantityWksp。 Id是其中的主键。两个表中的Id可以相同。现在我想编写一个查询来获取所需输出下显示的输出。我怎样才能做到这一点?

Table1
+----+--------+--------------+----------+----------+
| Id | PartNo | Nomenclature | Quantity |   Wksp   |
+----+--------+--------------+----------+----------+
|  1 | Part1  | Nomenc1      |        2 |    Wksp1 |
|  2 | Part2  | Nomenc2      |        4 |    Wksp1 |
|  3 | Part3  | Nomenc3      |        6 |    Wksp1 |
|  4 | Part4  | Nomenc4      |        8 |    Wksp1 |
+----+--------+--------------+----------+----------+

Table2
+----+--------+--------------+----------+-------+
| Id | PartNo | Nomenclature | Quantity | Wksp  |
+----+--------+--------------+----------+-------+
|  1 | Part1  | Nomenc1      |        1 | Wksp2 |
|  2 | Part2  | Nomenc2      |        3 | Wksp2 |
|  3 | Part3  | Nomenc3      |        5 | Wksp2 |
|  4 | Part11 | Nomenc11     |        7 | Wksp2 |
|  5 | Part7  | Nomenc7      |        9 | Wksp2 |
+----+--------+--------------+----------+-------+

必需的输出

+----+--------+--------------+-------+-------+
| Id | PartNo | Nomenclature | Wksp1 | Wksp2 |
+----+--------+--------------+-------+-------+
|  1 | Part1  | Nomenc1      |     2 |     1 |
|  2 | Part2  | Nomenc2      |     4 |     3 |
|  3 | Part3  | Nomenc3      |     6 |     5 |
|  4 | Part11 | Nomenc11     |     0 |     7 |
|  5 | Part7  | Nomenc7      |     0 |     9 |
|  6 | Part4  | Nomenc4      |     8 |     0 |
+----+--------+--------------+-------+-------+

Tables and required Output Image

3 个答案:

答案 0 :(得分:2)

首先创建一个查询来收集ID:

awk: cmd. line:1: BEGIN { FS="\t;" } {print  $1"\t"$2"\t"$3"\t"$4=$4-12"\t"$5=$4+50"\t"$6"\t"$7"\t"$8"\t"$9" "$10";"$11" "$12";"$13" "$14";"$15" "$16";"$17" "$18";"$19" "$20";"$21" "$22";"$23" "$24";"$25" "$26";"$27" "$28";"$29" "$30";"$31" "$32 ";" $33" "$34";"$35" "$36";"$37" "$38";" }
awk: cmd. line:1:                                                                                                                                                                                                                         ^ syntax error
awk: cmd. line:1: BEGIN { FS="\t;" } {print  $1"\t"$2"\t"$3"\t"$4=$4-12"\t"$5=$4+50"\t"$6"\t"$7"\t"$8"\t"$9" "$10";"$11" "$12";"$13" "$14";"$15" "$16";"$17" "$18";"$19" "$20";"$21" "$22";"$23" "$24";"$25" "$26";"$27" "$28";"$29" "$30";"$31" "$32 ";" $33" "$34";"$35" "$36";"$37" "$38";" }
awk: cmd. line:1:                                                                                                                                                                                                                                      ^ syntax error

将此保存为,例如 TBboth

然后将其用作来源:

SELECT Id
FROM Table1
UNION 
SELECT Id
FROM Tabel2;

答案 1 :(得分:0)

试试这个:

select Id , PartNo , Nomenclature, ISNULL(Wksp1,0) Wksp1, ISNULL(Wksp2,0) Wksp2
from 
(
 select  Id , PartNo , Nomenclature ,Wksp,  sum(Quantity) 'count' from Table1
 group by Id , PartNo , Nomenclature,Wksp
 union all
 select  Id , PartNo , Nomenclature ,Wksp,  sum(Quantity) 'count' from Table2
group by Id , PartNo , Nomenclature,Wksp
) src
pivot
(
  sum(count)
  for Wksp in ([Wksp1], [Wksp2])
) P;

答案 2 :(得分:-1)

如果您使用 MS SQL Server

,请尝试使用此方法
DECLARE @T1 TABLE
(
    Id INT,
    Part VARCHAR(20),
    Qty INT,
    Wksp VARCHAR(20) DEFAULT('Wksp 1')
)

DECLARE @T2 TABLE
(
    Id INT,
    Part VARCHAR(20),
    Qty INT,
    Wksp VARCHAR(20) DEFAULT('Wksp 2')
)


INSERT INTO @T1
VALUES(1,'Part1',10,'Wksp 1'),(2,'Part2',15,'Wksp 1'),(3,'Part3',12,'Wksp 1')


INSERT INTO @T1
VALUES(1,'Part1',10,'Wksp 2'),(2,'Part2',15,'Wksp 2'),(4,'Part4',20,'Wksp 2')

SELECT
    Id = coalesce(a.id,b.id),
    Part = coalesce(a.Part,b.Part),
    Qty = coalesce(a.Qty,b.Qty),
    Wksp =coalesce(a.Wksp,b.Wksp)
    FROM @T1 A
       FULL JOIN @T2 b
          on a.Id = b.Id