4
I’m studying about appointments at Sqlserver and have the following scenario:
I have a lot table that has the following fields:
Table of Lot (Lot)
Id INT
ProdutoID INT
Preco SMALLMONEY
Inicio DATETIME
Fim DATETIME
Table of Products (Products)
Id INT
Name VARCHAR
Tipo INT
Table of Types (Types)
Id INT
Name VARCHAR
NOTE: And a well summarized and example table
I need to perform a query that gives me the current price of the product. Ex.:
Types
Id | Name
01 | Teste
02 | Testando
Products
Id | Name | Tipo
01 | Celular | 01
02 | Câmera | 02
Batches
Id | ProdutoId | Preco | Inicio | Fim
1 | 1 | 50 | 2017-08-14 13:10:57.980 | 2017-08-16 13:10:57.980
2 | 1 | 100 | 2017-08-12 13:10:57.980 | NULL
As you can see, Lots has 2 Records the record with NULL is the official price of the product and the first registration is a promotion that starts on the 14th and ends on the 16th. Taking into account that the date of publication of this question is 15/08 the current price would be 50.
How do I select Product Name, Product Type, and Current Price? (Taking into account the date of promotion)
NOTE: Case should work also with these records:
Id | ProdutoId | Preco | Inicio | Fim
1 | 1 | 50 | 2017-08-14 13:10:57.980 | 2017-08-16 13:10:57.980
2 | 1 | 100 | 2017-08-12 13:10:57.980 | NULL
3 | 1 | 150 | 2017-08-17 13:10:57.980 | NULL
In this case, the value of that product will increase on the 17th.
Just so it doesn’t go blank, I’m doing the second consultation:
select p.Nome as 'Produto', t.Nome as 'Tipo', l.Preco, MAX(p.Inicio)
from Lote as l inner join Produtos as p on l.ProdutoId = p.Id
inner join Tipos as t on p.TipoId = t.Id
group by p.Nome, t.Nome, l.preco
But it doesn’t work in the right way obviously, I thank you already!
I received one less, but no comment on what is wrong with my question :/ How I could improve it to help in the community?
– Márcio Eric
Start and End columns are declared as
datetime
. They contain only date or date and time?– José Diz
@Josédiz as contains date and time.
– Márcio Eric
Could you change the examples by adding the timetable? It’s to reassess the proposal.
– José Diz