Showing posts with label tablestable1. Show all posts
Showing posts with label tablestable1. Show all posts

Friday, March 9, 2012

help with query

i have 2 tables
table1: ClassBase
table2: ScheduleBase

ClassBase (ClassId(guid), ClassName(nvchar))
ScheduleBase (ScheduleId(guid), ClassId(guid from classbase), Date (datetime))

what i want to do is get the top 10 classes scheduled
right now i have
SELECT ClassBase.ClassName FROM ClassBase INNER JOIN ScheduleBase ON ClassBase.ClassId = ScheduleBase.ClassId

this returns all, how do i add something like a distinct count()

thanksTry:


SELECT TOP 10 ClassBase.ClassName FROM ClassBase INNER JOIN ScheduleBase ON ClassBase.ClassId = ScheduleBase.ClassId
|||ScheduleBase rows:
guid, guid.from.class, xx/xx/xxxx
guid, guid.from.class, xx/xx/xxxx
guid, guid.from.class2, xx/xx/xxxx

what i want is to get class name from ClassBase then count how many times that class has been sheduled. so something that'll return

guid.from.class = class 1 -> scheduled 2 times
guid.from.class2 = class2 -> scheduled 1 time|||Sounds like a GROUP BY

Friday, February 24, 2012

Help with max()

I have two tables:
TABLE1 has policy_id and subm_no, multiple subm_no's for every policy_id.
TABLE2 has policy_id
I'm doing a join, WHERE table1.policy_id = table2.policy_id.
I need to display the value of many fields from both tables where
table1.subm_no is the max value of that subm_no FOR THAT POLICY_ID. Can
someone point me in the right direction? Thanks.SELECT Table1.*, Table2.*
FROM Table1
JOIN Table2 ON Table1.policy_id = Table2.policy_id
WHERE Table1.Subm_No =
(
SELECT MAX(Subm_No)
FROM Table1 Tx
WHERE Tx.policy_id = Table1.policy_id
)
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"Rick Charnes" <rickxyz--nospam.zyxcharnes@.thehartford.com> wrote in message
news:MPG.1d98a20592f0f5359898f9@.msnews.microsoft.com...
> I have two tables:
> TABLE1 has policy_id and subm_no, multiple subm_no's for every policy_id.
> TABLE2 has policy_id
> I'm doing a join, WHERE table1.policy_id = table2.policy_id.
> I need to display the value of many fields from both tables where
> table1.subm_no is the max value of that subm_no FOR THAT POLICY_ID. Can
> someone point me in the right direction? Thanks.