I have 2 tables one with names and id's and another with id's and dates,
I am trying to figure out how to create the helper method to query the two tables and return the list of names with the number of dates associated to their id.
Name Table
id Name
1 John
2 Tom
3 Frank
Date Table
id Date
1 1/3/19
1 1/4/19
1 1/5/19
2 1/4/19
Results:
Name Count
John 3
Tom 1
Frank 0
This is as far as I have gotten just pulling back all the names.
Future<List> queryAllNames() async {
Database db = await database;
List<Map> names =
await db.query(Names, columns: [id, Name]);
if (names.length > 0) {
return names;
}
return null;
}
I have figured out the raw query I need from sqlite
select Name.name, count(Date.date) from Name left join Date using(id) group by Name.name;
But still not able to translate to at sqflite helper method.
There is a rawQuery() method. Check out Raw SQL queries
Related
I've a postgres table with data like
Name
Attendance
Jackie
2
Jade
5
Xi
10
Chan
15
In my query I want all present by name, and if name doesn't exist return "null" instead of no row for that particular name
Eg
query where Name in ('Jackie', 'Jade', 'Cha', 'Xi')
Should return
Name
Attendance
Jackie
2
Jade
5
NULL
NULL
Xi
10
To produce the desired rows, you need to join with a table or set of rows which has all those names.
You can do this by inserting the names into a temp table and joining on that, but in Postgres you can turn an array of names into a set of rows using unnest. Then left join with the table to return a row for every value in the array.
select attendances.*
from
unnest(ARRAY['Jackie','Jade','Cha','Xi']) as names(name)
left join attendances on names.name = attendances.name;
I would like to expand existing question.
I have 3 tables:
Rivers' names and id - Let's name it river
Rivers' id and Hydrols' ids - Let's name it id
Hydrols' ids, volume and date - Let's name it data
I know how to select data from this 3 tables:
SELECT DISTINCT river.name, AVG(data.volume)
FROM data
INNER JOIN id
ON id.id_hydrol = data.id_hydrol
INNER JOIN river
ON river.id_river = id.id_river
AND river.name = 'NAME_1'
GROUP BY river.name
ORDER BY AVG(data.volume) DESC
What do i have to write instead of ? mark ?
How can i compare volume of 2 rivers with different names and date has to be the same?
How i put 2 different requests in corr() function?
I have a table which contains a employee ID and a database ID code in database DBX0 table 'FirstTable':
Employee ID
Database ID
123
1
456
2
789
2
149
3
The database name is stored in a different table 'databases' like :
Database ID
DB name
1
DBX1
2
DBX2
3
DBX3
So what I'm trying to do is join the first table with a separate table which is stored in the identified database, like
SELECT * from DBX0.dbo.FirstTable a join (SELECT [DB name] from DBX0.dbo.databases where [database ID] = a.[database ID]).dbo.OtherTable
(I don't think this will actually work with a subquery, but this would return the result that I want in theory.)
I have tried dynamic sql with a cursor to iterate through each database and then only return results based on database ID = database ID and Employee ID = Employee ID, but this returns the results in a separate result per database.
Also tried:
Select * from (SELECT * from DBX0.dbo.FirstTable a join '+#dbname+'.dbo.OtherTable on a.employeeid = b.employeeid and a.dbid = b.dbid)
in dynamic sql, but this didn't work either.
Is there any way to get all of the data in one result?
Good day, I have 3 tables which is products, users, and pivot product_user table.
and I want to fetch all the data in the main table if that row contains a pivot data.
Example
products table
ID Name
1 sample 1----------> is in pivot table **product_user**
2 sample 2----------> is in pivot table **product_user**
3 sample 3
users table
ID Name
1 user1-------------> is in the pivot table **product_user**
2 user2
3 user3-------------> is in the pivot table **product_user**
product_user
product_id user_id
1 1
3 2
In this scenario how can I fetch the data in the two main table that contains pivot data?
How can I achieve the result of this query in laravel way?
SELECT p.`name`, u.`first_name`
FROM product_user AS pu
LEFT JOIN products AS p
ON pu.`product_id` = p.id
LEFT JOIN `users` AS u
ON pu.`user_id` = u.`id`
result
name first_name
sample 1 user firstname
You have a BelongsToMany relationship Product ↔ User:
class ProductUser extends Model {
public function product() {
return $this->belongsTo(Product::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
$pivots = ProductUser::with('product', 'user')->get();
foreach($pivots as $pivot) {
// $pivot->user->first_name
// $pivot->product->name
}
I want to get grouped data from a table in sqlite. For example, the table is like below:
Name Group Price
a 1 10
b 1 9
c 1 10
d 2 11
e 2 10
f 3 12
g 3 10
h 1 11
Now I want get all data grouped by the Group column, each group in one array, namely
array1 = {{a,1,10},{b,1,9},{c,1,10},{h,1,11}};
array2 = {{d,2,11},{e,2,10}};
array3 = {{f,3,12},{g,3,10}}.
Because i need these 2 dimension arrays to populate the grouped table view. the sql statement maybe NSString *sql = #"SELECT * FROM table GROUP BY Group"; But I wonder how to get the data from the resultset. I am using the FMDB.
Any help is appreciated.
Get the data from sql with a normal SELECT statement, ordered by group and name:
SELECT * FROM table ORDER BY group, name;
Then in code, build your arrays, switching to fill the next array when the group id changes.
Let me clear about GroupBy. You can group data but that time its require group function on other columns.
e.g. Table has list of students in which there are gender group mean Male & Female group so we can group this table by Gender which will return two set . Now we need to perform some operation on result column.
e.g. Maximum marks or Average marks of each group
In your case you want to group but what kind of operation you require on price column ?.
e.g. below query will return group with max price.
SELECT Group,MAX(Price) AS MaxPriceByEachGroup FROM TABLE GROUP BY(group)