Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,611 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 933 people online right now. Registration is fast and FREE... Join Now!




T-SQL how to count

 
Reply to this topicStart new topic

T-SQL how to count

RudyVB.net
post 16 Jul, 2008 - 07:10 AM
Post #1


D.I.C Head

**
Joined: 3 May, 2008
Posts: 61

Hello All!

CODE
SELECT @ResCnt =  COUNT(*)
FROM tbl1
WHERE 1Enabled = 'True'


The above code will count how many records have enabled to True. In this case, let's say 10 records are enabled.

CODE
SELECT     Time, Date
FROM         tblAppt
WHERE Date = '7/10/2008' And Time = '8:30 PM'


The above code here matches my time and date for an appointment.

Now what I would like to do is if there is a match of date and time, and it's more than @ResCnt(in this case 10), don't select the time of 8:30 PM.

Basicly, I need to have a dynamic drop down list, only showing available times. If 8:30 PM is taken 10 times, then is should not show is the ddl.

What is the best way to write a SP for that? Any ideas?

Thanks!

Rudy
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 16 Jul, 2008 - 07:25 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,967



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Not sure I'm following this one. It sounds almost like you're looking for this:

CODE

SELECT Time, Date
    FROM tblAppt
    WHERE Date = '7/10/2008'
    group by Date
    having count(Time) < (SELECT COUNT(*) FROM tbl1 WHERE 1Enabled = 'True')
User is offlineProfile CardPM

Go to the top of the page

RudyVB.net
post 16 Jul, 2008 - 12:02 PM
Post #3


D.I.C Head

**
Joined: 3 May, 2008
Posts: 61

QUOTE(baavgai @ 16 Jul, 2008 - 08:25 AM) *

Not sure I'm following this one. It sounds almost like you're looking for this:

CODE

SELECT Time, Date
    FROM tblAppt
    WHERE Date = '7/10/2008'
    group by Date
    having count(Time) < (SELECT COUNT(*) FROM tbl1 WHERE 1Enabled = 'True')


Hi baavgai!

Something like that. I tried that, but it doesn't work. I'm not even sure if this is the best way to do this. I may go back to the drawing board on this one, to see if there is a better way.

Thanks for the help!! smile.gif
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 16 Jul, 2008 - 01:14 PM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,967



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


I'd be curious what "doesn't work" means. However, if you're taking another tack, some quick advise...

Do not use field names like Date, Time, DateTime, etc, Stay away from all field names that could conceivably have meaning to the database itself. Always define a primary key.

On meaning, tbl1 has none. Also, using a count from tbl1 for any kind of filter criteria is ambiguous at best. Try to use names that imply meaning so a new comer to the system can readily understand it. Remember, leave something you've worked on long enough and that new comer will be you.

Good luck.
User is offlineProfile CardPM

Go to the top of the page

RudyVB.net
post 16 Jul, 2008 - 02:44 PM
Post #5


D.I.C Head

**
Joined: 3 May, 2008
Posts: 61

QUOTE(baavgai @ 16 Jul, 2008 - 02:14 PM) *

I'd be curious what "doesn't work" means. However, if you're taking another tack, some quick advise...

Do not use field names like Date, Time, DateTime, etc, Stay away from all field names that could conceivably have meaning to the database itself. Always define a primary key.

On meaning, tbl1 has none. Also, using a count from tbl1 for any kind of filter criteria is ambiguous at best. Try to use names that imply meaning so a new comer to the system can readily understand it. Remember, leave something you've worked on long enough and that new comer will be you.

Good luck.


Good advice. Thank you. When I run the following
CODE
SELECT [Time], Date
    FROM tblSchedule
    WHERE Date = '7/16/2008'
    group by Date
    having count(Time) < (SELECT COUNT(*) FROM tblVis WHERE VisEnabled = 'True')


The code is the same, just had changed the names to make it simple when I first posted.
I get this error
QUOTE
Column 'tblSchedule.Time' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Not sure how to handle that. In changing the field names, like TIME to FLDTIME. Can I change that in SQL 2005, and will it chnage my other SP's?

Thanks for all your help!

Rudy
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 16 Jul, 2008 - 03:47 PM
Post #6


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,967



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Yes, changing field names would impact everything. I wouldn't usually suggest it, but if you're starting over, you might as well benefit from the fresh start.

As to the error, I'm afraid my example has issues. This should get you some useful data:

CODE

SELECT [Time], Date, count(*)
    FROM tblSchedule
    WHERE Date = '7/16/2008'
    GROUP BY [Time], Date


To limit this based on that count, this should work:
CODE

SELECT [Time], Date, count(*) as ItemCount
    FROM tblSchedule
    WHERE Date = '7/16/2008'
    GROUP BY [Time], Date
    HAVING count(*) < (SELECT COUNT(*) as MaxCount FROM tblVis WHERE VisEnabled = 'True')


For more complex filters, this might work better for you:
CODE

SELECT a.[Time], a.Date
    FROM (
        SELECT [Time], Date, count(*) as ItemCount
            FROM tblSchedule
            WHERE Date = '7/16/2008'
            GROUP BY [Time], Date
    ) a
    WHERE a.ItemCount < (SELECT COUNT(*) as MaxCount FROM tblVis WHERE VisEnabled = 'True')


User is offlineProfile CardPM

Go to the top of the page

RudyVB.net
post 16 Jul, 2008 - 04:54 PM
Post #7


D.I.C Head

**
Joined: 3 May, 2008
Posts: 61

Cool!

The second one I think will work out pertfectly.

Thank you for your time! biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:35AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month