I have a table with LastName, FirstName, Wins, Losses, CompFormat and Medals columns. In case the person who told me not to use pictures on my last question sees this I tried your suggestion and couldn't figure it out so I have to use pictures. So don't bite my head off this time. I successfully added and grouped Wins with CompFormat like this...
("SELECT SUM(Wins) AS Total, FirstName, LastName, CompFormat FROM CompetitionDate GROUP BY LastName, FirstName, CompFormat;")
Which correctly produced this in my datagridview...
Instead of what I did I want to add counting losses and group it to look like this
Here is my access table...
I think you just need to add the Losses column:
SELECT FirstName, LastName, CompFormat,
SUM(Wins) AS Wins, SUM(Losses) as Losses
FROM CompetitionDate
GROUP BY LastName, FirstName, CompFormat
Related
Dataset
I have dataset looks like in the picture, where do I start to get query like i wrote on the title.
thanks
Its better to post the output format and sample data-set in question.
Following is something that you can use to get min and max ages across gender -
with agg_cte as
(select first_name, last_name, gender,age,
max(age) over (partition by gender) max_age,
min(age) over (partition by gender) min_age
from users)
select first_name,last_name,age,gender,
case when age=max_age and gender='F' then 'female_with_max_age'
when age=min_age and gender='F' then 'female_with_min_age'
when age=max_age and gender='M' then 'male_with_max_age'
when age=min_age and gender='M' then 'male_with_min_age' end as classification
from agg_cte
where age=max_age
or age=min_age
Refer to fiddle here.
I am new to SQL and looking for some help here. Please see the first screenshot. the first two records have exact values except for the country field. the second screenshot is what I want to retrieve. I want to get the first one (with a not-null country). I also want to keep the third record since it's the only record for id 345, although it has a null country. I tried the following query but didn't get me the results I want.
SELECT DISTINCT id, first, last, age, gender, city, state, zip
FROM Person
WHERE country IS NOT NULL
Something like
select id, first, last, age, gender, city, state, zip, min(country)
from persons
group by id, first, last, age, gender, city, state, zip
should work, since by default the ordering of varchar2 values is ascending and nulls are last.
You can use the row_number analytic function to do this.
By partitioning by id and then filtering by rn = 1, we ensure that we get no more than one row per distinct id value.
The order by clause in the row_number function is what determines which row gets returned. In this case, rows with a non-null country value are prioritized. If you have more complex rules to determine which row you want to return per id, just adjust the order by clause accordingly.
select id, first, last, age, gender, city, state, zip, country
from (select id, first, last, age, gender, city, state, zip, country
row_number() over(
partition by id
order by case when country is not null then 1 else 2 end) as rn
from tbl)
where rn = 1
I'm currently trying to list the number of visits attended by each vet. I have to include the vets name details with the count of visits attended. I then have to sort the report by the vets title.
Visit table structure:
visit_id, vet_id, pet_id, Visit_Date, Basic_Cost, Symptom, Treatment
Vet table structure:
vet_id, Surname, Forename, Title, Ext_Number, Position, Salary
Below is my initial SQL command that I have created. It doesn't work and I was wondering how to create an SQL command for the above question. All help is appreciated and please note I'm using ms access.
SELECT vet_id, Title, Forename, Surname, COUNT(vet_id)
FROM visit, vet
ORDER BY Title;
Assuming you want all vets even if they haven't had visits...
SELECT P.vet_id, Title, Forename, Surname, coalesce(COUNT(V.vet_id),0) as VetVisitCount
FROM VET P
LEFT JOIN Visit V
on P.Vet_ID = V.Vet_ID
GROUP BY P.vet_id, Title, Forename, Surname
ORDER BY Title;
SELECT vet_id, title, Forename, Surname,
(SELECT COUNT(visit.vet_id) FROM visit WHERE visit.vet_id = vet.vet_id) AS visits
FROM vet
ORDER BY vet.vet_id;
You need a GROUP BY
SELECT vet_id, Title, Forename, Surname, COUNT(vet_id)
FROM visit, vet
GROUP BY vet_id, Title, Forename, Surname
ORDER BY Title;
I have two tables in a SQLite DB, and both have the following fields:
idnumber, firstname, middlename, lastname, email, login
One table has all of these populated, the other doesn't have the idnumber, or middle name populated.
I'd LIKE to be able to do something like:
select idnumber, firstname, middlename, lastname, email, login
from users1,users2 group by login;
But I get an "ambiguous" error. Doing something like:
select idnumber, firstname, middlename, lastname, email, login from users1
union
select idnumber, firstname, middlename, lastname, email, login from users2;
LOOKS like it works, but I see duplicates. my understanding is that union shouldn't allow duplicates, but maybe they're not real duplicates since the second user table doesn't have all the fields populated (e.g. "20, bob, alan, smith, bob#bob.com, bob" is not the same as "NULL, bob, NULL, smith, bob#bob.com, bob").
Any ideas? What am I missing? All I want to do is dedupe based on "login".
Thanks!
As you say union will remove duplicate records (note that union all won't!). Two records are considered duplicates when all their column values match. In the example you considered in your question it is clear that NULL is not equal to 20 or 'alan' so those records won't be considered duplicates.
Edit:
[...] the only way I can think would be creating a new table [...]
That is not necessary. I think you can do the following:
select login, max(idnumber), max(firstname), max(middlename), max(lastname),
max(email) from (
select idnumber, firstname, middlename, lastname, email, login from users1
union
select idnumber, firstname, middlename, lastname, email, login from users2
) final
group by login
However, if you're sure that you only have different values on idnumber and middlename you can max only those fields and group by all the rest.
You could left join the incomplete table to the complete one via the login. Then programmatically manipulate the resulting set.
Someone else designed this table and I am not allowed to modify it so bear with me.
I am trying to insert multiple rows from one table into another. The table where I am inserting the rows has an ID but it does not auto-increment. I cannot figure out how to manually increment the id as I insert rows. The current code throws an error:
Error running query. Page15.CaseSerial is invalid in the select list
becauseit is not contained in either an aggregate function or the
GROUP BY clause.
I've tried adding a GROUP BY clause with no success.
Here's the code:
insert into page4 (serial, caseserial, linkserial, type, add1, add2, city, state, orgname, prefername, email, firstname, lastname, salutation, contactstatus, workphone, notes, cellphone, nametype, homephone, fax, zip, payments)
select id = max(serial), caseserial, linkserial, type, add1, add2, city, state,
orgname, prefername, email, firstname, lastname, salutation, contactstatus,
workphone, notes, cellphone, nametype, homephone, fax, zip, payments
from page16
It would be nice if I could write something to get the highest id from page4 and insert the next highest.
Thanks!
declare #maxId int
select #maxId = max(yourIdColumn)
from YourTable
set #maxId = #maxId + 1
insert into YourTable (yourIdColumn, ....)
values (#maxId, ....)
Disclaimer: not sure how this would transpose over to other RDBMS's, but this is with SQL Server in mind. Also, this handles inserting only one value. If you need to insert a set of values, then please let me know.