Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

newbie help pls Expand / Collapse
Author
Message
Posted Sunday, October 14, 2012 2:12 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, Visits: 271
Customer

CustomerID CustomerName Birthdate
1 John Doe 1/1/1970 08:31 AM
2 Jane Doe 1/1/1971 01:18 PM
3 Jon Public 1/1/1972 11:58 PM
4 Jane Public 1/1/1973 07:00 AM
5 John Smith 1/1/1974 08:31 AM
Order

OrderID PO Number OrderDate
1000 ABC123 1/1/2012 01:00 PM
2000 112233 2/1/2012 02:00 AM
3000 XYZ987 3/1/2012 03:00 PM
4000 50000 4/1/2012 04:00 AM
5000 Verbal 5/1/2012 05:00 AM
CustomerOrders

CustomerID OrderID IsShipped
1 1000 False
1 3000 True
3 4000 False
2 2000 True
5 5000 True

pls provide the SQL statement that will return all the CustomerNames who have never placed an order


--Pra--------------------------------------------------------------------------------
Post #1372532
Posted Sunday, October 14, 2012 3:07 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, June 30, 2014 3:46 AM
Points: 79, Visits: 466
Hi,

SELECT CustomerName FROM Customer c
LEFT OUTER JOIN CustomersOrders co ON c.CustomerID = co.CustomerID
WHERE co.CustomerID is null

Jeff.
Post #1372538
Posted Monday, October 15, 2012 8:32 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, Visits: 271
Thanku Jeff

--Pra--------------------------------------------------------------------------------
Post #1372759
Posted Monday, October 15, 2012 8:52 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:47 AM
Points: 2,422, Visits: 7,443
Here's how you should lay out your sample data: -
CREATE TABLE Customer (CustomerID INT, CustomerName VARCHAR(200), Birthdate DATETIME);
INSERT INTO Customer
SELECT CustomerID,CustomerName,Birthdate
FROM (VALUES(1,'John Doe','1/1/1970 08:31 AM'),(2,'Jane Doe','1/1/1971 01:18 PM'),
(3,'Jon Public','1/1/1972 11:58 PM'),(4,'Jane Public','1/1/1973 07:00 AM'),
(5,'John Smith','1/1/1974 08:31 AM'))a(CustomerID,CustomerName,Birthdate);

CREATE TABLE [Order] (OrderID INT, [PO Number] VARCHAR(10), OrderDate DATETIME);
INSERT INTO [Order]
SELECT OrderID, [PO Number], OrderDate
FROM (VALUES(1000,'ABC123','1/1/2012 01:00 PM'),(2000,'112233','2/1/2012 02:00 AM'),
(3000,'XYZ987','3/1/2012 03:00 PM'),(4000,'50000','4/1/2012 04:00 AM'),
(5000,'Verbal','5/1/2012 05:00 AM'))a(OrderID, [PO Number], OrderDate);

CREATE TABLE CustomerOrders (CustomerID INT, OrderID INT, IsShipped VARCHAR(5));
INSERT INTO CustomerOrders
SELECT CustomerID, OrderID, IsShipped
FROM (VALUES(1,1000,'False'),(1,3000,'True'),(3,4000,'False'),(2,2000,'True'),
(5,5000,'True'))a(CustomerID, OrderID, IsShipped);

That makes it so that anyone can just execute the script and have a mock-up of your data. You should include any indexes as well if you want the best performing solution.

Jeff's method is fine, but to show another method you could also do this: -
SELECT *
FROM Customer cus
WHERE NOT EXISTS (SELECT 1
FROM CustomerOrders ord
WHERE cus.CustomerID = ord.CustomerID);

They probably perform the same, but it's worth testing both methods on your actual data.



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1372767
Posted Monday, October 15, 2012 9:08 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, Visits: 271
For the same data Provide the SQL statement that would return every CustomerName and the count of orders they have ever placed.


I have tried using the below code

select Customer.CustomerName, Count(CustomerOrders.customerID) from Customer JOIN CustomerOrders ON customer.customerID=customerorders.CustomerID

giving an error:
Msg 8120, Level 16, State 1, Line 1
Column 'Customer.CustomerName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


--Pra--------------------------------------------------------------------------------
Post #1372772
Posted Monday, October 15, 2012 9:13 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 13,138, Visits: 11,977
prathibha_aviator (10/15/2012)
For the same data Provide the SQL statement that would return every CustomerName and the count of orders they have ever placed.


I have tried using the below code

select Customer.CustomerName, Count(CustomerOrders.customerID) from Customer JOIN CustomerOrders ON customer.customerID=customerorders.CustomerID

giving an error:
Msg 8120, Level 16, State 1, Line 1
Column 'Customer.CustomerName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


The error message tells you EXACTLY what the problem is. You need to use some grouping when aggregating data.

http://msdn.microsoft.com/en-us/library/ms177673%28v=sql.105%29.aspx

--edit--

I did not provide an answer because this scream of homework. If I provide you an answer you do not learn the topic.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1372774
Posted Monday, October 15, 2012 9:15 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:47 AM
Points: 2,422, Visits: 7,443
prathibha_aviator (10/15/2012)
For the same data Provide the SQL statement that would return every CustomerName and the count of orders they have ever placed.


I have tried using the below code

select Customer.CustomerName, Count(CustomerOrders.customerID) from Customer JOIN CustomerOrders ON customer.customerID=customerorders.CustomerID

giving an error:
Msg 8120, Level 16, State 1, Line 1
Column 'Customer.CustomerName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


Read your error message, then read this.

GROUP BY Customer.CustomerName

Make sure you read the link I've provided to explain the issue.


--EDIT--
Seems Sean beat me to it.



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1372775
Posted Monday, October 15, 2012 9:33 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, Visits: 271
So I get to use joins concept along with the group by clause in this case as i am going to use two differnt tables??? Not a question to you... I'll give it a try and get back again

--Pra--------------------------------------------------------------------------------
Post #1372779
Posted Monday, October 15, 2012 10:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, Visits: 271
I got it
SELECT Customer.CustomerName, Count(customerorders.customerID) AS Count
FROM Customer
JOIN CustomerOrders ON (Customer.CustomerID= CustomerOrders.CustomerID)
Group By CustomerName


--Pra--------------------------------------------------------------------------------
Post #1372818
Posted Monday, October 15, 2012 10:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 13,138, Visits: 11,977
prathibha_aviator (10/15/2012)
I got it
SELECT Customer.CustomerName, Count(customerorders.customerID) AS Count
FROM Customer
JOIN CustomerOrders ON (Customer.CustomerID= CustomerOrders.CustomerID)
Group By CustomerName


Perfect.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1372821
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse