|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How many rows exist in these two tables.The question that I want to have an answer to is how many rows does the Person table have and how many rows does table Car have. No matter if the Persons own a car or not and the same if the car have a owner or not. As an aid to be able to answer the question is some select question that is made to these tables. I have tried but I doesn't come to the right answer. Some background that you must have to be able to answer my question. We have two tables named Person and Car. The relation is that one Person can own one or at most two cars which you can say by see the result from the select statements below. The definition of the two tables create statement looks like this. create table Person { PersonID int NOT NULL identity (1, 1), PersonNr char(11) NOT NULL, Forename varchar(50) NOT NULL, Surname varchar(50) NOT NULL, City varchar(50) NOT NULL, Street varchar(50) NOT NULL } create table Car { carID int NOT NULL IDENTITY(1,1), Plates varchar(8) NOT NULL, PersonID int NULL } There are four select statements that has been made and executed. Below are the four select statements and the answer to each. (select statement 1) select * from Person P JOIN Car C ON P.PersonID = C.PersonID (1037854 row(s) affected) (select statement 2) select PersonNr, count(*) from Person P JOIN Car C ON P.PersonID = C.PersonID group by PersonNr having count(*) > 1 (132892 row(s) affected) (select statement 3) select PersonNr, count(*) from Person P JOIN Car C ON P.PersonID = C.PersonID group by PersonNr having count(*) > 2 (0 row(s) affected) (select statement 4) select count(distinct P.PersonID), count(distinct C.CarID) from Person P OUTER JOIN Car C ON P.PersonID = C.PersonID where C.CarID IS NULL OR P.PersonID IS NULL --------------- -------------------- 198898 114388 (1 row(s) affected) I have went through each select statement and have discussed of my own about to figure out the number of rows in the Person table and the number of rows in the car table in the following. According to the select statement 3 no Person have more then 2 cars. Am I right? Then according to my understanding about the select statements 2 132 892 Persons have two cars and according to select statement 1 1037854 Persons have cars but some persons have 2 cars. This means that if you make this calculation 1037854 - 132892 = 904962 will give you the number of Persons that have exact one car. Do you agree with me here? According to select statement 3 no person have more then 2 cars. Do you agree with me? I mean that in table person exist Persons that own a car and some Persons that doesn't have any car at all. If you add the number of Persons that have one car with Persons that doesn't have any car you will get the right answer. First the numer of Persons that have a car is 904962 persons. Then according to statement 4 198898 persons doesnt't have any car. If you sum these two numbers together you get 904962 + 198898 = 1103860 which would mean that 1103860 persons exist in table Person. In table Car one and the same car can't be own by two Persons. This mean that 904962 persons own one unique car and according to statement 4 114388 cars doesnt'r have any owner. If you sum these two figure up you get 904962 + 114388 = 101935 cars. According to my calculation there would exist 1103860 rows in table Person and 101935 cars in table Cars. But this answer is wrong. I'm not sure if both are wrong but what I do know is that both are not right. Have you any suggestion about the number of rows in table Person and Car? //Tony select count(*) from Person
select count(*) from Car Show quote "Tony Johansson" wrote: > Hello Experts! > > The question that I want to have an answer to is how many rows does the > Person table have and how many rows does table Car have. No matter if the > Persons own a car or not and the same if the car have a owner or not. > > As an aid to be able to answer the question is some select question that is > made to these tables. I have tried but I doesn't come to the right answer. > > Some background that you must have to be able to answer my question. > We have two tables named Person and Car. > The relation is that one Person can own one or at most two cars which you > can say by see the result from the select statements below. > > The definition of the two tables create statement looks like this. > create table Person > { > PersonID int NOT NULL identity (1, 1), > PersonNr char(11) NOT NULL, > Forename varchar(50) NOT NULL, > Surname varchar(50) NOT NULL, > City varchar(50) NOT NULL, > Street varchar(50) NOT NULL > } > create table Car > { > carID int NOT NULL IDENTITY(1,1), > Plates varchar(8) NOT NULL, > PersonID int NULL > } > > There are four select statements that has been made and executed. > Below are the four select statements and the answer to each. > (select statement 1) > select * > from Person P JOIN Car C ON P.PersonID = C.PersonID > (1037854 row(s) affected) > > (select statement 2) > select PersonNr, count(*) > from Person P JOIN Car C ON P.PersonID = C.PersonID > group by PersonNr > having count(*) > 1 > (132892 row(s) affected) > > (select statement 3) > select PersonNr, count(*) > from Person P JOIN Car C ON P.PersonID = C.PersonID > group by PersonNr > having count(*) > 2 > (0 row(s) affected) > > (select statement 4) > select count(distinct P.PersonID), count(distinct C.CarID) > from Person P OUTER JOIN Car C ON P.PersonID = C.PersonID > where C.CarID IS NULL OR P.PersonID IS NULL > --------------- -------------------- > 198898 114388 > (1 row(s) affected) > > I have went through each select statement and have discussed of my own > about to figure out the number of rows in the Person table and the number of > rows in the car table in the following. > > According to the select statement 3 no Person have more then 2 cars. Am I > right? > > Then according to my understanding about the select statements 2 132 892 > Persons have two cars and according to select statement 1 1037854 Persons > have cars but some persons have 2 cars. This means that if you make this > calculation 1037854 - 132892 = 904962 will give you the number of Persons > that have exact one car. Do you agree with me here? > > According to select statement 3 no person have more then 2 cars. Do you > agree with me? > > I mean that in table person exist Persons that own a car and some Persons > that doesn't have any car at all. > If you add the number of Persons that have one car with Persons that doesn't > have any car you will get the right answer. > First the numer of Persons that have a car is 904962 persons. Then > according to statement 4 198898 persons doesnt't have any car. If you sum > these two numbers together you get 904962 + 198898 = 1103860 which would > mean that 1103860 persons exist in table Person. > > In table Car one and the same car can't be own by two Persons. This mean > that 904962 persons own one unique car and according to statement 4 114388 > cars doesnt'r have any owner. If you sum these two figure up you get 904962 > + 114388 = 101935 cars. > > According to my calculation there would exist 1103860 rows in table Person > and 101935 cars in table Cars. But this answer is wrong. > > I'm not sure if both are wrong but what I do know is that both are not > right. > > Have you any suggestion about the number of rows in table Person and Car? > > > //Tony > > > And to this, I will add that it will be much easier to understand if you
replace the statement #4 with the following statements to determine the number of persons who doesn't have a car and vice-versa: Select count (*) From Person P where Not Exists (select * from Car C where P.PersonID = C.PersonID) Or: Select count (*) From Person P where P.PersonID Not IN (Select distinct PersonID from Car where Car.PersonID is Not Null) (The condition Car.PersonID is Not Null is important here, otherwise a Null value will be part of the result set and the IN clause will not work properly because of this.) And: Select count (*) from Car Where Car.PersonID is Null Show quote "KH" <K*@discussions.microsoft.com> wrote in message news:00BE7404-BCFB-48A4-9FD1-C0386EA583D8@microsoft.com... > > select count(*) from Person > select count(*) from Car > > > > "Tony Johansson" wrote: > >> Hello Experts! >> >> The question that I want to have an answer to is how many rows does the >> Person table have and how many rows does table Car have. No matter if the >> Persons own a car or not and the same if the car have a owner or not. >> >> As an aid to be able to answer the question is some select question that >> is >> made to these tables. I have tried but I doesn't come to the right >> answer. >> >> Some background that you must have to be able to answer my question. >> We have two tables named Person and Car. >> The relation is that one Person can own one or at most two cars which you >> can say by see the result from the select statements below. >> >> The definition of the two tables create statement looks like this. >> create table Person >> { >> PersonID int NOT NULL identity (1, 1), >> PersonNr char(11) NOT NULL, >> Forename varchar(50) NOT NULL, >> Surname varchar(50) NOT NULL, >> City varchar(50) NOT NULL, >> Street varchar(50) NOT NULL >> } >> create table Car >> { >> carID int NOT NULL IDENTITY(1,1), >> Plates varchar(8) NOT NULL, >> PersonID int NULL >> } >> >> There are four select statements that has been made and executed. >> Below are the four select statements and the answer to each. >> (select statement 1) >> select * >> from Person P JOIN Car C ON P.PersonID = C.PersonID >> (1037854 row(s) affected) >> >> (select statement 2) >> select PersonNr, count(*) >> from Person P JOIN Car C ON P.PersonID = C.PersonID >> group by PersonNr >> having count(*) > 1 >> (132892 row(s) affected) >> >> (select statement 3) >> select PersonNr, count(*) >> from Person P JOIN Car C ON P.PersonID = C.PersonID >> group by PersonNr >> having count(*) > 2 >> (0 row(s) affected) >> >> (select statement 4) >> select count(distinct P.PersonID), count(distinct C.CarID) >> from Person P OUTER JOIN Car C ON P.PersonID = C.PersonID >> where C.CarID IS NULL OR P.PersonID IS NULL >> --------------- -------------------- >> 198898 114388 >> (1 row(s) affected) >> >> I have went through each select statement and have discussed of my own >> about to figure out the number of rows in the Person table and the number >> of >> rows in the car table in the following. >> >> According to the select statement 3 no Person have more then 2 cars. Am I >> right? >> >> Then according to my understanding about the select statements 2 132 892 >> Persons have two cars and according to select statement 1 1037854 >> Persons >> have cars but some persons have 2 cars. This means that if you make this >> calculation 1037854 - 132892 = 904962 will give you the number of >> Persons >> that have exact one car. Do you agree with me here? >> >> According to select statement 3 no person have more then 2 cars. Do you >> agree with me? >> >> I mean that in table person exist Persons that own a car and some Persons >> that doesn't have any car at all. >> If you add the number of Persons that have one car with Persons that >> doesn't >> have any car you will get the right answer. >> First the numer of Persons that have a car is 904962 persons. Then >> according to statement 4 198898 persons doesnt't have any car. If you >> sum >> these two numbers together you get 904962 + 198898 = 1103860 which >> would >> mean that 1103860 persons exist in table Person. >> >> In table Car one and the same car can't be own by two Persons. This mean >> that 904962 persons own one unique car and according to statement 4 >> 114388 >> cars doesnt'r have any owner. If you sum these two figure up you get >> 904962 >> + 114388 = 101935 cars. >> >> According to my calculation there would exist 1103860 rows in table >> Person >> and 101935 cars in table Cars. But this answer is wrong. >> >> I'm not sure if both are wrong but what I do know is that both are not >> right. >> >> Have you any suggestion about the number of rows in table Person and Car? >> >> >> //Tony >> >> >> Hello again!
I don't have the database so I can't do select count(*) from Person and select count(*) from Car but by using the result from the select statements below it should be possible to calculate how many rows table person have and table Car. So I just want to know the number of rows in table Person and table Car. //Tony Show quote "Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)> skrev i meddelandet news:u6W3gx2fFHA.3936@tk2msftngp13.phx.gbl... > And to this, I will add that it will be much easier to understand if you > replace the statement #4 with the following statements to determine the > number of persons who doesn't have a car and vice-versa: > > Select count (*) From Person P where Not Exists (select * from Car C where > P.PersonID = C.PersonID) > > Or: > > Select count (*) From Person P where P.PersonID Not IN (Select distinct > PersonID from Car > where Car.PersonID is Not Null) > > (The condition Car.PersonID is Not Null is important here, otherwise a > Null value will be part of the result set and the IN clause will not work > properly because of this.) > > And: > > Select count (*) from Car Where Car.PersonID is Null > > > -- > Sylvain Lafontaine, ing. > MVP - Technologies Virtual-PC > E-mail: http://cerbermail.com/?QugbLEWINF > > > "KH" <K*@discussions.microsoft.com> wrote in message > news:00BE7404-BCFB-48A4-9FD1-C0386EA583D8@microsoft.com... >> >> select count(*) from Person >> select count(*) from Car >> >> >> >> "Tony Johansson" wrote: >> >>> Hello Experts! >>> >>> The question that I want to have an answer to is how many rows does the >>> Person table have and how many rows does table Car have. No matter if >>> the >>> Persons own a car or not and the same if the car have a owner or not. >>> >>> As an aid to be able to answer the question is some select question that >>> is >>> made to these tables. I have tried but I doesn't come to the right >>> answer. >>> >>> Some background that you must have to be able to answer my question. >>> We have two tables named Person and Car. >>> The relation is that one Person can own one or at most two cars which >>> you >>> can say by see the result from the select statements below. >>> >>> The definition of the two tables create statement looks like this. >>> create table Person >>> { >>> PersonID int NOT NULL identity (1, 1), >>> PersonNr char(11) NOT NULL, >>> Forename varchar(50) NOT NULL, >>> Surname varchar(50) NOT NULL, >>> City varchar(50) NOT NULL, >>> Street varchar(50) NOT NULL >>> } >>> create table Car >>> { >>> carID int NOT NULL IDENTITY(1,1), >>> Plates varchar(8) NOT NULL, >>> PersonID int NULL >>> } >>> >>> There are four select statements that has been made and executed. >>> Below are the four select statements and the answer to each. >>> (select statement 1) >>> select * >>> from Person P JOIN Car C ON P.PersonID = C.PersonID >>> (1037854 row(s) affected) >>> >>> (select statement 2) >>> select PersonNr, count(*) >>> from Person P JOIN Car C ON P.PersonID = C.PersonID >>> group by PersonNr >>> having count(*) > 1 >>> (132892 row(s) affected) >>> >>> (select statement 3) >>> select PersonNr, count(*) >>> from Person P JOIN Car C ON P.PersonID = C.PersonID >>> group by PersonNr >>> having count(*) > 2 >>> (0 row(s) affected) >>> >>> (select statement 4) >>> select count(distinct P.PersonID), count(distinct C.CarID) >>> from Person P OUTER JOIN Car C ON P.PersonID = C.PersonID >>> where C.CarID IS NULL OR P.PersonID IS NULL >>> --------------- -------------------- >>> 198898 114388 >>> (1 row(s) affected) >>> >>> I have went through each select statement and have discussed of my own >>> about to figure out the number of rows in the Person table and the >>> number of >>> rows in the car table in the following. >>> >>> According to the select statement 3 no Person have more then 2 cars. Am >>> I >>> right? >>> >>> Then according to my understanding about the select statements 2 132 >>> 892 >>> Persons have two cars and according to select statement 1 1037854 >>> Persons >>> have cars but some persons have 2 cars. This means that if you make this >>> calculation 1037854 - 132892 = 904962 will give you the number of >>> Persons >>> that have exact one car. Do you agree with me here? >>> >>> According to select statement 3 no person have more then 2 cars. Do you >>> agree with me? >>> >>> I mean that in table person exist Persons that own a car and some >>> Persons >>> that doesn't have any car at all. >>> If you add the number of Persons that have one car with Persons that >>> doesn't >>> have any car you will get the right answer. >>> First the numer of Persons that have a car is 904962 persons. Then >>> according to statement 4 198898 persons doesnt't have any car. If you >>> sum >>> these two numbers together you get 904962 + 198898 = 1103860 which >>> would >>> mean that 1103860 persons exist in table Person. >>> >>> In table Car one and the same car can't be own by two Persons. This mean >>> that 904962 persons own one unique car and according to statement 4 >>> 114388 >>> cars doesnt'r have any owner. If you sum these two figure up you get >>> 904962 >>> + 114388 = 101935 cars. >>> >>> According to my calculation there would exist 1103860 rows in table >>> Person >>> and 101935 cars in table Cars. But this answer is wrong. >>> >>> I'm not sure if both are wrong but what I do know is that both are not >>> right. >>> >>> Have you any suggestion about the number of rows in table Person and >>> Car? >>> >>> >>> //Tony >>> >>> >>> > > Hi
See inline: Show quote "Tony Johansson" <johansson.anders***@telia.com> wrote in message It is the number of persons that have one or more (actually 2!) cars.news:EGFxe.28481$d5.181665@newsb.telia.net... > Hello Experts! > > The question that I want to have an answer to is how many rows does the > Person table have and how many rows does table Car have. No matter if the > Persons own a car or not and the same if the car have a owner or not. > > As an aid to be able to answer the question is some select question that > is made to these tables. I have tried but I doesn't come to the right > answer. > > Some background that you must have to be able to answer my question. > We have two tables named Person and Car. > The relation is that one Person can own one or at most two cars which you > can say by see the result from the select statements below. > > The definition of the two tables create statement looks like this. > create table Person > { > PersonID int NOT NULL identity (1, 1), > PersonNr char(11) NOT NULL, > Forename varchar(50) NOT NULL, > Surname varchar(50) NOT NULL, > City varchar(50) NOT NULL, > Street varchar(50) NOT NULL > } > create table Car > { > carID int NOT NULL IDENTITY(1,1), > Plates varchar(8) NOT NULL, > PersonID int NULL > } > > There are four select statements that has been made and executed. > Below are the four select statements and the answer to each. > (select statement 1) > select * > from Person P JOIN Car C ON P.PersonID = C.PersonID > (1037854 row(s) affected) > > (select statement 2) > select PersonNr, count(*) > from Person P JOIN Car C ON P.PersonID = C.PersonID > group by PersonNr > having count(*) > 1 > (132892 row(s) affected) > > (select statement 3) > select PersonNr, count(*) > from Person P JOIN Car C ON P.PersonID = C.PersonID > group by PersonNr > having count(*) > 2 > (0 row(s) affected) > > (select statement 4) > select count(distinct P.PersonID), count(distinct C.CarID) > from Person P OUTER JOIN Car C ON P.PersonID = C.PersonID > where C.CarID IS NULL OR P.PersonID IS NULL > --------------- -------------------- > 198898 114388 > (1 row(s) affected) > > I have went through each select statement and have discussed of my own > about to figure out the number of rows in the Person table and the number > of rows in the car table in the following. > > According to the select statement 3 no Person have more then 2 cars. Am I > right? > > Then according to my understanding about the select statements 2 132 892 > Persons have two cars and according to select statement 1 1037854 Persons > have cars but some persons have 2 cars. This means that if you make this > calculation 1037854 - 132892 = 904962 will give you the number of Persons > that have exact one car. Do you agree with me here? > Sounds OK, but you this assumes that the same person does not have more than > According to select statement 3 no person have more then 2 cars. Do you > agree with me? Yes > > I mean that in table person exist Persons that own a car and some Persons > that doesn't have any car at all. > If you add the number of Persons that have one car with Persons that > doesn't have any car you will get the right answer. > First the numer of Persons that have a car is 904962 persons. Then > according to statement 4 198898 persons doesnt't have any car. If you sum > these two numbers together you get 904962 + 198898 = 1103860 which would > mean that 1103860 persons exist in table Person. one entry in the persons table. > There is no contraints to stop this happening.> In table Car one and the same car can't be own by two Persons. This mean > that 904962 persons own one unique car and according to statement 4 114388 > cars doesnt'r have any owner. If you sum these two figure up you get > 904962 + 114388 = 101935 cars. But one your assumptions I would have though this would be 1037854 + 114388 > Use the COUNT(*) queries suggested by others or sp_spaceused.> According to my calculation there would exist 1103860 rows in table Person > and 101935 cars in table Cars. But this answer is wrong. > > I'm not sure if both are wrong but what I do know is that both are not > right. > How? > Have you any suggestion about the number of rows in table Person and Car? > You may want to play with (and post) sample data. John Show quote > > //Tony > > You have an error in your calculations... I responded to your earlier
post... -- Show quoteWayne Snyder, MCDBA, SQL Server MVP Mariner, Charlotte, NC www.mariner-usa.com (Please respond only to the newsgroups.) I support the Professional Association of SQL Server (PASS) and it's community of SQL Server professionals. www.sqlpass.org "John Bell" <jbellnewspo***@hotmail.com> wrote in message news:Om8N5R7fFHA.1948@TK2MSFTNGP12.phx.gbl... > Hi > > See inline: > "Tony Johansson" <johansson.anders***@telia.com> wrote in message > news:EGFxe.28481$d5.181665@newsb.telia.net... >> Hello Experts! >> >> The question that I want to have an answer to is how many rows does the >> Person table have and how many rows does table Car have. No matter if the >> Persons own a car or not and the same if the car have a owner or not. >> >> As an aid to be able to answer the question is some select question that >> is made to these tables. I have tried but I doesn't come to the right >> answer. >> >> Some background that you must have to be able to answer my question. >> We have two tables named Person and Car. >> The relation is that one Person can own one or at most two cars which you >> can say by see the result from the select statements below. >> >> The definition of the two tables create statement looks like this. >> create table Person >> { >> PersonID int NOT NULL identity (1, 1), >> PersonNr char(11) NOT NULL, >> Forename varchar(50) NOT NULL, >> Surname varchar(50) NOT NULL, >> City varchar(50) NOT NULL, >> Street varchar(50) NOT NULL >> } >> create table Car >> { >> carID int NOT NULL IDENTITY(1,1), >> Plates varchar(8) NOT NULL, >> PersonID int NULL >> } >> >> There are four select statements that has been made and executed. >> Below are the four select statements and the answer to each. >> (select statement 1) >> select * >> from Person P JOIN Car C ON P.PersonID = C.PersonID >> (1037854 row(s) affected) >> >> (select statement 2) >> select PersonNr, count(*) >> from Person P JOIN Car C ON P.PersonID = C.PersonID >> group by PersonNr >> having count(*) > 1 >> (132892 row(s) affected) >> >> (select statement 3) >> select PersonNr, count(*) >> from Person P JOIN Car C ON P.PersonID = C.PersonID >> group by PersonNr >> having count(*) > 2 >> (0 row(s) affected) >> >> (select statement 4) >> select count(distinct P.PersonID), count(distinct C.CarID) >> from Person P OUTER JOIN Car C ON P.PersonID = C.PersonID >> where C.CarID IS NULL OR P.PersonID IS NULL >> --------------- -------------------- >> 198898 114388 >> (1 row(s) affected) >> >> I have went through each select statement and have discussed of my own >> about to figure out the number of rows in the Person table and the number >> of rows in the car table in the following. >> >> According to the select statement 3 no Person have more then 2 cars. Am I >> right? >> >> Then according to my understanding about the select statements 2 132 892 >> Persons have two cars and according to select statement 1 1037854 >> Persons have cars but some persons have 2 cars. This means that if you >> make this calculation 1037854 - 132892 = 904962 will give you the number >> of Persons that have exact one car. Do you agree with me here? > > It is the number of persons that have one or more (actually 2!) cars. > >> >> According to select statement 3 no person have more then 2 cars. Do you >> agree with me? > Yes >> >> I mean that in table person exist Persons that own a car and some Persons >> that doesn't have any car at all. >> If you add the number of Persons that have one car with Persons that >> doesn't have any car you will get the right answer. >> First the numer of Persons that have a car is 904962 persons. Then >> according to statement 4 198898 persons doesnt't have any car. If you >> sum these two numbers together you get 904962 + 198898 = 1103860 which >> would mean that 1103860 persons exist in table Person. > > Sounds OK, but you this assumes that the same person does not have more > than one entry in the persons table. > >> >> In table Car one and the same car can't be own by two Persons. This mean >> that 904962 persons own one unique car and according to statement 4 >> 114388 cars doesnt'r have any owner. If you sum these two figure up you >> get 904962 + 114388 = 101935 cars. > There is no contraints to stop this happening. > > But one your assumptions I would have though this would be 1037854 + > 114388 >> >> According to my calculation there would exist 1103860 rows in table >> Person and 101935 cars in table Cars. But this answer is wrong. >> >> I'm not sure if both are wrong but what I do know is that both are not >> right. >> > How? > >> Have you any suggestion about the number of rows in table Person and Car? >> > Use the COUNT(*) queries suggested by others or sp_spaceused. > > You may want to play with (and post) sample data. > > John > >> >> //Tony >> >> > > I though we agreed, although you did the extra math for people with exactly
one car? John Show quote "Wayne Snyder" <wayne.nospam.snyder@mariner-usa.com> wrote in message news:OAUSi68fFHA.3304@TK2MSFTNGP12.phx.gbl... > You have an error in your calculations... I responded to your earlier > post... > > -- > Wayne Snyder, MCDBA, SQL Server MVP > Mariner, Charlotte, NC > www.mariner-usa.com > (Please respond only to the newsgroups.) > > I support the Professional Association of SQL Server (PASS) and it's > community of SQL Server professionals. > www.sqlpass.org > > "John Bell" <jbellnewspo***@hotmail.com> wrote in message > news:Om8N5R7fFHA.1948@TK2MSFTNGP12.phx.gbl... >> Hi >> >> See inline: >> "Tony Johansson" <johansson.anders***@telia.com> wrote in message >> news:EGFxe.28481$d5.181665@newsb.telia.net... >>> Hello Experts! >>> >>> The question that I want to have an answer to is how many rows does the >>> Person table have and how many rows does table Car have. No matter if >>> the Persons own a car or not and the same if the car have a owner or >>> not. >>> >>> As an aid to be able to answer the question is some select question that >>> is made to these tables. I have tried but I doesn't come to the right >>> answer. >>> >>> Some background that you must have to be able to answer my question. >>> We have two tables named Person and Car. >>> The relation is that one Person can own one or at most two cars which >>> you can say by see the result from the select statements below. >>> >>> The definition of the two tables create statement looks like this. >>> create table Person >>> { >>> PersonID int NOT NULL identity (1, 1), >>> PersonNr char(11) NOT NULL, >>> Forename varchar(50) NOT NULL, >>> Surname varchar(50) NOT NULL, >>> City varchar(50) NOT NULL, >>> Street varchar(50) NOT NULL >>> } >>> create table Car >>> { >>> carID int NOT NULL IDENTITY(1,1), >>> Plates varchar(8) NOT NULL, >>> PersonID int NULL >>> } >>> >>> There are four select statements that has been made and executed. >>> Below are the four select statements and the answer to each. >>> (select statement 1) >>> select * >>> from Person P JOIN Car C ON P.PersonID = C.PersonID >>> (1037854 row(s) affected) >>> >>> (select statement 2) >>> select PersonNr, count(*) >>> from Person P JOIN Car C ON P.PersonID = C.PersonID >>> group by PersonNr >>> having count(*) > 1 >>> (132892 row(s) affected) >>> >>> (select statement 3) >>> select PersonNr, count(*) >>> from Person P JOIN Car C ON P.PersonID = C.PersonID >>> group by PersonNr >>> having count(*) > 2 >>> (0 row(s) affected) >>> >>> (select statement 4) >>> select count(distinct P.PersonID), count(distinct C.CarID) >>> from Person P OUTER JOIN Car C ON P.PersonID = C.PersonID >>> where C.CarID IS NULL OR P.PersonID IS NULL >>> --------------- -------------------- >>> 198898 114388 >>> (1 row(s) affected) >>> >>> I have went through each select statement and have discussed of my own >>> about to figure out the number of rows in the Person table and the >>> number of rows in the car table in the following. >>> >>> According to the select statement 3 no Person have more then 2 cars. Am >>> I right? >>> >>> Then according to my understanding about the select statements 2 132 >>> 892 Persons have two cars and according to select statement 1 1037854 >>> Persons have cars but some persons have 2 cars. This means that if you >>> make this calculation 1037854 - 132892 = 904962 will give you the >>> number of Persons that have exact one car. Do you agree with me here? >> >> It is the number of persons that have one or more (actually 2!) cars. >> >>> >>> According to select statement 3 no person have more then 2 cars. Do you >>> agree with me? >> Yes >>> >>> I mean that in table person exist Persons that own a car and some >>> Persons that doesn't have any car at all. >>> If you add the number of Persons that have one car with Persons that >>> doesn't have any car you will get the right answer. >>> First the numer of Persons that have a car is 904962 persons. Then >>> according to statement 4 198898 persons doesnt't have any car. If you >>> sum these two numbers together you get 904962 + 198898 = 1103860 which >>> would mean that 1103860 persons exist in table Person. >> >> Sounds OK, but you this assumes that the same person does not have more >> than one entry in the persons table. >> >>> >>> In table Car one and the same car can't be own by two Persons. This mean >>> that 904962 persons own one unique car and according to statement 4 >>> 114388 cars doesnt'r have any owner. If you sum these two figure up you >>> get 904962 + 114388 = 101935 cars. >> There is no contraints to stop this happening. >> >> But one your assumptions I would have though this would be 1037854 + >> 114388 >>> >>> According to my calculation there would exist 1103860 rows in table >>> Person and 101935 cars in table Cars. But this answer is wrong. >>> >>> I'm not sure if both are wrong but what I do know is that both are not >>> right. >>> >> How? >> >>> Have you any suggestion about the number of rows in table Person and >>> Car? >>> >> Use the COUNT(*) queries suggested by others or sp_spaceused. >> >> You may want to play with (and post) sample data. >> >> John >> >>> >>> //Tony >>> >>> >> >> > >
Other interesting topics
|
|||||||||||||||||||||||