SQL Can anyone find an error in my query? - sql

Was told by my professor that my output is incorrect for this query that I have to create. Can anyone point to why? Maybe he wants the query done differently? I thought the question was pretty simple. ERD, question, query and output are below. Any help is appreciated. Thank you
Write a query which displays all trades where more than 50000 shares were traded on the secondary markets. Please include the trade id, stock symbol, name of the company being traded, stock exchange symbol, number of shares traded, price total and currency symbol.
Output:
88 TM Toyota Motors NYSE 50300 4099128.08 $
28 TM Toyota Motors NYSE 50300 4099128.08 $
88 TYO:6201 Toyota Motors NYSE 50300 4099128.08 $
28 TYO:6201 Toyota Motors NYSE 50300 4099128.08 $
71 NYT The New York Times NYSE 84000 1046892 $
11 NYT The New York Times NYSE 84000 1046892 $
78 BA British Airways LSE 72000 14711256 £
18 BA British Airways LSE 72000 14711256 £
SELECT
trade.trade_id,
stock_listing.stock_symbol,
company.name company_name,
stock_exchange.symbol AS stock_exchange_symbol,
trade.shares AS number_of_shares_traded,
trade.price_total,
currency.symbol currency_symbol
FROM trade
JOIN stock_listing
ON stock_listing.stock_id = trade.stock_id
JOIN company
ON company.stock_id = trade.stock_id
JOIN stock_exchange
ON stock_exchange.stock_ex_id = trade.stock_ex_id
JOIN currency
ON currency.currency_id = company.currency_id
WHERE trade.shares > 50000;

I noticed that you have the same Trade IDs on multiple records. That is, it looks like multiple records could be considered a "trade". If that's the case, group by trade ID first, then find the count > 50,000.

Related

ScaNN weight features for similary search

I am using ScaNN to perform similarity searches and would like to place more emphasis on some features than others when performing a similarity search.
for example, if I have the following data
name | age | country | income
John 29 US $47k
Susan 28 US $44k
Bill 26 US $39k
Sarah 35 UK $100k
Jack 34 UK $90k
Maggie 37 UK $95k
and income has more importance, then given the following query:
George, 28, US, $100k
it would return
Sarah, Jack, Maggie
adding more weight to the income feature.
Training data values are normalized before building the similarity index
df_np = preprocessing.normalize(df[features])
and likewise the query values are normalized before performing a search
np_q = preprocessing.normalize([list(query.values())])

Pandas difficult to add new column with condition?

I was trying to do multiple group and also adding count to new column.
My input file
OrderDate Region Rep Item Units Unit Cost Total
----------------------------------------------------------
1/6/18 East Jones Pencil 95 1.99 189.05
1/23/18 Central Kivell Binder 50 19.99 999.50
2/9/18 Central Jardine Pencil 36 4.99 179.64
2/26/18 Central Gill Pen 27 19.99 539.73
3/15/18 West Sorvino Pencil 56 2.99 167.44
4/1/18 East Jones Binder 60 4.99 299.40
4/18/18 Central Andrews Pencil 75 1.99 149.25
4/18/18 West Jones Pencil 75 1.99 149.25
I am trying to do like
Region Rep Count same/diff
-------------------------------
east jones 2 2-same
jones
central Kivell 4 >3 differnce
Jardine
Gill
Andrews
West Sorvino 2 2-different
West jones1
My code:
df1 = pd.read_excel(excel_path, sheet_name = 'SalesOrders', index_col=0)
df3 = (df1.groupby('Region')['Rep'].value_counts())
print(df3)
Please help me to do this. Thanks
In rep column, based on Region i have done group by to know Rep values. if Rep member are same then 2 same people, consider central region has 4 different people working so it i greater than 3 .

Mathematical calculation in sql(oracle 11g)

i got a table like:
COST RETAIL DISCOUNT CATEGORY
18.75 30.95 FITNESS
14.2 22 FAMILY LIFE
37.8 59.95 3 CHILDREN
31.4 55.95 COMPUTER
12.5 19.95 COOKING
47.25 75.95 3.8 COMPUTER
21.8 25 COMPUTER
37.9 54.5 COMPUTER
48 89.95 4.5 FAMILY LIFE
19 28.75 COOKING
5.32 8.95 CHILDREN
17.85 29.95 1.5 SELF HELP
15.4 31.95 BUSINESS
21.85 39.95 LITERATURE
And my problem is:
Display the book Category and the average retail price after discount in all categories where average discounted price is less than the highest average retail price of books for all the categories.
I tried with the following query but cant get the solution as i think i did not understood the question properly.
select category, retail-discount as A, avg(A) as B, avg(retail) as C from books
where B<max(C)

MS Access, Excel, SQL, and New Tables

I'm just starting out with MS Access 2010 and have the following setup. 3 excel files: masterlist.x (which contains every product that I sell), vender1.x (which contains all products from vender1, I only sell some of these products), and vender2.x (again, contains all products from vender2, I only sell some of these products). Here's an example data collection:
masterlist.x
ID NAME PRICE
23 bananas .50
33 apples .75
35 nuts .87
38 raisins .25
vender1.x
ID NAME PRICE
23 bananas .50
25 pears .88
vender2.x
ID NAME PRICE
33 apples .75
35 nuts .87
38 raisins .25
49 kiwis .88
The vender lists get periodically updated with new items for sell and new prices. For example, vender1 raises the price on bananas to $.75, my masterlist.x would need to be updated to reflect this.
Where I'm at now: I know how to import the 3 excel charts into Access. From there, I've been researching if I need to setup relationships, create a macro, or a SQL query to accomplish my goals. Not necessarily looking for a solution, but to be pointed in the right direction would be great!
Also, once the masterlist.x table is updated, what feature would I use to see which line items were affected?
Update: discovered SQL /JOIN/ and have the following:
SELECT * FROM master
LEFT JOIN vender1
ON master.ID = vender1.ID
where master.PRICE <> vender1.PRICE;
This gives me the output (for the above scenario)
ID NAME PRICE ID NAME PRICE
23 bananas .50 23 bananas .75
What feature would instead give me:
masterlist.x
ID NAME PRICE
23 bananas .75
33 apples .75
35 nuts .87
38 raisins .25
Here is a heads up since you were asking for ideas to design. I don't really fancy your current table schema. The following queries are built in SQL Server 2008, the nearest syntax that I could get in sqlfiddle to MS Access SQL.
Please take a look:
SQLFIDDLE DEMO
Proposed table design:
vendor table:
VID VNAME
1 smp farms
2 coles
3 cold str
4 Anvil NSW
product table:
PID VID PNAME PPRICE
203 2 bananas 0.5
205 2 pears 0.88
301 3 bananas 0.78
303 3 apples 0.75
305 3 nuts 0.87
308 3 raisins 0.25
409 4 kiwis 0.88
masterlist:
ID PID MPRICE
1 203 0.5
2 303 0.75
3 305 0.87
4 308 0.25
Join queries can easily update your masterlist now. for e.g.:
When the vendor updates their prices for the fruits they provide you. Or when they stop supply on that product. You may use where clauses to add the conditions to the query as you desire.
Query:
SELECT m.id, p.vid, p.pname, p.pprice
FROM masterlist m
LEFT JOIN product p ON p.pid = m.pid
;
Results:
ID VID PNAME PPRICE
1 2 bananas 0.5
2 3 apples 0.75
3 3 nuts 0.87
4 3 raisins 0.25
Please comment. Happy to help you if have any doubts.

Display %change using SQL Server

Happy New Year!
I was wondering if we could write a SQL query in SQL Server that displays the %change.
Example, I am running this simple query with the output:
Select location, item_sold
from product
Location Item_sold
VA 20
CA 57
DC 44
MA 75
FL 101
Now, I would like the desired output as follow, which take the item sold from each location divided by the total.
Location Item_sold %Chg
VA 20 7%
CA 57 19%
DC 44 15%
MA 75 25%
FL 101 34%
TOTAL 297
In SQL Server 2005+, you can use the following:
SELECT location, item_sold, CAST(item_Sold AS FLOAT)/(SUM(Item_Sold) OVER()) [%Chg]
FROM product
For previous versions, try this:
SELECT A.location, A.item_sold,
CAST(A.item_Sold AS FLOAT)/(SELECT SUM(Item_Sold) Ammount FROM product) [%Chg]
FROM product A