Can sparql count the same statement of triples? - sparql

I want to count triples with the same statement , is that possible?
I have an turtle file of ontology , and i want to count the triples in apache jena fuseki server.
for an example , i have some triples like this :
### http://www.semanticweb.org/hp/ontologies/2022/11/Papua#pemerintah
:pemerintah rdf:type owl:NamedIndividual ,
:Topik ;
:melakukan :positif ;
:melalui :positif ;
:meluncurkan :positif ;
:membangun :positif ;
:menegaskan :positif ;
:mengalokasikan :positif ;
:mengedepankan :positif ;
:sejahterahkan :positif ;
:sejahterahkan :positif ;
:sejahterahkan :positif ;
:sejahterahkan :positif ;
:sejahterahkan :positif ;
:sejahterahkan :positif .
Can i count how many triples for :pemerintah :sejatherahkan :positif ?
when i count it , is the result is 6 or just 1?
And can you show me how to count that?

Related

Refactor querying hardcoded values in function

Does anybody have any suggestions on how to refactor below better? I've renamed the item names to simplify.
I've only inherited this (for an import process) and it does not look very efficient to me especially with the hardcoded values in the function. I am looking at putting the hardcoded values into a table and refer to it instead of the function, but I'm not sure how to make that work yet based on the repetitive columns in the query.
MyTable
MyTableId INT,
MyTableOtherColumn VARCHAR(100),
MyTableDesc1 VARCHAR(10),
MyTableDesc2 VARCHAR(10),
. . .
MyTable24 VARCHAR(10),
MyTable25 VARCHAR(10),
etc.
MyFunction
RETURNS #myFunctionTable TABLE
(
myFunctionDesc VARCHAR(256),
myFunctionCode VARCHAR(10)
)
AS
BEGIN
INSERT INTO #myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 1', 'MYCODE1');
INSERT INTO #myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 2', 'MYCODE2');
. . .
INSERT INTO #myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 99', 'MYCODE99');
INSERT INTO #myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 100', 'MYCODE100');
END
SELECT
MyTableId,
MyTableOtherColumn,
. . .
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc1),
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc2),
. . .
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc24),
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc25),
FROM myTable T
JOIN . . .
WHERE . . .
Just use conditional aggregation and outer apply:
SELECT MyTableId, MyTableOtherColumn,
. . .
mf.val1, mv.val2, . . .
FROM myTable T JOIN
. . . OUTER APPLY
(SELECT MAX(CASE WHEN MyFunctionDesc = MyTableDesc1 THEN myFunctionCode END) as val1,
MAX(CASE WHEN MyFunctionDesc = MyTableDesc2 THEN myFunctionCode END) as val2,
. . .
FROM MyFunction()
) mf
WHERE . . .

T-SQL use operator '&' like function SUM/COUNT/etc

Hi,
in SQL-Server (2014) i search too make a request like this:
SELECT
T.[Text],
&(T.[Numeric])
FROM
MyTable AS T
GROUP BY
T.[Text];
I would like use '&' and '|' like SUM()/MAX()/COUNT()/MIN() functions.
Somebody can help me ?
EDIT:
Need too:
SELECT
T.[Text],
|(T.[Numeric])
FROM
MyTable AS T
GROUP BY
T.[Text];
EDIT 2:
CREATE TABLE [dbo].[1L_Tests](
[ID_Test] [int] NOT NULL,
[Text] [varchar](5) NOT NULL,
[Numeric] [int] NOT NULL,
PRIMARY KEY ([ID_Test])
);
GO
INSERT INTO [dbo].[1L_Tests]
([ID_Test], [Text], [Numeric])
VALUES
(1, 'KCH', 0)
,(2, 'KCH', 12)
,(3, 'KCH', 13)
,(4, 'DAF', 9)
,(5, 'DAF', 7)
,(6, 'LDE', 29)
,(7, 'LDE', 37)
,(8, 'LDE', 46);
GO
SELECT
T.[Text],
&(T.[Numeric]) AS 'Inter',
|(T.[Numeric]) AS 'Union'
FROM
[1L_Tests] AS T
GROUP BY
T.[Text];
I Expected:
Text | Inter | Union
KCH ; 0 ; 13
DAF ; 1 ; 15
LDE ; 4 ; 63
SQL Server doesn't have a bitwise aggregate AND. If you know how many bits you want, you can do it bit-by-bit:
select . . .,
min(t.number & 1) | min(t.number & 2) | min(t.number & 4) | . . .
from . . .
Bitwise operations often suggest premature optimization in a database. It is usually better to represent the "bits" as separate flags.

create table with system date in Vertica

I am looking to create table from a table something like:
CREATE TABLE as archive.POSTPAID_GSMIS_`date +%Y%m%d%H%M%S`
(
select * from
POSTPAID.STAGE_GS10);
commit;
Wondering if I can even do it in Vertica?
I thought of storing value in a variable like:
\set x 'select now();'
create table :x (int a);
But \echo :x gives me select now();.
see the code bellow
dbadmin=> \set date `date +%Y%m%d%H%M%S`
dbadmin=> \echo :date
20170815112242
CREATE TABLE dba.POSTPAID_GSMIS_:date
as
select '1234' as id from dual
;
CREATE TABLE
dbadmin=> select * from dba.POSTPAID_GSMIS_:date;
id
------
1234
(1 row)
is this what you are looking for ?
Append Unix timestamp to a tablename:
dbadmin=> \set env `date +%s`
dbadmin=> \echo :env
1502843933
dbadmin=> create table dba.tbl_:env (id int);
CREATE TABLE
dbadmin=> select * from dba.tbl_1502843933;
id
----
(0 rows)
Also you can build you variable and use it on table create
dbadmin=> \set var `var="blabla" && echo $var`
dbadmin=> \echo :var
blabla
dbadmin=> create table dba.tbl_:var (id int);
CREATE TABLE
dbadmin=> select * from dba.tbl_blabla;
id
----
(0 rows)

Update table using random of multiple values in other table

Consider this data:
CREATE TABLE #Data (DataID INT, Code VARCHAR(2), Prefix VARCHAR(3))
INSERT INTO #Data (DataID, Code)
VALUES (1, 'AA')
, (2, 'AA')
, (3, 'AA')
, (4, 'AA')
, (5, 'AA')
, (6, 'AA')
CREATE TABLE #Prefix (Code VARCHAR(2), Prefix VARCHAR(3))
INSERT INTO #Prefix (Code, Prefix)
VALUES ('AA', 'ABC')
, ('AA', 'DEF')
, ('AA', 'GHI')
, ('AA', 'JKL')
I want to set the Prefix value in #Data to be a random Prefix from #Prefix with a matching Code.
Using a straight inner join just results in one value being used:
UPDATE D
SET Prefix = P.Prefix
FROM #Data AS D
INNER JOIN #Prefix AS P ON D.Code = P.Code
From reading other questions on here, NEWID() is recommended as a way of randomly ordering something. Changing the join to:
SELECT TOP 1 subquery ordering by NEWID()
still only selects a single value (though random each time) for every row:
UPDATE D
SET Prefix = (SELECT TOP 1 P.Prefix FROM #Prefix AS P WHERE P.Code = D.Code ORDER BY NEWID())
FROM #Data AS D
So, I'm not sure how to get a random prefix for each data entry from a single update statement. I could probably do some kind of loop through the #Data table, but I avoid ever touching loops in SQL and I'm sure that would be slow. The actual application of this will be on tens of thousands of records, with hundreds of prefixes for dozens of codes.
This is how to do it:
UPDATE d SET Prefix = ca.Prefix
FROM #Data d
CROSS APPLY(SELECT TOP 1 Prefix
FROM #Prefix p
WHERE d.DataID = d.DataID AND p.Code = d.Code ORDER BY NEWID()) ca
Notice d.DataID = d.DataID. This is here to force Sql Server engine to reevaluate subquery for each row in #Data table.

Conditional statements in SQL

Is it possible to insert duplicate values into a database row only if a different column in the same table is unique? Thanks for your help.
database table setup:
login_time int(15) [unique key] | user_id int(15) | time_worked varchar(64)
This is my insert statment.
What i need is to only insert if user_id or login_time is unique.
And to not insert if both user_id and login_time match another row in the database.
INSERT INTO `ucm_user_timelog` VALUES (' . intval($user_id) .
', ' . $timeWorked . ', ' . $logInTime . ')