-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQuery1.sql
More file actions
70 lines (52 loc) · 1.42 KB
/
SQLQuery1.sql
File metadata and controls
70 lines (52 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
create table Integrantes(
NOME varchar(255) primary key
)
select * from Integrantes
drop table Integrantes
insert into Integrantes Values
('Melque'),('Rilck'),('Danilo'),('Murilo')
create table Entrada(
DATA DATE,
NOME varchar(255),
VALOR float
)
ALTER TABLE [dbo].[Entrada] WITH CHECK ADD FOREIGN KEY([Nome])
REFERENCES [dbo].[Integrantes] ([Nome])
insert into Entrada Values
('2019-01-10','Melque',200),('2019-01-11','Rilck',100),('2019-01-11','Danilo',100),('2019-01-11','Murilo',100)
truncate table Entrada
create table Saida(
DATA DATE,
NOME varchar(255),
VALOR float
)
ALTER TABLE [dbo].[Saida] WITH CHECK ADD FOREIGN KEY([Nome])
REFERENCES [dbo].[Integrantes] ([Nome])
create table Log_Diario(
DATA DATE,
SALDO_INICIAL float,
SALDO_FINAL float,
PIP_INICIAL float,
PIP_FINAL float,
LUCRO float
)
insert into Log_Diario Values
('2019-11-01',296.54,1000,0.29654,1,703.46)
select * from Log_Diario
with SaldoDia as(
select Nome,SUM(Valor) as Total from Entrada
Where DATA <= format(getdate(),'yyyy-MM-dd')
group by NOME,VALOR
),
Debito as (
select Nome,SUM(Valor) as Total from Saida
Where DATA <= format(getdate(),'yyyy-MM-dd')
group by NOME,VALOR
)
select
SaldoDia.*,
ISNULL(Debito.Total,0) as Debito,
(SaldoDia.Total - ISNULL(Debito.Total,0))/(Select Sum(Valor) from Entrada) - (Select ISNULL(Sum(Valor),0) from Saida) as Taxa_Lucro
from SaldoDia
Left Join Debito
on SaldoDia.Nome = Debito.Nome