Hibernate Query in entire table with unique lines selects - mysql

I'm wondering how to filter Hibernate results.
For example, I have this example table:
--------------------------------------------
| ID | STRING | DATE |
--------------------------------------------
| 1 | "ABC" | 2014-11-07 21:45:00 |
--------------------------------------------
| 2 | "ABC" | 2014-11-07 22:45:00 |
--------------------------------------------
| 3 | "DCE" | 2014-11-07 22:48:00 |
--------------------------------------------
| 4 | "ABC" | 2014-11-07 23:48:00 |
--------------------------------------------
The result that I need is:
--------------------------------------------
| ID | STRING | DATE |
--------------------------------------------
| 3 | "DCE" | 2014-11-07 22:48:00 |
--------------------------------------------
| 4 | "ABC" | 2014-11-07 23:48:00 |
--------------------------------------------
All lines of table, BUT, when I have the same content in column STRING, the column with the most recent DATE is the one in result set.
In hibernate I know
- select ALL lines;
- select the repeated lines and keep only the most recent.
But I don't know how to combine the two situations together, and I can't find it referenced anywhere.
I have a fixed length in STRING, and I use a fake primary key in my code to identify the register, but the value is inserted by user, and I have no previous knowledge of the content in the column.

You can do this using a not exists clause:
select e.*
from example e
where not exists (select 1 from example e2 where e2.string = e.string and e2.date > e.date);
That is, select all rows where there is no other row with the same string and a larger date.

Related

SQL - Select column with certain value in it and other random values

Let me say i have a table called test with the following data
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
| 5 | 864296100119956 |
What i want to do is to be able to write a select statement that returns a 3 rows with two random values and one mandatory value from the t_number column
for example if the mandatory value is 864291100247345 the output should something like below
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 4 | 864296100098325 |
OR
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
I have tried the below query but it's not yielding the output i expect, in a sense that it does return a result but without the mandatory value
SELECT * FROM test WHERE t_number = 864291100247345 OR id LIMIT 3;
What is the best way to go about this?
Thank you.
You can use order by:
SELECT t.*
FROM test
ORDER BY (t_number = 864291100247345) DESC,
rand()
LIMIT 3;
This returns the mandatory number first and then random numbers after that.
MySQL treats boolean values (the result of the = expression) as numbers in a numeric context, with "1" for true and "0" for false. So the first expression in the order by sorts the result set with the "true" conditions first, followed by the others.

Mysql - Compare int field with comma separated field from another table

I have two tables in a MySQL database like this:
User:
userid |userid | Username | Plan(VARCHAR) | Status |
-----------+------------+--------------+---------------+---------+
1 | 1 | John | 1,2,3 |1 |
2 | 2 | Cynthia | 1,2 |1 |
3 | 3 | Charles | 2,3,4 |1 |
Plan: (planid is primary key)
planid(INT) | Plan_Name | Cost | status |
-------------+----------------+----------+--------------+
1 | Tamil Pack | 100 | ACTIVE |
2 | English Pack | 100 | ACTIVE |
3 | SportsPack | 100 | ACTIVE |
4 | KidsPack | 100 | ACTIVE |
OUTPUT
id |userid | Username | Plan | Planname |
---+-------+----------+------------+-------------------------------------+
1 | 1 | John | 1,2,3 |Tamil Pack,English Pack,SportsPack |
2 | 2 | Cynthia | 1,2 |Tamil Pack,English Pack |
3 | 3 | Charles | 2,3,4 |English Pack,Sportspack, Kidspack |
Since plan id in Plan table is integer and the user can hold many plans, its stored as comma separated as varchar, so when i try with IN condition its not working.
SELECT * FROM plan WHERE find_in_set(plan_id,(select user.planid from user where user.userid=1))
This get me the 3 rows from plan table but i want the desired output as above.
How to do that.? any help Please
A rewrite off your query what should work is as follows..
Query
SELECT
all columns you need
, GROUP_CONCAT(Plan.Plan_Name ORDER BY Plan.planid) AS Planname
FROM
Plan
WHERE
FIND_IN_SET(Plan.plan_id,(
SELECT
User.Plan
FROM
user
WHERE User.userid = 1
)
)
GROUP BY
all columns what are in the select (NOT the GROUP_CONCAT function)
You also can use FIND_IN_SET on the ON clause off a INNER JOIN.
One problem is that the join won't ever use indexes.
Query
SELECT
all columns you need
, GROUP_CONCAT(Plan.Plan_Name ORDER BY Plan.planid) AS Planname
FROM
User
INNER JOIN
Plan
ON
FIND_IN_SET(Plan.id, User.Plan)
WHERE
User.id = 1
GROUP BY
all columns what are in the select (NOT the GROUP_CONCAT function)
Like i said in the comments you should normalize the table structures and add the table User_Plan whats holds the relations between the table User and Plan.

Merge duplicate (row group) column cells

ID| Date1 | Date 2 |Total
-----------------------------------
1 | 15/02/2017 |02/02/2017 | 3 |
-----------------------------------
1 | 15/02/2017 |05/08/2017 | 3 |
-----------------------------------
1 | 15/02/2017 |12/12/2017 | 3 |
-----------------------------------
2 | 12/05/2017 |07/08/2017 | 2 |
-----------------------------------
2 | 12/05/2017 |10/08/2017 | 2 |
I have a table that is displaying data like above. I'm grouping that data on "ID" column. Values for Columns "Date1" & "Total" for a particular "ID" are the same but "Date2" value can be different in a given group.
How can i merge the cells across rows when the values are the same such that it displays like below?
ID| Date1 | Date 2 |Total
-----------------------------------
1 | 15/02/2017 |02/02/2017 | 3 |
--| |------------| |
1 | |05/08/2017 | |
--| |------------| |
1 | |12/12/2017 | |
---------------------------------|
2 | 12/05/2017 |07/08/2017 | 2 |
--| |------------| |
2 | |10/08/2017 | |
I did manage to find that "HideDuplicates" TextBox property, but while that will suppress the repetition of the cell values in adjacent rows it does not merge those duplicate cells down the column across rows
Its difficult to tell how the report is setup in terms of groups etc without seeing the design, but this is pretty simple to do from scratch.
Start with a simple table with just your detail rows, no grouping. Then right-click the detail row in the row group panel under the main report design area. Choose Add Group -> Parent Group
Choose your Date1 field in the group by drop down . Click OK and you're done.

Union in same table with ACCESS

I have a table with this sort of data:
+------------+----------+----------+
| Unique ID | Name | Class |
+------------+----------+----------+
| 1 | Name 1 | Class A |
| 2 | Name 2 | "" |
| 3 | Name 3 | Class C |
| 4 | Name 1 | "" |
| 5 | Name 4 | "" |
| 6 | Name 4 | "" |
+------------+----------+----------+
I am trying to do something I thought was simple, but i did not find so.
I am trying to "extract" only the lines with an empty string value in 'Class' for a group of equal names.
So in this case I would get this result :
+------------+----------+--------+
| Unique ID | Name | Class |
+------------+----------+--------+
| 2 | Name 2 | "" |
| 5 | Name 4 | "" |
+------------+----------+--------+
So not Name 1 because even though there is a line with "" there is another line with 'Class A'.
I thought a UNION would do the job but I am not gettgin anything because I think unions are for two tables but the problem here is I have the data in the same table.
Thank you for your help
Access syntax may be a bit different but this returns what you want in Oracle:
SELECT distinct Name, Class FROM table1 Where Name NOT in (select name from table1 where class is not null)
A Union melds two result sets, whether or not they come from the same table is irrelevant. What you want to do is omit from the result set the rows with the same name AND class is not null. Not having your query to expand on or change is a problem, but if you add a clause that says something like where "name not in (select name from table where class is not null);", that may do it.

mysql how to split a column into multiple columns with ambiguous number

Hello i want to split a resulting column in multiple columns just like on the link. But number of columns are not specific ;
Example
COL1 | OTHER COLUMNS
----------------------------------------
this,will,split | some value
also,this | some value
this,is,four,columns | some value
I want make this something like that ;
COL1 | COL2 | COL3 | COL4 | OTHER
----------------------------------------
this | will | split| NULL | some value
also | this | NULL | NULL | some value
this | is | four | columns| some value
edit
it looks like similar that question but not:
Can you split/explode a field in a MySQL query?
I want results in 1 row, I dont want something like that;
RESULT
-----
this
will
split
...
on that question you can see there is specific number of cols. bu i dont. :(
How to split a resulting column in multiple columns
I think you can create one relational table and add multiple entry in relational table, hear you don't need to think about column, you have to add entry in row.
eg.
Table 1:
ID | COL1 | OTHER COLUMNS
----------------------------------------
1 |this,will,split | some value
2 |also,this | some value
3 |this,is,four,columns | some value
Table2
ID | Table1_id | value
-------------------------
1 | 1 | this
2 | 1 | will
3 | 1 | split
4 | 2 | also
5 | 2 | this
6 | 3 | this
6 | 3 | is
6 | 3 | four
6 | 3 | columns
Please check this, i think fix your problem.