I have the following table:
Company webdomain
------- ---------
IBM ibm.com
IBM ibm.co.uk
IBM ibm.in
CSC csc.com
Infosys infy.com
Intel intel.com
Intel intel.co.in
Question: how many companies have more than one webdomain?
How do I represent this as a SQL query?
I tried the following:
select count(distinct company, webdomain)
from table
where company = 'IBM';
This gave the number of web domains for IBM as 3, but however, when I want to create the same effect on finding out all companies with the following query:
select company, count(distinct company, webdomain)
from table;
I get a single column, which is an empty value on company and some unrelated count.
I know that this will solve the problem:
select company, count(distinct company, webdomain)
from table
where company in (select distinct company from table);
But this last query takes way too long. Is there a better way to put it.
EDIT: Company, webdomain combination may not be unique. Ex: Two records with IBM, ibm.com.
you can just append "Group by" in your query to get desired result as:-
SELECT
`company`, `webdomain`, COUNT(DISTINCT `company`, `webdomain`)
AS
`count_of_unique_combination` FROM `info`
GROUP BY
`company`;
the output is:-
COMPANY WEBDOMAIN COUNT OF UNIQUE COMBINATION
CSC csc.com 1
IBM ibm.com 3
Infosys infy.com 1
Intel intel.com 2
Use GROUP BY to get te desired result. Like:
select company, webdomain, count(*) from table group by company, webdomain;
Try with GROUP BY clause like
SELECT company,
count(*)
FROM TABLE
GROUP BY company,
webdomain;
As per
Question: How many companies have more than one webdomain?
You may use the following query
select
Company,
count(*) as num_subdomains
from
your_table
group by Company
having num_subdomains > 1
Related
first time here. I'm a beginner
IM USING MYSQL.
I'm having trouble to get the ONLY SALESMAN who have SOLD a product to every CLIENT on the list
Compare the SALESMAN column with the CLIENT_KEY and return the value.
Could be only one salesman or two, I don't know.
This is a long list +8K transactions.
Below is a picture of the database
Thank you
Assuming you only have one table and all clients means all distinct client keys from your table.
SELECT SALESMAN
FROM YourTable
GROUP BY SALESMAN
HAVING count(DISTINCT CLIENT_KEY)=(SELECT COUNT(DISTINCT CLIENT_KEY) FROM YourTable)
The query sums all clients for each salesman, and checks if that number is equal to the sum of all distinct clients in the table.
if client list is a table then :
select salesman
from salesManTable s
group by salesman
having count(distinct clientid) = (select count(*) from clienttable)
I am working on a theatre booking system in MySql (My first SQL project). I have three tables:
Production (contains Title, BasicTicketPrice),
Performance (PerformanceDate, PerformanceTime, Title)
Booking (Email of person who booked, PerformanceDate, PerformanceTime, RowNumber).
Each person booked tickets for two or three performances (using their email to book).
I need to to write a query which will display the prices paid for all booked seats and I need to output the RowNumber, Email of person who booked and the Calculated Price.
I understand that I need to join these tables and make the query display a temporary column called Calculated Price but I don't know how to calculate the price.
I tried this:
SELECT DISTINCT b.RowNumber, b.Email, pr.BasicTicketPrice
FROM booking b, production pr performance p
WHERE p.Title=b.PerfDate*b.PerfTime*b.RowNumber;
SELECT CONCAT (PerfDate, PerfTime, RowNumber) AS BookingID FROM booking;
SELECT RowNumber, Email, CONCAT(PerfDate, PerfTime, RowNumber) AS BookingID FROM booking;
SELECT RowNumber, Email, CONCAT((CONCAT(PerfDate, PerfTime, RowNumber) AS BookingID
FROM booking)BasicTicketPrice*BookingID);
SELECT RowNumber, Email, CONCAT(PerfDate, PerfTime, RowNumber) AS BookingID INTEGER
FROM booking;
SELECT RowNumber FROM booking
LEFT JOIN (SELECT Title FROM performance WHERE '2017-11-01 19:00:00' Email IS NULL);
But it didn't work.
Any suggestions? I will be grateful for any ideas.
Assuming:
One row in Bookings per booked seat
Title to be a suitable primary key for Production
PerformanceDate, PerformanceTime to be a suitable primary composite key for Performance
You'll be looking to join the three tables together as per the keys assumed above. It seems you wish to group the bookings together per performance, by the person booking the tickets - if so, you'll need to use an aggregate to show the seat numbers (I've used GROUP_CONCAT to delimit them), as well as to COUNT the tickets purchased and multiply by the ticket cost.
SELECT
b.Email, prod.Title, per.PerformanceDate, per.PerformanceTime,
GROUP_CONCAT(RowNumber) AS BookedSeats,
COUNT(RowNumber) * prod.BasicTicketPrice AS TotalCost
FROM Booking b
INNER JOIN Performance per
ON b.PerformanceDate = per.PerformanceDate
AND b.PerformanceTime = per.PerformanceTime
INNER JOIN Production prod
ON per.Title = prod.Title
GROUP BY
b.Email, prod.Title, per.PerformanceDate, per.PerformanceTime, prod.BasicTicketPrice
ORDER BY prod.Title, per.PerformanceDate, per.PerformanceTime;
Technically, we should include all non-aggregated columns in the GROUP BY, hence prod.BasicTicketPrice is listed as well.
Please see above for the data structure. I am trying to write an SQL query to get the average number of customers for each session
My attempt:
select avg(A.NumberCustomer)
from(
select SessionName, count(distinct customers.Idcustomer) as NumberCustomer,
from customers, enrollments, sessions
where customers.Idcustomer=enrollments.Idcustomer and enrollments.Idsession=sessions.Idsession
group by sessions.SessionName
) A
But I seem to get an error on the from customers, enrollments, sessions line
Not sure about this, any help appreciated.
Thanks
You have and extra comma that you should to delete:
select avg(A.NumberCustomer)
from(
select SessionName,
count(distinct customers.Idcustomer) as NumberCustomer, #<--- here
from customers, enrollments, sessions
where customers.Idcustomer=enrollments.Idcustomer
and enrollments.Idsession=sessions.Idsession
group by sessions.SessionName
) A
By the way, I suggest to you to move to SQL'99 join syntax for readability reasons:
SELECT
avg(A.NumberCustomer)
FROM (
select
SessionName,
count(distinct customers.Idcustomer) as NumberCustomer
from customers
inner join enrollments
on customers.Idcustomer=enrollments.Idcustomer
inner join sessions
on enrollments.Idsession=sessions.Idsession
group by sessions.SessionName
) A
Also, nice diagram on question and remember to include your error message next time.
For the average number of customers in each session, you should be able to use just the enrollments table. The average would be the number of enrollments divided by the number of sessions:
select count(*) / count(distinct idSession)
from enrollments e;
This makes the following assumptions:
All sessions have at least one customer (your original query had this assumption as well).
No customer signs up multiple times for the same session.
I have a N:M relation that connects customers and purchase Customer_id,Purchase_id . I would like to know how many purchases each specific user made, from one query.
I tried this:
SELECT DISTINCT(Customer_id) AS ID,COUNT(Purchase_id) AS P FROM CustomerPruchases;
but it only gives me one row for the first customer in the DB. I want it for all.
If you group then aggregate functions like count() apply to each group and not to the complete table
SELECT Customer_id as ID,
COUNT(Purchase_id) AS P_COUNT
FROM CustomerPruchases
group by ID
I couldnt find this, i'm sure its simple.
Table (196 rows)
tech, name, area, manager
------------------------------
Joe,mary,pa,sally
Kim,brad,ga,tim
kelly,Joe,pa,sally
Joe,jose,pa,sally
a tech is assigned to multiple name/area/managers. I want to do a report that shows all the rows of the table with a column showing the number of assignments for the tech.
My desired results
tech, name, area, manager, (count of number of tech assignments)
Joe,mary,pa,sally,2
Kim,brad,ga,tim,1
kelly,Joe,pa,sally,1
Joe,jose,pa,sally,2
I'm guessing you want a subquery on the SELECT clause:
SELECT
name,
area,
manager,
(SELECT COUNT(*) FROM tablename WHERE tech = x.tech) AS assignments
FROM tablename x
And here is a possibly more efficient way to do the same thing:
SELECT
t.name,
t.area,
t.manager,
sub.assignments
FROM tablename t
INNER JOIN (
SELECT tech, COUNT(*) AS assignments
FROM tablename
GROUP BY tech
) sub
ON sub.tech = t.tech
select
a.tech, a.name, a.area, a.manager,
b.cnt
from table a, (
select count(*) cnt, tech
from table
group by tech) b
where a.tech=b.tech;
Is this what you want?
SELECT tech, count(name) AS total_records FROM db_table GROUP BY tech
Not sure this is what you're looking for though.
Oh yeah, looks like you really need to use subquery.
Thanks for the examples, that helped.
SELECT tech, name, area, manager, assignments
FROM table
INNER JOIN (
SELECT tech, COUNT(*) AS assignments
FROM table
GROUP BY tech
) t
USING (tech)
Explanation:
Select all columns from table and join them with table containing tech column and count of rows from table having particular tech. Keyword USING says how those two table merge.