MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old September 8th, 2004, 12:36 PM
vengeance vengeance is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 23 vengeance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
creating table from table

hi, i'm trying to create a new table for the following query but it seems that there always seem to be a syntax error near the keyword AS from the first line. Is this the correct way to create a table from another table?

CREATE TABLE max_login AS
DECLARE @freq int
SELECT TOP 1 login_user_id, COUNT(login_user_id) AS freq
FROM track_e_login
WHERE login_user_id != 0
GROUP BY login_user_id
ORDER BY freq DESC

Many thanks!

Reply With Quote
  #2  
Old September 8th, 2004, 01:04 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Code:
SELECT TOP 1 login_user_id, COUNT(login_user_id) AS freq
into maxlogins
FROM track_e_login
WHERE login_user_id <> 0
GROUP BY login_user_id
ORDER BY freq DESC 

Reply With Quote
  #3  
Old September 9th, 2004, 03:49 AM
vengeance vengeance is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 23 vengeance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
getting max(access) for each id

Hi. thanks for ur help.

Hope someone can help me with this too or rather ans my question for this. I obtained the below table in black font with this SQL. But the result which i need is the max(access), access and course for each id as shown in red.

1)Can I do this using 1 select statement as shown below or do I need to use 2 select statements?


DECLARE @access int
SELECT access_user_id, COUNT(*) AS access, access_cours_code
FROM track_e_access
WHERE access_user_id != 0
GROUP BY access_user_id, access_cours_code
ORDER BY access_user_id, access DESC


id freq course
1 21 PHY101
1 13 PHY102
1 6 PHY103
1 1 PHY104
6 1 AMAT103
7 5 CHEM101
9 11 CHEM101
9 6 PHY101
9 1 AMAT103
11 9 ENG102
19 1 CHIN101

What i need:
id freq course
1 21 PHY101
6 1 AMAT103
7 5 CHEM101
9 11 CHEM101
11 9 ENG102
19 1 CHIN101

Reply With Quote
  #4  
Old September 9th, 2004, 06:01 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Code:
select id,freq,access_course_code
from (
   SELECT access_user_id as id , 
              COUNT(*) AS freq, 
              access_cours_code
      FROM track_e_access
    WHERE access_user_id <> 0
    GROUP BY access_user_id, 
                  access_cours_code
      ) dt1
where freq = (
  select max(freq) 
    from (
    SELECT access_user_id as id , 
               COUNT(*) AS freq,
               access_course_code
       from  track_e_access
     WHERE access_user_id <> 0
    GROUP BY access_user_id, 
                  access_cours_code) dt2
    where dt1.id = dt2.id 
  )

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > creating table from table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway