Consider the table named school
I am trying to create a MyBatis Query with the below conditions
1. If the teacher value is not null
Get the top 2 rows of that teacher, sorted on the basis of created_time
2. If the teacher value is null
Get all the data from the table "school" in such a way that the top two rows of each teacher is obtained, which are sorted on the basis of created_time
The result should be:
The MyBatis query I tried is :
I have created a List<String> of teachers and tried to get the rows using for each, but this didn't work
#Select({"<script>", "SELECT", " * ", "FROM school a ",
"WHERE"
"<if test=\"teacher != null\"> a.teacher = #{teacher} </if>",
"<if test=\"teacher == null\">",
"<foreach collection='teacherList' item='t' index='i' open=' AND a.teacher IN (' separator=',' close=')'>",
"<script>",
"SELECT teacher FROM school WHERE teacher=#{t}",
"</foreach>",
"</script>",
"</if>" +
"ORDER BY created_time DESC",
"LIMIT #{size}",
"</script>"})
Related
I have the following query:
SELECT "IMPORTACIONCOLUMN3", "role", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "IMPORTACIONCOLUMN3", "role"
This gives me the following result:
I would like to achieve the following:
The "unique" values are grouped together (i.e. Instead of having 4 values of "Robot 1" these are grouped togehter in just 1 cell summing the count values.
The second group by or subquery has to be the same count, but with role instead of "IMPORTACIONCOLUM3"
Is it possible (for the second picture) to "link" the values either by index or adding an extra column to reference them (i.e. There's two "Solicitante" with a count value of "52" but it refers to "Robot 1" and other to "Solicitante" with count value of "58" links to "Robot 2"
The second image represent visually what I'm trying to explain.
I have been trying on my own but only have reached the following:
select "IMPORTACIONCOLUMN3", count("IMPORTACIONCOLUMN1")
from
(
select "IMPORTACIONCOLUMN1", count("role"), "IMPORTACIONCOLUMN3"
from MYTABLE
WHERE ID = 9
group by "IMPORTACIONCOLUMN1", "IMPORTACIONCOLUMN3"
) as tmp
group by "IMPORTACIONCOLUMN3"
But it is not yet the result I am looking for.
Thanks in advance for your help and tips!
EDIT:
Explaining my desired output in detail
Each one of "Robot 1, 2, 3" have roles such as "Solicitante", "Gerente", etc. with different values.
i.e. The first row "Humano" value "243" is the sum of "Agente de Compras - 95", "Gerente Financiero - 37", "Gerente Solicitante - 45", "Proovedor - 31", "Solicitante - 60".
I am linking these by the column "GRAFICOCOLUMNARECURSIVOID" with contains the index of whatever "Robot" these "roles" are from.
I would like to achieve a query containing subquerys that allows me to have this output.
Try this for question number 1:
SELECT "IMPORTACIONCOLUMN3", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "IMPORTACIONCOLUMN3"
the problem is Role: Robot 1 have 4 roles
and this for question 2:
SELECT "role", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "role"
Question 3, I don't understand what you are asking for. Please make an example.
I have two tables first table called TFile contains two columns: FromCity and ToCity.
They will have different values but from one column of the second table (TCity) and specifically from the column called CityName.Second table name TCity they have two column : IdCity AND CityName.
My problem I need to display data for two columns they got from second table FromCity and ToCity with inner join for two times.
I use this code to do that:
SqlCommand comm = new SqlCommand("select * from TFile " +
"inner join TCity AS A ON TFile.FromCity = A.IdCity " +
"inner join TCity AS B ON TFile.ToCity = B.IdCity " + " WHERE " + "TFile.Name", con);
Then display data to users as:
SqlDataReader srd = comm.ExecuteReader();
if (srd.HasRows)
{
while (srd.Read())
{
//FromCity
TextFrom.Text = srd["CityName"].ToString();
//ToCity
TextTo.Text = srd["CityName"].ToString();//=======================here problem
}
}
In the first line of the data display I can get the name of the city but if I repeat that in the second line it will just repeat the data. Here problem.I can't use a different name to access the second query instead of the field name CityName.This is the name of the field in the second table for which I display the names of the cities.
How can I access to data in this query:
"inner join TCity AS B ON TFile.ToCity = B.IdCity
So if I access to it then can display second data in this line:
TextTo.Text = srd["CityName"].ToString();
How can solve this problem ?
I bet you need to create field aliases to differentiate between values from the multiple joins on the same table. By the way, please make sure you protect yourself from "SQL injection" when using these types of queries. If you don't know what "SQL Injection" is then please take some time to look it up.
"select FromCityName=A.CityName, ToCityName=B.CityName, * from TFile "
And
//FromCity
TextFrom.Text = srd["FromCityName"].ToString();
//ToCity
TextTo.Text = srd["ToCityName"].ToString();//=======================here problem
I need help with a SQL query for room within an android app. There is a table questions. This table has 3 columns:
id question id_sub_cateory
Furthermore there is table_sub_categories with folloing columns:
id sub_category id_category
And table_categories:
id category id_subject
Additionally there is a table_subject:
id subject id_questionnaire
Last but not least table_questionnaire:
id questionnaire
Now I would like to have a SQL query to get following information:
table_questionnaire.id table_questionnaire.questionnaire questionCount subjectCount
So basically a list which lists all questionnaires with number of questions and subjects included. What would be the SQL query for that?
EDIT: So far I have used this query:
SELECT table_questionnaire.id AS 'id', table_questionnaire.questionnaire_name AS 'name', " +
"COUNT(table_question.id_questionnaire) AS 'questionCount', COUNT(DISTINCT table_question.subject) AS 'subjectCount' " +
"FROM table_questionnaire " +
"LEFT JOIN table_question ON table_questionnaire.id = table_question.id_questionnaire GROUP BY table_questionnaire.id"
But this one is an old version because I saved each question with an ID of questionnaire, subject, category and sub_category. I recognized that this information is redundant. Now I save only the ID of sub_category. But now I have to change the query. I just don't know how!?
This is the query which works:
SELECT
table_categories.id,
table_categories.id_subject,
table_categories.category,
table_sub_categories.id AS 'id_sub_category',
table_sub_categories.id_category,
table_sub_categories.subCategory,
COUNT(id_category) AS 'questionsCount'
FROM table_question
LEFT JOIN table_sub_categories ON table_question.id_sub_category = table_sub_categories.id
LEFT JOIN table_categories ON table_categories.id = id_category
WHERE table_categories.id_subject = :subjectID
GROUP BY table_categories.id
I'm using an Access 2007 database in ASP.Net where I need to extract data from a table if an item meets multiple 'AND' clauses.
For example:
Select * from tableA
where tableA.column1 = myvalue1
and (tableA.column2 = myvalue2
and tableA.column2 = myvalue3)
(The '(' and ')' parenthesis around the 'and' clauses are an Access requirement)
myvalue1 exists multiple times in column1 because column2 stores many different pieces of data for the column1 value.
However, I only want the results to show those column1 values which meet all of the column2 search criteria.
For example, column1 contains employee surnames, column2 contains the qualifications of the employee.
One employee in column1 may hold many qualifications in column2.
Searching with the 'OR' clause for employees who hold all the required qualifications for an assignment would produce a table with multiple rows for each employee.
What is required is a single row for each employee who holds every one of the required qualifications.
Let's say employee A has qualifications X, Y and Z. The employee's name will be in a row in column 1 each time a qualification is added to column2, so the employee's name appears in column1 3 times. Assume now employee B also has qualifications X, Y and Z but employee C has W, X and Y. If an assignment needs qualifications X,Y and Z I don't want employee C appearing in the results of a search as employee C won't hold the relevant qualifications.
When I use the above 'select' command with just one 'and' clause the search works fine, but with two 'and' clauses nothing is returned, even though I know there are items in column1 that meet both of the 'and' criteria.
What am I missing in the concatenation of the 'and' clauses?
The following solution will work, but performance will suffer. This will return 1 row per person, I can rewrite it to return both rows.
Select * from tableA
where tableA.column1 = myvalue1
and (tableA.column2 = myvalue2
and DCount("column2", "TableA", "column1 = """ & column1 & """ AND column2 = """ & myvalue3 & """" ) <> 0)
Your fundamental problem is that a select query without a group by or pivot clause only evaluates 1 row at a time. I'd probably pivot the data, concatenate it, and then evaluate it for better performance in the end. But that's more complicated, and requires more information on the table structure and what you want to do.
Based on new information, I'll submit this answer:
My gut feeling is that you can't do this in one simple query. This sounds cludgy, but I think you're going to need one query created that's a SELECT DISTINCT query, and that query will pull one record for each employee:
Select DISTINCT Column1 from tableA
Call this query something like qryEmployees
Next you're going to need to create a new table. Call it tblQualifications. It will contain 2 fields; EName and EQualifications. We're going to fill that in a minute.
Next you're going to need to loop through each record in qryEmployees and write the employee name and their qualifications into tblQualifications.
'Run through every Employee name
Do While qryEmployees.EOF = False
'Set up a temp variable, MyQual, and set it equal to Empty
MyQual = ""
'Pull all qualifications from TableA for the current Employee
Set rec = db.OpenRecordset ("Select Column1, column2 from TableA WHERE Column1 = '" & qryEmployees.Column1 & "'")
'Looping through the qualifications
Do While rec.EOF = False
'Add the qualification to the MyQual string
MyQual = MyQual & rec("Column2") & ", "
rec.MoveNext
Loop
'Now that we have all the qualifications in a string, trim off the last ", "
MyQual = left(MyQual, len(MyQual)-2)
'Open up tblQualifications and fill it with the Employee and a string of all of their qualifications
tblQualifications.AddNew
tblQualifications(0) = qryEmployees.Column1
tblQualifications(1) = MyQual
tblQualifications.Update
'Move on to the next Employee and repeat the process
qryEmployees.MoveNext
Loop
Now you have one table with Employee Name in one field, and all of his qualifications in the next. If you use the LIKE operator on this table, you should get your results.
Select * from tblQualifications
where tblQualifications.EName = myvalue1
and (tblQualifications.EQualifications LIKE myvalue2
and tblQualifications.EQualifications LIKE myvalue3)
It's not pretty, but it will work (with a little putzing with the code above, as I'm writing it off the top of my head and it's untested).
Thanks Johnny, your suggestion inspired me to write a slightly different approach to the solution.
I swapped the 'AND' to an 'OR' as others have suggested to extract any employee into a table if they have any of the qualifications.
The result is a table with every required qualification for every employee who has one of those qualifications.
I then did a count elsewhere of the number of qualifications required for the assignment and compared this to whether or not the employee had the required number of qualifications, as follows:
Dim objSearchTable As DataTable = DSSearch.Tables("Employees")
Dim i As Integer
If QualificationsCount > 0 Then
For i = 0 To objSearchTable.rows.count - 1
strEmployee = objSearchTable.Rows(i).Item("column1")
Dim foundRows As DataRow() = objSearchTable.[Select]("column1 = '" & strEmployee & "'")
Dim numberOfRecords As Integer = foundRows.count
If numberOfRecords < QualificationsCount Then
objSearchTable.Rows(i).Delete
End If
Next
End If
objSearchTable.AcceptChanges
This deletes every instance of an employee from the table if the count of qualifications is less than the number required.
The solution isn't as elegant as extracting only the correct employees from the database to begin with.
There is also an overhead on extracting every employee from the database, but the end result is a table with just one row for each employee with all the qualifications, which does what I need.
So I have the follow code.
Query query = getEntityManager()
.createQuery(
"SELECT m.memCategory, m.copClass, SUM(b.paidAmt) paidAmt FROM BuildingFundModel b "
+ "INNER JOIN b.memberModel m WHERE b.memberModel != NULL AND b.paidDt BETWEEN :dtFrom AND :dtTo "
+ "GROUP BY m.memCategory, m.copClass");
query.setParameter("dtFrom", dtFrom);
query.setParameter("dtTo", dtTo);
lstObj = query.getResultList();
However, when I run this, it always returns only two columns. Is that How Query.getResultList() should work?
This query will return a list containing one or more Object[]. The number of items in the list correlates to the number of rows returned, while each Object[] should contain 3 items, one for each column.
The Query.getResultList() method replaces the place holders in the query with the variables from the query.setParameter. It then executes the query returning what is selected in the SELECT statement.
Try this query:
Query query = getEntityManager().createQuery(
"SELECT m.memCategory, m.copClass, SUM(b.paidAmt)
FROM BuildingFundModel b
INNER JOIN b.memberModel m
WHERE b.memberModel IS NOT NULL AND b.paidDt BETWEEN :dtFrom AND :dtto
GROUP BY m.memCategory,m.copClass");
query.setParameter("dtFrom", dtFrom);
query.setParameter("dtTo", dtTo);
lstObj = query.getResultList();