1

I have some data in a table. Some of the data I have summarized in a second table, some more of the data is summarized in a third table.

I want to UNION ALL CORRESPONDING them and then use a WHERE command to exclude the redundant rows from the main table

My issue is that my three tables have mismatched column numbers, as some of the data in the summarized tables is redundant to the main table. Clearly I want to use JOIN so that all tables when used together end up with the same columns.

My problem is that I can't wrap my head around how to put the join in the union, or the union in the join or mesh the two together some how, and if so how.

The Column names of the two summary tables are subsets oft he columnnames in the main table....do I still need a join?

Here is what I have done so far (mostly autogenerated by MS Access, with some addition by me):

SELECT 
[QRY7 time report Query].ResID, 
First([QRY7 time report Query].[ResID(T)]) AS [Name], 
[QRY7 time report Query].Baselocation, 
[QRY7 time report Query].Destination,
[QRY7 time report Query].Project, 
[QRY7 time report Query].[Cust_id(T)],
[QRY7 time report Query].[Hrs P1], 
[QRY7 time report Query].[Hrs P2],
[QRY7 time report Query].[Hrs P3], 
[QRY7 time report Query].[Hrs P4], 
[QRY7 time report Query].[Hrs P5], 
[QRY7 time report Query].[Hrs P6]

FROM 
[Qry8a Overhead Summary] 
INNER JOIN 
([Qry8b Work From Base Summary] 
INNER JOIN 
[QRY7 time report Query] 
ON 
[Qry8b Work From Base Summary].ResID = [QRY7 time report Query].ResID
ON[Qry8a Overhead Summary].ResID = [QRY7 time report Query].ResID 

UNION CORRESPONDING
(ResID,[ResID(T)],Baselocation,[Hrs P1],[Hrs P2],[Hrs P3],[Hrs P4],[Hrs P5],[Hrs P6])

SELECT  
[Qry8a Overhead Summary].[ResID(T)],
[Qry8a Overhead Summary].Baselocation,
[Qry8a Overhead Summary].[Hrs P1],
[Qry8a Overhead Summary].[Hrs P2],
[Qry8a Overhead Summary].[Hrs P3],
[Qry8a Overhead Summary].[Hrs P4],
[Qry8a Overhead Summary].[Hrs P5],
[Qry8a Overhead Summary].[Hrs P6]
FROM 
[Qry8a Overhead Summary] 

UNION CORRESPONDING
(ResID,[ResID(T)],Baselocation,[Hrs P1],[Hrs P2],[Hrs P3],[Hrs P4],[Hrs P5],[Hrs P6])

SELECT  
[Qry8b Work From Base Summary] .[ResID(T)],
[Qry8b Work From Base Summary] .Baselocation,
[Qry8b Work From Base Summary].[Hrs P1],
[Qry8b Work From Base Summary].[Hrs P2],
[Qry8b Work From Base Summary].[Hrs P3],
[Qry8b Work From Base Summary].[Hrs P4],
[Qry8b Work From Base Summary].[Hrs P5],
[Qry8b Work From Base Summary].[Hrs P6]
FROM 
[Qry8b Work From Base Summary]
GROUP BY 
[QRY7 time report Query].ResID,
[QRY7 time report Query].Baselocation, 
[QRY7 time report Query].Destination, 
[QRY7 time report Query].Project, 
[QRY7 time report Query].[Cust_id(T)], 
[QRY7 time report Query].[Hrs P1],
[QRY7 time report Query].[Hrs P2], 
[QRY7 time report Query].[Hrs P3],
[QRY7 time report Query].[Hrs P4], 
[QRY7 time report Query].[Hrs P5],
[QRY7 time report Query].[Hrs P6], 
[QRY7 time report Query].Cust_id

HAVING 
((([QRY7 time report Query].Destination)<>"D-" &[QRY7 time report Query].[Baselocation]) 
AND 
(([QRY7 time report Query].Cust_id)<>2))

ORDER BY [QRY7 time report Query].ResID;

So as far as I can tell (still new at SQL) this is saying, pull in data from Time Report, and join the summary tables so that MS Access knows ResID matches up. Then, make a new table of the fields provided, where they correspond, making sure anything from the QRY7 data HAVING destination <> baselocation (Summarized in Overheads) and Cust_Id <> 2 (summarized in word from base).

Now what it's saying is that I've gotten the From syntax wrong, but I can't tell where this is. I don't even know if I'm doing all of this right

Edit:

Here is some a sample of the data in the 'Master' table:

ResID      Nm    Base     Proj      Destination Cust_id     P1     P2     P3
MJW     Mary  LONDON   Project1              Overhead           
                       Project2  A-LONDON    Overhead                  0.39%
                       Project3  UK          Overhead   1.24%       
                       Project12 B-LONDON    232        19.47% 60.02%  0.82%
                       Project13             232        12.44%      
                       Project16 A-LONDON    56                3.5%    
                       Project17 B-LONDON    56                        9.8%

And the over head summary:

ResID Nm     Base    Cust_id    P1    P2     P3
MJW   Mary   LONDON  Overhead   4.61% 2.31%  3.13%

And the Work form base summary

ResID Nm     Base    Cust_id    P1    P2     P3
MJW   Mary   LONDON  232        0.43% 5.51%  0.09%
MJW   Mary   LONDON  562        0.43% 5.51%  0.09%

Here is them merged together

R_Id Nm      Base    Dest        Proj      Cust_id       P1      P2     P3
MJW Mary    LONDON  UK          Overhead  SumOver       1%      7.4%   4.8% 
                     A-London    Project1  SumBaseCust1  1.68%   0%     24.93%
                     B-London    Project2  SumBaseCust1  2.48%   0%     4.3%
                                 Project13 232           10 %           0
                     B-LONDON    Project16 56            3.5%    
                     B-LONDON    Project17 56                    9.8%

Some of the entries are blank/null.

From memory the two summary tables contain the ResID, ProjectID and percentages, the Work From Base table on contains Customer-Id on top of this. The top two entries for Bob should come from the two summary tables, the third entry is an example entry that should not be dropped, the fourth has the same Destination as the base location so it will be dropped from the resultant table (I've added it so I can point out that some rows will be left out)

I have to JOIN to make the number of columns match (right?), and UNION to stitch the rows together (Right?). How do I combine them?

4

1 回答 1

0

使用具有唯一值的附加查询是否有意义?从两个汇总表开始,最后附加第一个表?

附加语句将允许您选择哪些列与哪些列匹配,并且唯一值将防止您复制数据。

或者,您可以追加到一个新表中,在该表中设置了要保持唯一的字段的主键?

于 2012-08-09T20:44:39.397 回答