I have this query
SELECT ?s WHERE {?a <http://xmlns.com/foaf/0.1/topic_interest> ?s}
which returns
aaa
aaa
aaa
bbb
bbb
ccc
However, I want to display it as
aaa | 3
bbb | 2
ccc | 1
I am using dotnetrdf. This is what i tried
SELECT (COUNT(*) AS ?s) WHERE {?a <http://xmlns.com/foaf/0.1/topic_interest> ?s}
and this just gives me the number of rows there are which is 3080.
Can you tell me how to make it right?
Thanks
This is because COUNT(*) simply counts result rows for each group
If there is no GROUP BY clause in your query then there is one implicit group of all results hence you just get the number of rows.
If you add a GROUP BY to your query like the following example you should get the result you want:
SELECT (COUNT(*) AS ?count)
WHERE
{
?a <http://xmlns.com/foaf/0.1/topic_interest> ?s}
} GROUP BY ?s
Related
table1:
code
category
ABC
ZMLND_XY_ABC
table2:
category
label
MLND
3
ZMLND
4
I'd like to map table1's category to table2's label if the part of table1's category separated by _ equals table2's category. For example, ZMLND_XY_ABC in table1 matches ZMLND in table2 while ZMLND_XY_ABC and MLND does not.
Query:
select * from table1 left join table2 on '_'||table1.category||'_' like '%_'||table2.category||'_%'
Result:
code | category | category_1 | label
ABC | ZMLND_XY_ABC | MLND | 3 <- _MLND_ should not match
_ZMLND_XY_ABC_
ABC | ZMLND_XY_ABC | ZMLND | 4
However, I replace _ by ,:
table1:
code
category
ABC
ZMLND,XY,ABC
select * from table1 left join table2 on ','||table1.category||',' like '%,'||table2.category||',%'
Result:
code
category
category_1
label
ABC
ZMLND,XY,ABC
ZMLND
4
Why are there different results?
Because of SQLITE like the _ is as special a character as the %.
As you seem to know, the % matches multiple characteres.
What you seem to have missed is that the _ matches any single character.
Compare e.g. https://www.sqlitetutorial.net/sqlite-like/
Have a look at https://www.sqlite.org/lang_expr.html concerning the ESCAPE optional syntax part, which probably can help you to write your query.
I am trying to optimize an sqlite query (PHP-PDO) by having one query run instead of many queries on the same data. I have a table with a simple structure:
word
-----
aaaa
bb
cccc
dddd
ffff
now, I have a list of words aaaa, gg, ffff. For each of them, if the item exists in the table, I return 1 or else 0.
Is it possible to make this with a single query which with the aforementioned data, would return something like:
aaaa 1
gg 0
ffff 1
Thank you in advance.
You can do it with EXISTS:
WITH cte(word) AS (VALUES ('aaaa'), ('gg'), ('ffff'))
SELECT c.word,
EXISTS (SELECT 1 FROM tablename t WHERE t.word = c.word) exists_in_the_table
FROM cte c
or with a LEFT join:
WITH cte(word) AS (VALUES ('aaaa'), ('gg'), ('ffff'))
SELECT DISTINCT c.word, t.word IS NOT NULL exists_in_the_table
FROM cte c LEFT JOIN tablename t
ON t.word = c.word
If there are not duplicate words in the table you can remove DISTINCT.
See the demo.
Results:
> word | exists_in_the_table
> :--- | ------------------:
> aaaa | 1
> gg | 0
> ffff | 1
The NTH function is really useful for extracting nested array elements in BQ, but its utility for a given table depends on each row's nested array containing the same amount of elements, and in the same order. If I have a 2+ column nested array where one column is variable name/ID, and the different instances of the array in different rows have inconsistent naming and/or ordering, is there an elegant way to fetch/pivot a variable based on the variable name/ID?
For example, if row1 has customDimensions array:
index value
4 aaa
23 bbb
70 ccc
and row2 has customDimensions array:
index value
4 ddd
70 eee
I'd want to run something like
SELECT
NTHLOOKUP(70, customdims.index, customdims.value) as val70,
NTHLOOKUP(4, customdims.index, customdims.value) as val4,
NTHLOOKUP(23, customdims.index, customdims.value) as val23
from my_table;
And get:
val70 val4 val23
ccc aaa bbb
eee ddd (null)
I've been able to get this sort of result by making a subquery for each desired index value, unnesting the array in each and filtering WHERE index = (value), but that gets really ugly as the variables pile up. Is there an alternative?
EDIT: Based on Mikhail's answer below (thank you!!) I was able to write my query more elegantly. Not quite as slick as an NTHLOOKUP, but I'll take it:
select id,
max(case when index = 41 then value[OFFSET(0)] else '' end) as val41,
max(case when index = 59 then value[OFFSET(0)] else '' end) as val59
from
(select
concat(array1.thing1, array1.thing2) as id,
cd.index,
ARRAY_AGG(distinct cd.value) as value
FROM my_table g
,unnest(array1) as array1
,unnest(array1.customDimensions) as cd
where index in (41,59)
group by 1,2
order by 1,2
) x
group by 1
order by 1
The best I can "offer" is below (BigQuery Standard SQL)
#standardSQL
WITH `project.dataset.my_table` AS (
SELECT ARRAY<STRUCT<index INT64, value STRING>>
[(4, 'aaa'), (23, 'bbb'), (70, 'ccc')] customDimensions
UNION ALL
SELECT ARRAY<STRUCT<index INT64, value STRING>>
[(4, 'ddd'), (70, 'eee')] customDimensions
)
SELECT cd.index, ARRAY_AGG(cd.value) VALUES
FROM `project.dataset.my_table`,
UNNEST(customDimensions) cd
GROUP BY cd.index
with result as below
Row index values
1 4 aaa
ddd
2 23 bbb
3 70 ccc
eee
I would recommend to stay with this flatten version as it serves most of practical cases I can think of
But if you still want to further pivot this - there are quite a number of posts related to how to pivot in BigQuery
I've been able to get this sort of result by making a subquery for each desired index value, unnesting the array in each and filtering WHERE index = (value), but that gets really ugly as the variables pile up. Is there an alternative?
Yes, you can use a user-defined function to encapsulate the common logic. For example,
CREATE TEMP FUNCTION NTHLOOKUP(
targetIndex INT64,
customDimensions ARRAY<STRUCT<index INT64, value STRING>>
) AS (
(SELECT value FROM UNNEST(customDimensions)
WHERE index = targetIndex)
);
SELECT
NTHLOOKUP(70, customDimensions) as val70,
NTHLOOKUP(4, customDimensions) as val4,
NTHLOOKUP(23, customDimensions) as val23
from my_table;
For example I have a DB which has following entries:
Item_Name |TransactionType |Qty
A |Purchase |50
A |Sale | 1
A |Sale | 2
B |Purchase |25
B | Sale | 1
B |Sale | 1
Above table is named as Entries, Now I want to write a query in MS-Access which will give following output?
Item_Name |Purchased_QTY |Sale_QTY
A |50 | 3
B |25 | 2
I want to check for each item what is the sales and purchase
Can someone help here
You can use crosstab query. SQL text of it below. Also this can be done using IIF() functions, but crosstab is faster in design and execution.
TRANSFORM Sum([DataTable].Qty) AS SumOfQty
SELECT [DataTable].Item_Name
FROM DataTable
GROUP BY [DataTable].Item_Name
PIVOT [DataTable].TransactionType;
I have a query result set like this:
value | id
500 | 1
400 | 1
300 | 2
and a product table:
product_name | id
product_1 | 1
product_2 | 2
product_5 | 3
product_6 | 4
product_9 | 5
product_0 | 6
Now I want to find the product_name from product table for every row in query result set and join the result to look like following:
The result after joining should be following:
product_name | id | value
product_1 | 1 | 500
product_1 | 1 | 400
product_2 | 2 | 300
I am getting confused how to use left join to achieve the result.
You'd join based on the value that's common between the two tables; in this case, it's id.
It doesn't seem like you want any NULL values for value, so you wouldn't use a LEFT JOIN; you'd want an INNER JOIN instead.
This would get you pretty much what you want; bear in mind that this is untested.
SELECT p.product_name, qr.id, value
FROM product p
INNER JOIN query_result_set qr ON qr.id = p.id
ORDER BY value DESC;
Also, your expectations are wrong - product_5 has an id of 3, so it wouldn't appear in this join result.