Showing posts with label select. Show all posts
Showing posts with label select. Show all posts

Friday, March 30, 2012

Prediction Accuracy

hi,

I am using time series agorithm.I need standard deviation in %. I am using SELECT StudID, PREDICTSTDEV([Perf]) FROM [Stud_Model].This one is giving me the standard deviation like this

StudID stDev

001 2.891298978779

002 2.797288978779.

But I need like this

StudID stDev

001 +50%

002 +51%(From The Previous) like that.

Is it Possible.

Thanks,

Karthik.

To get the standard deviation as a percentage, you just need to get the predicted value and divide e.g.

PredictStdev([Perf])/Predict([Perf]) // of course Predict(Perf) could be 0.

However, I'm not sure what you meant by "From the Previous", though

Prediction Accuracy

hi,

I am using time series agorithm.I need standard deviation in %. I am using SELECT StudID, PREDICTSTDEV([Perf]) FROM [Stud_Model].This one is giving me the standard deviation like this

StudID stDev

001 2.891298978779

002 2.797288978779.

But I need like this

StudID stDev

001 +50%

002 +51%(From The Previous) like that.

Is it Possible.

Thanks,

Karthik.

To get the standard deviation as a percentage, you just need to get the predicted value and divide e.g.

PredictStdev([Perf])/Predict([Perf]) // of course Predict(Perf) could be 0.

However, I'm not sure what you meant by "From the Previous", though

sql

Predicting in Trees

Hi! I have created a DMM using Trees. But when I go to the Mining Model Predition tab and select a Predict function, I get this in the criteria column: <Scalar column reference>[, EXCLUDE_NULL|INCLUDE_NULL][, INCLUDE_NODE_ID]. When select Result, I get this error: "An incorrect number of arguments are used in the function at line 3, column 3." I'm predicting a continuous variable.

But when I delete everything except <Scalar column reference> I get this error: "Parser: The syntax for '<' is incorrect."

When I delete everything in the criteria column, I get this: "Query execution failed."

If I change the criteria to "<Scalar column reference>,INCLUDE_NULL, INCLUDE_NODE_ID" I get the error again that the query execution failed.

I'm working from a data set I created. I had no problems with predictions using clustering, but can't seem to get Trees to work.

Hello,

<Scalar column reference> is supposed to be a placeholder for the actual column name. For example, if you are building a Decision Tree model to predict, say the [Bike Buyer] column (the example in the sample database coming with SQL Server 2005), the function call may look like: Predict( [Bike buyer]) or Predict( [Bike buyer], EXCLUDE_NULL).

Hope this helps

|||Very helpful! Thanks!

Wednesday, March 28, 2012

Precedence of MAX and WHERE

Hi
I'd like to create a query which returns the MAX of a group of dates so long
as the number is less than a given date. For example :
SELECT MAX(date), username
FROM mydatatable
WHERE date < '01/01/2005'
GROUP BY username
Will this do what I expect and return the username and date which is the
most recent before 01/01/2005 ?
Thanks
AndrewHi,
Your query looks good.
Thanks
Hari
SQL Server MVP
"Andrew Webb" <andrew.webb@.eme-med.co.uk> wrote in message
news:OTlNYqfuFHA.1572@.TK2MSFTNGP10.phx.gbl...
> Hi
> I'd like to create a query which returns the MAX of a group of dates so
> long as the number is less than a given date. For example :
>
> SELECT MAX(date), username
> FROM mydatatable
> WHERE date < '01/01/2005'
> GROUP BY username
>
> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
> Thanks
> Andrew
>|||You could compare these queries and see which one yields the results you
want. Word problems are tough to solve, usually better to provide specs as
described in http://www.aspfaq.com/5006 . Also, "date" is a really bad name
for a column. Not only is it a reserved word, it is also very tough to
decipher it... date of WHAT? Finally, do not use m/d/y or d/m/y date
formats when hard-coding date strings. The safest approach here is to use
YYYYMMDD format, then this can't be by software or humans.
CREATE TABLE dbo.myDataTable
(
username VARCHAR(32),
eventDate SMALLDATETIME
)
GO
SET NOCOUNT ON
INSERT myDataTable SELECT 'bob','20040101'
INSERT myDataTable SELECT 'bob','20050201'
INSERT myDataTable SELECT 'frank','20040101'
INSERT myDataTable SELECT 'frank','20040725'
GO
SELECT username, MAX(eventDate)
FROM dbo.myDataTable
WHERE eventDate < '20050101'
GROUP BY username
SELECT username, MAX(eventDate)
FROM dbo.myDataTable
GROUP BY username
HAVING MAX(eventDate) < '20050101'
GO
DROP TABLE dbo.myDataTable
GO
"Andrew Webb" <andrew.webb@.eme-med.co.uk> wrote in message
news:OTlNYqfuFHA.1572@.TK2MSFTNGP10.phx.gbl...
> Hi
> I'd like to create a query which returns the MAX of a group of dates so
> long as the number is less than a given date. For example :
>
> SELECT MAX(date), username
> FROM mydatatable
> WHERE date < '01/01/2005'
> GROUP BY username
>
> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
> Thanks
> Andrew
>|||Andrew,

> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
It is correct, but it could be more than one. It will select each username
and the max date for those username with date values less than '20050101'. I
f
a username does not have date values in this range then it will not appear i
n
the result.
AMB
"Andrew Webb" wrote:

> Hi
> I'd like to create a query which returns the MAX of a group of dates so lo
ng
> as the number is less than a given date. For example :
>
> SELECT MAX(date), username
> FROM mydatatable
> WHERE date < '01/01/2005'
> GROUP BY username
>
> Will this do what I expect and return the username and date which is the
> most recent before 01/01/2005 ?
> Thanks
> Andrew
>
>

Monday, March 26, 2012

Prblem to store XML result into an output veriable on SQL 2000

Hi,

I want to store the result of the query

SELECT * FROM Customer FOR XML AUTO,ELEMENTS

Into an output veriable. How will I do this in SQL Server 2000?

I've tried this in simple way like

declare @.x varchar(1000)

set @.x = (select * from customer for xml auto,elements)

select @.x

This is perfectly working in SQL 2005 but throwing error in 2000

also in I've tried this using cursor, TempTable on SQL Server 2000.

Please help me.

You can't able to do this in SQL Server 2000. In SQL Server 2000 we don't have XML datatype. It is introduced from SQL Server 2005 only.

The only possible solution is manullay concatinating the values, but it is very expensive and there is char length limitation (8000) may cause truncation of your data.

|||

Thank you very much.

Prblem to store XML result into an output veriable on SQL 2000

Hi,

I want to store the result of the query

SELECT * FROM Customer FOR XML AUTO,ELEMENTS

Into an output veriable. How will I do this in SQL Server 2000?

I've tried this in simple way like

declare @.x varchar(1000)

set @.x = (select * from customer for xml auto,elements)

select @.x

This is perfectly working in SQL 2005 but throwing error in 2000

also in I've tried this using cursor, TempTable on SQL Server 2000.

Please help me.

You can't able to do this in SQL Server 2000. In SQL Server 2000 we don't have XML datatype. It is introduced from SQL Server 2005 only.

The only possible solution is manullay concatinating the values, but it is very expensive and there is char length limitation (8000) may cause truncation of your data.

|||

Thank you very much.

PP: XML Variable DataLength returns as 5

Hi Folks
Here is what I found, when I execute this query
set nocount on
Declare @.xmlSourceDestinationAttributes XML
Select @.xmlSourceDestinationAttributes = ''
--Select @.xmlSourceDestinationAttributes
select Datalength(@.xmlSourceDestinationAttributes)
--
5
Question
======= How come I get a value of 5 even tough I passed nothing.Try using
select CAST(@.xmlSourceDestinationAttributes AS VARBINARY(MAX))
and you will see the BOM that is at the beginning of the xml document.
Dan
> set nocount on
> Declare @.xmlSourceDestinationAttributes XML
> Select @.xmlSourceDestinationAttributes = ''
> --Select @.xmlSourceDestinationAttributes
> select Datalength(@.xmlSourceDestinationAttributes

Friday, March 23, 2012

PowerBuilder connect to Access(get an error SQLSTATE = 01S01 )

I am trying to connect Microsoft Access Database from PowerBuilder(ODBC),
when execute a select clause, get an error SQLSTATE = 01S01(Row errors), bu
t
trying open the database from Microsoft Access 2000 tools , the row is very
good. I am puzzled this error. Someone help me to this problem, pleaseHi
This is a SQL Server newsgroup a better place to post this would be a
powerbuilder or Access newsgroup. Searching google for 01S01 returned quite
a
few hits
http://tinyurl.com/79ru9
this one looked promising
http://tinyurl.com/cr6u7
and indicates that is pssibly masking the real error which in this case the
truncation of a string.
John
"guo-feng lui via webservertalk.com" wrote:

> I am trying to connect Microsoft Access Database from PowerBuilder(ODBC
),
> when execute a select clause, get an error SQLSTATE = 01S01(Row errors),
but
> trying open the database from Microsoft Access 2000 tools , the row is ver
y
> good. I am puzzled this error. Someone help me to this problem, please
>sql

Wednesday, March 21, 2012

PostgreSQL

In a Union query, I have initialized one new column for sorting purpose. For
ex :
select ORD = 1 , t1.a, t1.b from table1 as t1
Union
select ORD = 0, t2.a, t2.b from table2 as t2
order by ORD.
ORD column is not present in thw table. It is used here for putting all the
rows of the second query before the rows of first query. The query is
getting executed in Windows which uses SQL 2000 server. But in Linux(RedHat)
when we are using PostgresSQL database, the same query gives error - "Unable
to parse 'ORD' ".
Is this thing not supported in PostgresSQL? If this is not supported then
what is the method of acheiving the same result?
Thanks,
Venkat
Try:
select 1 AS ORD , t1.a, t1.b from table1 as t1
Union
select 0, t2.a, t2.b from table2 as t2
order by ORD
column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
opposed to expression AS column_alias which is standard SQL.
Jacco Schalkwijk
SQL Server MVP
"Venkat" <venkat_kp@.yahoo.com> wrote in message
news:1092226310.253625@.sj-nntpcache-5...
> In a Union query, I have initialized one new column for sorting purpose.
> For
> ex :
> select ORD = 1 , t1.a, t1.b from table1 as t1
> Union
> select ORD = 0, t2.a, t2.b from table2 as t2
> order by ORD.
> ORD column is not present in thw table. It is used here for putting all
> the
> rows of the second query before the rows of first query. The query is
> getting executed in Windows which uses SQL 2000 server. But in
> Linux(RedHat)
> when we are using PostgresSQL database, the same query gives error -
> "Unable
> to parse 'ORD' ".
> Is this thing not supported in PostgresSQL? If this is not supported then
> what is the method of acheiving the same result?
>
> Thanks,
> Venkat
>
>
|||"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid > wrote
in message news:#YjU$q7fEHA.2848@.TK2MSFTNGP10.phx.gbl...
> Try:
> select 1 AS ORD , t1.a, t1.b from table1 as t1
> Union
> select 0, t2.a, t2.b from table2 as t2
> order by ORD
> column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
> opposed to expression AS column_alias which is standard SQL.
>
Thanks Jacco it worked for me.
regards,
Venkat

PostgreSQL

In a Union query, I have initialized one new column for sorting purpose. For
ex :
select ORD = 1 , t1.a, t1.b from table1 as t1
Union
select ORD = 0, t2.a, t2.b from table2 as t2
order by ORD.
ORD column is not present in thw table. It is used here for putting all the
rows of the second query before the rows of first query. The query is
getting executed in Windows which uses SQL 2000 server. But in Linux(RedHat)
when we are using PostgresSQL database, the same query gives error - "Unable
to parse 'ORD' ".
Is this thing not supported in PostgresSQL? If this is not supported then
what is the method of acheiving the same result?
Thanks,
VenkatTry:
select 1 AS ORD , t1.a, t1.b from table1 as t1
Union
select 0, t2.a, t2.b from table2 as t2
order by ORD
column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
opposed to expression AS column_alias which is standard SQL.
Jacco Schalkwijk
SQL Server MVP
"Venkat" <venkat_kp@.yahoo.com> wrote in message
news:1092226310.253625@.sj-nntpcache-5...
> In a Union query, I have initialized one new column for sorting purpose.
> For
> ex :
> select ORD = 1 , t1.a, t1.b from table1 as t1
> Union
> select ORD = 0, t2.a, t2.b from table2 as t2
> order by ORD.
> ORD column is not present in thw table. It is used here for putting all
> the
> rows of the second query before the rows of first query. The query is
> getting executed in Windows which uses SQL 2000 server. But in
> Linux(RedHat)
> when we are using PostgresSQL database, the same query gives error -
> "Unable
> to parse 'ORD' ".
> Is this thing not supported in PostgresSQL? If this is not supported then
> what is the method of acheiving the same result?
>
> Thanks,
> Venkat
>
>|||"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
in message news:#YjU$q7fEHA.2848@.TK2MSFTNGP10.phx.gbl...
> Try:
> select 1 AS ORD , t1.a, t1.b from table1 as t1
> Union
> select 0, t2.a, t2.b from table2 as t2
> order by ORD
> column_alias = expression is T-SQL specific and not ANSI-SQL standard, as
> opposed to expression AS column_alias which is standard SQL.
>
Thanks Jacco it worked for me.
regards,
Venkat

Tuesday, March 20, 2012

Post Deployment - Running Pkg With Set Values !!VOID

I was wondering if someone could provide me with a bit info on how to Set Values in the dtexecui?

I have a package that does a T-SQL SELECT query with a @.var, and that @.var needs to be set at runtime, in the Agent Job....

Thanks in advance.

Nevermind, having a slllllowwww moment this morning, need more coffee:)
In case others would like to know:

\Package.Variables[User::VAR_NAME].Properties[Value] : VAR_VALUE|||I've submitted a DCR to get a GUI interface as part of dtexecui that allows you to generate these property pathswithout typing them in yourself. It shouldn't be hard seeing as the same thing already exists in SSIS Designer.
In the meantime, the way to generate these property paths is using the XML configuration file wizard within SSIS Designer.

I'm sure you know this Jason :)

-Jamie|||Is it Feedback. Got a reference I can vote on? Drives me round the twist too.|||

DarrenSQLIS wrote:

Is it Feedback. Got a reference I can vote on? Drives me round the twist too.

Nah. I did it thru betaplace!

Post Deployment - Running Pkg With Set Values

I was wondering if someone could provide me with a bit info on how to Set Values in the dtexecui?

I have a package that does a T-SQL SELECT query with a @.var, and that @.var needs to be set at runtime, in the Agent Job....

Thanks in advance.

Nevermind, having a slllllowwww moment this morning, need more coffee:)
In case others would like to know:

\Package.Variables[User::VAR_NAME].Properties[Value] : VAR_VALUE|||I've submitted a DCR to get a GUI interface as part of dtexecui that allows you to generate these property pathswithout typing them in yourself. It shouldn't be hard seeing as the same thing already exists in SSIS Designer.
In the meantime, the way to generate these property paths is using the XML configuration file wizard within SSIS Designer.

I'm sure you know this Jason :)

-Jamie|||Is it Feedback. Got a reference I can vote on? Drives me round the twist too.|||

DarrenSQLIS wrote:

Is it Feedback. Got a reference I can vote on? Drives me round the twist too.

Nah. I did it thru betaplace!

POST BACK TO SERVER

hi all,

i want to filter data from a database using parameters supplied by the user via textboxes. i've been able to write the select statement. my problem now is, the code behind for the "view data" button. do i do "sqldatasource1.select" orpost the databack to theserver? if i'm topostback to theserver, whats the code i should use?

protected void button1_Click(object sender, Eventargs e)

{

????

}


I guess it depends. Are you simply displaying data within something like a GridView? If so, then just use GridView.DataBind() and set your Parameters within the SqlDataSource.Selecting event. You could also set up your Parameters to be ControlParameters and point them directly to your TextBoxes.

|||

well i had done that already. it was just the code behind i needed. i didnt put any code and at runtime i clicked the button and it posted to the server. so i guess thats all i need. thanks for the input though

Monday, March 12, 2012

possibly merge join bug?

i'm merge joining 2 data sources, one is oracle and the other is excel...the problem is in the oracle source, it's a sql statement like:

select hdr.div_ord_no, hdr.mtr_no, hdr.prod_cd
from qctrl_div_ord_header hdr,
(select max(sub.eff_dt_from) min_eff_dt_from, div_ord_no
from qctrl_div_ord_header sub
group by div_ord_no
) tmp
where hdr.eff_dt_from = tmp.min_eff_dt_from
and hdr.div_ord_no = tmp.div_ord_no

having that sql statement, merging will come out with 0 rows

however, having a simple query like:

select hdr.div_ord_no, hdr.mtr_no, hdr.prod_cd
from qctrl_div_ord_header hdr

merging will come out with 2 rows

you may think that the data in the first sql statement is not there for the merge, which causing the 0 rows, however, the data is there, i'm only joining by one column and definitely the data is there, the merge result should be 2 rows for both query statements

i believe this is a problem with SSIS, anyway around this?

Are the inputs to the MERGE JOIN sorted? I is a requirement that they are for MERGE JOIN to work correctly.

Note that setting IsSorted=Yes on the input does not mean that the data gets sorted for you!

-Jamie

|||yes, the input are sorted, everything should be setup correctly, hence, i got the merge to run and work as expected with the simple query

Possibly incorrect query result

/* Test table */
create table test (c1 char(1), c2 varchar(1));
insert into test values ('','');

/* Query */
select
c1,
len(c1) len_c1,
c2,
len(c2) len_c2
from test

The result of the len(c1) expression is 0. I would expect the correct result to be 1, since "c1" is a fixed-length character string type and the values are right-padded with spaces to fit the defined length, in this case 1.

I'm using SQL Server 2005.

Regards,
Ole Willy Tuv

LEN

Returns the number of characters, rather than the number of bytes, of the given string expression, excluding trailing blanks.

Change LEN to DATALENGTH and run this

select
c1,
datalength(c1) len_c1,
c2,
datalength(c2) len_c2
from test

select datalength(' '),len(' ')

Denis the SQL Menace

http://sqlservercode.blogspot.com/

Possible?: Count(*) returned by EXEC

Hi all,

I have a stored procdure which does a select and returns the records
directly -i.e. Not in output parameters e.g:

CREATE PROCEDURE up_SelectRecs(@.ProductName nvarchar(30)) AS

SELECT *
FROM MyTable
WHERE [Name]=@.ProductName

In another stored procedure I need to do the following:

SELECT COUNT(*)
FROM MyTable
WHERE [Name]=@.ProductName

As the select queries are actually a lot more complex that this, I'd
rather not duplicate the select code in 2 sp's to save the maintenance
effort - I'm looking for a way to execute the first procedure from the
second and just count the records returned - something like:

SELECT Count(*)
FROM EXEC up_SelectRecs @.ProductName

Any way to achieve this?

Thanks all

--James"James" <Jamesmitchard@.yahoo.co.uk> wrote in message
news:19d01a84.0501261535.1d7c6dd7@.posting.google.c om...
> Hi all,
> I have a stored procdure which does a select and returns the records
> directly -i.e. Not in output parameters e.g:
> CREATE PROCEDURE up_SelectRecs(@.ProductName nvarchar(30)) AS
> SELECT *
> FROM MyTable
> WHERE [Name]=@.ProductName
> In another stored procedure I need to do the following:
> SELECT COUNT(*)
> FROM MyTable
> WHERE [Name]=@.ProductName
> As the select queries are actually a lot more complex that this, I'd
> rather not duplicate the select code in 2 sp's to save the maintenance
> effort - I'm looking for a way to execute the first procedure from the
> second and just count the records returned - something like:
> SELECT Count(*)
> FROM EXEC up_SelectRecs @.ProductName
> Any way to achieve this?
> Thanks all
> --James

See here:

http://www.sommarskog.se/share_data.html

If you have SQL 2000 (you didn't mention which version you have), a
table-valued UDF would probably work well in your case:

select * from dbo.MyFunc(@.ProductName)
select count(*) from dbo.MyFunc(@.ProductName)

Simon

possible to select top 5 * of 2 or more individual criteria?

Hello,
I want to select the top 5 * from tbl1 where substring(fldx, 1, 1) = 'T'
but in the same output I also want to include
select top 5 * from tbl1 where substring(fldx, 1, 1) = 'S'
I have thousands of rows where fldx starts with 'T' and 'S'. Is it possible
to select the desired rows above in the same output? what is the tsql for
this?
Thanks,
RichTry
select top 5 * from tbl1 where substring(fldx, 1, 1) = 'T'
UNION ALL
select top 5 * from tbl1 where substring(fldx, 1, 1) = 'S'
Keep in mind that the top clause doesn't have much meaning without an ORDER
BY, unless you are relying on the automatic ordering done on a table's
primary key.
"Rich" wrote:

> Hello,
> I want to select the top 5 * from tbl1 where substring(fldx, 1, 1) = 'T'
> but in the same output I also want to include
> select top 5 * from tbl1 where substring(fldx, 1, 1) = 'S'
> I have thousands of rows where fldx starts with 'T' and 'S'. Is it possib
le
> to select the desired rows above in the same output? what is the tsql fo
r
> this?
> Thanks,
> Rich
>|||use union all.
btw, what does "top 5" mean without an "order by" clause? also, "where fldx
like 'T%'" would much likely produce a more efficient exec plan than the
substring function on the column.
dean
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:0D2A3956-4E96-4FDE-BA84-116F27C25856@.microsoft.com...
> Hello,
> I want to select the top 5 * from tbl1 where substring(fldx, 1, 1) = 'T'
> but in the same output I also want to include
> select top 5 * from tbl1 where substring(fldx, 1, 1) = 'S'
> I have thousands of rows where fldx starts with 'T' and 'S'. Is it
> possible
> to select the desired rows above in the same output? what is the tsql
> for
> this?
> Thanks,
> Rich
>|||SELECT * FROM
(SELECT TOP 5 * -- always use COLUMN LIST!
FROM tbl1
WHERE LEFT(fldx,1) = 'T'
ORDER BY '
) T
UNION ALL
(SELECT TOP 5 * -- always use COLUMN LIST!
FROM tbl1
WHERE LEFT(fldx,1) = 'S'
ORDER BY '
) S
ORDER BY '
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:0D2A3956-4E96-4FDE-BA84-116F27C25856@.microsoft.com...
> Hello,
> I want to select the top 5 * from tbl1 where substring(fldx, 1, 1) = 'T'
> but in the same output I also want to include
> select top 5 * from tbl1 where substring(fldx, 1, 1) = 'S'
> I have thousands of rows where fldx starts with 'T' and 'S'. Is it
> possible
> to select the desired rows above in the same output? what is the tsql
> for
> this?
> Thanks,
> Rich
>|||Thank you all for your replies. I was working with substring earlier on
picking out 3 letters from a word, so that stuck in my brain. And I forgot
about including Order By for Top clause, and I was not even thinking about
Union All.
Thanks all for your help.
Rich
"Rich" wrote:

> Hello,
> I want to select the top 5 * from tbl1 where substring(fldx, 1, 1) = 'T'
> but in the same output I also want to include
> select top 5 * from tbl1 where substring(fldx, 1, 1) = 'S'
> I have thousands of rows where fldx starts with 'T' and 'S'. Is it possib
le
> to select the desired rows above in the same output? what is the tsql fo
r
> this?
> Thanks,
> Rich
>|||"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:ez$bhXSIGHA.1180@.TK2MSFTNGP09.phx.gbl...
> SELECT * FROM
> (SELECT TOP 5 * -- always use COLUMN LIST!
> FROM tbl1
> WHERE LEFT(fldx,1) = 'T'
> ORDER BY '
> ) T
> UNION ALL
> (SELECT TOP 5 * -- always use COLUMN LIST!
> FROM tbl1
> WHERE LEFT(fldx,1) = 'S'
> ORDER BY '
> ) S
> ORDER BY '
Have you done a search for SELECT * in 2005 bol?
:)|||Do 800 hits make it a good practice? I don't think so.
ML
http://milambda.blogspot.com/|||> Have you done a search for SELECT * in 2005 bol?
Microsoft does plenty of things that violate best practices. Doesn't mean
you should do it too, and it certainly doesn't mean that I should advocate
it either.|||> Have you done a search for SELECT * in 2005 bol?
Microsoft does plenty of things that violate best practices. Doesn't mean
you should do it too, and it certainly doesn't mean that I should advocate
it either.|||"ML" <ML@.discussions.microsoft.com> wrote in message
news:B77D3202-71FC-412D-A0BB-E8EEE669A599@.microsoft.com...
> Do 800 hits make it a good practice? I don't think so.
Perhaps the next time you advocate someone reading BOL
to get an intro to sql server you should use a asterick :)

Friday, March 9, 2012

Possible to parse a column in a Select statement?

I have a column called SEGMENTED_BLOCK sample data:
X,X,X
XX,XX,XX,
TYZC123456,X,X,
TOYZ654321,1234,777777

I need to do something that has the effect of

SELECT
(stuff before first comma) as FIRST_ITEM,
(stuff after first comma, but before second) as NEXT_ITEM,
(stuff after second comma but before third(if any)) as THIRD_ITEM
FROM SEGMENT_XREF
WHERE LOOKUP_ITEM = 12345
ORDER BY FIRST_ITEM

FIRST_ITEM is pretty easy, but it gets uglier fast.
My attempts are horrendously ugly nested checkindex and substring statements.
Is there an easier way?Hi

Charindex is the usual way to segment strings even when it is like:
http://www.users.drew.edu/skass/sql...unction.sql.txt

John
"grok" <joe.hurzeler@.verizon.net> wrote in message
news:y36Wc.6100$O%4.4380@.nwrddc04.gnilink.net...
I have a column called SEGMENTED_BLOCK sample data:
X,X,X
XX,XX,XX,
TYZC123456,X,X,
TOYZ654321,1234,777777

I need to do something that has the effect of

SELECT
(stuff before first comma) as FIRST_ITEM,
(stuff after first comma, but before second) as NEXT_ITEM,
(stuff after second comma but before third(if any)) as THIRD_ITEM
FROM SEGMENT_XREF
WHERE LOOKUP_ITEM = 12345
ORDER BY FIRST_ITEM

FIRST_ITEM is pretty easy, but it gets uglier fast.
My attempts are horrendously ugly nested checkindex and substring
statements.
Is there an easier way?|||see
http://www.nigelrivett.net/f_GetEntryDelimiitted.html

It's a function that returns entries from a csv string.
I use it for gettig fields from data after bulk inserting but it can be
used for a single string too.

Nigel Rivett
www.nigelrivett.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Possible to join a storeproc result set to normal select?

I have a rather complex storeped proc that does many calculations and
returns a row of data. Now I need to take that result set and use it along
with another result set by way of a select statement. A simplified example:
exec sp_GetAmounts @.ID
returns: 100, 300, 400 , 500
select * from BK where BKID = @.ID
returns: Joe Scmoe, 1234 Main Street, 90-32920, 01/01/2005
Now Ideally, I'd like to have something that 'marries' the two queries and
returns:
Joe Scmoe, 1234 Main Street, 90-32920, 01/01/2005, 100, 300, 400 , 500
My real select statement is more complex than the above example, involving
several joins. Both results sets return much more data than in the above
examples.
Is this possible?You can grab the sp resultset in a temporary or normal table, and use it to
join with the result of the select statement.
Example:
use northwind
go
create table #t (
ShippedDate datetime,
OrderID int,
Subtotal money,
[Year] int
)
insert into #t
exec dbo.[Sales by Year] @.Beginning_Date = '19960101', @.Ending_Date =
'19961231'
select
oh.orderid, oh.orderdate,
t.[year],
t.subtotal
from
orders as oh
left join
#t as t
on oh.orderid = t.orderid
and oh.orderdate >= ltrim(t.[year]) + '0101'
and oh.orderdate < ltrim(t.[year] + 1) + '0101'
drop table #t
go
AMB
"Nelson F." wrote:

> I have a rather complex storeped proc that does many calculations and
> returns a row of data. Now I need to take that result set and use it along
> with another result set by way of a select statement. A simplified example
:
> exec sp_GetAmounts @.ID
> returns: 100, 300, 400 , 500
>
> select * from BK where BKID = @.ID
> returns: Joe Scmoe, 1234 Main Street, 90-32920, 01/01/2005
>
> Now Ideally, I'd like to have something that 'marries' the two queries and
> returns:
> Joe Scmoe, 1234 Main Street, 90-32920, 01/01/2005, 100, 300, 400 , 500
>
> My real select statement is more complex than the above example, involving
> several joins. Both results sets return much more data than in the above
> examples.
> Is this possible?
>
>|||Why not use a function instead
For example
USE Northwind
GO
CREATE FUNCTION MyFunc (@.CustId varchar(5))
RETURNS TABLE
AS
RETURN (SELECT CustomerId, Count(OrderID) NoOfOrders, MAX(OrderDate) AS
LastOrdered
FROM Orders
WHERE CustomerId = @.CustId
GROUP BY CustomerId)
GO
CREATE PROC MyProc @.CustId varchar(5)
AS
SELECT CT.CustomerId, CT.CompanyName, FN.NoOfOrders, FN.LastOrdered
FROM dbo.MyFunc(@.CustId) FN INNER JOIN Customers CT
ON FN.CustomerId = CT.CustomerId
GO
EXEC MyProc 'VINET'
"Alejandro Mesa" wrote:
> You can grab the sp resultset in a temporary or normal table, and use it t
o
> join with the result of the select statement.
> Example:
> use northwind
> go
> create table #t (
> ShippedDate datetime,
> OrderID int,
> Subtotal money,
> [Year] int
> )
> insert into #t
> exec dbo.[Sales by Year] @.Beginning_Date = '19960101', @.Ending_Date =
> '19961231'
> select
> oh.orderid, oh.orderdate,
> t.[year],
> t.subtotal
> from
> orders as oh
> left join
> #t as t
> on oh.orderid = t.orderid
> and oh.orderdate >= ltrim(t.[year]) + '0101'
> and oh.orderdate < ltrim(t.[year] + 1) + '0101'
> drop table #t
> go
>
> AMB
> "Nelson F." wrote:
>|||> Why not use a function instead
I can not answer this question because I have no idea what the sp is doing.
No code was posted with the msg.
AMB
"Andy B" wrote:
> Why not use a function instead
> For example
> USE Northwind
> GO
> CREATE FUNCTION MyFunc (@.CustId varchar(5))
> RETURNS TABLE
> AS
> RETURN (SELECT CustomerId, Count(OrderID) NoOfOrders, MAX(OrderDate) AS
> LastOrdered
> FROM Orders
> WHERE CustomerId = @.CustId
> GROUP BY CustomerId)
> GO
> CREATE PROC MyProc @.CustId varchar(5)
> AS
> SELECT CT.CustomerId, CT.CompanyName, FN.NoOfOrders, FN.LastOrdered
> FROM dbo.MyFunc(@.CustId) FN INNER JOIN Customers CT
> ON FN.CustomerId = CT.CustomerId
> GO
> EXEC MyProc 'VINET'
>
> "Alejandro Mesa" wrote:
>|||Sorry AMB, i was offering Nelson an alternative solution to yours given the
ouput he had specified.
I should've replied to his message and not yours
Andy
"Alejandro Mesa" wrote:
> I can not answer this question because I have no idea what the sp is doing
.
> No code was posted with the msg.
>
> AMB
>
> "Andy B" wrote:
>|||Thanks to both of you both solutions work well!

Possible to have a sql query like this...

Say i have a string formatted as...

s = 32,45,2,13,4

in my SQL stored procedure... i want to do something like...

SELECT * FROM t1 WHERE t1.a IN s

I know that will not work with the comma character seperating each value...but is there a way to do it so that it work? i dont think there is...but i just want to make sure.This would work:


CREATE PROCEDURE GetProductsByID
(
@.s varchar(255)
)

AS

DECLARE @.SELECT varchar(500)
SET @.SELECT = 'SELECT * FROM Products WHERE Products.ProductID IN (' + @.s + ')'

EXEC(@.SELECT)

|||do you know why it has to be done this way? would it have worked if a delimiter was | rather than ,?

and say we have 33,23,45,65

will that also return rows wid ID's of 3, b/c of 33?