Question

In: Computer Science

Need the mongodb queuries translated into sql. All files are From stocks except the first is...

Need the mongodb queuries translated into sql. All files are From stocks except the first is from zips. Will give thumbs up!

db.zips.aggregate([
{$match: {state: "IL"}},
{$group: {_id: "$city", totalzips: {$sum: 1}}},
{$match: {totalzips: {$gte: 3}}},
{$sort: {totalzips: -1}}
])

db.stocks.aggregate([
{$match: {"Sector": "Information Technology", "Dividend Yield": {$gt: 0}}},
{$project: {_id: 0, Name: 1}}
])

db.stocks.aggregate([
{$match: {Sector: "Health Care"}},
{$sort: {"Earnings/Share": -1}},
{$limit: 10},
{$project: {_id: 0, Name: 1, "Earnings/Share": 1}}
])

db.stocks.aggregate([
{$match: {Sector: "Health Care"}},
{$group: {_id: "$Sector", TotalEarnings: {$sum: "$EBITDA"}}}
])

db.stocks.aggregate([
{$match: {Sector: "Industrials"}},
{$project: {_id: 0, Name: 1, Symbol: 1, OutstandingShares:{$divide: ["$Market Cap", "$Price"]}}},
{$sort: {OutstandingShares: -1}}
])

thank you so much!

Solutions

Expert Solution

MongoDB :db.zips.aggregate([
{$match: {state: "IL"}},
{$group: {_id: "$city", totalzips: {$sum: 1}}},
{$match: {totalzips: {$gte: 3}}},
{$sort: {totalzips: -1}}

])

SQL:

SELECT city,Sum(1) AS totalzips FROM Zips

WHERE state="IL" GROUP BY city

HAVING SUM(city) >=3

ORDER BY totalzips DESC;

EXPALANTION:-

Here, we are fetching data from the Zips collection.We are considering collection as a table here.

Grouping is done on city and city is being taken as _id meaning we will be displaying city with the aggregated data.

Match is used to filter data in mongo on the basis of state="IL" .So ,we are using WHERE clause.

Match is also used with totalZips ,this is aggregated data so in SQL we use HAVING clause.

Sort is done using totalZips ,here -1 means in descending order so we did same in SQL.

MongoDB:

db.stocks.aggregate([
{$match: {"Sector": "Information Technology", "Dividend Yield": {$gt: 0}}},
{$project: {_id: 0, Name: 1}}
])

SQL:

SELECT Name FROM Stocks

WHERE Sector= "Information Technology" AND [Dividend Yield] >0;

EXPALANTION:-

we are using aggregate on stocks collection .

Match is used for filtering data with two conditions to be satisfied ,so in SQL we are using WHERE clause with AND operator.

gt here means greater than .

project means getting particular field only so {$project: {_id: 0, Name: 1}} here ,0 means that field will not be selected and 1 means that field will be selected.

MongoDB: db.stocks.aggregate([
{$match: {Sector: "Health Care"}},
{$sort: {"Earnings/Share": -1}},
{$limit: 10},
{$project: {_id: 0, Name: 1, "Earnings/Share": 1}}
])

SQL:

SELECT Name,[Earnings/Share] FROM Stocks

WHERE Sector= "Health Care"

ORDER BY [Earnings/Share] DESC

LIMIT 10;

EXPALANTION:-

we are using aggregate on stocks collection .

Match is used for filtering data with one conditions to be satisfied ,so in SQL we are using WHERE clause.

We are sorting by Earning/share field in descending order.

We are limiting data to 10 object so LIMIT 10 is used.

project means getting particular field only so {$project: {_id: 0, Name: 1, "Earnings/Share": 1}} ,0 means that field will not be selected and 1 means that field will be selected.

MongoDB: db.stocks.aggregate([
{$match: {Sector: "Health Care"}},
{$group: {_id: "$Sector", TotalEarnings: {$sum: "$EBITDA"}}}
])

SQL:

SELECT Sector,SUM(EBITDA) AS TotalEarnings FROM Stocks

WHERE Sector= 'Health Care'

GROUP BY sector;

EXPALANTION:-

Here, we are fetching data from the Stocks collection.We are considering collection as a table here.

Grouping is done on sector and sector is being taken as _id meaning we will be displaying sector with the aggregated data.

Match is used to filter data in mongo on the basis of sector so we are using WHERE clause.

AS(ALIAS) is used for naming the resultant column.

MongoDB: db.stocks.aggregate([
{$match: {Sector: "Industrials"}},
{$project: {_id: 0, Name: 1, Symbol: 1, OutstandingShares:{$divide: ["$Market Cap", "$Price"]}}},
{$sort: {OutstandingShares: -1}}
])

SQL:

SELECT   Name, Symbol,([Market Cap]/Price) AS OutstandingShares FROM Stocks

WHERE Sector= "Industrials"

ORDER BY OutstandingShares DESC;

EXPALANTION:-

we are using aggregate on stocks collection .

Match is used for filtering data with one conditions to be satisfied ,so in SQL we are using WHERE clause.

We are sorting by OutstandingShares field in descending order.

project means getting particular field only so {$project: {_id: 0, Name: 1, Symbol: 1, OutstandingShares:{$divide: ["$Market Cap", "$Price"]}}} ,0 means that field will not be selected and 1 means that field will be selected.

{$divide: ["$Market Cap", "$Price"]} means dividing [market cap] column by price column

AS(ALIAS) is used for naming the resultant column.


Related Solutions

Need SQL commands for these questions based on the bowling league database; When was the first...
Need SQL commands for these questions based on the bowling league database; When was the first tournament date and where was it played? What are the number of tournaments per location?
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
How to display in SQL Server, for example, all the author First Names are FIVE LETTERS...
How to display in SQL Server, for example, all the author First Names are FIVE LETTERS LONG if I have the Author Table. I used the WHERE LENGTH ua_fname=5   This is not returning names with 5 letters. Can someone please suggest to me the correct one?
Setting up the field office includes all except Select one: a. Preparing construction administration files, forms,...
Setting up the field office includes all except Select one: a. Preparing construction administration files, forms, and documentation b. Establishing utilities for the field office c. Establishing utilities for the field office d. Sighting the location for the temporary trailer office e. Clearing and Grabbing
there are files called LinkedList.h, and Array.h and all the functionality you need has already been...
there are files called LinkedList.h, and Array.h and all the functionality you need has already been implemented. There is also a file called TimeSupport.h that allows you to measure the running time of code segments. There is also a file called RandomSupport.h, which you can use to generate good random numbers. In your app.cpp , create a demo illustrating a scenario where storing numbers in a linked list is more efficient than an array. Your demo should generate a sufficiently...
To calculate the time value of money, we need to consider all of the following except...
To calculate the time value of money, we need to consider all of the following except the... -Length of time the money is on deposit. -Type of investment. -Principal. -Amount of the savings. Annual interest rate.
   All of the following would represent the first line of defense against microbial invasion except -...
   All of the following would represent the first line of defense against microbial invasion except - the secretion of histamine by basophils - the presence of lysosome within tears - the flushing action of urination -the production of mucus within the nasal cavity -the keratinized epithelium of the epidermis
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this...
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
This is for python coding First of all, you need to know what Natural Numbers are....
This is for python coding First of all, you need to know what Natural Numbers are. They are the positive integers (whole numbers) 1, 2, 3, etc., and zero as well. For this project, there are TWO PORTIONS: PORTION 1: Write a program to find the SUM of the first n natural numbers the user wants to add. The program should first prompt the user for how many natural numbers are to be summed. The use for loop to do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT