Converting data from matrix to vector format - mysql

I have huge set of data in an excel file in the following format
School_id percentage year subject
1100 90 2005 maths
1100 95 2006 maths
1100 81 2005 science
2310 45 2007 biology
I want to convert this data to this format
School_id year maths science biology
1100 2005 90 81
1100 2006 95
23100 2007 45
I dont have any idea for how to do this conversion. Will this be possible with excel or mysql or any other tool? Need some suggestions.
Thanks in advance

Yea so like they said pivot tables. To get your results, I had to do it using CONCATENATE function. There might be a better way. But this is how I did it:
first do a CONCATENATE column:
Then insert your pivot table and select the right options:
Then de-concatenate your School_id and Year
Like I said there might be a better way to do this - but then you would just need to organize your headers the way you want them and you should have what you are looking for. Good Luck.

Related

combining similar records to a single record in MS Access

I'm working MS Access 2016, on a table that has student results: So the fields are simply studentID, Test, and Score. For reporting purposes, I need to generate a CSV file that has a student's TestScore values all in one row. So if I had:
StudentID: Test: TestScore:
A123 TestA 80
A123 TestB 90
B123 TestA 70
B123 TestB 95
How do I generate a table for export that looks like:
StudentID: TestA: TestB:
A123 80 90
B123 70 95
I don't think crosstabs would work because not all students in the table have taken all the same tests. And there are several thousand cases. I also have come to understand that may not be possible via SQL in MS Access.
Many thanks in advance for any helpful advice.
You can add the ColumnHeadings property of the Crosstab Query, include all tests - "TestA";"TestB";...
Are there only a set number or could this grow?
So thank you to Andre and Alex for guiding me to this solution, which works for the original question:
Table name here is "Output".
TRANSFORM Last(Output.[TestScore]) AS LastOfTestScore
SELECT Output.[StudentID]
FROM [Output]
GROUP BY Output.[StudentID]
PIVOT Output.[Test];
For someone else searching for a similar solution, note that I used "Last" instead of something else (you might want First, Count, etc.).

Can I query 285 IDs and URLs in a single elegantly formed SQL query

I have to query 285 IDs and paths. At first glance, there are no real patterns to the ID's and I'm not certain there's a way to achieve this end with a simple and elegant query.
However, I'm by no means an expert and still learning SQL. So, hoping for some guidance.
Below is an extract of the first 50 enttries i'm trying to match:
ID path
261617 /About/Factfile
31 /About/Factfile/18060
761 /About/Factfile/18060/11550
762 /About/Factfile/18060/11552
763 /About/Factfile/18060/11555
35 /About/Factfile/scotlandsnapshot
63 /About/Government/background
74 /About/Government/sgprevious
1555 /About/Government/sgprevious/2007-2011
328782 /About/Government/sgprevious/2011-2016
1553 /About/Government/sgprevious/sgprevious1999-2003
1554 /About/Government/sgprevious/sgprevious2003-2007
46 /About/Information/expenditure
271169 /About/Information/expenditure/GPC
329992 /About/Information/expenditure/GPC/epc500-16-17
297247 /About/Information/expenditure/GPC/GPC
297249 /About/Information/expenditure/GPC/GPC/GPC
297243 /About/Information/expenditure/GPC/GPC500
271168 /About/Information/expenditure/over-25k
1550 /About/Information/expenditure/over-25k/background
1551 /About/Information/expenditure/over-25k/reports
22138 /About/Information/expenditure/over-25k/reports/2011-2012
291275 /About/Information/expenditure/over-25k/reports/expenditure
22137 /About/Information/expenditure/over-25k/reports/Expenditure2010
266779 /About/Information/expenditure/over-25k/reports/reports
303729 /About/Information/expenditure/over-25k/reports/Test
316271 /About/Information/expenditure/over-25k/reports/Test1
276826 /About/Information/expenditure/PSRA2010
293815 /About/Information/expenditure/PSRA2010/2011-12-PSRduties
318093 /About/Information/expenditure/PSRA2010/duties-13-14
311621 /About/Information/expenditure/PSRA2010/duties-2012-13
294347 /About/Information/expenditure/PSRA2010/historic-efficiency-reports
276831 /About/Information/expenditure/PSRA2010/historicexpenditure10-11and11-12
261611 /About/People
23769 /About/People/14944/Events-Engagements/MinisterialEngagements
148038 /About/People/14944/Events-Engagements/MinisterialEngagements/2008
148037 /About/People/14944/Events-Engagements/MinisterialEngagements/2009
148036 /About/People/14944/Events-Engagements/MinisterialEngagements/2010
148039 /About/People/14944/Events-Engagements/MinisterialEngagements/201112
268177 /About/People/14944/Events-Engagements/MinisterialEngagements/2012-13
296737 /About/People/14944/Events-Engagements/MinisterialEngagements/2013-14Engagements
304646 /About/People/14944/Events-Engagements/MinisterialEngagements/2014-15Engagements
317634 /About/People/14944/Events-Engagements/MinisterialEngagements/MinisterialEngagements
987 /About/People/14944/Special-Advisers
254426 /About/People/14944/Special-Advisers/gifts-hospitality
1048 /About/People/14944/travel
23479 /About/People/14944/travel/airtravel
23481 /About/People/14944/travel/ferrytravel
23483 /About/People/14944/travel/MinisterialCarJourneys
148029 /About/People/14944/travel/MinisterialCarJourneys/2010-11
Should this be split into 2 queries, 1 for the ID's and another for the paths?
Thank you all in advance and please forgive me if this is n00b stuff.
Kind regards,
V
SELECT id, path
FROM tbl
ORDER BY path
LIMIT 285;

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

Access Calculated Field

I am having difficulty trying to make a calculated field that I need. So here is what I am trying to do:
I have a query that combines the information based on three tables. The most important fields that for the application are as follows:
Family Income Age Patient
15,000 18 Yes
28,000 25 No
30,000 1 Yes
From here I want to make a calculated field that gives the correct program the patient was enrolled in. based on these fields ie:
Program Minimum Income Maximum Income Minimum Age Maximum Age Patient
Children's 0 20,000 1 19 Yes
Adult 0 12,000 19 65 No
Non Patient 0 20,000 1 19 No
Adult 2 12,000 50,000 19 65 No
Etc.
to create:
Family Income Age Patient Program
15,000 18 Yes Children's
28,000 25 No Adult 2
30,000 1 Yes Children's 2
I know I can use IIf to hard code it in to the field, but then it will be really difficult for other people to update the information as the guidelines change. Is it possible to have the information stored in a table? and use the information on the table form etc, or will I need to use IIf
Any Ideas? is it possible to dynamically create the IIf in SQL using VBA while pulling the information from the table?
EDIT:::
Thank you for your response and for formatting my tables, I still have no idea how you changed it, but it looks amazing!
I tried to add the SQL you added down below, but I was not able to make it work. I'm not sure if I made a mistake so I included the SQL of my Query. The query currently returns 0 values, so I think I messed something up. (The real Query is embarassing...I'm sorry for that). Unfortunately, I have done everything in my power to avoid SQL, and now I am paying the price.
SELECT qry_CombinedIndividual.qry_PrimaryApplicant.[Application Date],
qry_CombinedIndividual.qry_PrimaryApplicant.[Eligibility Rep],
qry_CombinedIndividual.qry_PrimaryApplicant.Name,
qry_CombinedIndividual.qry_PrimaryApplicant.Clinic,
qry_CombinedIndividual.qry_PrimaryApplicant.Outreach,
qry_CombinedIndividual.qry_PrimaryApplicant.[Content Type ID],
qry_CombinedIndividual.qry_PrimaryApplicant.[Application Status],
qry_CombinedIndividual.qry_PrimaryApplicant.Renewal,
qry_CombinedIndividual.qry_Enrolled.EthnicityEnr,
qry_CombinedIndividual.qry_Enrolled.GenderEnr, qry_CombinedIndividual.AgeAtApp,
qry_CombinedIndividual.[Percent FPL], tbl_ChildrensMedical.MinPercentFPL,
tbl_ChildrensMedical.MaxPercentFPL, tbl_ChildrensMedical.MinAge,
tbl_ChildrensMedical.MaxAge, tbl_ChildrensMedical.Program
FROM qry_CombinedIndividual
INNER JOIN tbl_ChildrensMedical ON qry_CombinedIndividual.qry_Enrolled.Patient = tbl_ChildrensMedical.Patient
WHERE (((qry_CombinedIndividual.AgeAtApp)>=[tbl_ChildrensMedical].[MinAge]
And (qry_CombinedIndividual.AgeAtApp)<[tbl_ChildrensMedical].[MinAge])
AND ((qry_CombinedIndividual.[Percent FPL])>=[tbl_ChildrensMedical].[MinPercentFPL]
And (qry_CombinedIndividual.[Percent FPL])<[tbl_ChildrensMedical].[MaxPercentFPL]));
Also there are many different programs. Here is the real Children's Table (eventually I would like to add adults if possible)
*Note the actual table uses FPL (which takes family size into account, but is used the same as income). I am again at a total loss as to how you formated the table.
Program Patient MinPercentFPL MaxPercentFPL MinAge MaxAge
SCHIP (No Premium) No 0 210 1 19
SCHIP (Tier 1) No 210 260 1 19
SCHIP (Tier 2) No 260 312 1 19
Newborn No 0 300 0 1
Newborn (Patient) Yes 0 300 0 1
Children's Medical Yes 0 200 1 19
CHIP (20 Premium) Yes 200 250 1 19
CHIP (30 Premium) Yes 250 300 1 19
Do I have the correct implementation for the table I have? Or should I be changing something. I can also send more information/sample data if that would help.
Thank you again!
I just created some tables with your sample data and used the following SQL. Your 3rd 'patient' doesn't match any of the ranges (Age 1, Income $30K)
SELECT tblPatient.PatName, tblPatient.FamInc, tblPatient.Age, tblPatient.Patient,
tblPatientRange.Program, tblPatientRange.MinInc, tblPatientRange.MaxInc, tblPatientRange.MinAge,
tblPatientRange.MaxAge, tblPatientRange.Patient
FROM tblPatient INNER JOIN tblPatientRange ON tblPatient.Patient = tblPatientRange.Patient
WHERE (((tblPatient.FamInc)>=[tblPatientRange]![MinInc] And (tblPatient.FamInc)<=[tblPatientRange]![MaxInc])
AND ((tblPatient.Age)>=[tblPatientRange]![MinAge] And (tblPatient.Age)<=[tblPatientRange]![MaxAge]));

MySQL query involving complicated summing function

I don't know how to put my requirement in words but let me explain it with a table and output that I desire
The table looks like this
Package LanguageUsed UnitsSold
lmn basic 43
xyz pascal 20
abc basic 50
cba c 20
Output I want is
LanguageUsed UnitsSold
basic 93 43+50
pascal 20 20+ nothing
c 20 20+ nothing
I need to display number of packages sold in each language
How can I do this in MySQL?
I need to display number of packages sold in each language
You can do:
SELECT languageUsed, SUM(UnitsSold)
FROM yourTABLE
GROUP BY languageUsed