473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Basic TSQL - DISTINCT in PK

I'm in the process of trying to teach myself T-SQL out of a WROX book, and am having a problem wrapping my head around this example, I understand the concept of Primary Keys through using MS Access, but haven't used TSQL extensively (or at all for that matter...)

Using the classic "Northwind" database, using this query:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(OrderID)
  2. FROM [Order Details]
  3.  
returns 2155, which is the total number of rows with an OrderID, the primary key. Now, as I understood it, a PK has to be unique, but yet this query on the same DB:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(DISTINCT OrderID)
  2. FROM [Order Details]
  3.  
returns 830 rows. If the PK (OrderID) is unqiue, how is the DISTINCT function returning fewer rows? Wouldn't that imply there are duplicate PKs?

I'm sure there's a simple answer to this, but I just can't seem to get my head around it... Thanks!
Sep 1 '06 #1
1 5018
ramasp
19
For example, if you have purchased 3 products from super market, the orderid is same for al the products. Look at the orderdetail table in nothwind database. The product id is different. Use sp_help orderdetails command to see the keys on the table. Composite primary key can be defined on two or more columns.
Sep 4 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
Hi; I'm brand spanking new to sqlserver ( nice so far ). I need to make a simple data change across a list of tables. Basically replace an old date with a new date. However, the people I am...
2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
11
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the...
1
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
5
by: pankaj_wolfhunter | last post by:
Greetings, When i log onto sql server using tsql i get the following line of information: locale is...
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
2
by: Carlton Kirby | last post by:
I need to execute a job on a SQL Express 2005 instance (no SQLAgent). The job will be executed manually by a user, so it doesn't need to be scheduled to run automatically. I thought I could...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.