Mathematical calculation in sql(oracle 11g) - sql

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)

Related

SQL Can anyone find an error in my query?

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.

SQL code guidance

I've recently had to start using SQL, and am struggling a little with how to code some more advanced capabilities etc, so was hoping for some guidance.
I have one database that contains high level data split by geography, NAICS industry and size of company, with field names such as:
CBSACODE VERTICAL COMPANYSIZE DEVICES
01010 Agriculture 1-10 100
01010 Education 20-99 50
01010 Healthcare 200-499 250
01010 Manufacturing 100-199 150
01010 Manufacturing 1-10 80
78910 Agriculture 1-10 25
78910 Government 500+ 400
78910 Agriculture 11-19 60
78910 Finance 100-199 310
78910 Retail 20-99 200
I have a second database which is at a customer level but has some overlapping fields
CUSTOMER NAME VERTICAL COMPANYSIZE ZIPCODE CBSACODE
Customer A Agriculture 1-10 12345 78910
Customer B Manufacturing 100-199 54321 01010
What I am trying to do is to display the list of customers, and then display the sum of 'Number of devices' based on matching the CBSA Code, Vertical Industry and Company Size for each customer against the first database.
CUSTOMER NAME CBSADEVICES CBSA+VERTICALDEVICES COSIZEDEVICES
Customer A 995 85 25
Customer B 630 230 150
I started trying to write a query using SUM and CASE WHEN etc but quickly got overwhelmed.
I'm using MS SQL Server 2016 (Express).
Any guidance would be greatly appreciated.....I've read through a number of various threads, but have yet to get my head around it sadly.

How to query DBpedia online using SQL?

DBpedia just released their data as tables, suitable to import into a relational database. How can I query this data online using SQL?
Dataset:
http://wiki.dbpedia.org/DBpediaAsTables
I took the raw data, uploaded it to BigQuery, and made it public. So far I've done it with the 'person' and the 'place' table. Check them at https://bigquery.cloud.google.com/table/fh-bigquery:dbpedia.person.
Now is easy to know what are the most popular alma maters, for example:
SELECT COUNT(*), almaMater_label
FROM [fh-bigquery:dbpedia.person]
WHERE almaMater_label != 'NULL'
GROUP BY 2
ORDER BY 1 DESC
It's a little more complicated than that, as some people have more than one alma mater - and the particular way DBpedia encodes that. I left the complete query at http://www.reddit.com/r/bigquery/comments/1rjee7/query_wikipedia_in_bigquery_the_dbpedia_dataset/.
Btw, the top alma maters are:
494 Harvard University
320 University of Cambridge
314 University of Michigan
267 Yale University
216 Trinity College Cambridge
You can also do joins between tables.
For example, for each building (from the place table) that has an architect: What year was that architect born? How many buildings with an architect born that year are listed in DBpedia?
SELECT COUNT(*), LEFT(b.birthDate, 4) birthYear
FROM [fh-bigquery:dbpedia.place] a
JOIN EACH [fh-bigquery:dbpedia.person] b
ON a.architect = b.URI
WHERE a.architect != 'NULL'
AND birthDate != 'NULL'
GROUP BY 2
ORDER BY 2
Results:
...
8 1934
13 1935
9 1937
7 1938
17 1939
7 1941
1 1943
15 1944
10 1945
12 1946
7 1947
9 1950
20 1951
1 1952
...
(Google BigQuery has a free monthly quota to query, up to a 100GB each month)
(DBpedia data from version 3.4 on is licensed under the terms of the Creative Commons Attribution-ShareAlike 3.0 license and the GNU Free Documentation License. http://dbpedia.org/Datasets#h338-24)

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.

Transpose groups/subgroups in sql oracle

I have date column which i have to divide in 6 quarters and calculate count,ratio- A/Count, Avg(colC) by State. Date column i have converted to YYYY Q format. I was wondering if i can get results shown below. i am using oracle 11g. I am just trying to write a sql which can give me results in above format. I am able to Group results in quarter but unable to further subgroup it to show count,Ratio and Avg under each quarter. –
I have 2 tables that i need to use to get data below.
Table 1 Table 2
Customer_id St_Nme St_Cd Customer_id No_of_sales Time_spent Date
1 Alabama AL 1 4 4.5 01122012
2 California CA 2 7.5 9.33 03062012
Desired Output
Count-Count of sales
Ratio-Time_spent/Count of sales
Avg - Average of time spent
Q42012 Q32012 Q22012 Q12012 Q42011 Q32012
count Ratio Avg count Ratio Avg count Ratio Avg
State
Alabama 3 4.5 1.2 8 7.4 3.2 65 21.1 34.4
A.. 4 7.5 3.2 5 9.4 5.2 61 25.1 39.4
A.. 9 6.5 5.2 4 3.4 3.7 54 41.1 44.4
Boston
Cali..
Den..