get customer count using service builder finder - hsqldb

I have created a customer table using service builder like as shown below
_ _ _ _ _ _ _ _ _ _ _
| Id | CusId | CusName |
|_ _ |_ _ _ _| _ _ _ _ |
| 11 | 215 | John |
|_ _ |_ _ _ _| _ _ _ _ |
| 12 | 206 | Lino |
|_ _ |_ _ _ _| _ _ _ _ |
| 13 | 215 | John |
|_ _ |_ _ _ _| _ _ _ _ |
| 14 | 215 | John |
|_ _ |_ _ _ _| _ _ _ _ |
| 15 | 206 | Lino |
|_ _ |_ _ _ _| _ _ _ _ |
| 16 | 207 | Nino |
|_ _ |_ _ _ _| _ _ _ _ |
My service.xml is given below
service.xml
<entity name="Customer" local-service="true" remote-service="false" json-enabled="true">
<column name="Id" type="int" primary="true"/>
<column name="CusId" type="int" />
<column name="CusName" type="String" />
<finder name="CustomerCount" return-type="Collection">
<finder-column name="CusId"/>
</finder>
</entity>
Can anyone please tell me how to generate the below output using finder
_ _ _ _ _ _ _ _
| CusId | Count |
|_ _ _ _|_ _ _ _|
| 215 | 3 |
|_ _ _ _|_ _ _ _|
| 206 | 2 |
|_ _ _ _|_ _ _ _|
| 207 | 1 |
|_ _ _ _|_ _ _ _|
I am using Liferay 6.2 and database as HSQLDB

The SQL query to generate the output is:
SELECT "CusId", COUNT("CusId") FROM "CustomerCount" GROUP BY "CusId"
This assumes the table and column names are double-quoted in the database which makes them case-sensitive.

Related

How do I write an asynchronous test for Twisted-based code using coroutines, pytest, and hypothesis?

I would like to write some new tests for a Twisted-based application which itself uses async def to define coroutines for asynchronous operations and I would like to use async def to define the tests themselves as coroutines too.
The application already has a large pytest-based test suite so I would like to continue to use pytest.
I would also like to use Hypothesis to write these new tests as property-based tests.
Here's an example of the general shape that I imagine such a test would have:
from hypothesis import given
from hypothesis.strategies import integers
from twisted.internet.defer import succeed
from pytest_twisted import ensureDeferred
#given(integers())
#ensureDeferred
async def test_foo(x):
await succeed(None)
assert isinstance(x, int)
However, this version fails because Hypothesis doesn't know what to do with the coroutine:
#given(integers())
> #ensureDeferred
E hypothesis.errors.InvalidArgument: Hypothesis doesn't know how to run async test functions like test_foo. You'll need to write a custom executor, or use a library like pytest-asyncio or pytest-trio which can handle the translation for you.
E See https://hypothesis.readthedocs.io/en/latest/details.html#custom-function-execution
../../../Scratch/test_async.py:8: InvalidArgument
The linked documentation suggests doing something like this to teach Hypothesis how to run the test:
from pytest_twisted import _run_inline_callbacks, _async_pytest_pyfunc_call
# Copied from pytest_twisted and modified to support *args
def pytest_pyfunc_call(pyfuncitem, *args):
_run_inline_callbacks(_async_pytest_pyfunc_call, pyfuncitem, *args)
return not None
test_foo.hypothesis.inner_test = lambda x: pytest_pyfunc_call(test_foo, x)
This results in a pytest_twisted error:
#given(integers())
> #ensureDeferred
../../../Scratch/test_async.py:8:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../Scratch/test_async.py:20: in <lambda>
test_foo.hypothesis.inner_test = lambda x: pytest_pyfunc_call(test_foo, x)
../../../Scratch/test_async.py:17: in pytest_pyfunc_call
_run_inline_callbacks(_async_pytest_pyfunc_call, pyfuncitem, *args)
.tox/py39/lib/python3.9/site-packages/pytest_twisted.py:356: in _run_inline_callbacks
blockon_default(d)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
d = <Deferred at 0x7f60da488d60>
def blockon_default(d):
current = greenlet.getcurrent()
> assert (
current is not _instances.gr_twisted
), "blockon cannot be called from the twisted greenlet"
E AssertionError: blockon cannot be called from the twisted greenlet
E assert <greenlet.greenlet object at 0x7f60dd5bc250 (otid=0x7f60e239af40) current active started> is not <greenlet.greenlet object at 0x7f60dd5bc250 (otid=0x7f60e239af40) current active started>
E + where <greenlet.greenlet object at 0x7f60dd5bc250 (otid=0x7f60e239af40) current active started> = _instances.gr_twisted
E Falsifying example: <lambda>(
E x=0,
E )
.tox/py39/lib/python3.9/site-packages/pytest_twisted.py:82: AssertionError

SQL in VBA right join with no data from first table

I try to join data from multiple workbooks and use it in current workbook instead of VLOOKUP function. So I do not want return key column, just those that match criteria in key column in current workbook.
I got "Syntax error in FROM clause."
Everything works fine without "RIGHT JOIN" part. I use ADO.
"SELECT t1.number " & _
"FROM" & _
"(SELECT * FROM [Sheet1$] " & _
"IN '" & ThisWorkbook.Path & "\Src1.xlsm' " & _
"[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=YES;'] " & _
"UNION ALL " & _
"SELECT * FROM [Sheet1$] " & _
"IN '" & ThisWorkbook.Path & "\Src2.xlsb' " & _
"[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=YES;']" & _
"UNION ALL " & _
"SELECT * FROM [Sheet1$] " & _
"IN '" & ThisWorkbook.Path & "\Src2.xlsb' " & _
"[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=YES;']) t1" & _
"RIGHT JOIN [Sheet1$] " & _
"IN '" & ThisWorkbook.FullName & "' " & _
"[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=YES;'] t2 ON t2.key = t1.key;"
Data looks like
ThisWorkbook.Fullname:
key | someColumns | number
k1 | somedata |
k3 | somedata |
k5 | somedata |
\Src1.xlsm (also Src2):
key | number
k1 | 15
k2 | 11
k3 | 8
k4 | 16
k5 | 7
Likely result in Thisworkbook.fullname
key | someColumns | number
k1 | somedata | 15
k3 | somedata | 8
k5 | somedata | 7
Try
Dim Ws As Worksheet
Dim Rs As Object
Sub getRs(strSQL As String)
Dim strConn As String
Dim i As Integer
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=Excel 12.0;"
Set Rs = CreateObject("ADODB.Recordset")
Rs.Open strSQL, strConn
End Sub
Sub test()
Dim strQuery As String
strQuery = "SELECT t1.number " & _
"FROM [Sheet1$] as t2 LEFT JOIN " & _
"(SELECT * FROM [Sheet1$] " & _
"IN '" & ThisWorkbook.Path & "\Src1.xlsm' " & _
"[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=YES;'] " & _
"UNION ALL " & _
"SELECT * FROM [Sheet1$] " & _
"IN '" & ThisWorkbook.Path & "\Src2.xlsb' " & _
"[Excel 12.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=YES;']) as t1 " & _
"ON t1.key = t2.key Where not isnull(t2.key) "
getRs strQuery
Range("c2").CopyFromRecordset Rs
Rs.Close
Set Rs = Nothing
End Sub

Text to Columns delimitter

I'm trying to do Text to Columns with the OtherChar not just limited to one. Currently my code looks like this:
With Selection
.TextToColumns Destination:=[A2], _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=True, _
Space:=True, _
Other:=True, _
OtherChar:="-", _
TrailingMinusNumbers:=True
End With
But I want the OtherChar to include these characters "(, ), [, ], /, *". Is there a way to do that?
Quick and dirty code. Do not use * as delimiter as it will be treated as all in replace
Dim aDelim
Dim delim
Dim oRange As Range
aDelim = Split("(,),[,],/", ",")
Set oRange = Selection
For Each delim In aDelim
oRange.Replace What:=delim, Replacement:="-"
Debug.Print oRange.Text, delim
Next
oRange.TextToColumns Destination:=[A2], _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=True, _
Space:=True, _
Other:=True, _
OtherChar:="-", _
TrailingMinusNumbers:=True

Assign column name of max value to row based on comparison across columns

OID_ _ HOS1 _ _ HOS2 _ _ HOS3
A _ _ _ _ 3 _ _ _ _ _7 _ _ _ _10
B _ _ _ _ 12 _ _ _ _ 5 _ _ _ _ 8
C _ _ _ _ 3 _ _ _ _ 13_ _ _ _ 7
Consider this table of hospital visit in a given region (OID). What I would like to do is in sql automatically determine the last two columns of the table above. I want to compare all of the hospitals visits (HOS1, HOS2, HOS3) for each region (A, B, C) and for each region assign the highest number of visits to a MAXV column and the code of the hospital with the highest visits to a column MAXH so that it looks like the table below.
OID_ _ HOS1 _ _ HOS2 _ _ HOS3 _ _ MAXV _ _ MAXH
A _ _ _ _ 3 _ _ _ _ _ 7_ _ _ _ _ 10 _ _ _ 10 _ _ _ HOS3
B _ _ _ _ 12 _ _ _ _ 5 _ _ _ _ _ 8 _ _ _ _12 _ _ _ HOS1
C _ _ _ _ 3 _ _ _ _ 13 _ _ _ _ _ 7 _ _ _ _13 _ _ _ HOS2
For SQL Server 2005+:
SELECT T.*, C.ColValue MAXV, C.ColName MAXH
FROM YourTable T
OUTER APPLY (SELECT TOP 1 *
FROM (SELECT 'HOS1', HOS1
UNION ALL
SELECT 'HOS2', HOS2
UNION ALL
SELECT 'HOS3', HOS3) X(ColName,ColValue)
ORDER BY ColValue DESC) C
Here is a demo of this.
And here are the results:
╔═════╦══════╦══════╦══════╦══════╦══════╗
║ OID ║ HOS1 ║ HOS2 ║ HOS3 ║ MAXV ║ MAXH ║
╠═════╬══════╬══════╬══════╬══════╬══════╣
║ A ║ 3 ║ 7 ║ 10 ║ 10 ║ HOS3 ║
║ B ║ 12 ║ 5 ║ 8 ║ 12 ║ HOS1 ║
║ C ║ 3 ║ 13 ║ 7 ║ 13 ║ HOS2 ║
╚═════╩══════╩══════╩══════╩══════╩══════╝
select
t.[OID], t.[HOS1], t.[HOS2], t.[HOS3],
c.MAXV, c.MAXH
from Table1 as t
outer apply (
select top 1 *
from (values
(HOS1, 'HOS1'),
(HOS2, 'HOS2'),
(HOS3, 'HOS3')
) as a(MAXV, MAXH)
order by a.MAXH desc
) as C
sql fiddle demo

Left Join works with table but fails with query

The following left join query in MS Access 2007
SELECT
Table1.Field_A,
Table1.Field_B,
qry_Table2_Combined.Field_A,
qry_Table2_Combined.Field_B,
qry_Table2_Combined.Combined_Field
FROM Table1
LEFT JOIN qry_Table2_Combined
ON (Table1.Field_A = qry_Table2_Combined.Field_A)
AND (Table1.Field_B = qry_Table2_Combined.Field_B);
is expected by me to return this result:
+--------+---------+---------+---------+----------------+
|Field_A | Field_B | Field_A | Field_B | Combined_Field |
+--------+---------+---------+---------+----------------+
|1 | | | | |
+--------+---------+---------+---------+----------------+
|1 | | | | |
+--------+---------+---------+---------+----------------+
|2 |1 |2 |1 |John, Doe |
+--------+---------+---------+---------+----------------+
|2 |2 | | | |
+--------+---------+---------+---------+----------------+
[Table1] has 4 records, [qry_Table2_Combined] has 1 record.
But it gives me this:
+--------+---------+---------+---------+----------------+
|Field_A | Field_B | Field_A | Field_B | Combined_Field |
+--------+---------+---------+---------+----------------+
|2 |1 |2 |1 |John, Doe |
+--------+---------+---------+---------+----------------+
|2 |2 |2 | |, |
+--------+---------+---------+---------+----------------+
Really weird is that the [Combined_Field] has a comma in the second row. I use a comma to concatenate two fields in [qry_Table2_Combined].
If the left join query uses a table created from the query [qry_Table2_Combined] it works as expected.
Why does this left join query not give the same result for a query and a table? And how can i get the right results using a query in the left join?
Looking at you logic, it seems that you only want combined fields where field_A = "2" (SELECT '2' AS Field_A). I suspect that this is causing the problem. Would it be possible to go about a solution in a different way, for example:
SELECT
t1.Field_A,
t1.Field_B,
t2.Field_B As t2B,
[t2].[Col_1] & ", " & [t2].[Col_2] AS Combined
FROM t1 LEFT JOIN t2
ON t1.Field_B = t2.Field_B
WHERE t1.Field_A="2"
UNION ALL
SELECT
t1.Field_A,
t1.Field_B,
"None" As t2B,
"None" AS Combined
FROM t1
WHERE t1.Field_A<>"2"
Concatenation: change the & operators to + operators and the result should be as expected.
Missing rows: I can reproduce this issue but cannot explain it, other than to say a) it's probably a bug and b) it will probably never get fixed :(
For sanity I tested the same code in SQL Server and it works as expected.
As a general point an outer join can be simulated using union and padding the missing values e.g. pseudo code:
( A JOIN B )
UNION
( A NOT MATCH B { A.*, <pad values for B> } )
In your case and in Access SQL:
SELECT Table1.Field_A, Table1.Field_B,
qry_Table2_Combined.Field_A,
qry_Table2_Combined.Field_B,
qry_Table2_Combined.Combined_Field
FROM Table1
INNER JOIN qry_Table2_Combined
ON (Table1.Field_A = qry_Table2_Combined.Field_A)
AND (Table1.Field_B = qry_Table2_Combined.Field_B)
UNION ALL
SELECT Table1.Field_A, Table1.Field_B,
NULL AS Field_A,
NULL AS Field_B,
NULL AS Combined_Field
FROM Table1
WHERE NOT EXISTS ( SELECT *
FROM qry_Table2_Combined
WHERE (Table1.Field_A = qry_Table2_Combined.Field_A)
AND (Table1.Field_B = qry_Table2_Combined.Field_B) );
The above seems to produce the results you were expecting.
Access repro code, with concatenation fix, uncomment code for suggested workaround:
Sub EXfewfTempler()
On Error Resume Next
Kill Environ$("temp") & "\DropMe.mdb"
On Error GoTo 0
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
Environ$("temp") & "\DropMe.mdb"
With .ActiveConnection
Dim Sql As String
Sql = "CREATE TABLE Table1 ( Field_A VARCHAR(10), Field_B VARCHAR(10) );"
.Execute Sql
Sql = "CREATE TABLE Table2 ( Field_B VARCHAR(10), Col_1 VARCHAR(10), Col_2 VARCHAR(10));"
.Execute Sql
Sql = "CREATE VIEW qry_Table2_Combined AS SELECT '2' AS Field_A, Table2.Field_B, Table2.Col_1 + ', ' + Table2.Col_2 AS Combined_Field FROM Table2; "
.Execute Sql
Sql = "INSERT INTO Table1 VALUES (1, NULL);"
.Execute Sql
Sql = "INSERT INTO Table1 VALUES (1, NULL);"
.Execute Sql
Sql = "INSERT INTO Table1 VALUES (2, 1);"
.Execute Sql
Sql = "INSERT INTO Table1 VALUES (2, 2);"
.Execute Sql
Sql = "INSERT INTO Table2 VALUES (1, 'John', 'Doe');"
.Execute Sql
Sql = _
"SELECT " & _
"Table1.Field_A, " & _
"Table1.Field_B, " & _
"qry_Table2_Combined.Field_A, " & _
"qry_Table2_Combined.Field_B, " & _
"qry_Table2_Combined.Combined_Field " & _
"FROM Table1 " & _
"LEFT JOIN qry_Table2_Combined " & _
" ON (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
"AND (Table1.Field_B = qry_Table2_Combined.Field_B);"
' Sql = _
' "SELECT Table1.Field_A, Table1.Field_B, " & _
' " qry_Table2_Combined.Field_A, " & _
' " qry_Table2_Combined.Field_B, " & _
' " qry_Table2_Combined.Combined_Field " & _
' " FROM Table1 " & _
' " INNER JOIN qry_Table2_Combined " & _
' " ON (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
' " AND (Table1.Field_B = qry_Table2_Combined.Field_B) " & _
' "UNION ALL " & _
' "SELECT Table1.Field_A, Table1.Field_B, " & _
' " NULL AS Field_A, " & _
' " NULL AS Field_B, " & _
' " NULL AS Combined_Field " & _
' " FROM Table1 " & _
' " WHERE NOT EXISTS ( SELECT * " & _
' " FROM qry_Table2_Combined " & _
' " WHERE (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
' " AND (Table1.Field_B = qry_Table2_Combined.Field_B) );"
Dim rs
Set rs = .Execute(Sql)
MsgBox rs.GetString(2, , vbTab & vbTab, , "<NULL>")
End With
Set .ActiveConnection = Nothing
End With
End Sub
isn't this a problem with MSAccess parsing. for a test change the field names in the query to Field_C and Field_D and see if you still have the same problem