Oracle Statement repeating itself 9 times!! I need suggestions - sql

I got a script for oracle database that must have something wrong, the code runs okay, but the results are that, for each line of the result, are another 8 lines with the same result. Instead of showing just one line, It's showing 9
What could be wrong in the script below?
SELECT P.IDPESSOA AS CodigoCompanhia,
E.NOMEEMPRESA AS NomeCompanhia,
L.LACDEBCRE AS TipoOperacao,
L.TRGDTINCLUSAO AS DataLancamento,
P.PLNDATDIA AS DataContabilizacao,
L.PLACONTA AS ContaContabil,
C.PLANOME AS DescricaoContaContabil,
C.PLANATUREZA AS NaturezaContaContabil,
L.LACVALOR AS ValorContabil,
M.MOESIGLA AS Moeda,
L.LACHIST1||' '||L.LACHIST2||' '||L.LACHIST3||' '||L.LACHIST4||' '||L.LACHIST5 AS HistoricoLancamento,
C.PLAGRUPO AS ClasseConta,
L.IDUSUARIOINCLUSAO AS PreparerID,
NVL(PE.NOME, U.NOMEUSUARIO) AS NomeCompletoFuncionario,
CG.DESCRICAO AS CargoFuncionario,
TO_CHAR(PC.PERNOME)||'/'||TO_CHAR(P.PEREXERCICIO) AS PeriodoContabil,
TO_CHAR(P.PLNPLANIL)||'-'||TO_CHAR(L.LACNUMLAN)||'-'||LACDEBCRE AS NumeroDocumento,
C.PLASUBGR3 AS ContasCompensacaoTransitorias,
P.PLNPLANIL, L.LACNUMLAN, P.PLNCODIGO
FROM PLANILHA P, LANCAMENTO L, EMPRESAPROP E, PLANOCONTA C, PERIODO PC,
PARAMGLOBAL PG, MOEDA M, USUARIOSISTEMA U, PESSOA PE, FUNCIONARIO F, CARGO CG
WHERE P.PLNCODIGO = L.PLNCODIGO
AND P.PLNDATDIA >= TO_DATE('01/01/2013','DD/MM/YYYY')
AND P.PLNDATDIA <= TO_DATE('30/04/2013','DD/MM/YYYY')
AND P.PEREXERCICIO = PC.PEREXERCICIO
AND P.PERNUMERO = PC.PERNUMERO
AND P.IDPESSOA = E.IDPESSOA
AND P.IDPESSOA = PG.IDPESSOA
AND PG.MOEDACORRENTE = M.MOECODIGO
AND L.PLANO = C.PLANO
AND L.PLACONTA = C.PLACONTA
AND L.IDUSUARIOINCLUSAO = U.IDUSUARIO
AND U.IDUSUARIO = PE.IDPESSOA(+)
AND PE.IDPESSOA = F.IDPESSOA(+)
AND F.IDCARGO = CG.IDCARGO(+)
AND P.IDPESSOA = 1
ORDER BY P.IDPESSOA, P.PLNDATDIA, P.PLNPLANIL, L.LACNUMLAN;
thanks

Add the primary key of every table that's referenced in the select to your list of output columns. This seems very much like a 1:n relation on one of the tables; you can identify which it is by checking which PKs are different in each set of "equal" 9 rows.

You can also use 'group by' before 'order by' clause

Related

Raw query must include the primary key from django

grade = request.GET.get('grade')
year = request.GET.get('year')
subject = request.GET.get('subject')
marks_sheet1=Marks_sheet.objects.raw("select id,unique_id,student_name,attendance,%s FROM school_marks_sheet a, school_marks_details b WHERE term = 'Term1' and a.grade = %s and a.year=%s and a.unique_id = b.details_id_id order by student_name",[subject,grade,year])
I got this issue when I try this code. can anyone solve this.
I want to take the output according to the query

Why is my WHERE clause not working as intended?

I am running a query to apply a number depending on the WHERE clause which verifies information from different tables
UPDATE EstadoDoc
SET PuntajePrevio = 10
FROM Investigador i, Autores a, Documentos d, EstadoDoc e, Periodo p
WHERE i.IdInv = a.IdInv
AND a.IdDoc = d.IdDoc
AND d.Tipo = 'AR'
AND e.Estado IN ('A', 'RyR')
AND p.FechaDesde <= e.FechaEst
AND p.FechaHasta >= e.FechaEst
AND p.IdPeriodo = 2019
This is the query I'm running and it's supposed to put the value 10 in PuntajePrevio depending on the WHERE clause, but I'm noticing it's not working correctly because it is applying the value to 2 column were it shouldn't.
To further explain here is a screenshot of the data it affected - it should be only taking into consideration all the ones that have d.tipo = 'AR' but is it not.

SQL GROUP BY function returning incorrect SUM amount

I've been working on this problem, researching what I could be doing wrong but I can't seem to find an answer or fault in the code that I've written. I'm currently extracting data from a MS SQL Server database, with a WHERE clause successfully filtering the results to what I want. I get roughly 4 rows per employee, and want to add together a value column. The moment I add the GROUP BY clause against the employee ID, and put a SUM against the value, I'm getting a number that is completely wrong. I suspect the SQL code is ignoring my WHERE clause.
Below is a small selection of data:
hr_empl_code hr_doll_paid
1 20.5
1 51.25
1 102.49
1 560
I expect that a GROUP BY and SUM clause would give me the value of 734.24. The value I'm given is 211461.12. Through troubleshooting, I added a COUNT(*) column to my query to work out how many lines it's running against, and it's giving a result of 1152, furthering reinforces my belief that it's ignoring my WHERE clause.
My SQL code is as below. Most of it has been generated by the front-end application that I'm running it from, so there is some additional code in there that I believe does assist the query.
SELECT DISTINCT
T000.hr_empl_code,
SUM(T175.hr_doll_paid)
FROM
hrtempnm T000,
qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE 1 = 1
AND T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
AND (T175.hr_paym_type = 'd' OR T175.hr_paym_type = 't')
GROUP BY T000.hr_empl_code
ORDER BY hr_empl_code
I'm really lost where it could be going wrong. I have stripped out the additional WHERE AND and brought it down to just T166.hr_empl_code = T175.hr_empl_code, but it doesn't make a different.
By no means am I any expert in SQL Server and queries, but I have decent grasp on the technology. Any help would be very appreciated!
Group by is not wrong, how you are using it is wrong.
SELECT
T000.hr_empl_code,
T.totpaid
FROM
hrtempnm T000
inner join (SELECT
hr_empl_code,
SUM(hr_doll_paid) as totPaid
FROM
hrtpaytp T175
where hr_paym_type = 'd' OR hr_paym_type = 't'
GROUP BY hr_empl_code
) T on t.hr_empl_code = T000.hr_empl_code
where exists
(select * from qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
)
ORDER BY hr_empl_code
Note: It would be more clear if you have used joins instead of old style joining with where.

sql is duplicating my results

I believe the problem is within my joins but i am unable to correct it. The SQL should return 3 rows however it is duplicating and returning 12 rows instead. Any help would be much appreciated!
SELECT J.JOURNEY_NUMBER,
L.DESCRIPTION,
L.USE_CODE,
J.REAL_START_DATE,
J.REAL_END_DATE,
S.STOP_ID,
SN.WRIN_ID,
J.JOURNEY_ID
FROM PDA_STG.JOURNEY J,
PDA_STG.RESTAURANT R,
PDA_STG.LOCATION L,
PDA_STG.SERIAL_NUMBER SN,
PDA_STG.STOP S
WHERE J.JOURNEY_ID = R.JOURNEY_ID
AND l.loc_id = r.rest_loc_id
AND J.JOURNEY_ID = S.JOURNEY_ID
AND S.STOP_ID = SN.STOP_ID
AND SN.WRIN_ID = '00768669'
AND j.dc_loc_id = '994'
AND J.JOURNEY_ID = '357020'
AND J.PLANNED_START_DATE < '20-APR-17'
ORDER BY J.JOURNEY_ID DESC
You are probably joining records that you don't want to join for which you'd have to add some join criteria. (For instance if the serial number could change for a stop, i.e. you keep old serial numbers with a date, you'd only want the latest serial number, not all.)
In order to find the flaw in your query you can select * and see what records you are actually selecting.
Thanks for the feedback, i just done as India.Rocket said and it worked perfectly.
without sample it's difficult to tell what's wrong with the query. But
if rows are exact duplicate then just put a distinct after select.
That should do the job – India.Rocket 46 mins ago

Selecting more than one column to query in sql

I am having a bit of trouble, probably from my understanding of SQL. Here is the SQL I am currently using:
CREATE TEMPORARY TABLE Temp
(
sPropertyCode VARCHAR(9),
sDataDate DATE,
PRIMARY KEY (sPropertyCode)
);
INSERT IGNORE Temp (sPropertyCode, sDataDate)
SELECT sPropertyCode, sDataDate
FROM tasks as t, task_data AS d
WHERE t.iTaskId = d.iTaskId
AND iRemoved = 1
AND sDataType = 'sAgencyAgreementDate'
AND iBusinessStreamId = 9;
SELECT t.sPropertyCode, sDataDate, SFirstSeen, sTaskType
FROM tasks AS t, temp AS a
WHERE iRemoved = 1
AND iBusinessStreamId = 9
AND sTaskType IN ('RF', 'IF', 'CM')
AND t.sPropertyCode = a.sPropertyCode
ORDER BY sPropertyCode, sFirstSeen;
DROP TABLE Temp;
So the references 'RF', 'IF' and 'CM' are tasks that we receive. Each propertycode can touch each of these tasks once, and only once. I would like to show the date that each one of these was touched by the propertycode. It is working at the moment but it is showing it in three columns with the tasks types in one column. I would like each task to show in a seperate column with the date it was first seen in its own corresponding column.
So from the picture below is how it is currently laid out with the code above.
And here is how I would like it to look, instead of the tasks showing down the side, I would like them to show accross in columns with their own specific dates
Thank you in advance :)
SELECT t.sPropertyCode, sDataDate, SFirstSeen, a1.sTaskType, a2.sTaskType, a3.sTaskType
FROM tasks AS t
INNER JOIN temp AS a1 on t.sPropertyCode = a1.sPropertyCode and a1.sTaskType = 'RF'
INNER JOIN temp as a2 on t.sPropertyCode = a2.sPropertyCode and a2.sTaskType = 'IF'
INNER JOIN temp as a3 on t.sPropertyCode = a3.sPropertyCode and a3.sTaskType = 'CM'
WHERE iRemoved = 1 AND iBusinessStreamId = 9
ORDER BY sPropertyCode, sFirstSeen;
You can also user outer join if not always have all 3 tast type.
What is the difference in meaning between the CM,IF and RF columns for any row in your 5-col table ? They're always the very same value in the example you listed.