MySQL - Connect three different tables in transposed fashion - mysql

I have three tables:
business:
id name
1 Charlie's Bakery
2 Mark's Pizza
3 Rob's Market
balanco_manual:
id business_id year unit
012 1 2015 ones
123 1 2014 tens
364 2 2014 cents
conta_balanco:
id conta balanco_id valor
412 12.3 012 12324
344 12.5 012 54632
414 14.1 364 344122
789 12 364 2312415
646 12 123 342
I need to combine them all in the business table and make them look like this:
business:
id name 12.3-2015 12.5-2015 11.56-2015 12-2014 2015-unit 2014-unit
1 Charlie's Bakery 12324 54632 NaN 342 ones tens
2 Mark's Pizza NaN NaN NaN 2312415 NaN cents
3 Rob's Market NaN NaN NaN NaN NaN NaN
Explaining a little bit further: the business table has basic registries about the businesses, balanco_manual has yearly information of each one of those businesses and conta_balanco has details of the yearly information in balanco_manual.
Trying to put that last table into words:
- First I need to join business with balanco_manual, combining the "id" column in business with the "business_id" column in balanco_manual. Note that I combine unit and year in one single column named "[year]-unit". Let's call this table "new_business" to make it easir to understand
- After, I need to combine "new_business" with conta_balanco in a similar way we did with the "unit" column. Each "conta" should be combined with the year and become a column "conta-[year]".
I'm quite a beginner with SQL and I'm having interesting difficulties. Could someone help me to crack that out?

Related

MySQL: Select Results Not As Expected: Newbie Alert

I used SQL back in the 90s and starting to get back into it for a project at work.
I want to store departmental budget data so I can run queries and use it in Power Bi.
I have a simple table with columns for the department (fund) and corresponding monthly expense info. I am trying to run a query where I can see the expenses for one fund under the supplies account -- can't get it to work. I know I will probably kick myself when I see the solution so here we go...
Select * FROM expenses
FundID Fund FY Account January February March April May June July August
9999 tester 2019 Grant Assistance to INT'L Individuals 1 2 3 4 5 6 7 8
9999 tester 2019 Grant Assistance to INT'L Organizations 21 22 23 24 25 26 27 28
9999 tester 2019 Grant Assistance to U.S. Individuals 31 32 33 34 35 36 37 38
9999 tester 2019 Grant Assistance to U. S. Organizations 41 42 43 44 45 46 47 48
9999 tester 2019 Salaries, etc. 4500 4500 4500 4500 4500 4500 4500 4500
Table = expenses
Columns
FundID, Fund, FY, Account, January, February… December
SELECT *
FROM expenses;
— results in all data from sample table (as expected)
SELECT *
FROM expenses
WHERE Fund = 'associate1';
— results in all data for ‘associate1’ (as expected)
SELECT *
FROM expenses
WHERE Fund = 'associate1' AND Account = 'Supplies';
— results in alert advising ‘empty result set (i.e. zero rows) (not expected)
—- rows/record is populated
Expected:
FundID Fund FY…. December populated with associate1’s data for supplies.
I also attempted query: SELECT * FROM expenses WHERE Account = ‘Supplies’; (Nothing)

how to create schema for similar data for different areas

I have to create tables to store weather for different areas for different entities. What is the best way to create table structure for this. For example:
maximum temp for Wales
year jan feb
1950 0.5 2.5
1955 1. 2
.
.
2017 0.5 2
minimum temp for Wales
year jan feb
1995 -5 -7
.
.
2018 -8 -9
is it better to create multiple tables for each city for different recording or is there a better way.
areas table
-----------
id
name
temperatures table
------------------
id
area_id
year
month
min_temp
max_temp

5 point average in SSRS

I try to put a 5 point avg in my chart. I add a trendline, but it looks like this:
And then I created a new series to calculate there the avg. and this looks like this:
but I would like to show this in a 5 point average. How can I do this?
This answer is based on my experience with Excel, not reporting services, but it is probably the same problem.
Your chart is probably a scatter plot rather than a line chart (note: this is Excel terminology). A scatter plot does not have an intrinsic ordering in the data. A line chart does.
The solution (for a scatter plot) is simply to sort the data by the x-values. The same will probably work for you. If you are pulling the data from a database, then order by can accomplish this. Otherwise, you can sort the data in the application.
Using this post as a starting point you can see that it is possible to calculate a moving average for a chart using the SQL query that pulls the data from the database.
For example, using this table in my database called mySalesTable
myDate sales myDate sales myDate sales
---------- ------ ---------- ------ ---------- ------
01/01/2015 456 16/01/2015 546 31/01/2015 658
02/01/2015 487 17/01/2015 12 01/02/2015 121
03/01/2015 245 18/01/2015 62 02/02/2015 654
04/01/2015 812 19/01/2015 516 03/02/2015 261
05/01/2015 333 20/01/2015 1 04/02/2015 892
06/01/2015 449 21/01/2015 65 05/02/2015 982
07/01/2015 827 22/01/2015 15 06/02/2015 218
08/01/2015 569 23/01/2015 656 07/02/2015 212
09/01/2015 538 24/01/2015 25 08/02/2015 312
10/01/2015 455 25/01/2015 549 09/02/2015 21
11/01/2015 458 26/01/2015 261
12/01/2015 542 27/01/2015 21
13/01/2015 549 28/01/2015 21
14/01/2015 432 29/01/2015 61
15/01/2015 685 30/01/2015 321
You can pull out this data, and create a Moving average based on the last 5 dates by using the following query for your dataset
SELECT mst.myDate, mst.sales, avg(mst_past.sales) AS moving_average
FROM mySalesTable mst
JOIN mySalesTable as mst_past
ON mst_past.myDate
BETWEEN DATEADD(D, -4, mst.myDate) AND mst.myDate
GROUP BY mst.myDate, mst.sales
ORDER BY mst.myDate ASC
This is effectively joining a sub-table for each row consisting of the previous 4 dates and the current date, and finds the average for these dates, outputting that as the column moving_average
You can then chart both these fields as normal, to give the following output (with the data table so you and see the actual calculated moving average)
Hopefully this will help you. Please let me know if you require further assistance

SQL: Aggregate function returning 0's when using WHERE filter

I have a table with 10 columns listing results of a marketing campaign including person name, date, residence area, request, and response.
The request column is always a number, indicating the number of survey requests we've sent a specific person.
The response column is a number from 0 to X, representing the number of surveys a person has responded to take.
Now, when I look through the actual table, there are probably around 10% response rate. With lots of non-zero entries in the response column.
However, when I write an aggregate function like this:
SELECT person, date, SUM(requests), SUM(response)
FROM analytics.SurveyResults0304
WHERE group_type = 'Youth'
GROUP BY person, date;
I get a correct number for SUM(requests), but I get a big fat "0" for SUM(response)?
It's the same for all 3 group types. It returns a 0 for SUM(response)
Update: it works fine when I don't include the group_type filter, but how come I can't use it with the WHERE filter?
Thanks!!!
EDIT2: Sample Table
Person Date Group_Type Requests Response Neighborhood FirstName
-------- -------- ----------- -------- -------- ------------ ---------
Nixon 3/3/2013 Youth 3 3 Chinatown Richard
Clinton 3/3/2013 Youth 4 0 Gunhill Bill
Mao 3/3/2013 Youth 5 0 Berryville Chairman
Nixon 3/4/2013 Youth 17 2 Townsford Richard
Gates 3/3/2013 Elderly 41 5 Chinatown Bill
Gates 3/4/2013 Elderly 0 0 Chinatown Bill
Gates 3/5/2013 Elderly 0 0 Chinatown Bill
Gates 3/6/2013 Elderly 0 0 Chinatown Bill
For example
When I do:
SELECT SUM(requests), SUM(response)
FROM analytics.SurveyResults0304
WHERE group_type = 'Youth';
It returns 70 for request, and 0 for response across the board.
Your code seems to be working. See SQL Fiddle built from your data sample.
May be you have extra spaces and/or tabs in your Group_Type column?
PERSON DATE SUM(REQUESTS) SUM(RESPONSE)
--------------------------------------------------------
Clinton March, 03 2013 00:00:00+0000 4 0
Mao March, 03 2013 00:00:00+0000 5 0
Nixon March, 03 2013 00:00:00+0000 3 3
Nixon March, 04 2013 00:00:00+0000 17 2

Dynamic Columns In Reporting Services Tablix

I have a data query that returns data in the following format:
Name Period Value
-----------------------------
Bob Jan 123
Bob Feb 456
Bob Mar 789
Tom Jan 321
Tom Feb 654
Tom Mar 987
Joe Jan 147
Joe Feb 258
Joe Mar 369
The different periods are constant between names, but will be different between executions of the report (ie, I may query a report on Jan/Feb/Mar now, or Apr/May/Jun later). I'm trying to put that into a table in my Reporting Services report, that would look something like this:
Name Jan Feb Mar
----------------------------
Bob 123 456 789
Tom 321 654 987
Joe 147 258 369
Can anyone point me to an example of doing something like that? I'm not even sure how to describe that 'rotation'(?) of the data. The columns should be dynamic based on what Period values are in the dataset.
Found the answer right after posting. Here's what I did.
Created a new Tablix. Dragged the Name field to the data row of the first column. Dragged the Value column to the data row of the second column. Dragged the Period field to the header row of the second column. That created a new group, and a new second level header column. I then copied the value from that new top header column to the header cell below it, and deleted the whole new header row. When asked whether to delete the row and associated groups, or just row, choose just the row. You should be left with a Row Group and two Column Groups, and it should render as intended.