Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making Crosstab Query - "Number of Services" Matrix

Status
Not open for further replies.

Milin

Technical User
Jan 5, 2000
33
US
Hello, <br><br>I can't quite figure out how to do this - do I need SQL? ...I am a newbie at it<br><br>I have households of customers(all grouped by a unique household group id number) and the magazine (represented by a magazine code) that they responded to.&nbsp;&nbsp;What I want to create is a matrix that tells me &quot;In households that have a response to magazine X, within those same households there were also 20 responses to magazine y, 10 responses to magzine z, etc...&quot;.&nbsp;&nbsp;I don't want to see the actual households/group id numbers, just the cumulative results of my &quot;product mix&quot; of magazines in households.<br><br>I can already make a matrix with magazine code horizonal and household group id number verticle, but what I want is to see the magazine code on both axis. SO a household could be counted in multiple rows, since they would show up in the product mix of every magazine they had a response to.<br><br>Do I make sense? hard to explain in words. I previously used a banking database that gave me the &quot;cross-sell&quot; of products in households...ie..I could see that if someone had an IRA in their household they were more likely to also own a CD.<br><br>Thanks for any help or adviseQ<br>Milin
 
you Might need to make a graph. <p>DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br> Ask me how Bar-codes can help you be more productive.
 
in your crosstab query design view where you have the households as a row heading and magazines as a column heading , drag in magazines again and set it as a row heading also this will give<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mag1&nbsp;&nbsp;&nbsp;mag2&nbsp;&nbsp;&nbsp;mag3<br>res1 mag1<br>res1 mag2<br>res2 mag1<br>res2 mag2<br><br>hope thats what you had in mind<br>
 
Interesting, I've never done one like this before. <br>go14, I tried your method but don't quite understand. Did you skip the crosstab query and build this directly? This is how I did it, getting only count, not percentages:<br><br>Using a structure of <br>Table2: Household, Mag, Owned (Yes/No)<br><br>Query1: <br>SELECT Table2.Mag, Table2_1.Mag, Count(Table2_1.Owned) AS CountOfOwned<br>FROM Table2 INNER JOIN Table2 AS Table2_1 ON Table2.PKHousehold = Table2_1.PKHousehold<br>GROUP BY Table2.Mag, Table2_1.Mag;<br><br>Query1_Crosstab:<br>TRANSFORM Sum(Query1.CountOfOwned) AS SumOfCountOfOwned<br>SELECT Query1.Table2.Mag<br>FROM Query1<br>GROUP BY Query1.Table2.Mag<br>PIVOT Query1.Table2_1.Mag;<br><br>
 
Thanks for your replies!<br><br>I think Elizabeth is going towards what I'm trying to do - but I'm not sure because I don't quite understand what you wrote.&nbsp;&nbsp;What is Tablel2_1?&nbsp;&nbsp;Here's some more info - I have a query that gives me householdID, MagCode, and other address info blah blah. My query and table records look like:<br><br>10&nbsp;&nbsp;Joe Smith&nbsp;&nbsp;123 Elm&nbsp;&nbsp;&nbsp;mag1<br>10&nbsp;&nbsp;Betty Smith 123 Elm&nbsp;&nbsp;Mag2<br>10&nbsp;&nbsp;John Smith 123 Elm&nbsp;&nbsp;&nbsp;mag3<br>20&nbsp;&nbsp;John Doe 1313 E main&nbsp;&nbsp;mag4<br>20&nbsp;&nbsp;John Doe 1313 E main&nbsp;&nbsp;mag2<br>20&nbsp;&nbsp;John Doe 1313 E main&nbsp;&nbsp;mag2<br><br>with 10 & 20 being the household numbers&nbsp;&nbsp;&nbsp;<br><br>I want to make something like<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mag1 mag2 mag3 mag4<br>mag1 total(1)&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br>mag2 total(2)&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>mag3 total(1)&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>mag4 total(1)&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>where Total would be the total number of households that contain mag (but not the household numbers themselves - I already have a cross tab to do that. So, there are a total of two households that have mag2. Within those two mag2 households, one of them also has a mag1 and one of them also has a mag3.&nbsp;&nbsp;And really, I should create a second calculation for each cell that totals the number of mag codes - so I have the count of households and the count of magcodes.<br><br>I'm way above myself - can this be done in Access? Would Crystal Reports be better at this?&nbsp;&nbsp;or should I be paying good money and hiring one of you programmer-types? :)<br>Thanks for your thoughts<br>Milin
 
Good, your table structure looks like mine.<br><br>In the first query, I added Table2 to the query grid twice. The first time it calls itself Tabel2, the second time it defaults to the name Table2_1. This is how Access handles the naming automatically. BTW, I guess the name I used for the Household was actually PKHousehold. If you just sub your table and fieldnames for mine you should be able to paste the SQL into the query window and run it. My crosstab didn't have totals, but here's how it looks if you add them:<br><br>TRANSFORM Sum([CountOfOwned])<br>SELECT [Table2_1].[Mag], Sum([CountOfOwned]) AS [Total Of CountOfOwned]<br>FROM Query1<br>GROUP BY [Table2_1].[Mag]<br>PIVOT [Table2].[Mag];
 
elizabeth..<br>This is cool!!! It almost works - what I notice is that in the joining of the table twice in query1, it is counting my magcode totals twice, so the intersection cell of mag1 and mag1 on the crosstab query is very inflated.&nbsp;&nbsp;I noticed that when I deleted the table2 copy that query1 totalled correctly, but then the crosstab wouldn't work. Otherwise I've followed it exactly as you've written it.<br>Now what :)
 
If you just want the total of each magazine, leave out the line that creates totals. Look at the intersection of rowA with columnA for the total of A magazines. The totals in <i>this</i> report represent a <i>row</i> total.
 
Sorry elizabeth - I didn't explain it well...<br>the formulas I'm using are your Query1 and Query1_crosstab.&nbsp;&nbsp;I'm assuming, like you said, that the intersection of rowA with columnA is the total for A magazine. At first this looked right, but I went back and did testing against my master table (table2) - I queried for all magazine A records and came out with a much lower number.&nbsp;&nbsp;What I noticed was that query1 was totalling this higher number, but if I removed the Table2_1 references, the query would total the correct number of Magazine A. -- but then the crosstab won't work.&nbsp;&nbsp;So I guess some how query1 is overcounting the total number of magazines, even though the table is correct.<br><br>Thanks for your help - this is very cool!<br>
 
Sorry, I don't understand why this is happening with you. Mine are counting accurately. I have 5 household records with MagA and my intersection of rowA with ColA is 5.
 
ok elizabeth - I think I see why. in my households, I sometimes have duplicate responses to magazine A (partly a dirty data problem and partly someone that responded to the same mag, say 6 mos apart). So I can use this report based on the numbers being the total individual response by mag. My final questions (really! ;-) ) would there be a way to have the queries look for and count only the unique household numbers where magazine A resides -- whether it be 1 or 10 responses to the same mag? That would give me a true household report.<br><br>Thanks so much
 
Yup, set your first query's Unique Values property to yes. In the QBE grid, right click in the relationship window and select Properties (or click on the Properties Icon), select Unique Values/Yes. If you then view your SQL window you'll see the word DISTINCT added.<br><br>BTW, you can use the Find Duplicates query wizard to clean up your db, and once it's done you can set a unique index in your table on the combiend fields Household ID & MagID to disallow further dupes. If you're not familiar with building concatenated indexes, just yell. Nobody is really counting the number of questions you ask in a row :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top