[英]MS Access: Query to combine rows
I have 3 tables: 我有3张桌子:
The Clients
table has basic information: Clients
表具有基本信息:
ClientID, Name, Address...
The User_Defined_Definitions
has the title for the user def fields: User_Defined_Definitions
具有用户def字段的标题:
StartDate, SalesRep, Website...
Then the User_Defined_Data
table contains the data to fill those fields for each client in the client table. 然后, User_Defined_Data
表包含用于填充客户端表中每个客户端的那些字段的数据。
I've built a query which pulls all the data I need, but it pulls one row for each of the User_Defined_Data
rows that exist for that client. 我建立了一个查询,该查询将提取我需要的所有数据,但对于该客户端存在的每个User_Defined_Data
行,它都会提取一行。 What I would like to do is pull one row for each client, and append as many columns as needed to fill all the user defined fields into the one row. 我想做的是为每个客户端拉一行,然后根据需要追加任意列,以将所有用户定义的字段填充到该行中。
ClientID Name Address Description Data
155555 Calvin 123 Fake St StartDate 10/10/2013
155555 Calvin 123 Fake St SalesRep Tom
155555 Calvin 123 Fake St Website http://www.fakesite.com
155556 Steve 456 Root Rd StartDate 8/5/2013
155557 Kathy 715 Main St StartDate 5/5/2010
155557 Kathy 715 Main St SalesRep Jessica
155557 Kathy 715 Main St Website http://www.fakesite2.com
155558 Jenny 345 Fake St StartDate 9/9/2012
155558 Jenny 345 Fake St Website http://www.fakesite3.com
The first three columns come from the Client
table. 前三列来自Client
表。 The Description
column comes from the User_Defined_Definitions
table. Description
列来自User_Defined_Definitions
表。 And the Data
comes from the User_Defined_Data
table. Data
来自User_Defined_Data
表。
And I'd like to see it similar to this 我希望看到与此类似的内容
ClientID Name Address StartDate SalesRep Website
155555 Calvin 123 Fake St 10/10/2013 Tom http://www.fakesite.com
155556 Steve 456 Root Rd 8/5/2013
155557 Kathy 715 Main St 5/5/2010 Jessica http://www.fakesite2.com
155558 Jenny 345 Fake St 9/9/2012 http://www.fakesite3.com
I know this is possible using sql, but I've just usually written scripts to deal with the data after the export. 我知道使用sql可以实现,但是我通常只是编写脚本来处理导出后的数据。 But this time I need to be able to do it in Access so the table can be re-opened 2 months from now and it will re-query the data in the same way. 但是这次我需要能够在Access中执行此操作,以便可以从2个月后重新打开该表,并且它将以相同的方式重新查询数据。
Let me know if there's anything else I can provide to help get the right result. 让我知道是否还有其他方法可以帮助您获得正确的结果。
ADDON 添加在
I took the long way around and made individual queries for each of the UDF's, then consolidated them all by running a query agains't each to place them all in one line. 我走了很长一段路,并对每个UDF进行了单独的查询,然后通过再次运行查询来合并它们,而不是将每个查询都放在一行中。 I am still curious how to do this with SQL though because right now I'm in the midst of changing CRM and Accounting software for my organization, and it's become a daunting task. 我仍然很好奇如何使用SQL进行操作,因为现在我正处于为组织更改CRM和会计软件的过程中,这已成为一项艰巨的任务。
Here's the query I was using to pull the UDFs onto separate lines like outlined above. 这是我用来将UDF拉到上面概述的单独行上的查询。
SELECT dbo_AMGR_Client_Tbl.Client_Id, dbo_AMGR_Client_Tbl.Name,
dbo_AMGR_Client_Tbl.Address_Line_1, dbo_AMGR_Client_Tbl.Address_Line_2,
dbo_AMGR_Client_Tbl.City, dbo_AMGR_Client_Tbl.State_Province,
dbo_AMGR_Client_Tbl.Zip_Code, dbo_AMGR_User_Field_Defs_Tbl.Description,
dbo_AMGR_User_Fields_Tbl.DateCol, dbo_AMGR_User_Fields_Tbl.NumericCol,
dbo_AMGR_User_Fields_Tbl.AlphaNumericCol
FROM dbo_AMGR_User_Field_Defs_Tbl INNER JOIN (dbo_AMGR_Client_Tbl INNER JOIN
dbo_AMGR_User_Fields_Tbl ON dbo_AMGR_Client_Tbl.Client_Id =
dbo_AMGR_User_Fields_Tbl.Client_Id) ON (dbo_AMGR_User_Field_Defs_Tbl.Type_Id =
dbo_AMGR_User_Fields_Tbl.Type_Id) AND (dbo_AMGR_User_Field_Defs_Tbl.Code_Id =
dbo_AMGR_User_Fields_Tbl.Code_Id)
GROUP BY dbo_AMGR_Client_Tbl.Name_Type, dbo_AMGR_Client_Tbl.Client_Id,
dbo_AMGR_Client_Tbl.Name, dbo_AMGR_Client_Tbl.Address_Line_1,
dbo_AMGR_Client_Tbl.Address_Line_2, dbo_AMGR_Client_Tbl.City,
dbo_AMGR_Client_Tbl.State_Province, dbo_AMGR_Client_Tbl.Zip_Code,
dbo_AMGR_User_Field_Defs_Tbl.Description, dbo_AMGR_User_Fields_Tbl.DateCol,
dbo_AMGR_User_Fields_Tbl.NumericCol, dbo_AMGR_User_Fields_Tbl.AlphaNumericCol
HAVING (((dbo_AMGR_Client_Tbl.Name_Type)="C") AND ((dbo_AMGR_Client_Tbl.Name) Not Like "*HOUSE*"))
ORDER BY dbo_AMGR_Client_Tbl.Name;
It seems you have multiple rows related to one clientID
in the User_Defined_Data
table. 似乎您有多个与User_Defined_Data
表中的一个clientID
相关的行。
Why don't you rebuild that table so that it contains all the fields you require and is one-to one with Clients
? 为什么不重建该表,使其包含您需要的所有字段,并且与Clients
一对一?
Otherwise you may use CASE
statement in your query and filter its contents by description
. 否则,您可以在查询中使用CASE
语句,并按description
过滤其内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.