I have 2 tables, one contains people and another contains enrolments with a reference to the people id's, I have to create a view where only results that appear in the enrolments more than 4 times are included. I'm okay with making the view and getting the columns I need but the syntax is so confusing and the course I'm doing is very unclear. I think I need to use count() but I can't get it to count what I need it to. How do you output a table that only includes the people that appear in the enrollment table more than 4 times? Sorry if the question is a bit bad I'm extremely sleep deprived and confused.
edit: here are some example tables and what I tried to do-
enrolment table relevant info:
id
student
462583
1010093
464457
1010093
469823
1010093
471345
1010093
473239
1010093
475371
1010093
477419
1010093
479797
1010093
572312
1010138
577147
1010138
578866
1010138
580596
1010138
582497
1010138
so students 1010093 and 1010138 would fit the criteria because they appear more than 4 times, but there are many entries that do not appear more than 4 times.
people table relevant info:
(id is the id that enrolment refers to in student column).
id
uniid
name
10000019
8758024
Emery Schubert
10000021
9808692
Ann Moore
10000025
9833783
Zhen-Tian Chang
10000026
7610575
John Carrick
10000035
9837669
Pamela Mort
10000037
9049091
Sami Korell
10000049
9869271
Mengistu Amberber
10000051
9375982
Colin Fong
10000053
9146607
Dianne Montgomerie
10000073
9804805
Grant Walter
1010093
2220747
Barbara Fremder
1010138
2240781
Say-Kit Ezergailis
1011114
2119574
Evangelos McDonald
1011293
2291530
Grace Hoekstra
1011474
2261154
Chee Jairaj
my attempt was this:
create or replace view Q1(uniid,name) as
select people.uniid, people.name
from people left outer join enrolments on (people.id = enrolments.student)
group by people.uniid, people.name having count(enrolments.student) > 4;
here's a sample of the output:
uniid
name
3100280
Mia Wiech
3225571
Cora Prochaska
3335780
Vinh Ha
3255146
Moyang Liu Hongtao
3365147
Frances Ellers
3327487
Keerati Meechowna
3397549
Shane Dinham
3372084
Benjamin Tenenbaum
3252837
Kayserline McFarlane
3350110
Jose Varas
3258061
Alison Lettich
3345581
Snehal Sethu
This was the expected output in full:
uniid
name
3001394
Jeffrey Caldwell
3087372
Philip Lun
3108948
Sugianto Arsie
3122927
Seaton Warburton
3132303
Amy Berg
3134147
Hannah Kola
3160479
Ksenia Mardakhaeva
3163349
Kerry Plant
3173796
Adam Rositano
3187169
Giles Erol
3207313
Livio Tjia
3209530
Su Song
3229297
Yoke Anthoney
3245227
Pollyanna Risk
3272803
Jesus Ferrer
3294743
Deviani Hongganata
Related
Related to PowerShell 5.1
I was playing around with XML to show how to handle conflicting namespaces. Here's the example I created:
<Employees>
<ms:Employee id='1' xmlns:ms="MicrosoftEmployees">
<FirstName>Bill</FirstName>
<LastName>Gates</LastName>
</ms:Employee>
<ms:Employee id='2' xmlns:ms="MicrosoftEmployees">
<FirstName>Paul</FirstName>
<LastName>Allen</LastName>
</ms:Employee>
<ap:Employee id='1' xmlns:ap="AppleEmployees">
<Name>Steve Jobs</Name>
</ap:Employee>
<ap:Employee id='2' xmlns:ap="AppleEmployees">
<Name>Steve Wozniak </Name>
</ap:Employee>
</Employees>
The scenario might be combining data from two different companies.
PowerShell demonstration program:
cls
$filename = "c:\XMLClass\IntroSamples\Sample05_Simpler_Namespace.xml"
[xml]$xmlDoc = Get-Content $filename
$xmlDoc.Employees.Employee[0]
$xmlDoc.Employees.Employee[1]
$xmlDoc.Employees.Employee[2]
$xmlDoc.Employees.Employee[3]
Output:
id ms FirstName LastName
-- -- --------- --------
1 MicrosoftEmployees Bill Gates
2 MicrosoftEmployees Paul Allen
1
2
Is there anyway to get a more logical output?
It seems like PowerShell locks into the first schema it sees for the Employee element, then cannot show the Name element of the Apple employees. This actually makes sense, but I was just checking to see if there is something fancier to handle this that I might be missing.
I know I could use SelectSingleNodes and XPath, but was just trying to see if and how PowerShell could handle this "out of the box".
If I reverse the code:
$xmlDoc.Employees.Employee[2]
$xmlDoc.Employees.Employee[3]
$xmlDoc.Employees.Employee[1]
$xmlDoc.Employees.Employee[0]
Then the output is:
id ap Name
-- -- ----
1 AppleEmployees Steve Jobs
2 AppleEmployees Steve Wozniak
1 ms:Employee
2 ms:Employee
Use format list to see all the properties. Format-table doesn't handle different sets of properties well.
$xmldoc.employees.employee | format-list
id : 1
ms : MicrosoftEmployees
FirstName : Bill
LastName : Gates
id : 2
ms : MicrosoftEmployees
FirstName : Paul
LastName : Allen
id : 1
ap : AppleEmployees
Name : Steve Jobs
id : 2
ap : AppleEmployees
Name : Steve Wozniak
I am using the musicbrainz.org postgresql database, which I have installed locally and have accessed via python.
The database is a list of music artists and associated criteria. Here is the schema :
How can I create a SQL query that outputs all tracks for a release group? I am able to get the proper artist info and releases associated with a specific band but the tracks for the release are wrong using the below query's:
strsql_band = "SELECT artist.id, artist.gid, artist.name, artist.comment FROM artist WHERE artist.name=%s AND type>1 ORDER BY type, artist.last_updated DESC"
strsql_memberid = "SELECT entity0, link.begin_date_year, link.end_date_year FROM l_artist_artist l JOIN link ON l.link=link.id WHERE entity1=%s AND link_type=103"
strsql_release = "SELECT id, release_group.name FROM release_group WHERE artist_credit=%s"
# This does not return the correct tracks for the release
strsql_track = "SELECT id, position, name, length FROM track WHERE artist_credit=%s LIMIT 15"
Any help would be greatly appreciated, I have been banging my head on this for hours now.
medium and track table will give you this information
Here is the query that worked for my case:
SELECT
array_agg(track.name) as track_list,
release.gid as release_gid,
release.release_group as release_group_id
FROM track
INNER JOIN medium
ON track.medium=medium.id
INNER JOIN release
ON medium.release=release.id
GROUP BY release.gid, release.release_group limit 5;
Sample Results
release_gid release_group track_list
00000363-74c9-4c3b-bef7-a433eb9687e3 950559 {"Small Screen",Alice,Dauntless,Lullaby,"Grinding the Mill","Four Colors","2000 Gods","This Life","More Than Meets the Eye","The World"}
00000ba0-d5e2-41d7-9c5d-48e8eb04226a 388941 {"Ai, Vanna","Dod māmiņa, kam dodama","Mēs sūdzēsimies Strazdbūrā","Ar Eiropu plecos","Mīļais mans (Oi ļuļi, oi ļuļi)","Tu tik lūr, monamūr","Kocēni, Dikļi un Tožas","Sev augstu laimi vēlēju","Happy mērsrags","Tur es dzēru, tur man tika","Pritonā pie eglītes","Kur ir manas dāvaniņas","Iespraud svecīti","Ints un Ģirts","Mazs putniņš kājām... (Vai Rīga gatava?)","Melngalvju blūzs",Siergriezējs,"Zeva dziesma","Kentaura skaistumkopšanas salonā","Es sakūru uguntiņu (Gaisā skrēja)","Tu nesit... (Nestandarta žoklis)"}
00000ff3-8918-400f-89d5-ec80f52465bf 530779 {"SING IT","Precious Days",トキメキ}
0000172d-6e43-4d7d-8647-da718593a97a 2382060 {"J'ai besoin d'un doliprane","Cœur brisé","Mon proprio","J'me demande","Extra extra terrestre","Bienvenue chez moi (dans ma coloc)","L'amour c'est du pipeau","La caissière du casino",Jean-Marc,"Toutes des salopes"}
can anyone provide me with solution for my coding. For your information, I made a query from my answer table which only consist id, staff_id, dept_name, question_id, ans, evaluator, and year. Below is my code:-
// Make a mysqli Connection
$connect = new mysqli('localhost', 'root', '', 'cpsdatabase');
//Mean by staff Id
$dept_name = $_GET['dept_name'];
$query = "SELECT staff_id,dept_name, AVG(ans)
FROM hodanswer WHERE dept_name='$dept_name'
group by staff_id";
$result=mysqli_query($connect, $query);
// Print out result
while($row = mysqli_fetch_array($result))
{
echo "The mean of staff id =  ". $row['staff_id']."   from department  ".$row['dept_name']."       is    ". $row['AVG(ans)'];
echo "<br />";
}
I want to find mean and I did get the result. My problem is I want to retrieve the staff name based on staff id but staff name does not include in answer table. Staff name provided in staff table. How can I retrieve staff_name from table STAFF and display result based on code above. Please help me.
If I'm understanding your tables correctly then this should work. This will perform an inner join between tables STAFF and hodanswer and will display all staff id and staff name from table STAFF, respectively, where the staff id is equal to the staff_id(s) that are present in table hodanswer.
SELECT a.id, a.staff_name FROM STAFF a INNER JOIN hodanswer b ON a.id = b.staff_id;
Google up SQL INNER JOIN
I am getting a tad frustrated and was wondering if you can help:
I have a Pitch Values Table with the following Columns PitchValues_Skey, PitchType_Skey (this is a foreign key), Start Date, End Date and finally value:
For Example:
1 7 01/01/2010 31/12/2010 £15
2 7 01/01/2011 31/12/2011 £20
And all I want to do is update my Bookings table with how much each booking is going to be, so I put together the code below which worked fine when I only had 2010 data, but I know have 2011 and 2012 and want to update it but it will only update with the 2010 prices.
SELECT Bookings.Booking_Skey, DATEDIFF(day, Bookings.ArrivalDate, Bookings.DepartureDate) * PitchValues.Value AS BookingValue,
PitchValues.PitchType_Skey
FROM Bookings INNER JOIN
PitchValues ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
WHERE (Bookings.Booking_Skey = 1)
So when I run the query above I would expect to see one line of data but instead I see 4 (See Below)
I would expect this:
Booking_Skey BookingValue PitchType_Skey
1 420 4
But I get this
Booking_Skey BookingValue PitchType_Skey
1 420 4
1 453.6 4
1 476.7 4
1 476.7 4
All sorted now, thanks for your help.
SELECT Bookings.Booking_Skey, DATEDIFF(DAY, Bookings.ArrivalDate, Bookings.DepartureDate) * PitchValues.Value AS BookingValue, PitchValues.PitchType_Skey
FROM Bookings
INNER JOIN PitchValues ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
AND Bookings.ArrivalDate BETWEEN PitchValues.StartDate AND PitchValues.EndDate
WHERE (Bookings.Booking_Skey = 1)
I have an Entity Framework model that has two tables, client and postcode. Postcode can have many clients, client can have 1 postcode. They are joined on the postcode.
The two tables are mapped to views.
I have some clients that do not have a Postcode in the model, however in the DB they do!
I ran some tests and found postcodes that were returning clients when I do Postcode.Clients but not all of the clients? In the db a postcode had 14 related clients but EF was only returning the first 6. Basically certain postcodes are not returning all the data.
Lazy loading is turned on and I have tried turning it off without any luck.
Any ideas?
I am using VS 2010, C#, .NET 4.0, EF4 and SQL Server 2008
Thanks
UPDATE:
I have been running through this in LinqPad. I try the following code
Client c = Clients.Where(a => a.ClientId == 9063202).SingleOrDefault();
c.PostcodeView.Dump();
This returns null.
I then take the generated SQL and run this in a separate SQL query and it works correctly (after I add the # to the start of the variable name)
SELECT TOP (2)
[Extent1].[ClientId] AS [ClientId],
[Extent1].[Surname] AS [Surname],
[Extent1].[Forename] AS [Forename],
[Extent1].[FlatNo] AS [FlatNo],
[Extent1].[StNo] AS [StNo],
[Extent1].[Street] AS [Street],
[Extent1].[Town] AS [Town],
[Extent1].[Postcode] AS [Postcode]
FROM (SELECT
[ClientView].[ClientId] AS [ClientId],
[ClientView].[Surname] AS [Surname],
[ClientView].[Forename] AS [Forename],
[ClientView].[FlatNo] AS [FlatNo],
[ClientView].[StNo] AS [StNo],
[ClientView].[Street] AS [Street],
[ClientView].[Town] AS [Town],
[ClientView].[Postcode] AS [Postcode]
FROM [dbo].[ClientView] AS [ClientView]) AS [Extent1]
WHERE 9063202 = [Extent1].[ClientId]
GO
-- Region Parameters
DECLARE #EntityKeyValue1 VarChar(8) = 'G15 6NB'
-- EndRegion
SELECT
[Extent1].[Postcode] AS [Postcode],
[Extent1].[ltAstId] AS [ltAstId],
[Extent1].[ltLhoId] AS [ltLhoId],
[Extent1].[ltChcpId] AS [ltChcpId],
[Extent1].[ltCppId] AS [ltCppId],
[Extent1].[ltWardId] AS [ltWardId],
[Extent1].[ltAst] AS [ltAst],
[Extent1].[ltCpp] AS [ltCpp],
[Extent1].[ltWard] AS [ltWard],
[Extent1].[WardNo] AS [WardNo],
[Extent1].[Councillor] AS [Councillor],
[Extent1].[ltAdminCentre] AS [ltAdminCentre],
[Extent1].[ltChcp] AS [ltChcp],
[Extent1].[Forename] AS [Forename],
[Extent1].[Surname] AS [Surname],
[Extent1].[AreaNo] AS [AreaNo],
[Extent1].[LtAomId] AS [LtAomId],
[Extent1].[OOHltCoordinatorId] AS [OOHltCoordinatorId],
[Extent1].[OvernightltCoordinatorId] AS [OvernightltCoordinatorId],
[Extent1].[DayltCoordinatorId] AS [DayltCoordinatorId]
FROM (SELECT
[PostcodeView].[Postcode] AS [Postcode],
[PostcodeView].[ltAstId] AS [ltAstId],
[PostcodeView].[ltLhoId] AS [ltLhoId],
[PostcodeView].[ltChcpId] AS [ltChcpId],
[PostcodeView].[ltCppId] AS [ltCppId],
[PostcodeView].[ltWardId] AS [ltWardId],
[PostcodeView].[ltAst] AS [ltAst],
[PostcodeView].[ltCpp] AS [ltCpp],
[PostcodeView].[ltWard] AS [ltWard],
[PostcodeView].[WardNo] AS [WardNo],
[PostcodeView].[Councillor] AS [Councillor],
[PostcodeView].[ltAdminCentre] AS [ltAdminCentre],
[PostcodeView].[ltChcp] AS [ltChcp],
[PostcodeView].[Forename] AS [Forename],
[PostcodeView].[Surname] AS [Surname],
[PostcodeView].[AreaNo] AS [AreaNo],
[PostcodeView].[LtAomId] AS [LtAomId],
[PostcodeView].[DayltCoordinatorId] AS [DayltCoordinatorId],
[PostcodeView].[OOHltCoordinatorId] AS [OOHltCoordinatorId],
[PostcodeView].[OvernightltCoordinatorId] AS [OvernightltCoordinatorId]
FROM [dbo].[PostcodeView] AS [PostcodeView]) AS [Extent1]
WHERE [Extent1].[Postcode] = #EntityKeyValue1
Ended up removing the relationship and manually getting child data.
Nasty but cannot find a reason why this is happening. Cheers for the comments