Apache Jena Aggregate multiple expressions - sparql

For the following data.ttl file:
A SAVINGS_1 200
A SAVINGS_2 300
A SAVINGS_3 370
A EMAIL emailidone
B SAVINGS_1 400
B SAVINGS_2 300
B SAVINGS_3 100
B EMAIL emailidtwo
C SAVINGS_1 200
C SAVINGS_2 600
The query:
SELECT ?NAME ?SAVINGS_1_VALUE ?SAVINGS_2_VALUE ?SAVINGS_3_VALUE WHERE {
?NAME SAVINGS_1 ?SAVINGS_1_VALUE .
?NAME SAVINGS_2 ?SAVINGS_2_VALUE .
?NAME SAVINGS_3 ?SAVINGS_3_VALUE .
?NAME EMAIL ?email . }
Gives Output:
NAME SAVINGS_1_VALUE SAVINGS_2_VALUE SAVINGS_3_VALUE
A 200 300 370
B 400 300 100
Is there a way to calculate the MAXIMUM of SAVINGS_1, SAVINGS_2 and SAVINGS_3 for individuals A, B and C who also has EMAIL using JENA ARQ Processor?
The expected query output would be
Expected Query output: NAME SAVINGS_1_VALUE SAVINGS_2_VALUE SAVINGS_3_VALUE MAXIMUM
A 200 300 370 370
B 400 300 100 400
MAX() function takes in only one argument, it can calculate the maximum of ?SAVINGS_1_VALUE or maximum of ?SAVINGS_2_VALUE and so on. Is there a way to evaluate maximum of ?SAVINGS_1_VALUE, ?SAVINGS_2_VALUE, ?SAVINGS_3_VALUE projections and add the result as another column? If the existing Jena Processor is not able to achieve this then what could be possible extension points to achieve this? Custom Aggregators also seem to evaluate on individual variable expression.
Note: There are multiples of variable expressions on which I want to evaluate this! So bind (if ... Is not an option for me to achieve this!

Related

Search and match indexes in two different columns, return the sum of a third column - Postgresql

I have a table called "tax_info", this table stores the information of the land tax of my city, it goes like this:
taxpayer_code | condominium_num | lot_area | built_area
-------------------------------------------------------------
0010030078-2 | 00-0 | 143 | 130
0010030079-1 | 02-7 | 283 | 57
0010030080-1 | 02-7 | 283 | 48
0010030081-1 | 02-7 | 283 | 50
the taxpayer code first 3 numbers refer to the city district, the next 3 to the block within the district, and the next 4 can refer to the lot in a block if the condo number is 00-0, or to an apartment, or store, etc if the condo number is different than 00-0, in which case all equal condo numbers refer to the same lot within the block.
what I want to do is pass a list of "taxpayer_code" and get the "lot_area" and "built_area" for the lots. the problem is, if the person lives in a condo, her apartment is a fraction of the total built area for the lot. So, if I search for code 0010030078% (the -X number doesn't matter) the result is:
Lot Area = 143 and Built Area = 130
But if I search for 0010030080%, the result I expect is:
Lot Area = 283 and Built Area 155
And if I search for 0010030078%, 0010030079%, the result:
Lot Area = 426 and Built Area 285
So the database should get the taxpayer codes, then look if the condominium number is different from 00-0 for each code passed, if so, it should add to the sum all the other taxpayer codes that share the same condo number within the same district and block. (ideally, if tax codes belonging to different districts or blocks are passed a warning should be returned, and if more tax codes are added to the sum, a listing with all codes added would be nice, but it's okay if that's too much of a hassle!).
I am new to SQL and can't wrap my head around this, I appreciate every help you can give me, thanks!
Hmmm . . . use a subquery and window functions to add up the values that you want:
select ti.*
from (select ti.*,
(case when condominium_num <> '00-0'
then sum(built_area) over (partition by condominium_num)
else built_area
end) as real_built_area
from tax_info ti
) ti
where . . .

Access SQL Summarize Data

I can get the output that I want from the data my query provides in Excel, but it would be nice to be able to do it in Access directly.
I return a table like this:
Date kName NumKits ActKits dName NumDoses ActDoses
---------------------------------------------------------------------
11/03/2018 AA 5 1000 BB 12 3400
12/03/2018 CC 7 1100 AA 10 4120
etc..
Any name could appear in either column, maybe both columns at the same time. The criteria for the data as provided is a date range.
I want this information summarized, as follows:
(Sum) (Sum) (Count) (Sum) (Sum) (Count)
Name KitMade KitActivity KitSession DosesMade DoseActivity DoseSession
----------------------------------------------------------------------------
AA 26 42015 3 40 35420 4
BB (etc...)
retaining the date filter.
My initial thought is to do the query twice, separating out Kits and Doses then combining them back together; but I'm sure that there must be a simpler way?
Using Access 2016, data has all been changed from actual information.
You seem to want something like this:
select name, sum(numkits) as numkits, sum(actkits) as actkits, sum(kitcnt) as numkits,
sum(numdoses) as numdoses, sum(actdoses) as actdoses, sum(dosecnt) as dosecnt
from (select kname as name, 1 as kitcnt, numkits, actkits, 0 as dosecnt, 0 as numdoses, 0 as actdoses
from t
union all
select kname, 0 as kitcnt, 0 as numkits, 0 as actkits, 1 as dosecnt, numdoses, actdoses
from t
) as t
group by name;
I think some older versions of MS Access do not support union all in the FROM clause. If that is a problem, you can get around it by creating a view.

The multi-part identifier could not be bound sql where clause

I am trying to multiply two tables using the code below to get a new calculated column. I am using the SQL Server Express Edition and SQL Server Management Studio.
select
[Yield_unpiv].[SubPrecinct],
[Yield_unpiv].[LandUse],
[Yield_unpiv].[Yield] * [LU_Rules_Final].[AM_GenRateFinal] * [LU_Rules_Final].[AM_In_factor] / [LU_Rules_Final].[UnitValue] as AM_In
from
Yield_unpiv, LU_Rules_Final
where
[Yield_unpiv].[LandUse]=[LU_Rules_Final].[LandUse]
However, I get the following error
The multi-part identifier "Yield_unpiv.LandUse" could not be bound.
The Yield_unpiv is set up like this:
SubPrecinct | Yield | LandUse
P1 Unique #s LDResi
P1 MDResi
. .
. .
. .
P2 LDResi
P2 MDResi
. .
. .
. .
And the LU_Rules_Final is set up like this:
LandUse | UnitValue | AM_GenRateFinal | AM_In_factor
LDResi 1 2.5 0.5
BulkyGoods 100 7 0.7
MDREsi 1 0.52 0.1
.
.
.
Not sure why I am getting this multi-part identifier "Yield_unpiv.LandUse" could not be bound error.
Please help!
I don't know the exact cause of your error, but it likely has to do with that the column LandUse appears in both tables. I speculate that refactoring it to use explicit joins would fix the problem:
SELECT
t1.[SubPrecinct],
t1.[LandUse],
t1.[Yield] * t2.[AM_GenRateFinal] * t2.[AM_In_factor] / t2.[UnitValue] AS AM_In
FROM Yield_unpiv t1
INNER JOIN LU_Rules_Final t2
OM t1.[LandUse] = t2.[LandUse];
If this fixes your problem, then it means one more reason not to use the archaic pre ANSI-92 style of SQL joins. Always use explicit joins whenever possible.

Sql query to add comparison columns dynamically

My table looks like
Fields Jack Mike Bruce ... Tony
Salary 150 300 125 ... 150
CTC 100 100 250 ... 500
Here Jack is the base user and I need to compare the salary and CTC of Mike , Bruce ,Tony upto n columnof users in the table and add comparison rating columns such as the output looks like,
Fields Jack Mike Mike_rating Bruce Bruce_rating ... Tony
Salary 150 300 high 125 low ... 150
CTC 100 100 equal 250 high ... 500
Output Explanation
The user list grows dynamically and corresponding rating column needed to be added.
Jack Salary is 150 and Mike is 300 . So Mike_Rating column should have value as high else low else if two values are equal then equal
Any Help would be appreciated . Thank you
Create a trigger on table user. Trigger is used exactly for things like this. So, for example, if u create a trigger on table Users to be activated every time after insert, then u can get the inserted data, and using it you can make changes to your table. You would also probably want to create triggers for delete and update operations on User table. You can find details on how to create a trigger here

Difference operation on Tables

DB - Oracle and MSSQL
How do I get A difference B ?
A is what I need - but if a similar record exists in B , don't show it .
Similar here means 2 columns are same . [This makes it unique btw]
All that I know about A and B is that there is a 2 column combination is UNIQUE record .
Say Country and City in the below example . It might have entirely different columns but one record per Country and City on which I need to do my diffence operation .
I have 2 tables , one that says here is the country and the city I am interested in .
A
Country City Population Dentiy
USA NewYork 10 mil
USA Nevada 1 mil
Usa Penn 3 mil
Another table that says "I have a list of surveyed Cities by some of our guys maybe if you have the data in this table you don't do anything , we will get it for you" .
B
Usa NewYork
SA Capetown
I want A difference B , i.e things that are there in A but not there B . And A might have other columns too ... If a Country City in A is there in B , I don't want that record .
How do I achieve this ?
PS : The table A and B might be dynamically computer one's ! Using subquery , so I might use with A as and with B as ...
*A is a dynamically generated table , same with B * so I need to use suitable aliasings . The answer should assume this pre-condition .
A minus B:
SELECT
a.*
FROM a
LEFT OUTER JOIN b ON (a.key = b.key)
WHERE b.key IS NULL
or (MSSQL):
SELECT col1,col2,col3 FROM a
EXCEPT
SELECT col1,col2,col3 FROM b