I'm trying to display ALL the fields for all entries in the table analysis which meet criteria in other tables, WITHOUT displaying any duplicate rows in analysis.
Because I want to show all the fields in analysis, I can't use DISTINCT. So I use wildcard with GROUP BY but the query returns fields not just in analysis. It's bringing in fields from personal and exam as well.
SELECT *
FROM analysis
JOIN personal
ON analysis.FirstName=personal.FirstName
AND analysis.LastName=personal.LastName
JOIN exam
ON personal.P_ID=exam.P_ID
WHERE exam.Level='B1'
AND exam.Certification='Full'
GROUP BY analysis.LastName
I can get what I want using DISTINCT and specifying EVERY field in analysis, but there are currently 13 fields, so I'd prefer some neater syntax than:
SELECT DISTINCT analysis.LastName, analysis.FirstName, analysis.X, analysis.y, ETC ETC
FROM analysis
JOIN personal
ON analysis.FirstName=personal.FirstName
AND analysis.LastName=personal.LastName
JOIN exam
ON personal.P_ID=exam.P_ID
WHERE exam.Level='B1'
AND exam.Certification='Full'
SELECT analysis.*
FROM analysis
JOIN personal
ON analysis.FirstName=personal.FirstName
AND analysis.LastName=personal.LastName
JOIN exam
ON personal.P_ID=exam.P_ID
WHERE exam.Level='B1'
AND exam.Certification='Full'
Try above code.
Hope this will helps.
Related
I have two tables called transactions (TransactionID, HotelID, service..) and services (id, userid, HotelID, type) and I need to create view from there. In here 1st table has 15 tuples and 2nd one has 8 tuples.
When I wrote this SQL query:
CREATE VIEW summary
AS
SELECT TransactionID, userid, HotelID, service
FROM transactions, services
WHERE transactions.HotelID = services.name
I got 105 results in summary view. How I get rid of this duplication issue.
As far as i understands your question, i think you need to use group concate and group by using join query.
using group by and group concate duplication of record will be solve.
and using join you can get common data from both table.
try below query.
currently i am not having knowledge of your table structure that's why i take assumption of your query and created my own tables and as per them created query.
Eg:
select
c.country_id,c.country_name,group_concat(s.state_id),group_concat(s.state_name) from country c inner join state s on c.country_id =s.country_id group
by c.country_id;
if you have any query feel free to ask.
I am sure this question has already been answered, but I can't find it or the answer was too complicated. I am new to SQL and am not sure how to word this generically.
I have a mySQL database of software installed on devices. My query to pull all the data has more fields and more joins, but for brevity I just included a few. I need to add another dimension to create a report that lists every case where a device has more than one installation of software from the same product family.
sample
Right now I have code kind of like this and it is not doing what I need. I have seen some info on exists but the examples didn't account for multiple joins so the syntax escapes me. Help?
select
devices.name,
sw_inventory.product,
products.family_name,
sw_inventory.ignore_usage,
from sw_inventory
inner join products
on sw_inventory.product=products.product_name
inner join devices
on sw_inventory.device_name=devices.name
where sw_inventory.ignore=0
group by devices.name, products.family_name
There are plenty of answers out there on this topic but I definitely understand not always knowing terminology. you are looking for how to find duplicates values.
Basically this is a two step process. 1 find the duplicates 2 relate that back to the original records if you want those. Note the second part is optional.
So to literally find all of the duplicates of the query you provided
ADD HAVING COUNT(*) > 1 after group by statements. If you want to know how many duplicates add a calculated column to count them.
select
devices.name,
sw_inventory.product,
products.family_name,
sw_inventory.ignore_usage,
NumberOfDuplicates = COUNT(*)
from sw_inventory
inner join products
on sw_inventory.product=products.product_name
inner join devices
on sw_inventory.device_name=devices.name
where sw_inventory.ignore=0
group by devices.name, products.family_name
HAVING COUNT(*) > 1
I have got a somewhat complicated problem. This is my situation (ERD).
For a dashboard i need to create a pivot table that shows me the total amount of competences used by the vacancies. Therefore I need to:
Count the amount of vacancies per template
Count the amount of templates per competence
and last: multiply these numbers to get the total amount of comps used.
I have the first query:
SELECT vacancytemplate_id, count(id)
FROM vacancies
group by vacancytemplate_id;
And the second query isn't that difficult either, but I don't know what the right solution will be. I'm literally brainstuck. My mind can't comprehend how I can achieve the next step and put it down in a query. Please kind stranger, help me out :)
EDIT: my desired result is something like this
NameOfComp, NrOfTimesUsed
Leading, 17
Inspiring, 2
EDIT2: the meta query it should look like:
SELECT NameOfComp, (count of the competences used by templates) * (number of vacancies per template)
EDIT3: http://sqlfiddle.com/#!9/2773ca SQLFiddle
Thanks a lot!
If I am understanding your request correctly, you are wanting a count of competences per vacancy. This can be done very simply due to your table structure:
Select v.ID, count(*) from vacancy as v inner join CompTemplate_Table as CT
on v.Template_ID = CT.Template_ID group by v.ID;
The reason you can do only one join is because there will be a record in the CompTemplate_Table for every competency in each template. Additionally, the same key is used to join vacancy to templates as is used to join templates to CompTemplate_Table, so they represent the same key value (and you can skip joining the Templates table if you don't need data from there).
If you are wanting to add this data to a pivot table, I will leave that exercise to you. There are a number of tutorials available if you do a quick google search and it should not be that hard.
UPDATE: For the second query you are looking at something like:
Select cp.NameOfComp, count(*) from vacancy as v inner join CompTemplate_Table as CT
on v.Template_ID = CT.Template_ID inner join competencies as CP
on CP.ID = CT.Comp_ID
group by CP.NameOfComp
The differences here are you are adding in the comptetencies table, as you need data from that, and grouping by the CP.NameOfComp instead of the vacancy id. You can also restrict this to specific templates, competencies, or vacancies by adding in search conditions (e.g. where CP.ID = 12345)
In one of my tables, some customers have multiple lines - this could be due to re-visits from technicians etc. What I want to do is for each customer ID, analyse whether a re-vist has taken place and place a marker against their name.
I have tried to combine an if/in statement that analyses the max/min visit dates for each customert ID. So if the max>min its classed as a "re-visit", however, i keep getting a syntax error.
Can someone help?
This is a job for two SQL queries:
1st query:
SELECT customerID, count(customerID) as visitCount
FROM tableOfInterest
GROUP BY customerID
2nd query uses first query:
UPDATE customerManifest INNER JOIN queryAbove ON queryAbove.customerID = customerManifest.customerID
SET customerManifest.multipleVisitIndicatorField to queryAbove.visitCount
I am using the following JOIN statement:
SELECT *
FROM students2014
JOIN notes2014 ON (students2014.Student = notes2014.NoteStudent)
WHERE students2014.Consultant='$Consultant'
ORDER BY students2014.LastName
to retrieve a list of students (students2014) and corresponding notes for each student stored in (notes2014).
Each student has multiple notes within the notes2014 table and each note has an ID that corresponds with each student's unique ID. The above statement is returning a the list of students but duplicating every student that has more than one note. I only want to display the latest note for each student (which is determined by the highest note ID).
Is this possible?
You need another join based on the MAX noteId you got from your select.
Something like this should do it (not tested; next time I'd recommed you to paste a link to http://sqlfiddle.com/ with your table structure and some sample data.
SELECT *
FROM students s
LEFT JOIN (
SELECT MAX(NoteId) max_id, NoteStudent
FROM notes
GROUP BY NoteStudent
) aux ON aux.NoteStudent = s.Student
LEFT JOIN notes n2 ON aux.max_id = n2.NoteId
If I may say so, the fact that a table is called students2014 is a big code smell. You'd be much better off with a students table and a year field, for many reasons (just a couple: you won't need to change your DB structure every year, querying across years is much, much easier, etc, etc). Perhaps you "inherited" this, but I thought I'd mention it.
GROUP the query by studentId and select the MAX of the noteId
Try :
SELECT
students2014.Student,
IFNULL(MAX(NoteId),0)
FROM students2014
LEFT JOIN notes2014 ON (students2014.Student = notes2014.NoteStudent)
WHERE students2014.Consultant='$Consultant'
GROUP BY students2014.Student
ORDER BY students2014.LastName