Showing posts with label col1. Show all posts
Showing posts with label col1. Show all posts

Friday, March 23, 2012

How to refer to calculated field ?

When I do the following sql statement, I got the error "Invalid column name
'buy'."
SELECT Buy = case when [col1]='BUY' then 1 else 2 end,
Ddif = case when [buy]=0 then 1 else 2 end --> error here
FROM tbl1
How can I refer to Buy in the same sql statement ?
Thanks.That is not a calculated field, it is a column alias. You cannot refer it in
other clauses in the same select statement unless it is nested in a derived
table. Your options are:
SELECT col,
< expression using col >
FROM ( SELECT CASE WHEN col = 'buy'
THEN 1 ELSE 2
END
FROM tbl ) t ( col ) ;
You can also reuse the same expression to avoid the derived table construct
as well.
Anith|||You can't. You'll have to use a derived table. Something like:
select DerivedTable.Buy
,Ddif = case when DerivedTable.Buy=0 then 1 else 2 end
from (
SELECT Buy = case when [col1]='BUY' then 1 else 2 end
FROM tbl1
) DerivedTable
Of course this is untested.
ML
http://milambda.blogspot.com/|||Your first 'BUY' statement is pretty simple - just 1 or 2 based on col1 =
BUY, so how about :
SELECT Buy = case when [col1]='BUY' then 1 else 2 end,
Ddif = case when [col1]<>'BUY' then 1 else 2 end
FROM tbl1
Or something like that. In other words, build the logic for Ddif back to
the source column unaltered by the first case statement.
However, there may be a problem in your logic in terms of what happens first
and second, etc. See what you think:
If you are saying:
For COL1 if 'BUY' then 1 else 2
then after that is done there will be 0 rows where BUY = 0, the only options
are 1 or 2.
So the dDif column will always be 2 no matter what.
"fniles" wrote:
> When I do the following sql statement, I got the error "Invalid column nam
e
> 'buy'."
> SELECT Buy = case when [col1]='BUY' then 1 else 2 end,
> Ddif = case when [buy]=0 then 1 else 2 end --> error here
> FROM tbl1
> How can I refer to Buy in the same sql statement ?
> Thanks.
>
>|||Thank you everybody for your help.

> You can also reuse the same expression to avoid the derived table
> construct
How can I reuse the same expression ? In my case, I will need to reuse the
column alias in more than 1 places in the same sql statement.
For ex:
SELECT right('0000'+ convert(varchar(4),COL1),4) AS BUY,
ABC = case when BUY>0 then
(
Left(BUY,charindex('.',BUY)-1)*32
+
substring(BUY,charindex('.',BUY)+1,2)*2
+
case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
)
else Null
end
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:OamRSFf%23FHA.328@.TK2MSFTNGP14.phx.gbl...
> That is not a calculated field, it is a column alias. You cannot refer it
> in other clauses in the same select statement unless it is nested in a
> derived table. Your options are:
> SELECT col,
> < expression using col >
> FROM ( SELECT CASE WHEN col = 'buy'
> THEN 1 ELSE 2
> END
> FROM tbl ) t ( col ) ;
> You can also reuse the same expression to avoid the derived table
> construct as well.
> --
> Anith
>|||I am wondering since I will do this in a stored procedure and I need to use
the derived column more than once in the same sql statement, would it be
possible to use a temporary table for the derived column, and if it is how
can I do that ? Thanks.
The sql statement:
SELECT right('0000'+ convert(varchar(4),COL1),4) AS BUY,
ABC = case when BUY>0 then
(
Left(BUY,charindex('.',BUY)-1)*32
+
substring(BUY,charindex('.',BUY)+1,2)*2
+
case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
)
else Null
end
from (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
tblA.Account = tblC.Account
WHERE (((tblA.Quantity)<>'0'))
"fniles" <fniles@.pfmail.com> wrote in message
news:%23SD3Bsn%23FHA.360@.TK2MSFTNGP09.phx.gbl...
> Thank you everybody for your help.
>
> How can I reuse the same expression ? In my case, I will need to reuse the
> column alias in more than 1 places in the same sql statement.
> For ex:
> SELECT right('0000'+ convert(varchar(4),COL1),4) AS BUY,
> ABC = case when BUY>0 then
> (
> Left(BUY,charindex('.',BUY)-1)*32
> +
> substring(BUY,charindex('.',BUY)+1,2)*2
> +
> case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
> )
> else Null
> end
>
> "Anith Sen" <anith@.bizdatasolutions.com> wrote in message
> news:OamRSFf%23FHA.328@.TK2MSFTNGP14.phx.gbl...
>|||Please post a simplified version of your table schema with a few sample data
& expected results. Include a brief explanation of what you are trying to
accomplish here.
Based on the code snippet you posted, it looks mostly like some formatting
issue that, in general, can be handled differently, perhaps even outside the
database server.
Anith|||I am actually trying to convert Access db to SQL Server, and the query I
posted was a part of a longer query that I need to convert.
CREATE TABLE tblA (
Price float NULL,
CC varchar(50) NULL,
Account varchar(50) NULL,
Quantity smallint NULL,
:
)
CREATE TABLE tblB (
CC varchar(50) NULL,
:
)
CREATE TABLE tblC (
Account varchar(50) NULL,
:
)
SELECT LEFT(convert(varchar(8),Price),8)+'0000'
,8) AS BUY,
HClose = case when BUY>0 then
(
Left(BUY,charindex('.',BUY)-1)*32
+
substring(BUY,charindex('.',BUY)+1,2)*2
+
case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
)
else Null
end
from (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
tblA.Account = tblC.Account
WHERE (((tblA.Quantity)<>'0'))
Example:
tblA
Price CC Account
6 1212.25 ES 12345
6 0 ES 12345
1 1585.5 NQ 12345
1 51 NQ 12345
tblB
Symbol
ES
NQ
tblC
Account
12345
Result:
Buy HClose Account
ES 1212.2500 77618 12345
ES 0.0000 12345
NQ 1585.5000 101540 12345
NQ 51.0000 3352 12345
I am also having a problem converting a floating number to a 4 decimal
floating number: ex: from 1212.25 to 1212.2500, 0 to 0.0000, etc.
In my Stored Procedure, can I just derived the column Buy in a temp table,
and refer it in the next sql statement ?
Thanks.
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:uSBRY5n%23FHA.3104@.TK2MSFTNGP15.phx.gbl...
> Please post a simplified version of your table schema with a few sample
> data & expected results. Include a brief explanation of what you are
> trying to accomplish here.
> Based on the code snippet you posted, it looks mostly like some formatting
> issue that, in general, can be handled differently, perhaps even outside
> the database server.
> --
> Anith
>|||Just to add, the derived column HClose is also going to be used again in the
same query.
This is from the Access query:
IIf([buy]=0,[Hprice]-[HCLOSE],[HCLOSE]-[Hprice]) AS Hdif
"fniles" <fniles@.pfmail.com> wrote in message
news:etFhZ7o%23FHA.1676@.TK2MSFTNGP09.phx.gbl...
>I am actually trying to convert Access db to SQL Server, and the query I
>posted was a part of a longer query that I need to convert.
> CREATE TABLE tblA (
> Price float NULL,
> CC varchar(50) NULL,
> Account varchar(50) NULL,
> Quantity smallint NULL,
> :
> )
> CREATE TABLE tblB (
> CC varchar(50) NULL,
> :
> )
> CREATE TABLE tblC (
> Account varchar(50) NULL,
> :
> )
> SELECT LEFT(convert(varchar(8),Price),8)+'0000'
,8) AS BUY,
> HClose = case when BUY>0 then
> (
> Left(BUY,charindex('.',BUY)-1)*32
> +
> substring(BUY,charindex('.',BUY)+1,2)*2
> +
> case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
> )
> else Null
> end
> from (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
> tblA.Account = tblC.Account
> WHERE (((tblA.Quantity)<>'0'))
> Example:
> tblA
> Price CC Account
> 6 1212.25 ES 12345
> 6 0 ES 12345
> 1 1585.5 NQ 12345
> 1 51 NQ 12345
> tblB
> Symbol
> ES
> NQ
>
> tblC
> Account
> 12345
>
> Result:
> Buy HClose Account
> ES 1212.2500 77618 12345
> ES 0.0000 12345
> NQ 1585.5000 101540 12345
> NQ 51.0000 3352 12345
>
> I am also having a problem converting a floating number to a 4 decimal
> floating number: ex: from 1212.25 to 1212.2500, 0 to 0.0000, etc.
> In my Stored Procedure, can I just derived the column Buy in a temp table,
> and refer it in the next sql statement ?
> Thanks.
>
> "Anith Sen" <anith@.bizdatasolutions.com> wrote in message
> news:uSBRY5n%23FHA.3104@.TK2MSFTNGP15.phx.gbl...
>|||This is a part of the original Access query that I need to convert to SQL
Server:
SELECT Format([tblA].[PRICE],"0.0000") AS BUY,
IIf(Val([BUY])>0,(Left$([BUY],InStr([BUY],".")-1)*32+Mid$([BUY],InStr([BUY],".")+1,2))*2+(IIf(Mid$([BUY],InStr(
[BUY],".")+3,1)=5,1,0)),Null)
AS HClose,
IIf([buy]=0,1-[HCLOSE],[HCLOSE]-1) AS Hdif,...
FROM (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
tblA.Account = tblC.Account
WHERE (((tblA.Quantity)<>"0"));
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:uSBRY5n%23FHA.3104@.TK2MSFTNGP15.phx.gbl...
> Please post a simplified version of your table schema with a few sample
> data & expected results. Include a brief explanation of what you are
> trying to accomplish here.
> Based on the code snippet you posted, it looks mostly like some formatting
> issue that, in general, can be handled differently, perhaps even outside
> the database server.
> --
> Anith
>sql

Sunday, February 19, 2012

How to query by grouping one column

Hello, everyone:
I have a table like:
Col1 Col2
1 A
2 B
1 D
1 P
2 F
2 W
How to query this table to return by Col1 like
Col1 Col2
1 A,D,P
2 B,F,W
Thanks a lot
ZYTUSE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99(Col1 int, Col2 char(1))
GO

INSERT INTO myTable99(Col1, Col2)
SELECT 1, 'A' UNION ALL
SELECT 2, 'B' UNION ALL
SELECT 1, 'D' UNION ALL
SELECT 1, 'P' UNION ALL
SELECT 2, 'F' UNION ALL
SELECT 2, 'W' UNION ALL
SELECT 3, 'X' UNION ALL
SELECT 3, 'Y' UNION ALL
SELECT 4, 'Z' UNION ALL
SELECT 5, 'O'
GO

DECLARE @.MAX_Col1 int, @.Col1 int, @.strCol2 varchar(8000)
DECLARE @.rs table(Col1 int, strCol2 varchar(8000))

SELECT @.MAX_Col1 = MAX(Col1), @.Col1 = MIN(Col1) FROM myTable99

WHILE @.MAX_Col1 > = @.Col1
BEGIN
SELECT @.strCol2 = COALESCE(@.strCol2 + ', ','') + Col2 FROM myTable99 WHERE Col1 = @.Col1
INSERT INTO @.rs(Col1, strCol2) SELECT @.Col1, @.strCol2
SELECT @.Col1 = MIN(Col1), @.strCol2 = null FROM myTable99 WHERE Col1 > @.Col1
END

SELECT * FROM @.rs
GO

SET NOCOUNT OFF
DROP TABLE myTable99
GO|||Your code is always so thorough. I bet your desk is spotless. ;)|||Naw, not spotless, nut definetly not a pig sty

I need to stay organized though...I hate to have to remember things...

Except where the tequila is...|||When you forget that, you probably shouldn't be having any more anyway.|||See!

No Need to remember|||To Brett Kaiser
Why so difficult?
You can create function:
CREATE FUNCTION GetAllOnLine(@.id int)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @.Result VARCHAR(8000)
SET @.Result = ''

SELECT @.Result = @.Result + col2 + ' ,'
FROM YourTable
WHERE col1=@.id

RETURN @.Result
END

And use it:

SELECT Col1, GetAllOnLine(Col1)
FROM YourTable|||OK...needs a little cleaning up...

CREATE FUNCTION GetAllOnLine(@.id int)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @.Result VARCHAR(8000)

SELECT @.Result = COALESCE(@.Result + ', ','') + col2
FROM myTable99
WHERE col1=@.id

RETURN @.Result
END
GO

SELECT Col1, dbo.GetAllOnLine(Col1) FROM MyTable99

But don't you double the access paths to the table? One for the UDF and another for the SELECT? If it's a lot of rows I think mine would be more effecient...(though I still would like to know why anyone would like to do this)

Also, I don't usually like to make my UDF's some very specific, I prefer them to be as generic as possible. You could only use this for one very specific case.

Do you mind if I blog your solution as another example?|||This is execution plan of my result query:
|--Compute Scalar(DEFINE:([Expr1002]=[dbo].[MyGetAllOnLine2]([Mail].[CC])))
|--Hash Match(Aggregate, HASH:([Mail].[CC]), RESIDUAL:([Mail].[CC]=[Mail].[CC]))
|--Clustered Index Scan(OBJECT:([MWolf_Mail].[dbo].[Mail].[PK_Mail]))
It is better plan that I saw. Unfortunately, specific UDF is pay for good performance.|||Oh. Forum engine replace my plan on smile!!!
:-()|||With code tags perhaps...very nice plan..do you mind if I blog this with the appropriate credit?

This is execution plan of my result query:
|--Compute Scalar(DEFINE:([Expr1002]=[dbo].[MyGetAllOnLine2]([Mail].[CC])))
|--Hash Match(Aggregate, HASH:([Mail].[CC]), RESIDUAL:([Mail].[CC]=[Mail].[CC]))
|--Clustered Index Scan(OBJECT:([MWolf_Mail].[dbo].[Mail].[PK_Mail]))
It is better plan that I saw. Unfortunately, specific UDF is pay for good performance.|||If i understood you correctly, you want to post my example somewhere for others to see it and learn (please apologize my bad English)
If i'm right i have no objections else please correct me