Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
paul.rowling
Yak Posting Veteran
81 Posts |
Posted - 2003-01-22 : 05:39:32
|
| Hi I'm new to SQL and need some help. I've created a query to find the total amount of time someone spends on a sunbed by summing the individual visits. The query is shown below:SELECT CLIENTSESSION.CLIENTID, SUM(CLIENTSESSION.DURATION) AS TOTAL_DURATIONFROM CLIENTSESSIONGROUP BY CLIENTSESSION.CLIENTIDI now want to be able to find all of those clients who's total duration is greater than 60 and then append these records into a new table.Can anyone help??CheersPaul |
|
|
Crespo24
Village Idiot
144 Posts |
Posted - 2003-01-22 : 05:53:55
|
SELECT CLIENTSESSION.CLIENTID, SUM(CLIENTSESSION.DURATION) AS TOTAL_DURATIONINTO #NEW_TABLEFROM CLIENTSESSIONGROUP BY CLIENTSESSION.CLIENTIDHAVING SUM(CLIENTSESSION.DURATION) > 60Regards |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-01-22 : 05:54:09
|
| INSERT INTO yourtableSELECT CLIENTSESSION.CLIENTID, SUM(CLIENTSESSION.DURATION) AS TOTAL_DURATIONFROM CLIENTSESSIONWHERE sum(clientsession.duration) > 60GROUP BY CLIENTSESSION.CLIENTID-------Moo. |
 |
|
|
paul.rowling
Yak Posting Veteran
81 Posts |
Posted - 2003-01-22 : 06:26:02
|
| Hi,I'm using crespo 24's method at the moment which works great, but I need to make it a bit more complicated i.e I need to pull info from another table to insert into a new table. My query at the moment looks like this:SELECT CLIENTSESSION_IMPORT.CLIENTID, SUM(CLIENTSESSION_import.DURATION) AS TOTAL_DURATION, CLIENT_IMPORT.TITLE, CLIENT_IMPORT.FIRST_NAME, CLIENT_IMPORT.MIDDLE_NAME, CLIENT_IMPORT.LAST_NAMEFROM CLIENTSESSION_IMPORT INNER JOIN CLIENT_IMPORT ON (CLIENTSESSION_IMPORT.CLIENTID = CLIENT_IMPORT.CLIENTID)GROUP BYCLIENTSESSION_import.CLIENTIDhaving SUM(CLIENTSESSION_import.DURATION) > 60but I am getting an "invalid column reference" error. I can't see anything wrong with the column names. I've also left out the INTO #NEW TABLE statement at the moment as I want to check its selecting the right criteria.CheersPaul |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-01-22 : 06:31:18
|
| I don't think it is the cause of your problem, but you will need to include your additional fields into the group by clause.-------Moo. |
 |
|
|
Crespo24
Village Idiot
144 Posts |
Posted - 2003-01-22 : 07:31:21
|
quote: Hi,I'm using crespo 24's method at the moment which works great, but I need to make it a bit more complicated i.e I need to pull info from another table to insert into a new table. My query at the moment looks like this:SELECT CLIENTSESSION_IMPORT.CLIENTID, SUM(CLIENTSESSION_import.DURATION) AS TOTAL_DURATION, CLIENT_IMPORT.TITLE, CLIENT_IMPORT.FIRST_NAME, CLIENT_IMPORT.MIDDLE_NAME, CLIENT_IMPORT.LAST_NAMEFROM CLIENTSESSION_IMPORT INNER JOIN CLIENT_IMPORT ON (CLIENTSESSION_IMPORT.CLIENTID = CLIENT_IMPORT.CLIENTID)GROUP BYCLIENTSESSION_import.CLIENTIDhaving SUM(CLIENTSESSION_import.DURATION) > 60but I am getting an "invalid column reference" error. I can't see anything wrong with the column names. I've also left out the INTO #NEW TABLE statement at the moment as I want to check its selecting the right criteria.CheersPaul
Here you goCREATE TABLE CLIENTSESSION (CLIENTID SMALLINT, DURATION NUMERIC(9,2))INSERT INTO CLIENTSESSION (CLIENTID, DURATION)SELECT '01', '20.00'UNION ALLSELECT '01', '35.00'UNION ALLSELECT '03', '25.00'UNION ALLSELECT '03', '50.00'UNION ALLSELECT '02', '10.00'UNION ALLSELECT '02', '30.00'UNION ALLSELECT '02', '30.00'CREATE TABLE CLIENT(CLIENTID SMALLINT, SURNAME VARCHAR(50), FORENAMES VARCHAR(50))INSERT INTO CLIENT(CLIENTID, SURNAME, FORENAMES)SELECT '01', 'CRESPO', 'RIO'UNION ALLSELECT '02', 'NIGHTELF', 'CRESPO'UNION ALLSELECT '03', 'SAVIOLA', 'DIEGO'SELECT A.CLIENTID, SUM(A.DURATION) AS TOTAL_DURATION, B.SURNAME, B.FORENAMESFROM CLIENTSESSION AS A INNER JOIN CLIENT AS B ON A.CLIENTID = B.CLIENTID GROUP BY A.CLIENTID, B.SURNAME, B.FORENAMESHAVING SUM(A.DURATION) > 60 DROP TABLE CLIENTSESSIONDROP TABLE CLIENT |
 |
|
|
paul.rowling
Yak Posting Veteran
81 Posts |
Posted - 2003-01-22 : 07:32:01
|
| Hi,Mr Mist, what you suggested did cure the problem, but I now have another problem. I have now altered the sql above to include the insert statement and have added additional fields , now I thought that by using the group by clause this would find only one instance of the clientid. The problem is that when I go to insert the records I get an error stating that there is a violation of the primary key, as the table that the records are being inserted into is going to hold only one instance of the clientid.My SQL is as follows:INSERT INTO CLIENT (CLIENTID, FIELD_1, TITLE, FIRST_NAME, MIDDLE_NAME, LAST_NAME,DOB, ADDRESS1, ADDRESS2, ADDRESS3, ADDRESS4, ADDRESS5, ADDRESS6, POSTCODE,HOME_TELEPHONE, WORK_TELEPHONE, MOBILE_TELEPHONE, EMAIL_ADDRESS, GENDER,DOCTOR_NAME, DOCTOR_TELEPHONE, SESSIONDISCOUNT, COURSEDISCOUNT, PRODUCTDISCOUNT,CREDIT_LIMIT, DO_NOT_MAIL, DATE_ENROLLED, BRANCH_ID,SKIN_TYPE, SOURCE_ID, MEMBERSHIP_ID)SELECTCLIENTSESSION_IMPORT.CLIENTID,SUM(CLIENTSESSION_IMPORT.DURATION) AS TOTAL_DURATION,CLIENT_IMPORT.TITLE,CLIENT_IMPORT.FIRST_NAME,CLIENT_IMPORT.MIDDLE_NAME,CLIENT_IMPORT.LAST_NAME,CLIENT_IMPORT.DOB,CLIENT_IMPORT.ADDRESS1,CLIENT_IMPORT.ADDRESS2,CLIENT_IMPORT.ADDRESS3,CLIENT_IMPORT.ADDRESS4,CLIENT_IMPORT.ADDRESS5,CLIENT_IMPORT.ADDRESS6,CLIENT_IMPORT.POSTCODE,CLIENT_IMPORT.HOME_TELEPHONE,CLIENT_IMPORT.WORK_TELEPHONE,CLIENT_IMPORT.MOBILE_TELEPHONE,CLIENT_IMPORT.EMAIL_ADDRESS,CLIENT_IMPORT.GENDER,CLIENT_IMPORT.DOCTOR_NAME,CLIENT_IMPORT.DOCTOR_TELEPHONE,CLIENT_IMPORT.SESSIONDISCOUNT,CLIENT_IMPORT.COURSEDISCOUNT,CLIENT_IMPORT.PRODUCTDISCOUNT,CLIENT_IMPORT.CREDIT_LIMIT,CLIENT_IMPORT.DO_NOT_MAIL,CLIENT_IMPORT.DATE_ENROLLED,CLIENT_IMPORT.BRANCH_ID,CLIENT_IMPORT.SKIN_TYPE,CLIENT_IMPORT.SOURCE_ID,CLIENT_IMPORT.MEMBERSHIP_IDFROMCLIENTSESSION_IMPORTINNER JOIN CLIENT_IMPORT ON (CLIENTSESSION_IMPORT.CLIENTID = CLIENT_IMPORT.CLIENTID)GROUP BYCLIENTSESSION_IMPORT.CLIENTID,CLIENT_IMPORT.TITLE,CLIENT_IMPORT.FIRST_NAME,CLIENT_IMPORT.MIDDLE_NAME,CLIENT_IMPORT.LAST_NAME,CLIENT_IMPORT.DOB,CLIENT_IMPORT.ADDRESS1,CLIENT_IMPORT.ADDRESS2,CLIENT_IMPORT.ADDRESS3,CLIENT_IMPORT.ADDRESS4,CLIENT_IMPORT.ADDRESS5,CLIENT_IMPORT.ADDRESS6,CLIENT_IMPORT.POSTCODE,CLIENT_IMPORT.HOME_TELEPHONE,CLIENT_IMPORT.WORK_TELEPHONE,CLIENT_IMPORT.MOBILE_TELEPHONE,CLIENT_IMPORT.EMAIL_ADDRESS,CLIENT_IMPORT.GENDER,CLIENT_IMPORT.DOCTOR_NAME,CLIENT_IMPORT.DOCTOR_TELEPHONE,CLIENT_IMPORT.SESSIONDISCOUNT,CLIENT_IMPORT.COURSEDISCOUNT,CLIENT_IMPORT.PRODUCTDISCOUNT,CLIENT_IMPORT.CREDIT_LIMIT,CLIENT_IMPORT.DO_NOT_MAIL,CLIENT_IMPORT.DATE_ENROLLED,CLIENT_IMPORT.BRANCH_ID,CLIENT_IMPORT.SKIN_TYPE,CLIENT_IMPORT.SOURCE_ID,CLIENT_IMPORT.MEMBERSHIP_IDhaving SUM(CLIENTSESSION_IMPORT.DURATION) > 60Any help would be greatly appreciated as I think I am almost there!!!CheersPaul |
 |
|
|
Crespo24
Village Idiot
144 Posts |
Posted - 2003-01-22 : 07:44:56
|
Check the data in your CLIENT_IMPORT table and check for duplicate IDs... that's where your problem is. It sounds like some members have more than one entry with different details. Edited by - Crespo24 on 01/22/2003 07:45:28 |
 |
|
|
paul.rowling
Yak Posting Veteran
81 Posts |
Posted - 2003-01-22 : 07:56:32
|
| Hi,Sorry to be a pain, but how do I check for duplicate clientid's |
 |
|
|
Crespo24
Village Idiot
144 Posts |
Posted - 2003-01-22 : 08:26:36
|
quote: Hi,Sorry to be a pain, but how do I check for duplicate clientid's
SELECT CLIENTIDFROM CLIENT_IMPORTGROUP BY CLIENTIDHAVING COUNT(*) > 1if you get any rows returned then you have a problem!Another way is this :SELECT COUNT(*) FROM CLIENTSELECT DISTINCT COUNT(*) FROM CLIENTif they differ then you know you have duplicates, but stick to the first method though. |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-01-22 : 08:27:52
|
quote: Hi,Sorry to be a pain, but how do I check for duplicate clientid's
Select count(*), IMPORT.CLIENTIDfrom [whichevertabletheyarein]having count(*) > 1But, you are I think correct in your assumption that grouping on the ID would limit the output to one line per client, so maybe the problem is elsewhere. Is the output table definitely empty?EDIT because all of that goes out the window once you group on more than one thing. D'oh.-------Moo.Edited by - mr_mist on 01/22/2003 08:46:01 |
 |
|
|
paul.rowling
Yak Posting Veteran
81 Posts |
Posted - 2003-01-22 : 08:41:57
|
| Hi it was my fault, as I had left 1 record in the table I was trying to insert to so duplication would have occurred. With regards to the above query, can I just remove the group by clause to stop the grouping and will the query still work or will I have to make other ammendments?CheersPaul |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-01-22 : 08:48:14
|
| You can't remove the grouping because you have a sum in there for the client session length. You'd only be able to remove the grouping if you somehow linked to this information in another manner. You may find it easier to go about it in two stages if you can't get it to work in one.Populate your output table with client details then add in the total session times, or something.-------Moo. |
 |
|
|
|
|
|
|
|