There's always a way. The question is "Should we do this". Note by doing this you hide the brand data. If the data is reordered, sorted, the sorter loses the ability to know which brand is for what row. If this is just a printed report that is not to be resorted or expected to a PDF where a user can not manipulate the data then ok.. But if this is so a user can dump to excel and manipulate later... better to have all the data.
I personally find this display of information distasteful on electronic data, but ok on printed forms or static reports. The reason: on electronic I am able to import into excel sort and play with the data; but if columns are "missing" redundant data then electronic manipulation becomes problematic.
Working SQLfiddle
CREATE Table foo (
brand varchar(20),
description varchar(20),
prodcode varchar(20));
Insert into foo values ('Brand1', 'Description1', 'Product1'),
('Brand1', 'Description2', 'Product2'),
('Brand1', 'Description3', 'Product4'),
('Brand2', 'Description3', 'Product4'),
('Brand2', 'Description3', 'Product4'),
('Brand1', 'Description3', 'Product3');
QUERY:
SELECT case when@var <> f.brand then @Var :=brand end as Brand
, f.description
, f.prodcode
FROM (SELECT * FROM foo ORDER BY brand, description, prodcode) f
CROSS JOIN (SELECT @var := '') b
RESULTS:
Brand description prodcode
Brand1 Description1 Product1
(null) Description2 Product2
(null) Description3 Product3
(null) Description3 Product4
Brand2 Description3 Product4
(null) Description3 Product4
Why this works:
The magic happens in the cross join and on the case evaluation.
There's 1 table. We're sorting the records before we join. We're creating a user variable called @var and setting it to '' on the first record in in table foo. Then we assign @var to the brand of the first record. When the SQL cross joins to the 2nd filed @var is now the brand of the first record. If the brands match, no record is displayed, if the brands don't match, we change the value of @var and display the new value.
Risks:
- we if other table joins occur, or the order is not as desired in the table, we would need to do a subquery first on "foo" to order by brand
- applying any order by to this which involves brand1 is now worthless
- It's not as re-usable. Another module which may want to leverage the data (view) is no longer able to do so because brand has been hidden on some of the records.