sql query for the following statement? - sql

My table 'Customer' contains customerid , firstname, lastname, company,city,state,country, email, invoicetotal
Question: For countries that have at least two customers using yahoo as email provider, display the name alongside the revenue
My solution:
select county,sum(invoiceTotal)from customer where email like '%yahoo%'
group by Country,Email having Count(Country)>2
I am unable to get proper result the no of rows displayed in my output are different from the number of rows in expected output,Can any1 tell me where have i gone wrong???

You are grouping by email as well - just group by Country and you should be fine
select
county,
sum(invoiceTotal)
from customer
where email like '%yahoo%'
group by Country
having Count(Country)>2

You can't group by email, since that's unique. Fortunately, you don't have to.

select
county,
sum(invoiceTotal)
from customer
where email like '%yahoo%'
group by Country
having Count(Country)>=2
Because the statement say at least then you need put >=.

Related

Get Groups of Customers Names based on their Title

Get the Count of Contact's person by their title. (Table: Customers) below:
Now, below SQL code will get group counts, but I want to add also the names associated with each group if possible please:
SELECT ContactTitle, Count(ContactTitle) FROM Customers
GROUP BY ContactTitle;
Database: This is from NorthWind database.
You can use
SELECT ContactTitle, ContactName, Count(ContactTitle) over (partition by ContactTitle) as usersCoun FROM Customers;

Grouping other fields by common email

I'd appreciate any assistance people could provide with this one. I've tried to follow a couple of other posts (GROUP BY to combine/concat a column), but when I do this, it combines everything.
I have a table in SQL that holds contact data. Some contacts in the database have the same email address, particularly admin#, accounts#, etc. We also have a list of contact preferences for each contact: Emergency Contact, Accounts, WHS, etc. I'm trying to select the results of the table, but only one row per email address.
My data looks like this:
However, I'd like it to group the rows together that have the same email address (I don't care about the names). I'd also like it to look at the contact preference fields and if even one of the joined fields had a yes, show a Y, otherwise show a N - see below example
As I mentioned, all my tests have been unsuccessful I'm afraid, so I would appreciate any assistance that you can provide.
You can use the following:
;WITH selected as (
select min(ContactID) as ContactID, Mail, max(EmergencyContact) as EmergencyContact,
max(AccountsCon) as AccountsCon, max(WHSContact) as WHSContact
from t
GROUP BY Mail
)
select t.ContactID, t.BusinessID, t.BusinessName, t. FirstName, t.LastName, t.Position, t.Mail, s.EmergencyContact, s.AccountsCon, s.WHSContact
from selected as s
inner join t as t ON t.ContactID = s.ContactID
This way you get the contactid, businessid, businessname, firstname, lastname and position from the first record found with each email and the last 3 columns you get using max (taking advantage that Y is greater than N, so if there is at least one Y it will get Y).
I think you can just use aggregation:
select businessid, businessname, max(firstname),
max(lastname), max(position),
email,
max(emergencycontact),
max(accountscor),
max(whcontact)
from t
group by businessid, businessname, email;

ORDER BY + IN statement?

The marketing department wants to focus on the customers from Noth America first. Get the ID, last name and country of all customers. Build the list by bringing up the customers living in Canada and in the USA first, and finally order them by ID. Tip: use the IN expression in the ORDER BY clause.
I've tried many times
SELECT CustomerID, LastName, Country
FROM Customer
ORDER BY Country IN ('Canada', 'USA'), CustomerID
but it doesn't seem to work, as it takes the specified fields from all customers in the order they appear in the original table (which is also ordered by ID), even if I remove CustomerID from the ORDER BY clause, whithout caring to which country they belong to.
What should I do? I'm really new to SQL, and have no idea on how to fix this.
Edit: WHERE ins't suitable at all, as I need to take in consideration all customers, only making sure the Canadian and American ones appear at the top of the list.
Also I'm unsure statements like UNION, AS, EXCEPT and things like that are meant to be used, because the tutorial didn't go that deep already.
Not every DBMS has a boolean datatype. So the result of
Country IN ('Canada', 'USA'),
which is a boolean, can not be sorted in these DBMS.
You can use a CASE expression, however, to assign a value:
SELECT CustomerID, LastName, Country
FROM Customer
ORDER BY CASE WHEN Country IN ('Canada', 'USA') THEN 1 ELSE 2 END, CustomerID;
SELECT CustomerID, LastName, Country
FROM Customer
ORDER BY Country IN ('Canada', 'USA') desc, CustomerID asc
IN expression don't return value so you can't sort
You can try:
SELECT CustomerID, LastName, Country
FROM Customer
WHERE Country='Canada'
UNION ALL
SELECT CustomerID, LastName, Country
FROM Customer
WHERE Country='USA'
ORDER BY CustomerID
Using ORDER BY with UNION, EXCEPT, and INTERSECT When a query uses the
UNION, EXCEPT, or INTERSECT operators, the ORDER BY clause must be
specified at the end of the statement and the results of the combined
queries are sorted. The following example returns all products that
are red or yellow and sorts this combined list by the column
ListPrice.
https://msdn.microsoft.com/en-us/library/ms188385.aspx#Union

SQL: Search for specific customer who have specific domian in their email address

I have a table which contains customer's info. I wanted to select only those customer who have below domain in their email address. How can I do that?
Domain such as
aol.com
gmail.com
yahoo.com
msn.com
How can I make a select query with where clause?
For Example:
select ID, FirstName, LastName from Customer_Info where Email like '%gmail.com'
I can't create like if I have many domain. So please suggect if any other option is available
There is a way to ignore "like" for such query
SELECT DISTINCT CI.CustomerID ,
CI.FirstName,
CI.LastName,
From Customer_Info CI with (nolock)
WHERE SUBSTRING(Email,PATINDEX('%#%',Email)+1,100) IN ('aol.com','gmai.com','yahoo.com','msn.com')

Having trouble with Distinct(Coloumn_name)

I'm having problems with getting the correct data from my tables.
I've got a table with information on customers sending letters.
My table have following coloumbs( Name, Postalcode, Country, Phone_number, Letters(The number of letters))
I want to get the numbers of letters send from every customer, depending on the Postal_code.
Select Distinct(Postal_Code), Count(*)
from Table_name
Group by Postal_Code, Letters
The result of this, is not exactly what I wanted, because I get duplicates of the postalcode, and the numbers of letters are wrong.
I'm pretty new to this stuff, so I hope someone can help me.
EDIT:
At the moment, i'm trying to get a date on the record, and my code i slike this now.
Select Postal_Code, Sum(Letters), To_Char(trunc(Start_time),'DD-MM-YYYY') AS StartTime
from Table_name
Group by Postal_Code
But I get an error when running it. The error says: ORA-00937: not a single-group group function.
I have googlet the problem, and tried with my start_time in the group by, but this gives me the wrong result.
If you use GROUP BY you don't need DISTINCT. Since letters is a number you need to SUM() them. So total letters for each postal code would be something along the lines of:
Select Postal_Code, SUM(letters)
from Table_name
Group by Postal_Code
GROUP BY gives you distinct values for all the columns in the clause. So grouping by POSTAL_CODE, LETTERS is why you get duplicate POSTAL_CODE values.
Not sure I completely understand what you're trying to do, but here is my interpretation:
Select Postal_Code
, sum(letters) no_of_letters
, Count(*) no_of_customers
from Table_name
Group by Postal_Code