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.
Files I need to be edited: I wrote these files and I put them in a...
Files I need to be edited: I wrote these files and I put them in a folder labeled Project 7. I am using this as a study tool for a personal project of mine. //main.cpp #include <iostream> #include "staticarray.h" using namespace std; int main( ) {    StaticArray a;    cout << "Printing empty array -- next line should be blank\n";    a.print();    /*    // Loop to append 100 through 110 to a and check return value    // Should print "Couldn't append 110"...
True/False 1. Common stocks and preferred stocks are the same except preferred stocks do not have...
True/False 1. Common stocks and preferred stocks are the same except preferred stocks do not have voting rights. 2. Risk is defined as the possibility that you will lose money when buying an investment.
Need SQL Tables Final Project should be included ER, NER, Table diagrams and SQL statements. The...
Need SQL Tables Final Project should be included ER, NER, Table diagrams and SQL statements. The final project is about developing an auction Web site. The details are as follows: BA is an online auction Web site. People can buy and sell items in this Web site. Buyers are people who like to buy items, and sellers are people who like to sell items. •Each seller can sell items. •Each item has a bidding start time, an end time, and...
   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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT