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.
Related
Let's say we've got the following (very simplified compared to my real use case) table:
Campaign ID
Campaign Name
Impressions
Clicks
Purchases
111111
Alfa
5000
120
3
111112
Beta
7000
140
6
111113
Gamma
6000
90
3
With the usage of temporary SQL table we can pull out a new data for campaign Delta and also updated data for the ongoing campaign Gamma, but campaigns Alfa and Beta are historical and therefore not available in the temporary table and we want to leave them as they are.
New data pulled through a query could look like this:
Campaign ID
Campaign Name
Impressions
Clicks
Purchases
111113
Gamma
9000
160
7
111114
Delta
1000
40
0
How would I formulate a query that will bring me this result:
Campaign ID
Campaign Name
Impressions
Clicks
Purchases
111111
Alfa
5000
120
3
111112
Beta
7000
140
6
111113
Gamma
9000
160
7
111114
Delta
1000
40
0
I am sure this might be a common problem, but so far I wasn't able to find an easily understandable solution.
Apologies if this seems very simple.
And thanks in advance for any help!
Have one query that selects from the new table
Have another query that selects records from your old table that don't have the same keys as any records in your new table
UNION the two queries together
I want to list the person's name and beside how many times this person got a 10.
Is there any command like this?
marcus gold 25
julia silver 34
jason platinium 12
sarah bronze 45
I have related two custom sql query in Tableau (via making relationship)
The outcome of the queries looks like :
Q1 : (It shows the starting time of the valid budget.If a user has multiple rows in this table, it means his/her budget has been updated with new amount)
id_user
budgete_start_date
budget_amount
1234
06-11-2021
120
1234
06-07-2022
200
56789
06-01-2022
1200
56789
06-07-2022
2000
643
05-05-2022
30
Q2 :(It shows the budget usage)
id_user
budgete_usage_date
amount_usage
1234
01-12-2021
50
1234
05-08-2022
100
56789
10-02-2022
60
56789
07-08-2022
500
643
05-07-2022
17
I need to find a way to create the following view to know what was the valid budget at
budgete_usage_date.
id_user
budgete_usage_date
amount_usage
valid budget
1234
01-12-2021
50
120
1234
05-08-2022
100
200
56789
10-02-2022
60
1200
56789
07-08-2022
500
2000
643
05-07-2022
17
30
How can I do that with calculated field in Tableau (with db made by relationship)?
If that's not possible, how can I do that directly in query? (changing the relationship to a single query)
I made data frame shown below which has 3 companies A,B and C. Companies buy certain amount of vouchers during 2016 to 2018 period. Some days eg. Company A buys 100 pieces for $3000 other days no company buys any.
I'd like see how these three companies compare for last two years when it comes to money spent for vouchers so my ideas was following:
Sum all money spent each month for each company, and plot them in bar graph or just standard line - so three lines each with different color.
Since its 2 years of data, there would be roughly 24 date-points on x axis
I tried something like: plt.bar(A['Datetime'], A['PaidTotal'])
But get: ufunc subtract cannot use operands with types dtype('
But this is just for one company anyway not for all 3 in one graph
(I can sort those dates, thats not a problem)
Company Name PaidTotal Datetime
585 CompanyA 218916.0 2016-10-14 10:51:07
586 CompanyB 430000.0 2017-01-23 11:05:08
591 CompanyB 546217.0 2016-09-26 14:20:00
592 CompanyC 73780.0 2016-12-07 07:52:01
593 CompanyA 132720.0 2016-10-04 16:14:10
595 CompanyC 52065.0 2016-11-12 14:32:40
For a bar chart you can call df.groupby('Company Name')['PaidTotal'].sum().plot.bar():
To see a line chart of all three over time, you can try this (the axes are wrong, but this is the general idea):
sums = df.groupby(['Company Name', 'Datetime'])['PaidTotal'].sum().reset_index(level=0)
for company in sums['Company Name'].unique():
sums[sums['Company Name'] == company]['PaidTotal'].plot();
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.