Solved How to create fixed number of records

thenoisydrum

Registered User.
Local time
Today, 15:24
Joined
Jul 26, 2012
Messages
52
Sorry for the rubbish thread title but I couldn't what else to call it.
I am appending to an empty table customer name, brand and then sales. So, 3 columns.

I have 4 brands in total. Some customers do not buy all of the 4 brands but I need to display the 4 brands, later in a SELECT query.
For example, Dave buys all 4 brands but Chris only buys the 1 (beta).
The data looks like this;
1707315617981.png


But I need it to look like this;
1707315692525.png


I know that I have done it before but I cannot think of how to achieve it?
Any help would be appreciated

Thanks

Drum
 
You could try using a cross join/cartesian query.
 
Code:
select
  x.Customer, X.Brand, P.Date, P.Quantity, ...
from
(
  select 
     C.CustomerID, C.customer,
     B.BrandId, B.Brand
  from
     Brands as B
     cross join
     Customer as C
) as X
left join
  Purchases as P ON P.CustomerId = X.CustomerId and P.BrandID = X.BrandId
=> Source for pivot table.
 
My colleagues have provided you with ideas on how to do this. But here is the catch: For properly normalized data, you NEVER actually store empty records long-term. Instead, you use queries to build the temporary image that you want. And with that in mind, Josef P's suggestion for a pivot table is entirely appropriate.
 
Gentlemen, the sample didn't show a crosstab. It showed a list with "missing" items which is a left join.
I hope that the data source does not have a field with a 'month-year' name. Therefore I thought of a pivot table.
 
Simple queries are based on a good database schema. I see a many to many relationship here.

Customer => Sales <= Brands
Sales contains IDCustomer, IDBrand, SalesDate, Amount

didn't show a crosstab
Hopefully not a table (after a January 24th there is a 25th and a 26th and so on), but the result of a crosstab query = pivot table.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom