I have 2 tables DraftProducts and Products.
The only difference between the 2 tables is the "phase" field.
I have productCode varchar(20), phase(tinyint), Name, Price, SpecialCells.... etc fields in my DraftProducts table.
the last phase is actually 1 record in the Product table for me.
Until now, when 1 user said that the draft work was finished, I was automatically recording the last phase in the Product table.
But when the last draft changes, it needs to be updated again. It needs to be deleted when deleted etc. a lot of additional work is going on.
In my database, some tables work from the DraftProducts table and some tables from the Products table.
But can I do this as "Products view" independent of the user?
How can I create a "ProductsView" with max(phase) 1 record? How should I do group by or distinct?
As a result of this table, the Products table or view I want should be like this.
p1=8 price, category, description etc. p2=15 price, category, description etc. p3=22 price, category, description etc.
CREATE TABLE [dbo].[DraftProducts](
[productCode] [varchar](20) NULL,
[phase] [varchar](50) NULL,
[name] [varchar](50) NULL,
[category] [varchar](20) NULL,
[description] [varchar](20) NULL,
[price] float NULL
)
CREATE UNIQUE CLUSTERED INDEX [DraftProductsIndex1] ON [dbo].[DraftProducts]
(
[productCode] ASC,
[phase] ASC
)
insert into DraftProducts (productCode,phase,name)
values
('p1',1,'aaaaaaaaaaaaaaa'),
('p2',2,'aaaaaaaaaaaaaaa'),
('p3',1,'aaaaaaaaaaaaaaa'),
('p2',15,'bbbbbbbbbbbbbbb'),
('p3',22,'bbbbbbbbbbbbbbb'),
('p1',8,'bbbbbbbbbbbbbbbbbbbbbbbbb'),
('p2',7,'ccccccccccccccc')
7 rows affected
select * from DraftProducts
/*
finish phase
p1=8
p2=15
p3=22
*/
productCode | phase | name | category | description | price |
---|---|---|---|---|---|
p1 | 1 | aaaaaaaaaaaaaaa | null | null | null |
p1 | 8 | bbbbbbbbbbbbbbbbbbbbbbbbb | null | null | null |
p2 | 15 | bbbbbbbbbbbbbbb | null | null | null |
p2 | 2 | aaaaaaaaaaaaaaa | null | null | null |
p2 | 7 | ccccccccccccccc | null | null | null |
p3 | 1 | aaaaaaaaaaaaaaa | null | null | null |
p3 | 22 | bbbbbbbbbbbbbbb | null | null | null |