How to create a MongoDB query with nested db.runCommand inside a find clause using java driver - mongodb-query

I'm using three collections (employee, department and location ) in a mongodb database. I need to list all departments of employee lives in a city starts with "P".
I have created following query using nested db.runCommand which is working fine in mongoDB.
db.Department.find({"id":{ $in: db.runCommand( {
// FInd distinct employee
distinct: "employee",
key:"department_id",
query: {$and:[{ "location_id":{$in: db.runCommand ( {
// FInd distinct Location
distinct: "Location",
key:"id",
query: {"city":{ "$regex": "P.*" }}}).values
// End - Location
}}
]}}).values
// End - employee
}}).limit(100)
How to create above query using mongodb-java-driver?

Related

Error: Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I am trying to complete an assignment on Microsoft SQL Management Server using the following instructions:
Create a view named VIEW1 that includes the following fields: store code, store name, region code, region name.
Create a view named VIEW2 that includes the following fields: employee code, employee first name, employee last name, store code, store name.
Create a view named VIEW3 that includes the following fields: store code, store name, the count of employees in the store. Test this view with the command
I have completed steps 1-2, but when I get to step 3 I run into a problem. Here is my step 3:
CREATE VIEW VIEW3
AS
SELECT s.STORE_CODE, s."STORE_NAME", COUNT(*) AS EMPLOYEES_COUNT
FROM EMPLOYEE e join STORE s ON s.STORE_CODE = e.STORE_CODE
GROUP BY e.STORE_CODE;
I keep getting the same error when I try to execute it:
Column 'STORE.STORE_CODE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
What do I need to change?
Each table or view column in any nonaggregate expression in the list must be included in the GROUP BY list
You need to add STORE_NAME column in the group by list
CREATE VIEW VIEW3
AS
SELECT s.STORE_CODE, s."STORE_NAME", COUNT(*) AS EMPLOYEES_COUNT
FROM EMPLOYEE e
JOIN STORE s ON s.STORE_CODE = e.STORE_CODE
GROUP BY s.STORE_CODE, s."STORE_NAME";

Use sql queries in Laravel 6 to count the number of workers in each department

I'm new in laravel and sql. I have two models (Worker and Department) and each model has one to many relationships. Each model has db tables workers and departments. My questions are:
Can we use sql query in laravel 6 to count number of workers in each department?
where we can use this sql query inside controller or model?
I used this query inside controller but gives me error, what is wrong with it?
public function count()
{
SELECT COUNT(id), department FROM Worker GROUP BY department;
}
Your Department Model:
class Department extends Model
{
public function workers()
{
return $this->hasMany('App\Worker');
}
}
So, you count how many workers are in each department like this:
$departments = App\Department::withCount('workers')->get();
See also:
Counting Related Models
Laravel best practices
To use raw query in Laravel controller, you must combine it in query builder. This will work:
DB::raw("SELECT COUNT(id), department FROM Worker GROUP BY department");

SQL: Parent, Child, Subchild, select parents only if all its child has a particular subchild

I have SQL tables Program(parent), Project(child), Member(sub-child). Member is the representation of User(table) for a Project.
Program is the combination of multiple projects and project will have list of members. An user can be member of multiple projects each having a Member copy.
I want to display the list of Programs of an User only if he is part of all the projects in the Program. For that, i want to write a SQL query to select the list of Programs
This is the table structure i have.
program {
id, name
}
project {
id, name
}
program_project {
id, program_id, project_id
}
member {
id, name, project_id, user_id
}
user {
id, name
}
I am trying with inner joins, but, that is not working out, any help would be appreciated.
Thanks
Can't be done with the schema you have shown, since you have project_id as a column on member - ie. each member has one (and only one) project, and so by definition cannot belong to "all the projects".
So if you want a member to belong to multiple projects, then you'll need to restructure so :
member { id, name }
member_project { id, member_id, project_id }
Then, for a given program, you can check with something like :
select * from member m
where not exists (
select * from program_project pp, member_project mp
where pp.program_id = :program-id
and pp.project_id <> mp.project_id
and mp.member_id = m.id
)

Oracle APEX - Is it possible to submit a value to session state without refreshing the entire page?

I've got a page that has a bunch of Select List items.
Each Select List item underneath the next executes an SQL query to retrieve the previous item's value from the session state, and displays a new list based on the result.
I can get everything to work by allowing each Select List item to 'Submit to Page', which refreshes the entire page. Seeing as there are 5+ Select List items, it's not very user-friendly to have the user wait on each refresh.
Is there a way to either get the value from a Select List into the session state without refreshing the page, or for an SQL Query to take the value from the previous Select List item and use it in it's query to display a new list?
I think you are looking for Cascading LOVs. Here is a short example on how to use them.
Let's say you want to have 2 select lists one with departments and one with the employees that work in the selected department. So we need a table DEPARTMENTS(department_id, deparment_name) with all your departments and a table EMP(emp_id, emp_name, department_id) with all the employees and in what department they work in.
Create your first select list named P1_DEPARTMENTS with the List of Values SQL Query like this select department_name, department_id from departments
Now create the second select list named P1_EMPLOYEES using the List of Values SQL Query
select emp_name, emp_id from EMP where department_id=:P1_DEPARTMENTS
Now in the Cascading LOV Parent Item(s) attribute of P1_EMPLOYEES select list you have to select the item that you want to pass as a parent in our case the P1_DEPARTMENTS.
Now when you select a value in the P1_DEPARTMENTS select list the P1_EMPLOYEES select list will refresh and you will get just the employees that work in the selected department.

How can I return multiple results from a single LinQ to SQL expression in one round-trip?

I'm rewriting an ancient piece of code that I've inherited and I'm looking at a query that performs a massive join on large tables, and then does a series of grouping actions on the resulting table to "distill out" data:
INSERT INTO #teh_temp_table
SELECT * FROM teh_large_table
INNER JOIN teh_other_large_table
SELECT customer_id, customer_name FROM * #teh_temp_table
GROUP BY customer_id, customer_name
ORDER BY customer_name
SELECT city_id, city_name FROM * #teh_temp_table
GROUP BY city_id, city_name
ORDER BY city_name
-- repeated n-2 times ...
This large SQL statement is sent to the DB server with a ADO.NET SqlCommand, and the data is returned as n separate results in one network round trip.
I'm having difficulties translating this to LinQ to SQL. I've been attempting to do something similar to:
from tlt in teh_large_table
join tolt in teh_other_large_table on tlt.pkey equals tolt.fkey into teh_temp_table
from tmp in teh_temp_Table
group tmp by new { tmp.customer_id, tmp.customer_name } into customers
from tmp in teh_temp_table
group tmp by new { tmp.city_id, tmp.city_name } into cities
select new { customers, cities }
but the compiler complains. Is there a way to issue an equivalent LinQ query that not only retrieves the data but also returns it, in a single network round-trip? As you can imagine, I don't want to perform that big nasty join more than once.
Other than trying to force distinct on the result sets, I'm not seeing the advantage of using the Group operation here. Since LINQ to SQL can't return multiple results from LINQ, you could push them into a your temp table and then group the results based on some type. Would the following work for you:
var bigTable = from large in teh_large_table
from other in teh_other_large_table
select new { ??? }; // TODO: Supply needed columns here
var kvPairs = from customer in bigTable
select new {Id = customer.Customer_id, Name = customer.Customer_name, Type="Customer"}
.Union(
from city in teh_temp_table
select new {Id = city.City_id, Name = city.City_name, Type="City"}
).Distinct();
var groupedKvPairs = from pair in kvPairs
group pair by pair.Type into grouped
select new {pairType = key,
vals = from row in grouped
orderby row.Name
select new { row.Id, row.Name}};
As an alternative, you could also set up a Stored Proc returning multiple results and then use the IMultipleResults interface to consume them. See http://www.thinqlinq.com/Default/Using-LINQ-to-SQL-to-return-Multiple-Results.aspx