SQL - Managing wide and long format - sql

I am trying to achieve the following challenge but I am not sure how to tackle it.
I have a table called "salesman_data" with 2 columns. Each columns represents a salesman (e.g. salesman_id_1 and salesman_id_2). Each column contains data about products which have been sold by the corresponding salesman. The data stored is the product ID. For instance, the salesman salesman_id_1 sold products whose ID are 123, 456, 789, etc. The same applies for salesman_id_2. The table is the following:
Table salesman_data
salesman_id_1
salesman_id_2
123
321
456
654
789
987
...
...
I have to calculate the sales ratio. The ratio is defined as (nb of product sold by the salesman) / (total nb product sold by everyone). The required output for the answer is each salesman with their sales ratio. Then ordering data by salesman id. In my understanding, the output should look like this:
salesman_id
ratio
1
30.1
2
69.9
Since the original table does not allow me to sort data by salesman id, I would need to create an entire new table beforehand. I was thinking about doing this:
Create a new table
Calculate the ratio
Insert the ratio into the newly created table
Query the data from the new table
I do not know if my process is the most efficient. What do you guys think ?

Related

how to merge tax table into an existing sentence

I have the following sentences in the first table:
key desc
single The single standard deduction for {2018} is {12000}.
MFJ The MFJ standard deduction for {2018} is {24000}.
I want to merge {info} from the second table:
key value
single*2018 12000
mfj*2018 24000
I want to be able to have a place holder {2018} so a select join can show the description in the first table to change when tax years are changed ie from 2018 to 2019.

Creating a map with sales data in SSRS, how do I connect my sales numbers to counties via county names?

My dataset looks something like this:
County Product Sales
CountyA ProductA 200
CountyA ProductB 150
CountyB ProductA 100
CountyB ProductB 300
CountyC Product A 50
etc...
I am using SSRS within Visual Studio. I am creating the map using the "Report Items" section of the toolbox.
I have the map with my state's counties, I associated the COUNTYNAME field with the County column in my data, but that's as far as I can get. I cannot seem to get the sales numbers to show on the map at all.
I can get the county names to show up on the map as labels, but that's not particularly helpful. If I choose [SUM(Sales)] nothing appears on the report.
Ultimately, I want a map of Product A sales vs Product B sales by county.
Is my dataset insufficient? And if so, what additional data do I need to add? Are there any steps I am missing to get the county associated with the sales column?

Grouping dimension members using mdx query on the fly with aggregation

I am new to MDX and I just want to ask if it is possible in MDX query to make aggregations and groupings on the fly.
Here is the scenario, I have a dimension called "Department". And it has department code values e.g.
1234
1257
1346
1390
I also had a measure called "Sales".
What I need to do here is to make a Calculated Member that will get the Maximum "Sales" grouped per department based on the its first two digits. For example, consider the following output when browsing the cube using the Department dimension and Sales Measure
Department | Sales
1234 | 100
1257 | 200
1346 | 100
1390 | 400
Then I need to make an MDX query to produce an output something like below,
Department | Sales
12xx | 200
13xx | 400
You will notice that Maximum Sales based on the two digits of each department concatenated with "xx" string were the expected output.
Well determining the maximum is not a problem. with <name> as max(<something>) but you should reconsider the approach with the on the fly grouping.
I'm sure, that it is achievable, although I cannot provide a solution, but it will perform poorly. (I'm assuming that the digits of the department are not implemented as measure)
If you need this grouping more often you should add an additional dimension, or better a hierarchy to the department dimension.

Microsoft Access 2010 - Updating Multiple Rows with Different values in ONE query

I have a question about updating multiple rows with different values in MS Access 2010.
Table 1: Food
ID | Favourite Food
1 | Apple
2 | Orange
3 | Pear
Table 2: New
ID | Favourite Food
1 | Watermelon
3 | Cherries
Right now, it looks deceptively simple to execute them separately (because this is just an example). But how would I execute a whole lot of them at the same time if I had, say, 500 rows to update out of 1000 records.
So what I want to do is to update the "Food" table based on the new values from the "New" table.
Would appreciate if anyone could give me some direction / syntax so that I can test it out on MS Access 2010. If this requires VBA, do provide some samples of how I should carry this out programmatically, not manually statement-by-statement.
Thank you!
ADDENDUM (REAL DATA)
Table: Competitors
Columns: CompetitorNo (PK), FirstName, LastName, Score, Ranking
query: FinalScore
Columns: CompetitorNo, Score, Ranking
Note - this query is a query of another query, which in turn, is a query of another query (could there be a potential problem here? There are at least 4 queries before this FinalScore query is derived. Should I post them?)
In the competitors table, all the columns except "Score" and "Ranking" are filled. We would need to take the values from the FinalScore query and insert them into the relevant competitor columns.
Addendum (Brief Explanation of Query)
Table: Competitors
Columns: CompetitorNo (PK), FirstName, LastName, Score, Ranking
Sample Data: AX1234, Simpson, Danny, <blank initially>, <blank initially>
Table: CompetitionRecord
Columns: EventNo (PK composite), CompetitorNo (PK composite), Timing, Bonus
Sample Data1: E01, AX1234, 14.4, 1
Sample Data2: E01, AB1938, 12.5, 0
Sample Data3: E01, BB1919, 13.0, 2
Event No specifies unique event ID
Timing measures the time taken to run 200 metres. The lesser, the better.
Bonus can be given in 3 values (0 - Disqualified, 1 - Normal, 2 - Exceptional). Competitors with Exceptional are given bonus points (5% off their timing).
Query: FinalScore
Columns: CompetitorNo (PK), Score, Ranking
Score is calculated by wins. For example, in the above event (E01), there are three competitors. The winner of the event is BB1919. Winners get 1 point. Losers don't get any points. Those that are disqualified do not receive any points as well.
This query lists the competitors and their cumulative scores (from a list of many events - E01, E02, E03 etc.) and calculates their ranking in the ranking column everytime the query is executed. (For example, a person who wins the most 200m events would be at the top of this list).
Now, I am required to update the Competitors table with this information. The query is rather complex - with all the grouping, summations, rankings and whatnots. Thus, I had to create multiple queries to achieve the end result.
How about:
UPDATE Food
INNER JOIN [New]
ON Food.ID=New.ID
SET Food.[Favourite Food] = New.[Favourite Food]

Rows to Dynamic columns in Access

I need a setup in Access where some rows in a table are converted to columns...for example, lets say I have this table:
Team Employee DaysWorked
Sales John 23
Sales Mark 3
Sales James 5
And then through the use of a query/form/something else, I would like the following display:
Team John Mark James
Sales 23 3 5
This conversion of rows to columns would have to be dynamic as a team could have any number of Employees and the Employees could change etc. Could anyone please guide me on the best way to achieve this?
You want to create a CrossTab query. Here's the SQL that you can use.
TRANSFORM SUM(YourTable.DaysWorked) AS DaysWorked
SELECT YourTable.Team
FROM YourTable
GROUP BY YourTable.Team
PIVOT YourTable.Employee
Of course the output is slightly different in that the columns are in alphabetical order.
Team James John Mark
Sales 5 23 3
For more detail see Make summary data easier to read by using a crosstab query at office.microsoft.com