In google BigQuery I have done a simple query to get how many music someone has listened.
What I need is to make a sum for all rows returned from the query below (some type of subquery)?
select count(1) cnt
from OF7.PETERV_TEST
where gender='F'
group by userId
Row f0_
1 14
2 1
3 7
4 18
5 1
6 4
7 2
8 2
expected result:
49
you can use:
SELECT sum(cnt)
FROM
(SELECT count(1) cnt
FROM OF7.PETERV_TEST
WHERE gender='F'
GROUP BY userId )
Related
I have following table:
Id
offset
length
5000
0
5
5001
5
5
5002
10
4
5003
14
4
5010
23
5
5011
28
5
Offset value in each row is based on summation of offset and length of previous row.
As you can see, 6 rows have been deleted between forth and fifth rows and I need to update again offset column based on regular way. My desired output would be as follow:
Id
offset
length
5000
0
5
5001
5
5
5002
10
4
5003
14
4
5010
18
5
5011
23
5
Is there a pure update SQL statement to achieve this in sqlite?
I Appreciate any help.
If your version of SQLite is 3.33.0+ you can use the UPDATE ... FROM... syntax with SUM() window function:
UPDATE tablename AS t1
SET offset = t2.offset
FROM (
SELECT Id, SUM(length) OVER (ORDER BY Id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) offset
FROM tablename
) AS t2
WHERE t2.Id = t1.Id AND t2.offset IS NOT NULL;
See the demo.
For previous versions use a correlated subquery:
UPDATE tablename AS t1
SET offset = COALESCE(
(SELECT SUM(t2.length) FROM tablename t2 WHERE t2.Id < t1.Id),
t1.offset
);
See the demo.
Im trying to check how many times two teams have played against each other, while each appearing once at home and once away.
In the next table, we can see that Team 1 played against Team 2 three times, twice away and once at home, and Team 3 played against Team 4 twice, once away and once at home.
how can I do it using sqlite?
id_home
id_away
date
1
2
2/12
2
1
3/12
3
4
4/12
4
3
5/12
2
1
6/12
You can group by sorted pairings of the id_home and id_away teams:
with matches as (select distinct t1.id_home h, (select t2.id_away from teams t2 where t2.id_home = t1.id_home) a from teams t1), m1 as (select case when h < a then h else a end a1, case when a < h then h else a end a2 from matches)
select m1.a1 team1, m1.a2 team2, (select sum(m1.a1 = t3.id_home) from teams t3) home, (select sum(m1.a1 = t3.id_away) from teams t3) away from m1 group by m1.a1, m1.a2;
Output:
team1 | team2 | home | away
1 2 1 2
3 4 1 1
I have two tables customer & home in a local database for a ipad application.
I have a SQL query
SELECT C.CustomerID AS 'CID', H.CustomerID AS 'HID', H.lotNumber AS 'lotNumber' FROM CustomerInformation C LEFT JOIN HomeInformation AS H ON ( H.CustomerID = C.CustomerID )
It returns the proper amount of records, and display my address data correctly. However, all the data in the HomeInformation table is null
CustomerID FirstName HomeID CusomterID lotNumber
1 Josh 1 1 73824
2 Smith 2 2 54353
3 Chris 3 3 75342
4 Bob 4 4 42342
5 John 5 5 24342
I have tried to use INNER join, Cross Join, and Outer Join, but no records are returned.
I have an example table:
ID | ArticleID | Price | SupplierID | dateAdded
1 1 100 1 2014-08-01
2 1 110 2 2014-08-01
3 2 105 1 2014-08-01
4 2 106 1 2014-08-01
5 2 101 2 2014-08-01
6 3 100 1 2014-08-01
7 1 107 2 2014-09-01
8 3 343 2 2014-09-01
9 3 232 2 2014-09-01
10 1 45 1 2014-09-01
I want to use .query on this table and select LAST value entered for each DISTINCT ArticleID for each SupplierID, resulting in:
ID | ArticleID | Price | SupplierID
10 1 45 1
9 3 232 2
6 3 100 1
7 1 107 2
4 2 106 1
5 2 101 2
I want to get price for last ArticleID entered for each SupplierID.
What should I enter into
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
I came up with this so far:
String[] columns = new String[]{DatabaseOpenHelper.KEY_ID, DatabaseOpenHelper.KEY_CENA, DatabaseOpenHelper.KEY_IZDELEK_ID};
Cursor crs = database.query(true,"prices", columns, selection, selectionArgs, null, null, null, null);
but now I'm stuck:S
Any hint how to do this?
You can also suggest raw query if possible..
Raw query would be like this:
SELECT ID, ArticleID, Price, SupplierID FROM your_table WHERE ID IN (SELECT max(ID) from your_table GROUP BY ArticleID, SupplierID);
I assumed the IDs are autoincremented and the more recent entries have higher ids. If that's not the case change the HAVING clause to operate on DATE column.
After fidling around a bit and help of a friend I have came with SQL query that does what I want, not sure about optimization:
select tab.* from cene tab inner join (
select izdelek_id, trgovina_id, Max(enter_date) as maxDate
from cene group by izdelek_id, trgovina_id) art
on (art.izdelek_id = tab.izdelek_id) and (art.trgovina_id = tab.trgovina_id) and (art.maxDate = tab.enter_date)
izdelek_id = ArticleID
trgovina_id = SupplierID
cene is the name of a table.
Hope it helps to somebody..
I am using oracle 11g and have written a stored procedure which stores values in temporary table as follows:
id count hour age range
-------------------------------------
0 5 10 61 10-200
1 6 20 61 10-200
2 7 15 61 10-200
5 9 5 61 201-300
7 10 25 61 201-300
0 5 10 62 10-20
1 6 20 62 10-20
2 7 15 62 10-20
5 9 5 62 21-30
1 8 6 62 21-30
7 10 25 62 21-30
10 15 30 62 31-40
now using this temp table i want to return two cursors. one for 61 and one for 62(age).
and for cursors there distinct range will be columns . for example cursor for age 62 should return following as dataset.
user 10-20 21-30 31-40
Count/hour count/hour count/hour
----------------------------------------------
0 5 10 - - - -
1 6 20 8 6 - -
2 7 15 - - - -
5 - - 9 5 - -
7 - - 10 25 - -
10 - - - - 15 30
this column range in temp table is is not a fixed values these are referenced from other table.
edited: i am using PIVOT for above problem, all examples i saw in internet are there for fixed values of column values (range in my case). how can i get dynamic values. following is the ex query:
SELECT *
FROM (SELECT column_2, column_1
FROM test_table)
PIVOT (SUM(column1) AS sum_values FOR (column_2) IN ('value1' AS a, 'value2' AS b, 'value3' AS c));
Instead of using handwritten value i am using following query inside 'IN'
SELECT * from(
with x as (
SELECT DISTINCT range
FROM test_table
WHERE age = 62 )
select ltrim( max( sys_connect_by_path(range, ','))
keep (dense_rank last order by curr),
',') range
from (select range,
row_number() over (order by range) as curr,
row_number() over (order by range) -1 as prev
from x)
connect by prev = PRIOR curr
start with curr = 1 )
it is giving error in this case. But when i using handwritten values its giving right output.
select * from (select user_id, nvl(count,0) count, nvl(hour,0) hour,nvl(range,0) range,nvl(age,0)
age from test_table)
PIVOT (SUM(count) as sum_count, sum(hour) as sum_hour for (range) IN
(
'10-20','21-30','31-40'
)
) where age = 62 order by userid
how can i give values dynamically there?
how can i do it.
Cursors are slow, I would recommend trying to do this in a query unless there's no alternative (or speed doesn't matter). You may want to look into: PIVOT / UNPIVOT which can rotate columns (in this case "range").
Here's some PIVOT / UNPIVOT documentation and examples:
http://www.oracle-developer.net/display.php?id=506
Based on your last edit:
Pretty sure you have two options:
Build dynamic sql based on the distinct values found in the "range" column.
You'll probably be stuck using a cursor again to build the column names but at least it will be limited to just the distinct ranges.
Oracle has a PIVOT XML command that you can use for this.
See: http://www.oracle.com/technetwork/articles/sql/11g-pivot-097235.html
And scroll down to the section: "XML Type"