MyBatis get resultMap as String - mybatis

I need to get from the database uniqe list if values in one column (type String). But if I try to set
<select id="getAllValues" parameterType="map" resultMap="java.lang.String">
SELECT distinct value_id FROM values
</select>
I receive the error - Result Maps collection does not contain value for java.lang.String
How can I get a list of Stings from the database via MyBatis?

Thanks to ave, I've found the solution:
<select id="getAllValues" parameterType="map" resultType="java.lang.String">
SELECT distinct value_id FROM values
</select>

Related

Query IN Clause with parameter

Here is my query,
<Request method="GET">
<Query>
select user_id from user
where user_type in ($userTypes)
</Query>
</Request>
How do I sent multiple values for this parameter $userType in metamug resource file. I'm using a GET request. And my database is postgres.
$userTypes is list of ids. I can pass it as comma separated string "1501,1502,1503" in the request.
Thanks.
You could use:
select user_id from user
where user_type in (SELECT unnest(string_to_array($userTypes, ',')));
DbFiddle Demo
you can also write it as
<Request method="GET">
<Query>
select user_id from user
where user_type =any(string_to_array($userTypes,',')::int[]);
</Query>
</Request>
I've typecast it as Integer array you can use any other datatype as per your need.

Sum a field in a jsonb column with an Ecto query

Say I have a jsonb type column in a Postgres DB, called info. One of the fields is bytes, which is stored as an integer in the info field.
If I try and sum the values of the info => bytes field in an Ecto Query, as below:
total_bytes = Repo.one(from job in FilesTable,
select: sum(fragment("info->>'bytes'")))
I get the error function sum(text) does not exist.
Is there a way to write the query above so that info => bytes can be summed, or would I have to just select that field from each row in the database, and then use Elixir to add up the values?
The error message says that it can't sum a text field. You need to explicitly cast the field to an integer so that sum works.
Also, it's incorrect to hardcode a column name in a fragment. It only works in this case because you're selecting from only one table. If you had some join statements in there with other tables with the same column name, the query won't work. You can use ? in the string and then pass the column as argument.
Here's the final thing that should work:
sum(fragment("(?->>'bytes')::integer", job.info)))

Generate DISTINCT Lists with ID T-SQL

For use in SSRS multi-select dropdowns, I need to generate multiple DISTINCT lists of values.
This is easily done. Select DISTINCT Department from DimEmployee
In order to set an SSRS default value, you must have an ID field also in your data set.. this is not achieved by the above query
I need to assign an arbitrary uniqueID to each of these records from my distinct list
Any advice appreciated.
Final output would be:
Select * From dsDeptList
Results...
ID | DeptName
1 | DeptName1
2 | DeptName2
etc.....
Just include the EmployeeID column twice (with column aliases). Use it as the ID and the value. The DISTINCT guarantees it's unique in the resultset.
Please try below code:
CREATE TABLE #Test(EmpName VARCHAR(10));
INSERT #Test VALUES('A'),('A'),('B'),('C'),('C');
SELECT DISTINCT DENSE_RANK() OVER (ORDER BY EmpName) ID,EmpName
FROM #Test;
DROP TABLE #Test;
I hope this will help.
I think GROUP BY should solve your problem
SELECT EmployeeID,EmpName FROM YourTable
GROUP BY EmployeeID,EmpName
You'll definitely avoid dublications

Postgres escape a single quote

I have the following postgres query:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN (<parameter>)
<parameter> is a dynamic text field where multiple ID's need to be inserted. If you type in
123, 456
as ID's, it results in:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN ('123,456')
Which doesn't run properly.
I can change the query, but I can't change the input field. If you type in
123','456
It results in:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN ('123'',''456')
When you change the query into:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN ('<parameter>')
And you type in
123,456
Then it results in:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN (''123'',''456'')
I've got it working for Mysql, but not for Postgresql. Any idea how to trick postgresql?
Try something like:
SELECT SUM(Cost)
FROM DB
WHERE ID != ALL(('{'||'123,456'||'}')::numeric[])
It will form array string from your input values : {123,456}, cast it to an array and check ID against all elements of array.

hive Expression Not In Group By Key

I create a table in HIVE.
It has the following columns:
id bigint, rank bigint, date string
I want to get avg(rank) per month. I can use this command. It works.
select a.lens_id, avg(a.rank)
from tableA a
group by a.lens_id, year(a.date_saved), month(a.date_saved);
However, I also want to get date information. I use this command:
select a.lens_id, avg(a.rank), a.date_saved
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);
It complains: Expression Not In Group By Key
The full error message should be in the format Expression Not In Group By Key [value].
The [value] will tell you what expression needs to be in the Group By.
Just looking at the two queries, I'd say that you need to add a.date_saved explicitly to the Group By.
A walk around is to put the additional field in a collect_set and return the first element of the set. For example
select a.lens_id, avg(a.rank), collect_set(a.date_saved)[0]
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);
This is because there is more than one ‘date_saved’ record under your group by. You can turn these ‘date_saved’ records into arrays and output them.