简体   繁体   中英

MS Access: Query to combine rows

I have 3 tables:

  • Clients
  • User_Defined_Definitions
  • User_Defined_Data

The Clients table has basic information:

ClientID, Name, Address...

The User_Defined_Definitions has the title for the user def fields:

StartDate, SalesRep, Website...

Then the User_Defined_Data table contains the data to fill those fields for each client in the client table.

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. 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. The Description column comes from the User_Defined_Definitions table. And the Data comes from the User_Defined_Data table.

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. 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.

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. 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.

Here's the query I was using to pull the UDFs onto separate lines like outlined above.

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.

Why don't you rebuild that table so that it contains all the fields you require and is one-to one with Clients ?

Otherwise you may use CASE statement in your query and filter its contents by description .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM