Showing posts with label tsql. Show all posts
Showing posts with label tsql. Show all posts

Thursday, March 29, 2012

Finding first X records that when sumed up meet a criteria (Threshold)

Hi I'm tryin to do a tsql statement that returns the first X records that meet a certain criteria (Threshold):

Say the table has 2 columns: ID & Count

And 2 rows:

ID, Count

1, 10

2, 10

I'm looking to return rows and values until a threshold is reached, so

if threshold is 8, query should return:

ID 1 and Count 8

if theshold i s 15, query should return

ID 1, Count 10

ID 2, Count 5

Any help on this would be appreciated, Thanks!

Hey Zorca. First of all, I'll assume for now that you're running SQL 2000. If you're using SQL 2005, let me know and I'll give you a simpler solution using new SQL 2005 features.

To achieve this, you'd first need to write a query to get the running totals for your given table. You can do this as follows (note that I'm using the table name 'ztmp_table'):

create table dbo.ztmp_table (iid int, cnt int)
go

insert dbo.ztmp_table (iid, cnt) select 1,10
insert dbo.ztmp_table (iid, cnt) select 2,10
insert dbo.ztmp_table (iid, cnt) select 3,10
go

-- Get a running total of the cnt column for the table
select t1.iid, min(t1.cnt) as mycnt, sum(t2.cnt) as runcnt
from ztmp_table t1
join ztmp_table t2
on t2.iid <= t1.iid
group by t1.iid
order by t1.iid
go

Once you have that in place, you then use that query to fulfill the remainder of your mission. I'm going to simplify it by wrapping the above query in a view, removing the order by clause, as follows:

create view dbo.ztmp_runtotals as

-- Get a running total of the cnt column for the table
select t1.iid, min(t1.cnt) as mycnt, sum(t2.cnt) as runcnt
from ztmp_table t1
join ztmp_table t2
on t2.iid <= t1.iid
group by t1.iid
go

Now that I have that view to make simpler code, you can use the following script to see how you can achieve what you're looking for:

declare @.thresh int
set @.thresh = 15

select iid,
case
when runcnt > @.thresh then (@.thresh - (runcnt - mycnt))
when runcnt <= @.thresh then mycnt
end as cnt
from dbo.ztmp_runtotals as tmp1
where tmp1.iid <=
isnull((select min(iid) from dbo.ztmp_runtotals where runcnt >= @.thresh),iid)
order by tmp1.iid

I'm not going to spend a lot of time going into detail on how it works, I'll let you figure that part out, but feel free to repost any questions you may have. Play around with the @.thresh parameter a bit to see the different results you get.

HTH,

Monday, March 26, 2012

finding active users from sysprocesses

I am trying to find a select on sysprocesses that would list all the active
logins. An active login is a login that has a TSQL statment being executed
on the server,

This didnt work to well! Any ideas. Thanks in advance.

select sp.loginame,
-- more columns
from master..sysprocesses sp
where sp.status not in ('sleeping','background' )
order by 1You can use the system stored procedure sp_who & sp_who2 for these purposes.

--
- Anith
( Please reply to newsgroups only )|||kr (zzb26 (at) email.com) writes:
> I am trying to find a select on sysprocesses that would list all the
> active logins. An active login is a login that has a TSQL statment being
> executed on the server,
> This didnt work to well! Any ideas. Thanks in advance.
> select sp.loginame,
> -- more columns
> from master..sysprocesses sp
> where sp.status not in ('sleeping','background' )
> order by 1

In which way did it not work? In any case, you definitely want to add
"or opentrn > 0". A process which has a open transaction is active,
even if it is sleeping.

I have a lock-monitoring routine, and the condition I use is

upper(p.cmd) <> 'AWAITING COMMAND'

I also check whether sysprocesses.blocked > 0.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Wednesday, March 21, 2012

Find the greatest of three columns per each row and display

Hi,

i have a problem where in i have to display the greatest marks scored by each student.

How can i do this? Is there any built in TSQL function.

Rgds..,

Aazad

You either use a CASE Statement or Pivot the data to make a relational query possible through the aggregation functions.

SELECT CASE WHEN COlA > COLB THEN COLA ELSE

(CASE WHEN COLB > COLC THEN COLB ELSE COLC END)

END

FROM SomeTable

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

Hi,

I worked. Nice one. I got the other way too..!

Rgds..,

Aazad.

Wednesday, March 7, 2012

Find location of data files

What is the tsql cmb to view the file structure location on a database?
This will help me with my attach and detach.sp_help '<dbname>'
sqlteam32 wrote:
> What is the tsql cmb to view the file structure location on a database?
> This will help me with my attach and detach.|||Sorry, make that:
sp_helpdb '<dbname>'
sqlteam32 wrote:
> What is the tsql cmb to view the file structure location on a database?
> This will help me with my attach and detach.|||try the following:
use <dbname>
sp_helpfile
sp_helpgroup
select * from sysfiles
use master
select * from sysaltfiles
"sqlteam32" <mtorpy@.gmail.com> wrote in message
news:1149798455.023852.166600@.h76g2000cwa.googlegroups.com...
> What is the tsql cmb to view the file structure location on a database?
> This will help me with my attach and detach.
>|||Note that following will yield an error. If a call to a stored procedure is
not the first thing in a batch, you must use the keyword EXECUTE (or EXEC)
Also, I'm not sure why you need sp_helpgroup, which returns security groups.
Perhaps you want sp_helpfilegroup?
USE <dbname>
EXEC sp_helpfile
EXEC sp_helpfilegroup
SELECT * FROM sysfiles
--
HTH
Kalen Delaney, SQL Server MVP
"Jayesh Antony Jose" <jayeshaj@.hotmail.com> wrote in message
news:OvXadVDjGHA.3800@.TK2MSFTNGP03.phx.gbl...
> try the following:
> use <dbname>
> sp_helpfile
> sp_helpgroup
> select * from sysfiles
>
> use master
> select * from sysaltfiles
>
> "sqlteam32" <mtorpy@.gmail.com> wrote in message
> news:1149798455.023852.166600@.h76g2000cwa.googlegroups.com...
>> What is the tsql cmb to view the file structure location on a database?
>> This will help me with my attach and detach.
>

Find location of data files

What is the tsql cmb to view the file structure location on a database?
This will help me with my attach and detach.sp_help '<dbname>'
sqlteam32 wrote:
> What is the tsql cmb to view the file structure location on a database?
> This will help me with my attach and detach.|||Sorry, make that:
sp_helpdb '<dbname>'
sqlteam32 wrote:
> What is the tsql cmb to view the file structure location on a database?
> This will help me with my attach and detach.|||try the following:
use <dbname>
sp_helpfile
sp_helpgroup
select * from sysfiles
use master
select * from sysaltfiles
"sqlteam32" <mtorpy@.gmail.com> wrote in message
news:1149798455.023852.166600@.h76g2000cwa.googlegroups.com...
> What is the tsql cmb to view the file structure location on a database?
> This will help me with my attach and detach.
>|||Note that following will yield an error. If a call to a stored procedure is
not the first thing in a batch, you must use the keyword EXECUTE (or EXEC)
Also, I'm not sure why you need sp_helpgroup, which returns security groups.
Perhaps you want sp_helpfilegroup?
USE <dbname>
EXEC sp_helpfile
EXEC sp_helpfilegroup
SELECT * FROM sysfiles
--
HTH
Kalen Delaney, SQL Server MVP
"Jayesh Antony Jose" <jayeshaj@.hotmail.com> wrote in message
news:OvXadVDjGHA.3800@.TK2MSFTNGP03.phx.gbl...
> try the following:
> use <dbname>
> sp_helpfile
> sp_helpgroup
> select * from sysfiles
>
> use master
> select * from sysaltfiles
>
> "sqlteam32" <mtorpy@.gmail.com> wrote in message
> news:1149798455.023852.166600@.h76g2000cwa.googlegroups.com...
>

Sunday, February 26, 2012

Find column names used in TSQL and Views

Is there are way to find out which View and Stored Procs contain a column
name or even a string? I am trying to rename a columnin a table from
Cust_name to CustName and want to find what views/stored procs will crash.
Thanks in advance,
Stewart Rogers
DataSort Software, L.C.
How do I find a stored procedure containing <text>?
http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html
AMB
"Datasort" wrote:

> Is there are way to find out which View and Stored Procs contain a column
> name or even a string? I am trying to rename a columnin a table from
> Cust_name to CustName and want to find what views/stored procs will crash.
> Thanks in advance,
> --
> Stewart Rogers
> DataSort Software, L.C.
|||looks like AMesa has you covered for sprocs.
This query will get you the views:
use pubs
go
select v.name
from sys.columns c (nolock) inner join sys.views v (nolock) on v.object_id =
c.object_id
where c.name = 'title'
TheSQLGuru
President
Indicium Resources, Inc.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:A3083FBC-6C03-4A28-8B48-F21D8297D3C4@.microsoft.com...[vbcol=seagreen]
> How do I find a stored procedure containing <text>?
> http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html
>
> AMB
>
> "Datasort" wrote:

Find column names used in TSQL and Views

Is there are way to find out which View and Stored Procs contain a column
name or even a string? I am trying to rename a columnin a table from
Cust_name to CustName and want to find what views/stored procs will crash.
Thanks in advance,
--
Stewart Rogers
DataSort Software, L.C.How do I find a stored procedure containing <text>?
http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html
AMB
"Datasort" wrote:
> Is there are way to find out which View and Stored Procs contain a column
> name or even a string? I am trying to rename a columnin a table from
> Cust_name to CustName and want to find what views/stored procs will crash.
> Thanks in advance,
> --
> Stewart Rogers
> DataSort Software, L.C.|||looks like AMesa has you covered for sprocs.
This query will get you the views:
use pubs
go
select v.name
from sys.columns c (nolock) inner join sys.views v (nolock) on v.object_id =c.object_id
where c.name = 'title'
TheSQLGuru
President
Indicium Resources, Inc.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:A3083FBC-6C03-4A28-8B48-F21D8297D3C4@.microsoft.com...
> How do I find a stored procedure containing <text>?
> http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html
>
> AMB
>
> "Datasort" wrote:
>> Is there are way to find out which View and Stored Procs contain a column
>> name or even a string? I am trying to rename a columnin a table from
>> Cust_name to CustName and want to find what views/stored procs will
>> crash.
>> Thanks in advance,
>> --
>> Stewart Rogers
>> DataSort Software, L.C.