|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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! |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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
)
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > creating table from table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|