Let's say I have a very simple table called test:
ID AGE NAME
1 12 Bob
1 13 Bob
2 13 John
3 9 Michael
3 11 Michael
I want to return all results that have the largest AGE in the table. So for this case, the result would be the entities with the age 13:
ID AGE NAME
1 13 Bob
2 13 John
I would think something like this should exist, my thought process would have been the following (even though I know the syntax would not work):
connection.getSqlQuery()
.from(test)
.max(test.age)
.list(test.id, test.age, test.name);
What would be the proper query for this?
You can create a subquery and use it in your where clause:
// Aggregate to get the maximum age
var subQuery = connection.getSqlQuery()
.select(test.age.max())
.from(test);
connection.getSqlQuery()
.from(test)
.where(test.age.eq(subQuery))
.list(test.id, test.age, test.name);
If you use JPA, you can also use var subQuery = JPAExpressions.select(...
Related
I need to find max salary and displaying in new attribute.
Employee table has following columns:
Id,
Name,
Salary
I have added new add transient variable with groovy expression using Edit Expression Editor :
max(Salary)
to display max salary which exists in whole table.
I have tried this example in 11g.
Table : Emp :
E_ID EMP_NAME E_SAL
2 emp2 5000000
4 e5 100000
5 emp2 300000
6 emp3 250000
7 emp3 100000
8 emp3 100000
3 eee 250000
1 emp 100000
In which E_ID - 2 has max salary as 5000000.
Now coming back to ADF:
I have created EMP table EO and VO both.
EMPEO
EMPVO - EmpView
Now in EmpView has all the attributes as Eid, EmpName, Esal.
Created a ViewAccessor of Same VO (EmpView) in EmpView
- Click on EmpView.
- Go to View Accessor. Click on Green plus symbol. Add the EmpView from left to right. It will be named as EmpView1.
Create a transient variable named as maxSal. Give a groovy expression
to it in a Expression component.
EmpView1.max("Esal")
Now,create a jsff page and add EmpView as Form or table (as per your
requirement) with newly created transient variable. Run the
application. It's working in my case.
Hope this may help.
I have a SQLite table content with following columns:
-----------------------------------------------
|id|book_name|chapter_nr|verse_nr|word_nr|word|
-----------------------------------------------
the sql query
select count(*) from content where book_name = 'John'
group by book_name, chapter_nr
in DB Browser returns 21 rows (which is the count of chapters)
the equivalent with ORMLite android:
long count = getHelper().getWordDao().queryBuilder()
.groupByRaw("book_name, chapter_nr")
.where()
.eq("book_name", book_name)
.countOf();
returns 828 rows (which is the count of verse numbers)
as far as I know the above code is translated to:
select count(*) from content
where book_name = 'John'
group by book_name, chapter_nr
result of this in DB Browser:
| count(*)
------------
1 | 828
2 | 430
3 | 653
...
21| 542
---------
21 Rows returned from: select count(*)...
so it seems to me that ORMLite returns the first row of the query as the result of countOf().
I've searched stackoverflow and google a lot. I found this question (and more interestingly the answer)
You can also count the number of rows in a custom query by calling the > countOf() method on the Where or QueryBuilder object.
// count the number of lines in this custom query
int numRows = dao.queryBuilder().where().eq("name", "Joe Smith").countOf();
this is (correct me if I'm wrong) exactly what I'm doing, but somehow I just get the wrong number of rows.
So... either I'm doing something wrong here or countOf() is not working the way it is supposed to.
Note: It's the same with groupBy instead of groupByRaw (according to ORMLite documentation joining groupBy's should work)
...
.groupBy("book_name")
.groupBy("chapter_nr")
.where(...)
.countOf()
EDIT: getWordDao returns from class Word:
#DatabaseTable(tableName = "content")
public class Word { ... }
returns 828 rows (which is the count of verse numbers)
This seems to be a limitation of the QueryBuilder.countOf() mechanism. It is expecting a single value and does not understand the addition of GROUP BY to the count query. You can tell that it doesn't because that method returns a single long.
If you want to extract the counts for each of the groups it looks like you will need to do a raw query check out the docs.
I've not much experience in sql.In my little java program using ucanaccess library I was able to perform some simple queries after creating a simple table (named ReportSales). The table is:
ID DATE PRODUCT SALES FEES
1 2014-10-02 productA 10.000 100
2 2014-09-02 productC 12.000 240
3 2014-09-02 productA 8.000 80
4 2014-11-02 productB 7.000 105
5 2014-08-02 productB 6.000 90
.. .......... ........ ...... ....
.. .......... ........ ...... ....
The last task is to create a pivot table in which I would insert the monthly sales per product. Something like:
PRODUCT AUG SEP OCT NOV
productA 0 8.000 10.000 0
productB 6000 0 0 7000
productC 0 12.000 0 0
And another pivot for monthly fees
My (wrong) attempt is:
SELECT [8] as AUG, [9] as SEP, [10] as OCT, [11] as NOV
FROM
(SELECT SALES,MONTH(DATE)
FROM ReportSales) AS tmp
PIVOT
(
SUM(SALES)
FOR MONTH(DATE) IN ([8], [9], [10], [11])
) AS PivotTable
Does anyone can help me? Thanks in advance
UCanAccess supports Pivot queries, only if they are built in Access, so you have firstly to create a crosstab query using the Access IDE.
Or you may simply create a new query in Access entering directly the following SQL:
TRANSFORM sum(ReportSales.sales) AS SalesCount
SELECT ReportSales.product
FROM ReportSales
GROUP BY ReportSales.product
PIVOT Format([date],"mmm");
Then assuming that you saved the query as query1, you have just to execute, with UCanAccess:
Select * from query1;
I'm having some trouble here to create an appropriate SQL query. Any help will be much appreciated!
Some background:
I have the following entities
Equipment
id
nickname
owner_indicator
{...}
EquipmentGroup_Equipment
equipment_id
equipment_group_id
EquipmentGroup
id
name
description
I need to do a SQL / JPA Hibernate query that returns me:
EquipmentGroup.name, EquipmentGroup.description, Equipment.owner_indicator
And this will be grouped by EquipmentGroup.id, so if I have 10 equipments inside the group it will return information grouped by the EquipmentGroup.
The thing is, when I have for example more than one owner_indicator inside a EquipmentGroup it will return 2 rows. This is SQL 101. But i must return only one line with a blank text instead of the Owner Indicator.
What is the easiest way to do this ? I'd be glad to have the answer in SQL, but much more than glad to have it in Criteria JPA, heh.
If it does matter, I'm using Oracle 12c.
Thanks!
EDIT
As requested, here is some data:
Equipment
id nickname owner_indicator
1 EQP01 'V'
2 EQP02 'T'
EquipmentGroup_Equipment
equipment_group_id equipment_id
1 1
1 2
EquipmentGroup
id name description
1 GRP1 Group 1
My wanted resultSet is:
Result
EquipmentGroup.name EquipmentGroup.description, Equipment.owner_indicator
GRP1 Group 1 (empty string)
That empty string would be returned because I don't want 2 rows, like
Result
EquipmentGroup.name EquipmentGroup.description, Equipment.owner_indicator
GRP1 Group 1 'T'
GRP1 Group 1 'V'
If anything more than that is needed please advise.
Thanks!
I thin k you must to use a main query on EquipmentGroup and a subquery about return data on Equipment.
If you have more than 1 equipment rows about one group you must return DISTINCT empty; if you have 1 row returns owner_indicator otherwise you can return 'None'
Try this:
SELECT DISTINCT eg.name, eg.description,
(SELECT
CASE
WHEN count(e.id) > 1 THEN DISTINCT 'EMPTY'
WHEN count(e.id) = 1 THEN e.owner_indicator
ELSE 'none'
END
FROM Equipment e
WHERE e.equipmentGroup.id = eg.id)
FROM EquipmentGroup eg
im try to execute sql query that will check if some time (like 10 sec) past from last timestamp update in my table and chenge other table if yes.
My question is if ther is any time-stamp conditional operator that can check this? For example
< , > ,=?
(I know that I can do it in to different query, but I'm try do to it in 1 query)..
Something like this
UPDATE Person SET isconnected=false
where person.email=(select from imalive where timestamp<10).
person:
email: dan#gmail.com
name: dan
age: 20
isAlive:
email: dan#gmail.com
lastseen: 2011-09-04 21:27:00
So at last if the person is last seen will be more then 10 sec he will go to isconnected =false.
Syntax would change slightly per database. Here's an example for SQL Server using not exists:
update Person
set IsConnected = 0
where IsConnected <> 0
and not exists
(
select *
from IsAlive
where Person.email = IsAlive.email
and IsAlive.timestamp > dateadd(s,-10,getdate())
)