Combining like minded entries in SQL [duplicate] - sql

This question already has answers here:
SQL Sum Multiple rows into one
(5 answers)
Closed 8 years ago.
What I would like to do is combine like minded sets into one clear entry. Here is some example data:
Item Warehouse Quantity
Apple Northeast 100
Apple Midwest 2000
Apple South 300
Orange Northeast 400
Orange Midwest 800
Orange South 100
Orange West 100
Strawberry Northeast 550
Strawberry Midwest 750
Strawberry South 250
Strawberry East 350
What I would like is for the SQL query to return the total quantity from all the warehouses. The hopeful output would be something such as:
Item Quantity
Apple 2400
Orange 1400
Strawberry 1900
Any help would be amazing, thank you!

select Item, sum(Quantity) as TotalQuantity
from {tablename}
group by Item;

Related

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 .

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.

Get data from string of specific values SQL

I'm rather new at SQL programming, and still struggling with the basics. I need to extract some specific rows, from a specified string of IDs.
ID Product City
1 Apple London
2 Banana Berlin
3 Orange Berlin
4 Orange Paris
5 Apple Paris
6 Banana Copenhagen
7 Banana Copenhagen
8 Banana London
9 Apple Paris
10 Orange London
11 Apple Berlin
12 Apple Copenhagen
13 Apple Paris
If I need to select ID=1,2,5,6,10,11,13 how do I extract these specific rows from the database?
I'm using SQLite.
Thanks in advance.
You should use the in clause
select * from your_table
where id in (1,2,5,6,10,11,13)

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.

Sum values from Duplicated rows

There's my sql data:
code name total
---------------
3 Sprite 2400
17 Coke 1500
6 Dew 1000
17 Coke 3000
6 Dew 2000
But code and name has duplicated values and I want to sum total from each duplicated field.
Something like this:
code name total
---------------
3 Sprite 2400
17 Coke 4500
6 Dew 3000
How could I do that in sql?
SELECT code, name, sum(total) AS total FROM table GROUP BY code, name