SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


newbie help pls


newbie help pls

Author
Message
prathibha_aviator
prathibha_aviator
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 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:-):-)--------------------------------------------------------------------------------
jfdelette
jfdelette
SSC Veteran
SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)

Group: General Forum Members
Points: 276 Visits: 515
Hi,


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



Jeff.
prathibha_aviator
prathibha_aviator
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 Visits: 271
Thanku Jeff

--Pra:-):-)--------------------------------------------------------------------------------
Cadavre
Cadavre
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14589 Visits: 8508
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.


Forever 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


Craig Wilkinson - Software Engineer
LinkedIn
prathibha_aviator
prathibha_aviator
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 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:-):-)--------------------------------------------------------------------------------
Sean Lange
Sean Lange
SSC Guru
SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)

Group: General Forum Members
Points: 100676 Visits: 18172
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 Modens 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)
Cadavre
Cadavre
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14589 Visits: 8508
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.


Forever 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


Craig Wilkinson - Software Engineer
LinkedIn
prathibha_aviator
prathibha_aviator
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 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:-):-)--------------------------------------------------------------------------------
prathibha_aviator
prathibha_aviator
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 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:-):-)--------------------------------------------------------------------------------
Sean Lange
Sean Lange
SSC Guru
SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)

Group: General Forum Members
Points: 100676 Visits: 18172
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 Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search