ConcatRelated function and the fifth argument - ms-access

I'm trying to achieve the following:
If you have:
ItemNo DocumentNo Quantity
AB1 WS1 10
AB1 WS2 10
I want:
ItemNo DocumentNo Quantity
AB1 WS1 | WS2 10
I'm not quite understanding how to use the fifth argument. I continue to get errors, stating that "|" is not valid.
Here's the code.
SELECT DISTINCT Query3.ItemNo, ConcatRelated("Query3.DocumentNo", "Query3", "Query3.ItemNo = " & [Query3.ItemNo] & " | " )
FROM Query3;
Here's the website:
http://allenbrowne.com/func-concat.html

I can't tell, but my DJoin function found here and this query:
SELECT
ItemNo,
DJoin("[DocumentNo]","[Query3]","[ItemNo] = '" & [ItemNo] & "'"," | ") AS DocumentNos,
Quantity
FROM
Query3
GROUP BY
ItemNo,
Quantity
HAVING
Count(*) >=2;
will provide this output:

Related

How to SUM a COUNT Column and display the answer in a Label

I have the following code
Dim cmd As New MySqlCommand("SELECT IdNumber, COUNT(DISTINCT date) AS 'Attendance' FROM record WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) GROUP BY idNumber ", cn)
Dim i As MySqlDataReader = cmd.ExecuteReader()
While i.Read
Label1.Text = i("SUM(Attendance)")
i.Close()
End While
I need to display the COUNT Column SUM in a label, but when i run the code i get the following error:
'Could not find specified column in results: SUM(Attendance)'
The COUNT Column(Attendance) is generated within the code to get specific COUNT of items in table record in a specific date period and returns the following:
+----------+------------+
| idNumber | Attendance |
+-----------------------+
| 88383 | 4 |
| 64785 | 2 |
+-----------------------+
and now i want to SUM those specific items and display them in a table like below:
Label = 6
If you only need to sum up all distinct date per IdNumber, you don't need to select idNumber then do a sub-query to sum up all attendance.
SELECT SUM(Attendance) as Label
from (
SELECT COUNT(DISTINCT date) AS 'Attendance'
FROM record
WHERE MONTH(date) = MONTH(CURRENT_DATE())
AND YEAR(date) = YEAR(CURRENT_DATE())
GROUP BY idNumber
);
Result:
Label
6

Translation Inner Join in JQL

I have a problem with INNER JOIN in my JQL. What I want to do is to retrieve all Users that have rate one User Z with a mean of >= 2. Users have to answer 3 questions that will determine the final rate of this User Z. There is 2 tables, User and Score.
Table User Table Score
Id_user Id_score Id_rated Id_rater Id_question Score
1 1 1 3 1 1
2 2 1 3 2 0
3 3 1 3 3 1
4 1 2 1 0
5 1 2 2 0
6 1 2 3 0
I want as a result only User 2 for example.
This is the error when I translare my query to JQL :
[32, 184] The join association path is not a valid expression.
My query works well in MySQL Workbench
SELECT *
FROM User
INNER JOIN (
SELECT ID_RATER, (AVG(Score.score)*5) as AvgScore
FROM Score
WHERE ID_RATED=1751
AND (
SELECT (AVG(Score.score) * 3)
FROM Score)
GROUP BY ID_RATER
) TabAvg
ON TabAvg.AvgScore >= 2
AND USER.ID=TabAvg.ID_RATER
Translating to JQL :
String sql =
"SELECT U "
+ "FROM User U "
+ "INNER JOIN "
+ "("
+ "SELECT S.id_rater, (AVG(S.Score) * 3) AS AvgScore "
+ "FROM Score S "
+ "WHERE S.id_rated= :id_rated"
+ "AND "
+ "("
+ "SELECT (AVG(S2.score) * 3) "
+ "FROM Score S2"
+ ") "
+ "GROUP BY S.id_rater"
+ ") TabAvg "
+ "ON TabAvg.AvgScore >= 2 "
+ "AND U.id_user = TabAvg.id_rater";
Well, I still don't know what the problem with
EntityManager.createQuery(sql)
but to bypass this, I used
EntityManager.createNativeQuery(sql)
by just copy/paste my working sql into my code.

Get row position after ordering MYSQL

I can't get the following to work as expected, tried what I have seen in other examples and solutions but still can't this right.
Dim pos As String = Nothing
Try
OpenUserDB()
Dim cmd43 As MySqlCommand = New MySqlCommand("SELECT COUNT(id) AS position FROM users " & _
"WHERE user_id <= #UserID ORDER BY food_total DESC", connUser)
cmd43.Parameters.AddWithValue("#UserID", UserID)
Dim reader As MySqlDataReader = cmd43.ExecuteReader
If reader.Read() Then
pos = reader("position").ToString() & positionName(reader("position").ToString())
End If
Return pos
Catch ex As Exception
Dim err As New ErrorLog
err.logError(Format(Date.Now, "dd-MM-yyy hh:mm:ss ") & ex.ToString)
Return Nothing
Finally
CloseUserDB()
End Try
table users
| id | userID | food_total |
| 1 | g01 | 84 |
| 2 | g02 | 83 |
| 3 | g03 | 34 |
I queried with UserID = g02 with expected result being 2, but got 3.
The user is suppose to be the 2nd User but it returns the wrong position. Help will be appreciated.
you might want to something like this.
Where you rank them all in the inner query based on food_total...the the outer query selects the information of that specific userid including the rank.
SELECT Id,userId,rank
FROM
(SELECT Id,userID,
#rank := IFNULL(#rank,0)+1 as rank
FROM users
ORDER BY food_total DESC) AS T
WHERE T.userID = 'g02'
sqlfiddle
Somehow #TinTran solution did not work for me with vb.net, but the following work fine;
SELECT Id,userId,rank
FROM
SELECT Id,userID, #rank := IFNULL(#rank,0)+1 as rank
FROM users ORDER BY food_total DESC) AS T, (SELECT #rank:=0) R
WHERE T.userID = 'g02'

Query rows using Order by A, if A values are the same, Order by B as the second standard

I want to query db rows using two standards: A first, B second.
That is: Order by A, if A values are the same, Order by B as the second standard
How to write the sql?
Example:
query table:
id | A | B
_ _ _ _ _ _
1 | 1 | 1
_ _ _ _ _ _
2 | 2 | 2
_ _ _ _ _ _
3 | 2 | 1
_ _ _ _ _ _
4 | 3 | 1
query result:
id
1
3
2
4
Order by is used to sort the result from a table in ASC | DESC based on one or more column names. It sorts by ASC in default.
Example:
Select * from Table1 order by A, B
In this example the results from Table1 is sorted in ASC by A as well as B. If A has the same values, then the results will be sorted by B in ASC
You can simply have multiple order-by's: ORDER BY A DESC,B for example.
To get the desired result:
Select * from SomeTable ORDER BY A ASC, B ASC

Parent Select SQL Query generating Child Select

These are my tables:
Table_Bill_Summary
| Date | PersonId | BillNo |
Table_Bill_Items
| BillNo | ItemId | Quantity |
Item_MasterTable
| ItemId | ItemName | ItemRate |
I am going to specify a PersonId. This is what I want to print:
SELECT *
FROM Table_Bill_Summary
WHERE PersonId=#X
(SELECT IM.ItemName,BI.Quantity,IM.ItemRate
FROM Table_Bill_Items AS BI
INNER JOIN Item_MasterTable AS IM ON BI.ItemId=IM.ItemId
WHERE BI.BillNo=??)
My result should look like:
23/04/2013 Person123 Bill32
Item20 23 100
Item21 20 200
Item23 1 300
23/04/2013 Person123 Bill39
Item2 2 100
Item11 40 800
Item43 1 700
I want a child Select Query to be run for each row retrieved by the parent Select query on the column BillNo
select FLD1,FLD2,FLD3 from
(
SELECT CAST(Date as varchar(10)) as FLD1 ,CAST(PersonId as varchar(10)) as FLD2,
cast(BillNo as varchar(10)) as FLD3,1 as FLD4, Billno as fld5
FROM Table_Bill_Summary WHERE PersonId =#X
union
SELECT CAST(IM.ItemName as varchar(10)) as FLD1 ,CAST(BI.Quantity as varchar(10)) as FLD2,CAST(IM.ItemRate as varchar(10)) as FLD3 ,2 as FLD4, BS.Billno as fld5
FROM Table_Bill_Summary BS inner join Table_Bill_Items AS BI on bs.BillNo=BI.BillNo
INNER JOIN Item_MasterTable AS IM ON BI.ItemId=IM.ItemId
WHERE Bs.PersonId =#X
) as x order by fld5,fld4
You can't do this in SQL. Instead, perform the operation in your application by fetching the joined results (bills and items) sorted by bill and keeping track of the last seen bill when iterating through the resultset:
string stm = #"
SELECT BillNo, Date, ItemName, Quantity, ItemRate
FROM Table_Bill_Summary
JOIN Table_Bill_Items USING (BillNo)
JOIN Item_MasterTable USING (ItemId)
WHERE PersonId = #Person
ORDER BY BillNo
";
MySqlCommand cmd = new MySqlCommand(stm, conn);
cmd.Prepare();
cmd.Parameters.AddWithValue("#Person", person_id);
MySQLDataReader rdr = cmd.ExecuteReader();
bool isMore = rdr.Read();
while (isMore) {
int current_bill = rdr.GetInt32(0);
// output bill
do {
// output item
} while (isMore = rdr.Read() && rdr.GetInt32(0) == current_bill);
}
You can't. (At least not without some queer tricks ;-) An SQL select statement will always result in a list of rows showing the same columns.
You would usually solve this with a reporting software or with another programming language and GUI.