Solved How to create fixed number of records (1 Viewer)

thenoisydrum

Registered User.
Local time
Today, 13:53
Joined
Jul 26, 2012
Messages
51
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:53
Joined
Oct 29, 2018
Messages
21,473
You could try using a cross join/cartesian query.
 

Josef P.

Well-known member
Local time
Today, 14:53
Joined
Feb 2, 2023
Messages
826
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:53
Joined
Feb 19, 2002
Messages
43,275
In your order details form, you have a combo that selects the product the client DOES buy. If he buys 2 products, you end up with 2 detail rows. If he buys all 4, you end up with four rows. Do you think Amazon adds a million rows to each order just in case someone orders one of every item they sell? Order details are added one at a time as items are selected. Don't get hung up on FOUR. Tomorrow, your company may sell FIVE items and you would have serious redesign on your hands.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:53
Joined
Feb 28, 2001
Messages
27,186
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:53
Joined
Feb 19, 2002
Messages
43,275
If you want to show four rows with quantity of 0 and then let the user change the 0 to 1, then you do that with a left join between product and OrderDetails. But, I do not recommend this design. The user will find it confusing.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:53
Joined
Feb 19, 2002
Messages
43,275
Gentlemen, the sample didn't show a crosstab. It showed a list with "missing" items which is a left join.
 

Josef P.

Well-known member
Local time
Today, 14:53
Joined
Feb 2, 2023
Messages
826
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.
 

ebs17

Well-known member
Local time
Today, 14:53
Joined
Feb 7, 2020
Messages
1,946
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

Top Bottom