How Query.getResultList() works - eclipselink

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();

Related

How to query twice at once from one table with inner join SQL ASP.NET

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

Two queries when Joined together adds more records and duplicates records

I have two queries. First Query: Raw data for the clock in. Second Query: Raw data for the clock out. When i combined the two queries together i get duplicated records. On my Raw Check in there are only 2 records and for my clock out query there are 6 records. When I combine the two Queries together i get the total of 12 records with a lot of duplicated entries on both sides.
First Query Code:
SELECT USERINFO.Badgenumber, USERINFO.name, DateValue([Time]) AS DateValue, TimeValue([Time]) AS TimeValue, acc_monitor_log.device_name, Dev.ClockINOut
FROM (Dev INNER JOIN acc_monitor_log ON Dev.DeviceName = acc_monitor_log.device_name) INNER JOIN USERINFO ON acc_monitor_log.pin = USERINFO.Badgenumber
WHERE (((USERINFO.name) Is Null Or (USERINFO.name) Like "*" & [Forms]![Query].[NameQ] & "*") AND ((DateValue([Time]))>=[Forms]![Query]![StartDateQ] And (DateValue([Time]))<=[Forms]![Query]![EndDateQ]) AND ((TimeValue([Time]))>=[Forms]![Query]![StartTimeQ] And (TimeValue([Time]))<=[Forms]![Query]![EndTimeQ]) AND ((Dev.ClockINOut)="Clock In"))
ORDER BY USERINFO.name, DateValue([Time]);
Second Query Code:
SELECT USERINFO.Badgenumber, USERINFO.name, DateValue([Time]) AS DateValue, TimeValue([Time]) AS TimeValue, acc_monitor_log.device_name, Dev.ClockINOut
FROM (Dev INNER JOIN acc_monitor_log ON Dev.DeviceName = acc_monitor_log.device_name) INNER JOIN USERINFO ON acc_monitor_log.pin = USERINFO.Badgenumber
WHERE (((USERINFO.name) Is Null Or (USERINFO.name) Like "*" & Forms!Query.NameQ & "*") And ((DateValue([Time]))>=Forms!Query!StartDateQ And (DateValue([Time]))<=Forms!Query!EndDateQ) And ((TimeValue([Time]))>=Forms!Query!StartTimeQ And (TimeValue([Time]))<=Forms!Query!EndTimeQ) And ((Dev.ClockINOut)="Clock Out"))
ORDER BY USERINFO.name, DateValue([Time]);
And the Combined Query of the two above queries:
SELECT RQOut.Badgenumber, RQOut.name, RQOut.DateValue, RQOut.TimeValue, RQOut.ClockINOut, RQIn.TimeValue, RQIn.device_name, RQIn.ClockINOut, [RQIn].[TimeValue]-[RQOut].[TimeValue] AS Timess
FROM RQOut INNER JOIN RQIn ON (RQOut.Badgenumber = RQIn.Badgenumber) AND (RQOut.name = RQIn.name) AND (RQOut.DateValue = RQIn.DateValue)
WHERE (((RQOut.name) Is Null Or (RQOut.name) Like "*" & [Forms]![Query].[NameQ] & "*") AND ((RQOut.DateValue)>=[Forms]![Query]![StartDateQ] And (RQOut.DateValue)<=[Forms]![Query]![EndDateQ]) AND ((RQOut.TimeValue)>=[Forms]![Query]![StartTimeQ] And (RQOut.TimeValue)<=[Forms]![Query]![EndTimeQ]))
ORDER BY RQOut.TimeValue, RQIn.TimeValue;
Please can anyone tell me why i am getting this. Thanks in advance.
INNER JOIN between two data sets returns records from both data sets where the fields match. If one record in the first data set matched 10 records (say) in the second dataset, it returns 10 records.
This is effectively the process the inner join is following in your last query: It will look at record 1 of RQin, then search for matching records in RQout. For the fields you asked to match up, ALL of the records in RQOut will be returned, because the fields you joined on match up for ALL the records. It then does the same for record 2 in RQIn, and again it matches with ALL the records on RQOut. This means you get 2 x 6 = 12 records returned.

How to return a potential NULL in SQL while performing a comparison on the potentially null value

Developing a query that will return information about an item stored across 4 tables. When all the fields have values my query works fine, however some of the data has null fields (which I can't change) that I need to perform my comparison on. When these show up, the row doesn't show up in the query results, even if all the other fields have values.
Here is what I have so far:
select [Item_NO], [Color_Name], [size_code], [style_code], [PERM_UNIT_PRICE]
FROM [USICOAL].[dbo].[ITEM], [USICOAL].[dbo].[COLOR], [USICOAL].[dbo].[SIZE], [USICOAL].[dbo].[STYLE]
where [ITEM_NO] in ('191202002944', '191202003026')
AND [USICOAL].[dbo].[ITEM].[COLOR_ID] = [USICOAL].[dbo].[COLOR].[COLOR_ID]
AND [USICOAL].[dbo].[ITEM].[SIZE_ID] = [USICOAL].[dbo].[SIZE].[SIZE_ID]
AND [USICOAL].[dbo].[ITEM].[STYLE_ID] = [USICOAL].[dbo].[STYLE].[STYLE_ID]
For these 2 items numbers, the Size_ID field is null. How can I get the results to reflect this null?
SELECT
[Item_NO]
,[Color_Name]
,[size_code]
,[style_code]
,[PERM_UNIT_PRICE]
FROM
[USICOAL].[dbo].[ITEM] i
LEFT OUTER JOIN
[USICOAL].[dbo].[COLOR] c
ON c.[COLOR_ID] = i.[COLOR_ID]
LEFT OUTER JOIN
[USICOAL].[dbo].[SIZE] s
ON s.[SIZE_ID] = i.[SIZE_ID]
LEFT OUTER JOIN
[USICOAL].[dbo].[STYLE] t
ON t.[STYLE_ID] = i.[STYLE_ID]
WHERE
[ITEM_NO] in ('191202002944', '191202003026')

Can nested query in JPQL access outer query

So I'm curious if a nested SELECT can reference it's outer SELECT in order to compare values. I haven't been able to test or see many examples on this topic.
As an example, I'm trying to write a query to select all Clothes rows that has a tag (some number) that is within a given list and has the highest time that is prior to given time (which is total number of seconds). The query in question is below:
SELECT c FROM Clothes c WHERE c.tag IN :tagList
AND (c.timeOfSale = (SELECT MAX(n.timeOfSale) FROM Clothes k
WHERE (c.tag = k.tag) AND (k.timeOfSale) < (:time))) GROUP BY c.tag
Is the comparison c.tag = k.tag valid? If not, is there an alternative?
#Query("SELECT b FROM Business b WHERE b <> :currentBusiness "
+ "and exists "
+ "(Select i from InterestMaster i, BusinessInterest bI where bI.interestMaster = i and bI.business = b"
+ "and i in (:userInterests))")
Page<Business> getCommunityBusiness(#Param("currentBusiness") Business currentBusiness, #Param("userInterests") List<InterestMaster> userInterests,Pageable pageable);
I am using the above JPQL and its working fine. So yes nested query can access outer query.
Yes. They're called correlated queries, where the subquery is evaluated for each row of outer query.

SQL Query - Distinct doesn't seem to filter

I'm utilizing four separate tables and i can't seem to figure out why my DISTINCT isn't filtering the results. I'm trying to get a single result for each acct.Name in this query. Regardless if i use DISTINCT or not, i get the exact same results.
Select DISTINCT
acct.Name,
inv.InvoiceNumber,
acct.AccountNumber,
addr.Line1,
addr.Line2,
addr.Line3,
addr.City,
addr.StateOrProvince,
addr.postalcode
FROM InvoiceBase inv, AccountBase acct
JOIN AccountExtensionBase base
ON base.AccountId = acct.AccountId
JOIN CustomerAddressBase addr
ON addr.ParentId = acct.AccountId
WHERE
inv.AccountId=acct.AccountId And
base.New_cocat_master = 1 And
base.New_CompanyId = 1 And
inv.StateCode = 0 And
inv.Name = '2013 ' + acct.AccountNumber
ORDER by acct.Name
The first result i get now has the first three values (acct.Name, inv.InvoiceNumnber, acct.AccountNumber) and the rest of the columns are blank. The second row has all of the columns with the information. I'm just trying to make the acct.Name to be DISTINCT
The rows are DISTINCT
This may be confusing when you are selecting multiple strings, since there might be hidden characters/spaces. Select the length of each one of those fields and compare the so called duplicate rows.
Turns out all i had to do was add in a simple clause in the WHERE, since the address is required for a valid invoice (where to send it):
WHERE
base.New_cocat_master = 1 And
base.New_CompanyId = 1 And
inv.StateCode = 0 And
inv.Name = '2013 ' + acct.AccountNumber And
addr.Line1 IS NOT NULL