How to use distinct in mssql with many tables? - sql

With the below SQL statement I get all information about a student as needed except for one problem which is duplicate records when in fact last entered record for each student needed only.
Student ID Year level Semester
20012-000001 20012-20013 1
20012-000001 20012-20013 2
20012-000001 20012-20013 3
20012-000001 20012-20013 4
20012-000001 20012-20013 5
sqlSTR = "SELECT tblStudent.StudentID AS '??? ??????' , (tblStudent.FirstName + ' ' + tblStudent.MiddleName + ' ' + tblStudent.GrandFatherName + ' ' + tblStudent.LastName) as '?????' ," & _
"tblStudent.Gender as '?????', tblStudent.Citizenship as '???????', tblStudent.Type as '??? ?????'," & _
"tblDepartment.DepartmentTitle as '?????', tblEnrolment.SchoolYear as '????? ????????',tblYearLevel.YearLevelTitle as '????? ???????', " & _
"'????'= CASE WHEN Len(tblGraduate.StudentID)<>12 THEN '???' ELSE ''End , " & _
"'???? ???'= CASE WHEN Len(tblDropped.StudentID)<>12 THEN '???' ELSE ''End , " & _
"'??????'= CASE WHEN tblStudent.Transferee ='True' THEN '???' ELSE ''End" & _
" FROM tblDropped RIGHT JOIN " & _
"(tblGraduate RIGHT JOIN " & _
"(tblDepartment RIGHT JOIN " & _
"(tblYearLevel RIGHT JOIN " & _
"(tblSection RIGHT JOIN " & _
"(tblStudent LEFT JOIN tblEnrolment " & _
"ON tblStudent.StudentID = tblEnrolment.StudentID) " & _
"ON tblSection.SectionID = tblEnrolment.SectionOfferingID) " & _
"ON tblYearLevel.YearLevelID = tblEnrolment.YearLevelID) " & _
"ON tblDepartment.DepartmentID = tblSection.DepartmentID) " & _
"ON tblGraduate.StudentID = tblStudent.StudentID) " & _
"ON tblDropped.StudentID = tblStudent.StudentID" & _
" " & sFilter & _
" ORDER BY tblStudent.StudentID DESC , tblYearLevel.YearLevelTitle ASC"

Add a Date Field to your table containing the records and filter by most recent. IF the table already has one include a WHERE clause by date?
If I understand your table correctly the last record is always in the last semester, if you know the semester just do a UNIQUE keyword for the Student_ID

Related

Encountered error Syntax error in JOIN operation

OBJECTIVE: trying to extract data using union all with join.
CODE:
MYSQL = "SELECT trans_id, item_id, SUM(OrigQty) AS NewOrigQty, SUM(TempQty) AS NewTempQty, " & _
"itm_name, itm_category, itm_details, itm_um, itm_cost " & _
"FROM (" & _
"SELECT T1.trans_id, T1.item_id, T1.itm_qty AS OrigQty, 0 AS TempQty, " & _
"T3.itm_name, T3.itm_category, T3.itm_details, T3.itm_um, T3.itm_cost FROM INVENTORY AS T1 " & _
"LEFT JOIN ITEM AS T3 ON T1.item_id = T3.itm_id " & _
"WHERE T1.trans_id = '" & NewSearchList.lvSearchWindow.SelectedItem.Text & "' " & _
"AND T1.itm_movement = 'TRANSFER IN' AND T1.inv_temporig_status = 'ORIGINAL' " & _
"UNION ALL " & _
"SELECT T2.trans_id, T2.item_id, 0 AS OrigQty, T2.itm_qty AS TempQty, " & _
"t4.itm_name, t4.itm_category, t4.itm_details, t4.itm_um, t4.itm_cost FROM INVENTORY AS T2 " & _
"LEFT JOIN ITEM AS t4 ON T1.item_id = T4.itm_id " & _
"WHERE T2.trans_id = '" & NewSearchList.lvSearchWindow.SelectedItem.Text & "' " & _
"AND T2.itm_movement = 'TRANSFER IN' AND T2.inv_temporig_status = 'TEMPORARY' " & _
") x " & _
"GROUP BY trans_id, item_id, itm_name, itm_category, itm_details, itm_um, itm_cost"
Set rsInventory = dbInventory.Execute(MYSQL)
ERROR: Syntax error in JOIN operation

SQL JOIN Master group to Result group

I am using Excel VBA and connecting to PGSQL using OleDB. I have 2 lines of SQL code that work fine on there own, but I now need to merge them.
The 1st query is the master group that finds the people I need for the 2nd query
Const sqlconnection = "Provider=oledb;"
Dim conn As New Connection
conn.ConnectionString = sqlconnection
conn.Open
Dim rs As Recordset
Sheets("Sheet1").Select
Range("A1").Select
Dim GRP As String
GRP = "SELECT h.master_id, p.surname, p.forename1, h.eventdate, h.code " _
& "FROM hist h INNER JOIN person p ON h.master_id=p.entity_id " _
& "AND code LIKE 'C10%' " _
& "ORDER BY h.master_id "
The 2nd query needs to show the results based on the 1st group of people it found.
Dim DATA As String
DATA = "SELECT latest.master_id, p.surname, p.forename1, " _
& "SUBSTRING(latestAP,1,10) eventdate, " _
& "SUBSTRING(latestAP,12,3) TX1, " _
& "SUBSTRING(latestAP,16,3) TX2 " _
& "FROM ( " _
& "SELECT master_id, " _
& "MAX(CAST(eventdate AS VARCHAR(10)) + '.' + RIGHT('00' + TEXT1,3)+ '.' + RIGHT('00' + TEXT2,3)) as latestAP " _
& "FROM ap " _
& "GROUP BY master_id) AS latest " _
& "LEFT JOIN person p ON latest.master_id = p.entity_id " _
& "ORDER BY master_id "
Set rs = conn.Execute(DATA)
With ActiveSheet.QueryTables.Add(Connection:=rs, Destination:=Range("A1"))
.Refresh
End With
rs.Close
Consider adding first query as an additional derived table to second query:
Dim DATA As String
DATA = "SELECT latest.master_id, p.surname, p.forename1, " _
& " SUBSTRING(latestAP,1,10) eventdate, " _
& " SUBSTRING(latestAP,12,3) TX1, " _
& " SUBSTRING(latestAP,16,3) TX2 " _
& "FROM ( " _
& " SELECT master_id, " _
& " MAX(CAST(eventdate AS VARCHAR(10)) + '.' + " _
& " RIGHT('00' + TEXT1,3) + '.' + " _
& " RIGHT('00' + TEXT2,3)) as latestAP " _
& " FROM ap " _
& " GROUP BY master_id) AS latest " _
& "INNER JOIN ( " _
& " SELECT h.master_id, p.surname, p.forename1, "_
& " h.eventdate, h.code " _
& " FROM hist h " _
& " INNER JOIN person p ON h.master_id=p.entity_id " _
& " AND code LIKE 'C10%') AS grp "
& "ON grp.master_id = latest.master_id"
& "LEFT JOIN person p ON latest.master_id = p.entity_id " _
& "ORDER BY master_id "

How to get last 5 games of a team out of my access database

Hi all below you see a screenshot of my database:
But now I want to be able to make a table that calculates every players last 5 games. As I'm totaly new to access db I really have no clue how to do this.
Can you guys help me a hand with this one?
When I use the 2nd snippet in the answer below I get these:
Below are SQL routes according to your data. To use in MS Access simply create a new query under Create Tab on Ribbon and place the below SQL in the SQL view of a new created query. You may need to adjust query according to your actual table names and/or fields.
SAME GAMES FOR ALL PLAYERS
Assuming every player shares the same last five games, you could run an aggregate query across all players, using a subquery in INNER JOIN clause to calculate last five game dates. Do note: subquery, LastFiveDates can be saved as its own query and used directly in INNER JOIN.
SELECT [LookUp to Players],
Sum(GamesWon) As SumOfGamesWon, Sum(GamesLost) As SumOfGamesLost,
Sum(OwnOdds) As SumOfOwnOdds, Sum(OppOdds) As SumOfOppOdds,
Sum(GamesPlayed) As SumOfGamesPlayed
FROM GamesTable
INNER JOIN
(
SELECT DISTINCT TOP 5 [Date]
FROM GamesTable
ORDER BY [Date] DESC
) As LastFiveDates
ON GamesTable.[Date] = LastFiveDates.[Date]
GROUP BY [LookUp to Players];
DIFFERING GAMES FOR EACH PLAYER
SIMPLE SELECT APPROACH
Now, if players differ in their last five games, you have to join on different queries or union queries. Again, the below uses a subquery in an inner join but you can save that LastFiveGames as its own stored query and join in INNER JOIN line.
SELECT GamesTable.[LookUp to Players],
Sum(GamesWon) As SumOfGamesWon, Sum(GamesLost) As SumOfGamesLost,
Sum(OwnOdds) As SumOfOwnOdds, Sum(OppOdds) As SumOfOppOdds,
Sum(GamesPlayed) As SumOfGamesPlayed
FROM GamesTable
INNER JOIN
(
SELECT [Lookup to Players], [Date],
(SELECT Count(*)
FROM GamesTable t2
WHERE GamesTable.[Date] <= t2.[Date]
AND GamesTable.[Lookup to Players] = t2.[Lookup to Players]) AS GameOrder
FROM GamesTable
) As LastFiveDates
ON GamesTable.[Date] = LastFiveDates.[Date]
AND GamesTable.[Lookup to Players] = LastFiveDates.[Lookup to Players]
WHERE LastFiveDates.GameOrder <= 5
GROUP BY GamesTable.[LookUp to Players];
DIFFERING GAMES FOR EACH PLAYER
VBA CREATE TABLE APPROACH
Due to performance issues of Access running the query as a stored query, VBA can re-create the GamesStats iteratively looping through all distinct players using the very first query condition for player.
Public Function GameTableStats()
Dim db As Database
Dim tbldef As TableDef, rst As Recordset
Dim strSQL As String, i As Integer
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT DISTINCT PlayerName FROM GamesTable", dbOpenDynaset)
For Each tbldef In db.TableDefs
If tbldef.Name = "GamesStats" Then
db.Execute "DROP TABLE [GamesStats]", dbFailOnError
End If
Next tbldef
rst.MoveLast
rst.MoveFirst
i = 1
Do While Not rst.EOF
If i = 1 Then
' FIRST PLAYER (MAKE-TABLE QUERY) '
strSQL = "SELECT GamesTable.[PlayerName]," _
& " Sum(GamesWon) As SumOfGamesWon, Sum(GamesLost) As SumOfGamesLost," _
& " Sum(OwnOdds) As SumOfOwnOdds, Sum(OppOdds) As SumOfOppOdds," _
& " Sum(GamePlayed) As SumOfGamePlayed" _
& " INTO GamesStats" _
& " FROM GamesTable" _
& " INNER JOIN" _
& " (" _
& " SELECT DISTINCT TOP 5 [Date], [PlayerName]" _
& " FROM GamesTable" _
& " WHERE [PlayerName]=""" & rst!PlayerName & """" _
& " ORDER BY [Date] DESC" _
& " ) As LastFiveDates" _
& " ON GamesTable.[Date] = LastFiveDates.[Date]" _
& " WHERE GamesTable.[PlayerName]= """ & rst!PlayerName & """" _
& " GROUP BY GamesTable.[PlayerName];"
Else
' ALL OTHER PLAYERS (INSERT APPEND QUERIES) '
strSQL = "INSERT INTO GamesStats ([PlayerName], [SumOfGamesWon], [SumOfGamesLost]," _
& " [SumOfOwnOdds], [SumOfOppOdds], [SumOfGamePlayed])" _
& " SELECT GamesTable.[PlayerName], " _
& " Sum(GamesWon) As SumOfGamesWon, Sum(GamesLost) As SumOfGamesLost, " _
& " Sum(OwnOdds) As SumOfOwnOdds, Sum(OppOdds) As SumOfOppOdds, " _
& " Sum(GamePlayed) As SumOfGamePlayed " _
& " FROM GamesTable " _
& " INNER JOIN " _
& " ( " _
& " SELECT DISTINCT TOP 5 [Date], [PlayerName] " _
& " FROM GamesTable " _
& " WHERE [PlayerName]=""" & rst!PlayerName & """" _
& " ORDER BY [Date] DESC" _
& " ) As LastFiveDates " _
& " ON GamesTable.[Date] = LastFiveDates.[Date]" _
& " WHERE GamesTable.[PlayerName]= """ & rst!PlayerName & """" _
& " GROUP BY GamesTable.[PlayerName];"
End If
db.Execute strSQL, dbFailOnError
i = i + 1
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set db = Nothing
MsgBox "Successfully created GamesStats table!", vbInformation
End Function

Calculating Margin from Two Tables SQL

So I am running into an issue that I have isolated to part of the WHERE statement and I can't figure it out. I am calculating margin for an item based on the cost and price tables associated with it. It works fine if I want to limit it to items whose list price is below 17%, but if I want to check the other price tiers, e.g. pricing if a customer buys +100 units, I get an ODBC error. This is my code that doesn't work:
strSQL = "SELECT IMFGR || ICOLOR || IPATT as Item, INAME as Description1, INAME2 as Description2, IPRCCD as PClass, ICSTCD as CClass, " _
& "c.TLASTC as Cost, p.$LIST as List, ((p.$LIST-c.TLASTC)/p.$LIST) as GPM, p.$P2 as Price2, ((p.$P2-c.TLASTC)/p.$P2) as GPM2, " _
& "p.$P3 as Price3, ((p.$P3-c.TLASTC)/p.$P3) as GPM3, p.$P4 as Price4, ((p.$P4-c.TLASTC)/p.$P4) as GPM4, p.$P5 as Price5, ((p.$P5-c.TLASTC)/p.$P5) as GPM5, " _
& "p.$P6 as Price6, ((p.$P6-c.TLASTC)/p.$P6) as GPM6, p.$P7 as Price7, ((p.$P7-c.TLASTC)/p.$P7) as GPM7, p.$P8 as Price8, ((p.$P8-c.TLASTC)/p.$P8) as GPM8, " _
& "p.$P9 as Price9, ((p.$P9-c.TLASTC)/p.$P9) as GPM9, p.$P10 as Price10, ((p.$P10-c.TLASTC)/p.$P10) as GPM10, p.$P11 as Price11, ((p.$P11-c.TLASTC)/p.$P11) as GPM11, " _
& "p.$P12 as Price12, ((p.$P12-c.TLASTC)/p.$P12) as GPM12 " _
& "FROM ITEM i " _
& "LEFT JOIN COST c " _
& "ON i.ICSTCD = c.TCSTCD " _
& "LEFT JOIN PRICE p " _
& "ON i.IPRCCD = p.$PRCCD " _
& "WHERE (IPRCCD != '') AND (p.$LIST# = 'LP') AND (IPOL1 != 'DI' AND IPOL2 != 'DI' AND IPOL3 != 'DI') AND (IPOL1 != 'BR' AND IPOL2 != 'BR' AND IPOL3 != 'BR') AND (IMFGR = '" & man & "') AND " _
& "((ICCTR != 'ZDS') AND (IPRODL != 'XXX')) AND " _
& "((p.$LIST-c.TLASTC)/p.$LIST) <= '" & margin & "' OR ((p.$P2-c.TLASTC)/p.$P2) <= '" & margin & "' " _
& "ORDER BY IMFGR"
The OR in the second to last line generates the error. If I change it to AND, it runs but then it looks for both statements to be true. I want to return a result if any of the margins are below a certain %. What am I doing wrong?
It isn't the p.$LIST# = 'LP' because the p.$p2, p.$p3, etc. are still in there with 'LP'
AND has precedence over OR. You need to wrap the OR clause in parens:
& "(((p.$LIST-c.TLASTC)/p.$LIST) <= '" & margin & "' OR ((p.$P2-c.TLASTC)/p.$P2) <= '" & margin & "') " _

Query - Error assigning value to variable in VB

I have the following code to query in VB6:
SQL = " if object_id('tempdb..#MovSeq','U') is not null drop table #MovSeq;" & _
" declare #Data_Inicio datetime, #Data_Fim datetime; set dateformat dmy; set #Data_Inicio = DataInicio; set #Data_Fim = DataFinal; set #Data_Fim = DateAdd(day, +1, #Data_Fim); " & _
" with Mov as ( SELECT EI.Cod_Empresa, EI.Cod_Estoque, EI.Cod_Produto, 'E' as Tipo_Mov, E.Dta_Entrada as Data_Mov, EI.id_Doc as NF, EI.Qtde, EI.V_Unitario, EI.V_Total " & _
" from Entrada_Itens as EI inner join Entrada as E on EI.Cod_Empresa=E.Cod_Empresa and EI.id_Doc=E.id_Doc " & _
" where E.Dta_Entrada >= #Data_Inicio and EI.Cod_Empresa='" & Sys.Empresa & "' and EI.Cod_Estoque='" & dcEstoque.BoundText & "' and EI.Cod_Produto='" & dtProdutos.BoundText & "' " & _
" Union " & _
" SELECT SI.Cod_Empresa, SI.Cod_Estoque, SI.Cod_Produto, 'S', S.Dta_Entrada , SI.id_Doc, -SI.Qtde, SI.V_Unitario, SI.V_Total " & _
" from Saida_Itens as SI inner join Saida as S on SI.Cod_Empresa=S.Cod_Empresa and SI.id_Doc=S.id_Doc " & _
" where S.Dta_Entrada >= #Data_Inicio and SI.Cod_Empresa='" & Sys.Empresa & "' and SI.Cod_Estoque='" & dcEstoque.BoundText & "' and SI.Cod_Produto='" & dtProdutos.BoundText & "' " & _
" Union " & _
" SELECT Cod_Empresa, Cod_Estoque, Cod_Produto, 'A', #Data_Inicio, null, null, null, null " & _
" From Estoque " & _
" where Cod_Empresa='" & Sys.Empresa & "' and Cod_Estoque='" & dcEstoque.BoundText & "' and Cod_Produto='" & dtProdutos.BoundText & "') " & _
" SELECT *, Seq= row_number() over (partition by Cod_Empresa, Cod_Estoque, Cod_Produto order by Data_Mov desc, Tipo_Mov desc) into #MovSeq from Mov; " & _
" create unique clustered index IndMovSeq on #MovSeq (Cod_Empresa, Cod_Estoque, Cod_Produto, Seq); " & _
" SELECT M.Cod_Empresa, M.Cod_Estoque, M.Cod_Produto, P.Descricao," & _
" Estoque= case when M.Seq=1 then E.Qtde_Estoque else (E.Qtde_Estoque - (SELECT sum(Mi.Qtde) from #MovSeq as Mi " & _
" Where Mi.Cod_Empresa = M.Cod_Empresa And Mi.Cod_Estoque = M.Cod_Estoque And Mi.Cod_Produto = M.Cod_Produto and Mi.Seq < M.Seq)) end " & _
" from #MovSeq as M inner join " & _
" Estoque as E on M.Cod_Empresa=E.Cod_Empresa and M.Cod_Estoque=E.Cod_Estoque and M.Cod_Produto=E.Cod_Produto inner join " & _
" Produtos as P on M.Cod_Empresa=P.Cod_Empresa and M.Cod_Estoque=P.Cod_Estoque and M.Cod_Produto=P.Cod_Produto " & _
" where Data_Mov < #Data_Fim " & _
" order by M.Cod_Empresa, M.Cod_Estoque, M.Cod_Produto, Seq desc; " & _
" drop table #MovSeq;"
the error appears 'invalid columm name' when run showing DataInicio as a reason
I know the error is in the assignment of the variable. but how to solve.
Thanks for the help...
Your type of code is very prone to SQL Injection attacks...
Are you escaping the variable values correctly? A simple quote (') in one of the variables can change the SQL string completely...
You should consider using parameters to pass values instead of building SQL commands using string concatenation.