1
I’m having a problem with a query, it’s a college job, a Dataware House creation. Well, I need to enter in my main table (f_contract), some information, the same are in other tables and I need to search from these tables the information for insertion in the table f_contract. The query for the current insertion is this:
(SELECT
I.CD_IMO,
F.CD_FUN,
I.NR_CEP,
(SELECT
NR_MES,
NR_ANO,
(SELECT
SUM(VL_PAR)
FROM
PARCELA
WHERE
YEAR(DT_VCTO) = NR_ANO
AND MONTH(DT_VCTO) = NR_MES
AND DT_PAGTO IS NULL
GROUP BY NR_ANO , NR_MES) TOT_REC,
(SELECT
SUM(VL_PAR)
FROM
PARCELA
WHERE
YEAR(DT_PAGTO) = NR_ANO
AND MONTH(DT_PAGTO) = NR_MES
AND DT_PAGTO IS NOT NULL
GROUP BY NR_ANO , NR_MES) TOT_PAGO
FROM
D_TEMPO)
FROM
FUNCIONARIO F,
CONTRATO C,
IMOVEL I,
LOGRADOURO L
WHERE
I.CD_IMO = C.CD_IMO
AND I.NR_CEP = L.NR_CEP
AND F.CD_FUN = C.CD_FUN
AND PARCELA.CD_CON = C.CD_CON);
I had done some tests with some select querys: For example, in that select I return the value of the installments that are paid and the total of the installments that are open using subselect and sum:
SELECT
NR_MES,
NR_ANO,
(SELECT
SUM(VL_PAR) TOT_PAGO
FROM
PARCELA
WHERE
YEAR(DT_VCTO) = NR_ANO
AND MONTH(DT_VCTO) = NR_MES
AND DT_PAGTO IS NULL
GROUP BY NR_ANO , NR_MES) TOT_REC,
(SELECT
SUM(VL_PAR) TOT_PAGO
FROM
PARCELA
WHERE
YEAR(DT_PAGTO) = NR_ANO
AND MONTH(DT_PAGTO) = NR_MES
AND DT_PAGTO IS NOT NULL
GROUP BY NR_ANO , NR_MES) TOT_PAGO
FROM
D_TEMPO
The Return of this query with the sum is this one:
Basically, I need to join 2 query to insert all the data in the f_contract table, the query of the sum of the plots grouped by year and month and the latter.
(SELECT
I.CD_IMO, F.CD_FUN, I.NR_CEP
FROM
FUNCIONARIO F,
CONTRATO C,
IMOVEL I,
LOGRADOURO L
WHERE
I.CD_IMO = C.CD_IMO
AND I.NR_CEP = L.NR_CEP
AND F.CD_FUN = C.CD_FUN);