Raw query must include the primary key from django - sql

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

Related

SQL query shows every result 3 times

I have this code which should give me results based on the category_id = 2 but somehow i get the right results but tripled. How can I prevent this. Here's the query:
SELECT * FROM partner_categories
JOIN partner
ON partner_categories.category_id = partner.category_id
WHERE partner.category_id = "2" AND partner.status = 1 ORDER BY
partner_name ASC
And here's what I get:
And this is the browser version:
So every partner_name is tripled. Why?
Please write a comment if I have to post any other information and thanks in advance.
Got it by myself. Just had to define also the partner_id because it's in both tables.
SELECT * FROM partner_categories
JOIN partner
ON partner_categories.category_id = partner.category_id
AND partner_categories.partner_id = partner.partner_id
WHERE partner.category_id = "2"
AND partner.status = 1
ORDER BY partner_name ASC

PostgreSQL update not working as expected

With 20+ years of experience with MS Access and SQL Server, I'm not a novice with respect to SQL, but I am new to PostgreSQL and I have encountered an issue that makes me feel like a complete noob. I have a simple UPDATE query in which I want to update the destination table d with data from the source View m:
UPDATE chgman.tc_data
SET reporttime = m.reporttime, endtime = m.endtime,
itismessage = m.itismessage, shortdesc = m.shortdesc,
longdesc = m.longdesc, severity = m.severity,
tc_source = m.tc_source, tc_state = m.tc_state,
ushr_state = m.ushr_state, mainroad = m.mainroad,
start_location = m.start_location, end_location = m.end_location
FROM
chgman.tc_matched_raw AS m
INNER JOIN
chgman.tc_data AS d ON d.tc_id = m.tc_id;
The result of the query is that EVERY row in table d is populated with data from the FIRST row of View m.
I am prepared for the embarrassment - please enlighten me as to what I have done wrong...
The from/update in Postgres works a bit differently from SQL Server/MS Access.
This should do what you want:
UPDATE chgman.tc_data d
SET reporttime = m.reporttime, . . .
FROM chgman.tc_matched_raw m
WHERE d.tc_id = m.tc_id;
You don't repeat the table in the FROM clause -- that is a new reference to the table.

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.

Oracle Statement repeating itself 9 times!! I need suggestions

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

how to optimize several "WHERE (Select .... ) = value" from same table

It's hard to compose a topic name for me. But I can show an example :
WHERE (SELECT [ID_Line] FROM [Event] WHERE [Event].[Name] = [A].[Col]) = 2
AND (SELECT [DataType] FROM [Event] WHERE [Event].[Name] = [A].[Col]) = 2
Here I'm processing 2 queries when I really need something like that :
WHERE (SELECT [ID_Line],[DataType] FROM [Event] WHERE [Event].[Name] = [A].[Col]) = 2,2
but SQL doesn't work with tuples, so must I make Inner Join here ?
you can try something like this :
WHERE EXISTS (
SELECT [ID_Line] FROM [Event] WHERE
[Event].[Name] = [A].[Col] AND
[Event].[ID_Line] = 2 AND
[Event].[DataType] = 2
)
If you provide more information about the complete query and your database structure, a more precise answer could be given. It is possible that this isn't the best solution.
You can try to melt the fields using a melting operator. In ORACLE PL/SQL you use || (double pipe), for example.