Showing posts with label rows. Show all posts
Showing posts with label rows. Show all posts

Wednesday, March 28, 2012

Multi-table UDF not returning all rows

I've been tearing my hair out over this UDF. The code works within a
stored procedure and also run ad-hoc against the database, but does not
run properly within my UDF. We've been using the SP, but I do need a
UDF instead now.

All users, including branch office, sub-companies and companies and so
on up the lines are in the same table. I need a function which returns
a row for each level, eventually getting to the master company all the
way at the top, but this UDF acts as though it can't enter the loop and
only inserts the @.userID and @.branchID rows. I have played with the
WHILE condition to no avail.

Any ideas on what I am missing?

(Running against SQL Server 2000)
----------------

ALTER FUNCTION udfUplineGetCompany (@.userID int)

RETURNS @.upline table (companyID int, companyname varchar(100), info
varchar(100))
AS
BEGIN

DECLARE @.branchID int
DECLARE @.companyID int
DECLARE @.tempID int

--Insert the original user data
INSERT INTO @.upline
SELECT tblusersid, companyname, 'userID'
FROM tblusers
WHERE tblusersid = @.userid

SELECT @.branchID = tblUsers.tblUsersID
FROM tblUsers
INNER JOIN tblUsersUsersLnk
ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
WHERE tblUsersUsersLnk.tblUsersID_Child = @.userid

--Up one level
INSERT INTO @.upline
SELECT tblusersid, companyname, 'branchID'
FROM tblusers
WHERE tblusersid = @.branchid

SET @.tempID = @.branchID

WHILE @.@.ROWCOUNT <> 0
BEGIN
SELECT @.companyID = tblUsers.tblUsersID
FROM tblUsers
INNER JOIN tblUsersUsersLnk
ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
WHERE tblUsersUsersLnk.tblUsersID_Child = @.tempID
AND tblUsersId <> 6

--Insert a row for each level up
INSERT INTO @.upline
SELECT tblusersid, companyname, 'companyID'
FROM tblusers
WHERE tblusersid = @.companyID

SET @.tempID = @.companyID

END

RETURN

ENDWell, if I try to run in as an ad-hoc query, it doesn't run properly at
all.

The use of @.@.rowcount is bound to go wrong. Also, the INSERT into the
table variable will be executed regardless of a match (or miss) of the
parent selection.

You should check the @.@.rowcount immediately after the "SELECT @.CompanyID
= ..." statement. If you do not want to handle the @.@.rowcount result at
that point, then make sure you move the @.@.rowcount status to a variable
immediately after the SELECT statement.

Gert-Jan

P.S. If you have proper primary key/foreign keys in place, then joining
to the tblUsers table in the SELECT statement is unnecessary.

nicolec@.octitle.com wrote:
> I've been tearing my hair out over this UDF. The code works within a
> stored procedure and also run ad-hoc against the database, but does not
> run properly within my UDF. We've been using the SP, but I do need a
> UDF instead now.
> All users, including branch office, sub-companies and companies and so
> on up the lines are in the same table. I need a function which returns
> a row for each level, eventually getting to the master company all the
> way at the top, but this UDF acts as though it can't enter the loop and
> only inserts the @.userID and @.branchID rows. I have played with the
> WHILE condition to no avail.
> Any ideas on what I am missing?
> (Running against SQL Server 2000)
> ----------------
> ALTER FUNCTION udfUplineGetCompany (@.userID int)
> RETURNS @.upline table (companyID int, companyname varchar(100), info
> varchar(100))
> AS
> BEGIN
> DECLARE @.branchID int
> DECLARE @.companyID int
> DECLARE @.tempID int
> --Insert the original user data
> INSERT INTO @.upline
> SELECT tblusersid, companyname, 'userID'
> FROM tblusers
> WHERE tblusersid = @.userid
> SELECT @.branchID = tblUsers.tblUsersID
> FROM tblUsers
> INNER JOIN tblUsersUsersLnk
> ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
> WHERE tblUsersUsersLnk.tblUsersID_Child = @.userid
> --Up one level
> INSERT INTO @.upline
> SELECT tblusersid, companyname, 'branchID'
> FROM tblusers
> WHERE tblusersid = @.branchid
> SET @.tempID = @.branchID
> WHILE @.@.ROWCOUNT <> 0
> BEGIN
> SELECT @.companyID = tblUsers.tblUsersID
> FROM tblUsers
> INNER JOIN tblUsersUsersLnk
> ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
> WHERE tblUsersUsersLnk.tblUsersID_Child = @.tempID
> AND tblUsersId <> 6
> --Insert a row for each level up
> INSERT INTO @.upline
> SELECT tblusersid, companyname, 'companyID'
> FROM tblusers
> WHERE tblusersid = @.companyID
> SET @.tempID = @.companyID
> END
> RETURN
> END|||Sometimes you look at something SO long you don't see the obvious...
thanks for the pointer, that worked perfect. For future newsgroup
reference, the new function text is at the bottom.

>P.S. If you have proper primary key/foreign keys in place, then joining
to the tblUsers table in the SELECT statement is unnecessary.

No keys anywhere in this database and it's driving me nuts. Every join
must be explicit.

----------------

ALTER FUNCTION udfUplineGetCompany (@.userID int)

RETURNS @.upline table (companyID int, companyname varchar(100), info
varchar(100), rows int)
AS
BEGIN

DECLARE @.branchID int
DECLARE @.companyID int
DECLARE @.tempID int
DECLARE @.rows int

SET @.rows = 1

INSERT INTO @.upline
SELECT tblusersid, companyname, 'userID', @.rows
FROM tblusers
WHERE tblusersid = @.userid

SELECT @.branchID = tblUsers.tblUsersID
FROM tblUsers
INNER JOIN tblUsersUsersLnk
ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
WHERE tblUsersUsersLnk.tblUsersID_Child = @.userid

INSERT INTO @.upline
SELECT tblusersid, companyname, 'branchID', @.rows
FROM tblusers
WHERE tblusersid = @.branchid

SET @.tempID = @.branchID

WHILE @.rows = 1
BEGIN
SELECT @.companyID = tblUsers.tblUsersID
FROM tblUsers
INNER JOIN tblUsersUsersLnk
ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
WHERE tblUsersUsersLnk.tblUsersID_Child = @.tempID
AND tblUsersId <> 6

SET @.rows = @.@.rowcount

INSERT INTO @.upline
SELECT tblusersid, companyname, 'companyID', @.rows
FROM tblusers
WHERE tblusersid = @.companyID
AND @.rows = 1

SET @.tempID = @.companyID

END

RETURN

END

Multi-table joins & tempdb growth / query performance

Hi
When I run a LARGE query (20 tables, some have 4 million rows), it
takes hours to run and fills up 35GB on tempdb.
Could someone tell me, when joining these tables, does SQL server join
the entire table into tempdb or only the columns that are needed, i.e.
:
* returned in output
* used as part of the join
* used in a later join
I'm trying to reduce the time AND the impact on tempdb space, and I
think I can do this by doing the following:
Say (simple example) TableA joins to TableB which joins to TableC
I want to return the fields: A.1, B.1, C.1
I have to join A to B on A.2 = B.2
I have to join B to C on B.3 = C.2
Table B has 10 columns (B.4, B.5, B.6, B.7, etc...)
Does SQL merge (join) into tempdb the following after the first join:
A.1, B.1, B.3 ' Or does it merge & store the entire table?
If it does the entire table, I think I can speed things up & save time
by doing:
SELECT B.1, B.2, B.3
INTO TableB_temp
FROM TableB
Then join the query on field 2 & 3, but now tempdb can ONLY put a max
of 3 fields from TableB_Temp into storage during query processing...
saving space & hopefully (read/write) time'
Or does SQL do this automatically?
Thanks
Sean"Sean" <plugwalsh@.yahoo.com> wrote in message
news:3698af3c.0405250724.523e4c7a@.posting.google.com...
> Hi
> When I run a LARGE query (20 tables, some have 4 million rows), it
> takes hours to run and fills up 35GB on tempdb.
> Could someone tell me, when joining these tables, does SQL server join
> the entire table into tempdb or only the columns that are needed, i.e.
> :
> * returned in output
> * used as part of the join
> * used in a later join
> I'm trying to reduce the time AND the impact on tempdb space, and I
> think I can do this by doing the following:
> Say (simple example) TableA joins to TableB which joins to TableC
> I want to return the fields: A.1, B.1, C.1
> I have to join A to B on A.2 = B.2
> I have to join B to C on B.3 = C.2
> Table B has 10 columns (B.4, B.5, B.6, B.7, etc...)
> Does SQL merge (join) into tempdb the following after the first join:
> A.1, B.1, B.3 ' Or does it merge & store the entire table?
> If it does the entire table, I think I can speed things up & save time
> by doing:
> SELECT B.1, B.2, B.3
> INTO TableB_temp
> FROM TableB
> Then join the query on field 2 & 3, but now tempdb can ONLY put a max
> of 3 fields from TableB_Temp into storage during query processing...
> saving space & hopefully (read/write) time'
> Or does SQL do this automatically?
A merge join requires that the columns that it is joining them on are both
sorted. If it is using the tempdb then it is having to sort them into order
first, then merging them. It would speed things up if you indexed the
columns (the Primary Key will already be indexed, but not necessarily the
Foreign Key). As an index is already sorted, then the mergedb should not be
required, and the whole thing should run much faster
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004sql

Multi-table joins & tempdb growth / query performance

Hi
When I run a LARGE query (20 tables, some have 4 million rows), it
takes hours to run and fills up 35GB on tempdb.
Could someone tell me, when joining these tables, does SQL server join
the entire table into tempdb or only the columns that are needed, i.e.
:
* returned in output
* used as part of the join
* used in a later join
I'm trying to reduce the time AND the impact on tempdb space, and I
think I can do this by doing the following:
Say (simple example) TableA joins to TableB which joins to TableC
I want to return the fields: A.1, B.1, C.1
I have to join A to B on A.2 = B.2
I have to join B to C on B.3 = C.2
Table B has 10 columns (B.4, B.5, B.6, B.7, etc...)
Does SQL merge (join) into tempdb the following after the first join:
A.1, B.1, B.3 ? Or does it merge & store the entire table?
If it does the entire table, I think I can speed things up & save time
by doing:
SELECT B.1, B.2, B.3
INTO TableB_temp
FROM TableB
Then join the query on field 2 & 3, but now tempdb can ONLY put a max
of 3 fields from TableB_Temp into storage during query processing...
saving space & hopefully (read/write) time?
Or does SQL do this automatically?
Thanks
Sean
"Sean" <plugwalsh@.yahoo.com> wrote in message
news:3698af3c.0405250724.523e4c7a@.posting.google.c om...
> Hi
> When I run a LARGE query (20 tables, some have 4 million rows), it
> takes hours to run and fills up 35GB on tempdb.
> Could someone tell me, when joining these tables, does SQL server join
> the entire table into tempdb or only the columns that are needed, i.e.
> :
> * returned in output
> * used as part of the join
> * used in a later join
> I'm trying to reduce the time AND the impact on tempdb space, and I
> think I can do this by doing the following:
> Say (simple example) TableA joins to TableB which joins to TableC
> I want to return the fields: A.1, B.1, C.1
> I have to join A to B on A.2 = B.2
> I have to join B to C on B.3 = C.2
> Table B has 10 columns (B.4, B.5, B.6, B.7, etc...)
> Does SQL merge (join) into tempdb the following after the first join:
> A.1, B.1, B.3 ? Or does it merge & store the entire table?
> If it does the entire table, I think I can speed things up & save time
> by doing:
> SELECT B.1, B.2, B.3
> INTO TableB_temp
> FROM TableB
> Then join the query on field 2 & 3, but now tempdb can ONLY put a max
> of 3 fields from TableB_Temp into storage during query processing...
> saving space & hopefully (read/write) time?
> Or does SQL do this automatically?
A merge join requires that the columns that it is joining them on are both
sorted. If it is using the tempdb then it is having to sort them into order
first, then merging them. It would speed things up if you indexed the
columns (the Primary Key will already be indexed, but not necessarily the
Foreign Key). As an index is already sorted, then the mergedb should not be
required, and the whole thing should run much faster
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004

Multi-table joins & tempdb growth / query performance

Hi
When I run a LARGE query (20 tables, some have 4 million rows), it
takes hours to run and fills up 35GB on tempdb.
Could someone tell me, when joining these tables, does SQL server join
the entire table into tempdb or only the columns that are needed, i.e.
:
* returned in output
* used as part of the join
* used in a later join
I'm trying to reduce the time AND the impact on tempdb space, and I
think I can do this by doing the following:
Say (simple example) TableA joins to TableB which joins to TableC
I want to return the fields: A.1, B.1, C.1
I have to join A to B on A.2 = B.2
I have to join B to C on B.3 = C.2
Table B has 10 columns (B.4, B.5, B.6, B.7, etc...)
Does SQL merge (join) into tempdb the following after the first join:
A.1, B.1, B.3 ' Or does it merge & store the entire table?
If it does the entire table, I think I can speed things up & save time
by doing:
SELECT B.1, B.2, B.3
INTO TableB_temp
FROM TableB
Then join the query on field 2 & 3, but now tempdb can ONLY put a max
of 3 fields from TableB_Temp into storage during query processing...
saving space & hopefully (read/write) time'
Or does SQL do this automatically?
Thanks
Sean"Sean" <plugwalsh@.yahoo.com> wrote in message
news:3698af3c.0405250724.523e4c7a@.posting.google.com...
> Hi
> When I run a LARGE query (20 tables, some have 4 million rows), it
> takes hours to run and fills up 35GB on tempdb.
> Could someone tell me, when joining these tables, does SQL server join
> the entire table into tempdb or only the columns that are needed, i.e.
> :
> * returned in output
> * used as part of the join
> * used in a later join
> I'm trying to reduce the time AND the impact on tempdb space, and I
> think I can do this by doing the following:
> Say (simple example) TableA joins to TableB which joins to TableC
> I want to return the fields: A.1, B.1, C.1
> I have to join A to B on A.2 = B.2
> I have to join B to C on B.3 = C.2
> Table B has 10 columns (B.4, B.5, B.6, B.7, etc...)
> Does SQL merge (join) into tempdb the following after the first join:
> A.1, B.1, B.3 ' Or does it merge & store the entire table?
> If it does the entire table, I think I can speed things up & save time
> by doing:
> SELECT B.1, B.2, B.3
> INTO TableB_temp
> FROM TableB
> Then join the query on field 2 & 3, but now tempdb can ONLY put a max
> of 3 fields from TableB_Temp into storage during query processing...
> saving space & hopefully (read/write) time'
> Or does SQL do this automatically?
A merge join requires that the columns that it is joining them on are both
sorted. If it is using the tempdb then it is having to sort them into order
first, then merging them. It would speed things up if you indexed the
columns (the Primary Key will already be indexed, but not necessarily the
Foreign Key). As an index is already sorted, then the mergedb should not be
required, and the whole thing should run much faster
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004

Monday, March 26, 2012

multiselect result changes while add additional dimension to filter?

Every Query I introduce with multiselect to restrict the my rows works only as long as I do not add a second dimension to the filter fields ?

My Query (derived from Management Studio) - if you run it once like this and once with the comment line enabled the count of columns returned change - what I really did not understand, because only the values should change (different slice)?

Could anyone explain this to me? or point me to a location with detailed explanation? (I have checked http://www.mosha.com/msolap/articles/mdxmultiselectcalcs.htm this but it does not hit specially my question)

WITH
MEMBER [Sales Territory].[Sales Territory Group].[{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Hier164MultiFilter__] AS 'AGGREGATE({ [Sales Territory].[Sales Territory Group].&[North America], [Sales Territory].[Sales Territory Group].&[Pacific] })'
SET [{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Axis1Set0] AS '{ { [Sales Territory].[Sales Territory].[All Sales Territories] }, AddCalculatedMembers([Sales Territory].[Sales Territory].[Group].MEMBERS)}'
SELECT
[{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Axis1Set0] DIMENSION PROPERTIES MEMBER_NAME, PARENT_UNIQUE_NAME , [Sales Territory].[Sales Territory].[Country].[Sales Territory Group] ON COLUMNS
FROM [Adventure Works]
WHERE
(
[{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Hier164MultiFilter__]
// , [Sales Channel].[Sales Channel].&[Internet]
)
CELL PROPERTIES VALUE, FORMATTED_VALUE, FORE_COLOR, BACK_COLOR

Noone with an explanation or hint to further documentation ?

Who is it possible that an aditional slice of an independ dimension changes the rows / columns if not filter or empty is used ?

HANNES

multiselect result changes while add additional dimension to filter?

Every Query I introduce with multiselect to restrict the my rows works only as long as I do not add a second dimension to the filter fields ?

My Query (derived from Management Studio) - if you run it once like this and once with the comment line enabled the count of columns returned change - what I really did not understand, because only the values should change (different slice)?

Could anyone explain this to me? or point me to a location with detailed explanation? (I have checked http://www.mosha.com/msolap/articles/mdxmultiselectcalcs.htm this but it does not hit specially my question)

WITH
MEMBER [Sales Territory].[Sales Territory Group].[{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Hier164MultiFilter__] AS 'AGGREGATE({ [Sales Territory].[Sales Territory Group].&[North America], [Sales Territory].[Sales Territory Group].&[Pacific] })'
SET [{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Axis1Set0] AS '{ { [Sales Territory].[Sales Territory].[All Sales Territories] }, AddCalculatedMembers([Sales Territory].[Sales Territory].[Group].MEMBERS)}'
SELECT
[{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Axis1Set0] DIMENSION PROPERTIES MEMBER_NAME, PARENT_UNIQUE_NAME , [Sales Territory].[Sales Territory].[Country].[Sales Territory Group] ON COLUMNS
FROM [Adventure Works]
WHERE
(
[{2120AF0D-0FD5-4B1F-8562-51CCF95CE627}Pivot8Hier164MultiFilter__]
// , [Sales Channel].[Sales Channel].&[Internet]
)
CELL PROPERTIES VALUE, FORMATTED_VALUE, FORE_COLOR, BACK_COLOR

Noone with an explanation or hint to further documentation ?

Who is it possible that an aditional slice of an independ dimension changes the rows / columns if not filter or empty is used ?

HANNES

Multirow insert statement,... how?

In MySQL I was able to insert multiple rows into the db like this:

REPLACE INTO Products (productid, price) VALUES (1, 55), (2, 88), (3, 99);

In transact-Sql (Sql Server 2000), this "REPLACE" keyword means something different. What it meant in MySQL is that if there is an existing row that has the same primary key value as one of the new rows being inserted, it will replace that old row with the new row.

How do I do this in SQL Server 2000?

Also, I can do this in MySQL:

INSERT INGORE INTO Products (productid, price) VALUES (1, 55), (2, 88), (3, 99);

This meant in MySQL is that if there is an existing row that has the same primary key value as one of the new rows being inserted, it will ignore that new row's insert and keep the values of the old row.

How do I do that also in SQL Server 2000?

ThanksYou can't easily. MySQL is CISC to MS SQLs RISC.
There is no equiv' REPLACE and no IGNORE. You have do everything the hard way.

You can use insert with a not exists (yuk) or populate a temp table var with the rows you want and load the same keyed values from the database. That way you can see the gaps (inserts) and the ones with data (updates). Still not gr8 but what you gonna do!|||Darn,... I have experience developing aps with both MySQL and SQL server and I think this is the first time ever that it's harder to do something with SQL SERVER. Usually, it's MySql that makes things harder.

No wonder why I couldn't find any documentation for the life of me.
Oh well!|||You could use, and I'm trying not to be sick when saying it, a datagram. I think that's got some of the features you want <shudder>. Might be worth a look though.|||How do I use a datagram? Can you give me some examples?|||try
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqlxml3/htm/dotnet_8704.asp|||You can insert multiple rows into a table by doing the following (works for SQL 7 & 2000, not sure about MySQL):

insert into yourtable(field1, field2, field3)
select
customerid,
firstname,
lastname
from
customers
where
customerid between 1 and 10

This would insert several rows from the customers table into yourtable.|||youre kiddin me right tingent? That is absolutely off the subject of what we are talking about|||"What it meant in MySQL is that if there is an existing row that has the same primary key value as one of the new rows being inserted, it will replace that old row with the new row."

Surely, though, by definition you will only ever have ONE row that matches the primary key so isn't this in effect an UPDATE statement? Or does the replace statement insert a new row if a match isn't found (you didn't mention it did!) ?|||Ouch...ok, I admit I didn't completly read the post so I didn't know what REPLACE was doing in MySQL. My bad...|||But the SQL i wrote does exactly what the the thread title says, Multi-row insert statement.|||>> But the SQL i wrote does exactly what the the thread title says, Multi-row insert statement

True but you do need to read the question. I think you caused a tiny little bit of offense 'cause judging by the question I think they know how do a standard insert. Don't worry though, no ones perfect - and don't let some of the posters tell you otherwise ;)|||I apologize, I didn't mean it at all the way it sounded. Sometimes things don't come off the right way when you type them over the internet. If we were in person and I would've said that to you, you would've took it the right way because I would've said it in a joking way.

Sorry|||Tingent, again, I apologize for the way that sounded.

The Mult-Row insert statement that you have extracts rows from another area and inserts them into the table. I need to insert brand new rows.|||hat's off to you javan15, what I jolly good egg you are.

Friday, March 23, 2012

Multiply Rows with T - SQL

I need to multiply rows of a table with each other. I found a function like this

exp(sum(ln(floatfield1))).

But I can not find the ln function. Is there any other possibility ?

Thanks for any help.

Uwe

in function ? is this for MS SQL Server ?

Post your table structure, some sample data and the expected result. We should be able to help you to come up with the query|||

Uwe, LOG is the natural logarithm function in SQL Server. Also, see some of the previous discussions of this question: http://groups.google.com/groups/search?q=aggregate+product+kass+sqlserver Steve Kass Drew University Uwe Helmer@.discussions.microsoft.com wrote:
> I need to multiply rows of a table with each other. I found a function
> like this
>
> exp(sum(ln(floatfield1))).
>
> But I can not find the ln function. Is there any other possibility ?
>
>
>
> Thanks for any help.
>
> Uwe
>
>

|||

In your SQL statment

exp(sum(LOG(floatfield1))).

In SQL

Constant e (2.71828182845905…) based Natural log is set as LOG and 10 based is written as LOG10.

Some examples:

LOG ( e ) = 1.0

EXP( LOG( n ) ) = n.

10 ^ LOG10(n) = n

This seelect statement

SELECT LOG (EXP (5))

returns 5.

Multiple Zip Search

I'm heaving a heck of a time to get a sql query working. I'm being passed a series of zip codes and I need to return a list of all the rows that match those zip codes in a database of about 40,000 leads.

Can someone clue me in to what the commandtext of my query should look like?

Thanks!

EXEC( 'SELECT * FROM YourTable where YourZip IN (''' + @.z + ''')' )

where @.z = list of zip codes.

hth|||Well if you are inclined to avoid D-SQL and are using SQL Server 2000, you can use a UDF
or many other techniques which would prove much better than D-SQL.|||My problem is that I'm a web guy with just enough SQL 2000 experience to be dangerous. I'm not exactly sure that the first answer given would help me because the zip codes are different for every query. I have a list of zip codes and I have a parameter set up in my dataadapter (@.zip), but I can't get any results. It's probably an issue with single or double quotes, but i'm not sure. I'm putting all the zip codes together into a session variable, trying to get something like this:

WHERE ZIP LIKE '%80000%' OR ZIP LIKE '%72322%' etc. There can be up to 75 zips in one query.

I really appreciate the help!|||A user defined function to create an query with IN ('zip,zip,etc') is your best bet.

Here is someting I wrote a long time ago but it was similar to what you could do.


IF EXISTS (SELECT 1 FROM sysobjects WHERE name = N'fnConvertListToStringTable')
DROP FUNCTION [dbo].[fnConvertListToStringTable]
GO

CREATE FUNCTION [dbo].[fnConvertListToStringTable] (
@.list varchar(1000)
)

RETURNS @.ListTable table (ItemID varchar(250) NULL)

AS

BEGIN

IF(SUBSTRING(@.list,LEN(@.list),1) <> ',')
BEGIN
SET @.list = @.list + ','
END

DECLARE @.StartLocation int
DECLARE@.Length int
SET @.StartLocation = 0
SET @.Length = 0

SELECT @.StartLocation = CHARINDEX(',',@.list,@.StartLocation+1)

WHILE @.StartLocation > 0
BEGIN

IF(LOWER(RTRIM(LTRIM(SUBSTRING(@.list,@.Length+1,(@.StartLocation-@.length) -1))))) = 'null'
INSERT INTO @.ListTable(ItemID) VALUES (Null)
ELSE
BEGIN
INSERT INTO @.ListTable(ItemID)
VALUES (RTRIM(LTRIM(SUBSTRING(@.list,@.Length+1,(@.StartLocation-@.length) -1))))
END
SET @.Length = @.StartLocation
SET @.StartLocation = CHARINDEX(',', @.list, @.StartLocation +1)
END

RETURN
END
GO

select * from dbo.fnConvertListToStringTable(' yjyj, 1345, 1234,NUll')

The code above actually put the values in a table and then used a where in (Select from that table) but in your case you can juse use the part that does the parsing at the beginning.sql

Wednesday, March 21, 2012

Multiple values int textbox...

Hi,
I need to display the result set in a textbox,
if there are multiple rows i need to show them as comma separated..
Can anyone give me idea of how it can be achieved..
Thanks in advance,I can't think of an easy way.
You need to get the resultset concatanating, not do it in report layout.
You could use a cursor (effectively a loop) in the SQL query - get the
result set and concatanate the rows into a variable and output the
variable as the result set. Cursors are quite slow though.
You could do the same thing in VB code using ADO if that's your thing.
Chris
CCP wrote:
> Hi,
> I need to display the result set in a textbox,
> if there are multiple rows i need to show them as comma separated..
> Can anyone give me idea of how it can be achieved..
> Thanks in advance,sql

Monday, February 20, 2012

multiple rows to one row and multiple columns

Hi,

I want to convert multiple rows to one row and multiple columns. I saw some examples with PIVOT but i could not get them to work.

Heres what i want to do:

This is the how the table is:

EmpID Designation

678 CFA 679 CFA 680 CFA 685 CFP 685 CIMA 685 IMCA

I want it to display as:

EmpID Designation1 Designation2 Designation3

678 CFA

679 CFA

680 CFA

685 CFP CIMA IMCA

could anyone provide some help on this?

Thanks

Below is one way to do it:

Code Snippet

select pt.EmpID, pt.[1] as Designation1, pt.[2] as Designation2

, pt.[3] as Designation3, pt.[4] as Designation4

from (

select t.EmpID, t.Designation

, ROW_NUMBER() OVER(PARTITION BY t.EmpID ORDER BY t.Designation) as rn

from your_table as t

) as t1

pivot (max(t1.Designation) for t1.rn in ([1], [2], [3], [4])

/* add max number of designations per emp*/

) as pt

Multiple Rows to One Column Results

I want to display the top 5 count of areacodes for each name
I

want to combine all the results for the areacodes into one column for

each user. like a csv. I tried my code below but the results just

return the same 5 of areacodes for all names on each area code row with

each callername. like
joe blow 123,456,755,312,465,567,555
bill jones 123,456,755,312,465,567,555

I just want the top 5 for each particular name.
I tried reading a few articles on putting multiple colums in one row and i could not figure out what i am missing!
Thanks

DECLARE @.Stage TABLE (RowID INT IDENTITY(1, 1), CallerName VARCHAR(256), AreaCode VARCHAR(3), Calls INT, theDate DATETIME,myareacodes int)
declare @.MyAreaCodes varchar(50)
INSERT @.Stage
(
CallerName,
AreaCode,
Calls,
theDate
--myAreacodes

) --This is where the area code data comes from I can get it to display in multiple colums, but -- I want the area codes to be on one line for each name
SELECT CallerName,
SUBSTRING(TargetNum, 2, 3) AS AreaCode,
COUNT(*) AS AreaCodeCount,
DATEADD(day, DATEDIFF(day, 0, GtCalcDate), 0) as myDate
FROM CDRMAIN
WHERE LEN(TargetNum) >= 11
AND TargetNum NOT LIKE '_800%'
AND GtCalcDate >= '2006-11-06'
AND GtCalcDate < '2006-11-07'
GROUP BY CallerName,
SUBSTRING(TargetNum, 2, 3),
DATEADD(day, DATEDIFF(day, 0, GtCalcDate), 0)
ORDER BY CallerName,
COUNT(*) DESC

-- Get Calls
SELECT s.CallerName,
s.AreaCode,
s.Calls,
s.theDate--,myareacodes
FROM @.Stage s
INNER JOIN (
SELECT CallerName,
MIN(RowID) mirw,
4 + MIN(RowID) marw
FROM @.Stage
GROUP BY CallerName

HAVING (CallerName = 'name1') OR
(CallerName = 'name2')
) q ON q.CallerName = s.CallerName AND s.RowID BETWEEN q.mirw AND q.marw
ORDER BY callername,Calls desc
--
set @.MyAreaCodes =''
--
SELECT top 5 @.MyAreaCodes = @.MyAreaCodes + ISNULL(AreaCode,'') + ',' from @.Stage

--
SELECT CallerNAme,@.MyAreaCodes AS MyAreaCodes from @.stage
Group By CallerName

This should do it

DECLARE @.Stage TABLE (RowID INT IDENTITY(1, 1), CallerName VARCHAR(256), AreaCode VARCHAR(3), Calls INT, theDate DATETIME,myareacodes int)
declare @.MyAreaCodes varchar(50)
INSERT @.Stage
(
CallerName,
AreaCode,
Calls,
theDate
--myAreacodes
) --This is where the area code data comes from I can get it to display in multiple colums, but -- I want the area codes to be on one line for each name
SELECT CallerName,
SUBSTRING(TargetNum, 2, 3) AS AreaCode,
COUNT(*) AS AreaCodeCount,
DATEADD(day, DATEDIFF(day, 0, GtCalcDate), 0) as myDate
FROM CDRMAIN
WHERE LEN(TargetNum) >= 11
AND TargetNum NOT LIKE '_800%'
AND GtCalcDate >= '2006-11-06'
AND GtCalcDate < '2006-11-17'
GROUP BY CallerName,
SUBSTRING(TargetNum, 2, 3),
DATEADD(day, DATEDIFF(day, 0, GtCalcDate), 0)
ORDER BY CallerName,
COUNT(*) DESC

select CallerName, coalesce(AreaCode1 + ', ' + AreaCode2 + ', ' + AreaCode3 + ', ' + AreaCode4 + ', ' + AreaCode5, AreaCode1 + ', ' + AreaCode2 + ', ' + AreaCode3 + ', ' + AreaCode4, AreaCode1 + ', ' + AreaCode2 + ', ' + AreaCode3, AreaCode1 + ', ' + AreaCode2, AreaCode1)
from (select s0.CallerName, s1.AreaCode AreaCode1, s2.AreaCode AreaCode2, s3.AreaCode AreaCode3, s4.AreaCode AreaCode4, s5.AreaCode AreaCode5
from (select CallerName, min(RowID) as RowID from @.stage group by CallerName) s0
left outer join @.stage s1 on s1.CallerName = s0.CallerName AND s1.RowID = s0.RowID
left outer join @.stage s2 on s2.CallerName = s0.CallerName AND s2.RowID = s0.RowID + 1
left outer join @.stage s3 on s3.CallerName = s0.CallerName AND s3.RowID = s0.RowID + 2
left outer join @.stage s4 on s4.CallerName = s0.CallerName AND s4.RowID = s0.RowID + 3
left outer join @.stage s5 on s5.CallerName = s0.CallerName AND s5.RowID = s0.RowID + 4) Top5Calls

Multiple rows returned by for xml explicit

I have an xml query that runs across five tables. For some reasosn after I
get to the third table my xml returned is breaking into two rows in the
recordset that is returned. It just stops on the first record returned and
breaks mid element name to the second record. I have attached my query up
through table five. It breaks down on the last few closing elements and put
them on a second row. Not sure why this is happening and dealing with the
returned data is a pain. i have never seen for xml explicit returning the
xml as multiple rows before.
Rob
SELECT
1 AS Tag
,NULL AS Parent
,null as [catsubcatproduct!1!category]
,null as [category!2!catname!element]
,null as [category!2!catdesc!element]
,null as [category!2!subcategory!element]
,null as [subcategory!3!subcatname!element]
,null as [subcategory!3!subcatdesc!element]
,null as [subcategory!3!product!element]
,null as [product!4!productname!element]
,null as [product!4!productdesc!element]
,null as [product!4!company!element]
,null as [company!5!companyname!element]
,null as [company!5!companydesc!element]
union
SELECT
2 AS Tag
,1 AS Parent
,null as [catsubcatproduct!1!category]
,tcategory.catname as [category!2!catname!element]
,tcategory.catdesc as [category!2!catdesc!element]
,null as [category!2!subcategory!element]
,null as [subcategory!3!subcatname!element]
,null as [subcategory!3!subcatdesc!element]
,null as [subcategory!3!product!element]
,null as [product!4!productname!element]
,null as [product!4!productdesc!element]
,null as [product!4!company!element]
,null as [company!5!companyname!element]
,null as [company!5!companydesc!element]
from tcategory
--for xml explicit
union
SELECT
3 AS Tag
,2 AS Parent
,null as [catsubcatproduct!1!category]
,tcategory.catname as [category!2!catname!element]
,null as [category!2!catdesc!element]
,null as [category!2!subcategory!element]
,tsubcategory.subcatname as [subcategory!3!subcatname!element]
,tsubcategory.subcatdesc as [subcategory!3!subcatdesc!element]
,null as [subcategory!3!product!element]
,null as [product!4!productname!element]
,null as [product!4!productdesc!element]
,null as [product!4!company!element]
,null as [company!5!companyname!element]
,null as [company!5!companydesc!element]
from tcategory inner join tcatsubcat on tcategory.catname=tcatsubcat.catname
inner join
tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname
union
SELECT
4 AS Tag
,3 AS Parent
,null as [catsubcatproduct!1!category]
,tcategory.catname as [category!2!catname!element]
,null as [category!2!catdesc!element]
,null as [category!2!subcategory!element]
,tsubcategory.subcatname as [subcategory!3!subcatname!element]
,null as [subcategory!3!subcatdesc!element]
,null as [subcategory!3!product!element]
,tproduct.productname as [product!4!productname!element]
,tproduct.productdesc as [product!4!productdesc!element]
,null as [product!4!company!element]
,null as [company!5!companyname!element]
,null as [company!5!companydesc!element]
from tcategory inner join tcatsubcat on tcategory.catname=tcatsubcat.catname
inner join
tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname inner join
tsubcatproduct on tcatsubcat.subcatname=tsubcatproduct.subcatname inner join
tproduct on tsubcatproduct.productname=tproduct.productname
order by[category!2!catname!element],[subcateg
ory!3!subcatname!element]
for xml explicitI assume that you use the query analyzer of SQL Server 2000 to see the
result?
If so, that is the wrong tool to get reliable XML results. XML is streamed
back in rowset chunks over TDS. In order to see the data as the stream it
is, you either use the command stream object in ADO/OLEDB (or the equivalent
ADO.Net API), or - if you use ODBC - you need to concatenate the result
yourself.
QA basically shows the rowsets explicit and thus adds the linebreaks every
2034 or so characters...
Note that this will be much better dealt with in the SQL Server 2005
management studio. Hyperlinked XML opens an XML editor. Pretty if I may
say so myself... :-)
Best regards
Michael
"roblenderman" <roblenderman@.discussions.microsoft.com> wrote in message
news:73B244E1-6717-46A5-8C88-68A35AFAA66F@.microsoft.com...
>I have an xml query that runs across five tables. For some reasosn after I
> get to the third table my xml returned is breaking into two rows in the
> recordset that is returned. It just stops on the first record returned
> and
> breaks mid element name to the second record. I have attached my query up
> through table five. It breaks down on the last few closing elements and
> put
> them on a second row. Not sure why this is happening and dealing with the
> returned data is a pain. i have never seen for xml explicit returning the
> xml as multiple rows before.
> Rob
>
> SELECT
> 1 AS Tag
> ,NULL AS Parent
> ,null as [catsubcatproduct!1!category]
> ,null as [category!2!catname!element]
> ,null as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,null as [subcategory!3!subcatname!element]
> ,null as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,null as [product!4!productname!element]
> ,null as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> union
> SELECT
> 2 AS Tag
> ,1 AS Parent
> ,null as [catsubcatproduct!1!category]
> ,tcategory.catname as [category!2!catname!element]
> ,tcategory.catdesc as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,null as [subcategory!3!subcatname!element]
> ,null as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,null as [product!4!productname!element]
> ,null as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> from tcategory
> --for xml explicit
> union
>
> SELECT
> 3 AS Tag
> ,2 AS Parent
> ,null as [catsubcatproduct!1!category]
> ,tcategory.catname as [category!2!catname!element]
> ,null as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,tsubcategory.subcatname as [subcategory!3!subcatname!element]
> ,tsubcategory.subcatdesc as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,null as [product!4!productname!element]
> ,null as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> from tcategory inner join tcatsubcat on
> tcategory.catname=tcatsubcat.catname
> inner join
> tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname
>
> union
> SELECT
> 4 AS Tag
> ,3 AS Parent
> ,null as [catsubcatproduct!1!category]
> ,tcategory.catname as [category!2!catname!element]
> ,null as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,tsubcategory.subcatname as [subcategory!3!subcatname!element]
> ,null as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,tproduct.productname as [product!4!productname!element]
> ,tproduct.productdesc as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> from tcategory inner join tcatsubcat on
> tcategory.catname=tcatsubcat.catname
> inner join
> tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname inner join
> tsubcatproduct on tcatsubcat.subcatname=tsubcatproduct.subcatname inner
> join
> tproduct on tsubcatproduct.productname=tproduct.productname
>
> order by[category!2!catname!element],[subcateg
ory!3!subcatname!element]
> for xml explicit|||You are right. I was using the sql reader and not the xmlreader. The sql
reader returns records and the xmlreader returns one xml. If you use the
sqlreader you can concat the records and it will work but it is a waste to d
o
it that way. Thanks
Rob
"Michael Rys [MSFT]" wrote:

> I assume that you use the query analyzer of SQL Server 2000 to see the
> result?
> If so, that is the wrong tool to get reliable XML results. XML is streamed
> back in rowset chunks over TDS. In order to see the data as the stream it
> is, you either use the command stream object in ADO/OLEDB (or the equivale
nt
> ADO.Net API), or - if you use ODBC - you need to concatenate the result
> yourself.
> QA basically shows the rowsets explicit and thus adds the linebreaks every
> 2034 or so characters...
> Note that this will be much better dealt with in the SQL Server 2005
> management studio. Hyperlinked XML opens an XML editor. Pretty if I m
ay
> say so myself... :-)
> Best regards
> Michael
> "roblenderman" <roblenderman@.discussions.microsoft.com> wrote in message
> news:73B244E1-6717-46A5-8C88-68A35AFAA66F@.microsoft.com...
>
>

Multiple rows returned by for xml explicit

I have an xml query that runs across five tables. For some reasosn after I
get to the third table my xml returned is breaking into two rows in the
recordset that is returned. It just stops on the first record returned and
breaks mid element name to the second record. I have attached my query up
through table five. It breaks down on the last few closing elements and put
them on a second row. Not sure why this is happening and dealing with the
returned data is a pain. i have never seen for xml explicit returning the
xml as multiple rows before.
Rob
SELECT
1 AS Tag
,NULLAS Parent
,nullas [catsubcatproduct!1!category]
,nullas [category!2!catname!element]
,nullas [category!2!catdesc!element]
,nullas [category!2!subcategory!element]
,nullas [subcategory!3!subcatname!element]
,nullas [subcategory!3!subcatdesc!element]
,nullas [subcategory!3!product!element]
,nullas [product!4!productname!element]
,nullas [product!4!productdesc!element]
,nullas [product!4!company!element]
,nullas [company!5!companyname!element]
,nullas [company!5!companydesc!element]
union
SELECT
2 AS Tag
,1AS Parent
,nullas [catsubcatproduct!1!category]
,tcategory.catnameas [category!2!catname!element]
,tcategory.catdesc as [category!2!catdesc!element]
,nullas [category!2!subcategory!element]
,nullas [subcategory!3!subcatname!element]
,nullas [subcategory!3!subcatdesc!element]
,nullas [subcategory!3!product!element]
,nullas [product!4!productname!element]
,nullas [product!4!productdesc!element]
,nullas [product!4!company!element]
,nullas [company!5!companyname!element]
,nullas [company!5!companydesc!element]
from tcategory
--for xml explicit
union
SELECT
3 AS Tag
,2AS Parent
,nullas [catsubcatproduct!1!category]
,tcategory.catnameas [category!2!catname!element]
,nullas [category!2!catdesc!element]
,nullas [category!2!subcategory!element]
,tsubcategory.subcatnameas [subcategory!3!subcatname!element]
,tsubcategory.subcatdescas [subcategory!3!subcatdesc!element]
,nullas [subcategory!3!product!element]
,nullas [product!4!productname!element]
,nullas [product!4!productdesc!element]
,nullas [product!4!company!element]
,nullas [company!5!companyname!element]
,nullas [company!5!companydesc!element]
from tcategory inner join tcatsubcat on tcategory.catname=tcatsubcat.catname
inner join
tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname
union
SELECT
4 AS Tag
,3AS Parent
,nullas [catsubcatproduct!1!category]
,tcategory.catnameas [category!2!catname!element]
,nullas [category!2!catdesc!element]
,nullas [category!2!subcategory!element]
,tsubcategory.subcatnameas [subcategory!3!subcatname!element]
,nullas [subcategory!3!subcatdesc!element]
,nullas [subcategory!3!product!element]
,tproduct.productnameas [product!4!productname!element]
,tproduct.productdescas [product!4!productdesc!element]
,nullas [product!4!company!element]
,nullas [company!5!companyname!element]
,nullas [company!5!companydesc!element]
from tcategory inner join tcatsubcat on tcategory.catname=tcatsubcat.catname
inner join
tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname inner join
tsubcatproduct on tcatsubcat.subcatname=tsubcatproduct.subcatname inner join
tproduct on tsubcatproduct.productname=tproduct.productname
order by[category!2!catname!element],[subcategory!3!subcatname!element]
for xml explicit
I assume that you use the query analyzer of SQL Server 2000 to see the
result?
If so, that is the wrong tool to get reliable XML results. XML is streamed
back in rowset chunks over TDS. In order to see the data as the stream it
is, you either use the command stream object in ADO/OLEDB (or the equivalent
ADO.Net API), or - if you use ODBC - you need to concatenate the result
yourself.
QA basically shows the rowsets explicit and thus adds the linebreaks every
2034 or so characters...
Note that this will be much better dealt with in the SQL Server 2005
management studio. Hyperlinked XML opens an XML editor. Pretty cool if I may
say so myself... :-)
Best regards
Michael
"roblenderman" <roblenderman@.discussions.microsoft.com> wrote in message
news:73B244E1-6717-46A5-8C88-68A35AFAA66F@.microsoft.com...
>I have an xml query that runs across five tables. For some reasosn after I
> get to the third table my xml returned is breaking into two rows in the
> recordset that is returned. It just stops on the first record returned
> and
> breaks mid element name to the second record. I have attached my query up
> through table five. It breaks down on the last few closing elements and
> put
> them on a second row. Not sure why this is happening and dealing with the
> returned data is a pain. i have never seen for xml explicit returning the
> xml as multiple rows before.
> Rob
>
> SELECT
> 1 AS Tag
> ,NULL AS Parent
> ,null as [catsubcatproduct!1!category]
> ,null as [category!2!catname!element]
> ,null as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,null as [subcategory!3!subcatname!element]
> ,null as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,null as [product!4!productname!element]
> ,null as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> union
> SELECT
> 2 AS Tag
> ,1 AS Parent
> ,null as [catsubcatproduct!1!category]
> ,tcategory.catname as [category!2!catname!element]
> ,tcategory.catdesc as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,null as [subcategory!3!subcatname!element]
> ,null as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,null as [product!4!productname!element]
> ,null as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> from tcategory
> --for xml explicit
> union
>
> SELECT
> 3 AS Tag
> ,2 AS Parent
> ,null as [catsubcatproduct!1!category]
> ,tcategory.catname as [category!2!catname!element]
> ,null as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,tsubcategory.subcatname as [subcategory!3!subcatname!element]
> ,tsubcategory.subcatdesc as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,null as [product!4!productname!element]
> ,null as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> from tcategory inner join tcatsubcat on
> tcategory.catname=tcatsubcat.catname
> inner join
> tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname
>
> union
> SELECT
> 4 AS Tag
> ,3 AS Parent
> ,null as [catsubcatproduct!1!category]
> ,tcategory.catname as [category!2!catname!element]
> ,null as [category!2!catdesc!element]
> ,null as [category!2!subcategory!element]
> ,tsubcategory.subcatname as [subcategory!3!subcatname!element]
> ,null as [subcategory!3!subcatdesc!element]
> ,null as [subcategory!3!product!element]
> ,tproduct.productname as [product!4!productname!element]
> ,tproduct.productdesc as [product!4!productdesc!element]
> ,null as [product!4!company!element]
> ,null as [company!5!companyname!element]
> ,null as [company!5!companydesc!element]
> from tcategory inner join tcatsubcat on
> tcategory.catname=tcatsubcat.catname
> inner join
> tsubcategory on tcatsubcat.subcatname=tsubcategory.subcatname inner join
> tsubcatproduct on tcatsubcat.subcatname=tsubcatproduct.subcatname inner
> join
> tproduct on tsubcatproduct.productname=tproduct.productname
>
> order by[category!2!catname!element],[subcategory!3!subcatname!element]
> for xml explicit
|||You are right. I was using the sql reader and not the xmlreader. The sql
reader returns records and the xmlreader returns one xml. If you use the
sqlreader you can concat the records and it will work but it is a waste to do
it that way. Thanks
Rob
"Michael Rys [MSFT]" wrote:

> I assume that you use the query analyzer of SQL Server 2000 to see the
> result?
> If so, that is the wrong tool to get reliable XML results. XML is streamed
> back in rowset chunks over TDS. In order to see the data as the stream it
> is, you either use the command stream object in ADO/OLEDB (or the equivalent
> ADO.Net API), or - if you use ODBC - you need to concatenate the result
> yourself.
> QA basically shows the rowsets explicit and thus adds the linebreaks every
> 2034 or so characters...
> Note that this will be much better dealt with in the SQL Server 2005
> management studio. Hyperlinked XML opens an XML editor. Pretty cool if I may
> say so myself... :-)
> Best regards
> Michael
> "roblenderman" <roblenderman@.discussions.microsoft.com> wrote in message
> news:73B244E1-6717-46A5-8C88-68A35AFAA66F@.microsoft.com...
>
>

Multiple rows of data into 1 row results?

Is there a way to get multiple rows of data to show in 1 row of results?
I have a Data Table (ID, Name, Date, etc), Facility Table (ID, Name) and FacilityKey Table (Data ID and Facility ID). The Data table has a 1 to many relationship with the FacilityKey table.

I want to return the Data ID and each Facility Name associated with that Data ID to display in a DataGrid (preferably each Facility Name in the same cell on the grid too), but am stumped on how to get teh query to work (or where to look in the SQL documentation for something like this).

Thanks!What you are trying to do is typically called a pivot table or cross tab query.

Take a look at this post:view post 350666. Someone has posted a suggestion to use a table control to accomplish this. See if anything there helps you. If not, post again :-)

Terri|||I think I may have to do it outside of SQL. I don't want to do any sort of aggregation of the data - just display it in 1 row instead of multiple and I don't see anywhere how to do the SQL cross tab without using an aggregate function of some sort.|||Well, you could write a UDF that would take the DataID as a parameter and would return a comma-delimited list of all of the facilities for that DataID. Like this:


CREATE FUNCTION getFacilities
(@.DataID int)
RETURNS varchar(8000)
AS
BEGIN

DECLARE @.facilities VARCHAR(8000)
SET @.facilities = ''

SELECT
@.facilities = @.facilities + ', ' + FacilityTable.FacilityName
FROM
DataTable
INNER JOIN
FacilityKeyTable ON DataTable.DataID = FacilityKeyTable.KeyDataID
INNER JOIN
FacilityTable ON FacilityKeyTable.KeyFacilityID = FacilityTable.FacilityID
WHERE
DataTable.DataID = @.DataID

IF @.facilities <> ''
BEGIN
SET @.facilities = SUBSTRING(@.facilities,3,LEN(@.facilities)-2)
END

RETURN (@.facilities)

END

You would use that function like this:

SELECT DataID, dbo.getFacilities(DataID) FROM DataTable

Terri|||Thank you! That is exactly what I was looking for!|||If efficiency is a priority for you then this function is a poor solution since under the hood it means you are doing N queries for each row returned where N is the number of facilities. A cross-tab query will be more efficient.

Multiple rows into single column

I'm working on a Reports and I need to combine data from multiple
rows (with the same EventDate) into one comma separated string. This is how the data is at the moment:

EmployeeID EventDate
2309 2005-10-01 00:00:00.000
2309 2005-10-01 03:44:50.000
2309 2005-10-01 08:59:00.000
2309 2005-10-01 09:29:44.000

I need it in the following format:

EmpID EventDate EventTime
2309 2005-10-01 00:00:00,03:44:50,08:59:00,09:29:44

There is no definite number of EventTime per Employee for a particular EventDate. And i don't want to use Iterative Methods.

Thanks & Regards
Rajan

Hi Rajan,

If you have access to sql server magazine take a look at this article, http://www.sqlmag.com/Article/ArticleID/93907/sql_server_93907.html

You can see the code even if you don't have membership so should still be useful. Listing 3 will probably be the most useful, although it is not particularly efficient.

Hope this helps.

Chris

|||

Another alternative might be something like:

declare @.mockup table
( EmployeeId integer,
EventDate datetime
)
insert into @.mockup values (2308, '1/1/2005')
insert into @.mockup values (2309, '10/1/2005')
insert into @.mockup values (2309, '10/1/2005 3:44:50')
insert into @.mockup values (2309, '10/1/2005 8:59')
insert into @.mockup values (2309, '10/1/2005 9:29:44')
insert into @.mockup values (2310, '11/1/2005')

select employeeId,
replace(replace(
( select e.eventDate as [data()]
from @.mockup e
where a.employeeId = e.employeeId
order by e.eventDate
for xml path ('')
), ' ', ','), 'T', ' ')
as EventDates
from @.mockup a
group by employeeId
order by employeeId

-- employeeId EventDates
-- -- -
-- 2308 2005-01-01 00:00:00
-- 2309 2005-10-01 00:00:00,2005-10-01 03:44:50,2005-10-01 08:59:00,2005-10-01 09:29:44
-- 2310 2005-11-01 00:00:00

Multiple rows into one row

Hello I am wondering if this is possible.

I have a two tables one contains Plan information and another that
contains product information about the plan

ex:
Plan table
PlanID Plan_name
1 a
2 b

Product Table
ProductID PlanID Comments
1 1 com1
2 1 com2
3 1 com3

What I am looking to do if possible would be the following

Plan Product1 Comments1 Product2 Comments2
1 1 com1 2 com2

I am wondering down what path I should explore cause I am new to this.
I am using sql 2005Chicagoboy27 wrote:

Quote:

Originally Posted by

Hello I am wondering if this is possible.
>
I have a two tables one contains Plan information and another that
contains product information about the plan
>
ex:
Plan table
PlanID Plan_name
1 a
2 b
>
Product Table
ProductID PlanID Comments
1 1 com1
2 1 com2
3 1 com3
>
What I am looking to do if possible would be the following
>
Plan Product1 Comments1 Product2 Comments2
1 1 com1 2 com2
>
>
I am wondering down what path I should explore cause I am new to this.
I am using sql 2005


Take a look at PIVOT in Books Online. Also Itzik Ben-Gan has articles
on PIVOT in the last two issues of SQL Server Magazine. www.sqlmag.com
--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||Thanks I will explore that option...

David Portas wrote:

Quote:

Originally Posted by

Chicagoboy27 wrote:

Quote:

Originally Posted by

Hello I am wondering if this is possible.

I have a two tables one contains Plan information and another that
contains product information about the plan

ex:
Plan table
PlanID Plan_name
1 a
2 b

Product Table
ProductID PlanID Comments
1 1 com1
2 1 com2
3 1 com3

What I am looking to do if possible would be the following

Plan Product1 Comments1 Product2 Comments2
1 1 com1 2 com2

I am wondering down what path I should explore cause I am new to this.
I am using sql 2005


>
Take a look at PIVOT in Books Online. Also Itzik Ben-Gan has articles
on PIVOT in the last two issues of SQL Server Magazine. www.sqlmag.com
>
--
David Portas, SQL Server MVP
>
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
>
SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--

Multiple rows into one query result row...

I have an abstract relational database underneath some business objects... For instance, say I have two tables...

TABLE 1: A simple list of people...

ID USER
---
1 Mike
2 John

TABLE 2: Name/Value pairs of attributes linked to table 1 by ID...

ID NAME VALUE
------
1 Hair Brown
1 Eyes Blue
2 Weight 200

So you can see from this that Mike has brown hair and blue eyes, and that John weighs 200 lbs.

I want a query that selects a person and all their attributes (whatever they may be), and returns the results on one row like this (when run with a WHERE clause that selects user Mike).

USER HAIR EYES
------
Mike Brown Blue

And returns this when run with a WHERE clause that selects user John...

USER WEIGHT
-----
John 200

Any ideas? Thanks in advance!Wow! Talk about stuff that will give you nightmares!

-PatP|||This question has been asked and anwered hundreds of times in all forums. Do some research!
Hint: Search for PIVOT TABLE, CROSSTAB QUERY, etc... :mad:|||This question has been asked and anwered hundreds of times in all forums. Do some research!
Hint: Search for PIVOT TABLE, CROSSTAB QUERY, etc...I think this one is a twist on the usual cross tab... It looks like they want the query schema to change from row to row, which goes way beyond a cross-tab in my mind. It makes my head hurt just thinking about it. A cross-tab seems simple to code and use in comparison to this request.

-PatP|||Thanks, Pat. I'm being bludgeoned on a few boards over this. People keep saying cross-tab, which is not what I'm after here...

It does indeed make the head hurt...|||I would classify this as a candidate for a recursive query. Search through this forum for some good ideas.|||candidate for dynamic sql

step 1: select distinct name from table2 where id=n

if you don't know n, do a join and use WHERE table1.name = 'fred'

step 2: construct multiple LEFT OUTER JOIN query, from table1 to table2 as many times as there are attributes that fred has (from step1), aliasing each table2.value to the corresponding column name

step 3: execute the dynamic query

easy peasy|||I think that Rudy's suggestion is a good one, if you can allow all of the rows in a given result set to have the same schema. This is probably the closest answer possible to what you want using standard SQL tools. Supporting irregularly shaped result sets is possible using some tools, but not using standard recordset-oriented tools.

As Fibber used to say: "T'ain't pretty, McGee!"

-PatP

Multiple Rows into one by adding new columns

Help...this is driving me nuts.

I have a table with the following columns:

Name|Address|Child|ChildAge

Bill - 1 ParkPlace - Ben - 2

Bill - 1 ParkPlace - Jen - 4

Jill - 2 ParkPlace - Kev - 6

Jill - 2 ParkPlace - Bev - 7

Jill - 2 ParkPlace - Jeb - 7

There can be multiple Children/Age per name so there are multiple rows. I would like to create a table from this that put all children w/age on one row by adding as many Child and Age columns as needed:

Name|Address|Child|ChildAge|Child|ChildAge|Child|ChildAge...

Bill - 1 ParkPlace - Ben - 2 - Jen - 4

Jill - 2 ParkPlace - Kev - 6 - Bev - 7 - Jeb - 7

Thanks for any help.

Bill:

What you are asking to do is generally considered a bad design. What is motivating you to want to do this? I think it would be better to address the root problem or set of problems than to jump right in to all of these columns. Also, which version of SQL Server are you using?

Kent

|||

Thanks Kent.

Sorry, I should've been a little clearer. My goal is to create a report (spreadsheet) from the existing table as described above. I don't necessarily need to create a new table. I just thought that it would be best to address the layout before dumping it into excel.

I am using 2005 with Integration Services.

|||

Code Snippet

createfunction dbo.ListChildren ( @.Name as varchar(25), @.Address as varchar(25))

returns varchar(8000)

as

begin

declare @.list varchar(8000)

select @.list =coalesce( @.list +' - ','')+ Child +'-'+cast(ChildAge as varchar(3))

from tbl where Name = @.Name AND Address = @.Address

return @.list

end

GO

createtable dbo.tbl (Name varchar(25), Address varchar(25),

Child varchar(25), ChildAge int)

insertinto dbo.tbl values('Bill','1 ParkPlace','Ben', 2)

insertinto dbo.tbl values('Bill','1 ParkPlace','Jen', 4)

insertinto dbo.tbl values('Jill','2 ParkPlace','Kev', 6)

insertinto dbo.tbl values('Jill','2 ParkPlace','Bev', 7)

insertinto dbo.tbl values('Jill','2 ParkPlace','Jeb', 7)

select Name, Address, dbo.ListChildren(Name, Address)as Children

from dbo.tbl

groupby Name, Address

|||

This suggestion may provide you the output you desire.

Code Snippet


SET NOCOUNT ON


DECLARE @.MyTable table
( RowID int IDENTITY,
Name varchar(20),
Address varchar(25),
ChildName varchar(20),
ChildAge int
)


INSERT INTO @.MyTable VALUES( 'Bill', '1 ParkPlace', 'Ben', 2 )
INSERT INTO @.MyTable VALUES( 'Bill', '1 ParkPlace', 'Jen', 4 )
INSERT INTO @.MyTable VALUES( 'Jill', '2 ParkPlace', 'Kev', 6 )
INSERT INTO @.MyTable VALUES( 'Jill', '2 ParkPlace', 'Bev', 7 )
INSERT INTO @.MyTable VALUES( 'Jill', '2 ParkPlace', 'Jeb', 7 )


SELECT DISTINCT
Name,
Address,
Children = substring( ( SELECT ', ' + ( ChildName + ' -' + cast( ChildAge AS varchar(2))) AS [text()]
FROM @.MyTable t2
WHERE t2.Name = t1.Name
ORDER BY Name
FOR XML path(''), elements
), 3, 1000
)
FROM @.MyTable t1


Name Address Children
-- -
Bill 1 ParkPlace Ben -2, Jen -4
Jill 2 ParkPlace Kev -6, Bev -7, Jeb -7

Multiple Rows into One

ISBN Parent_ISBN Title FMonth Forecast SQL TABLE 043967XXXX 043967XXXX MY BOOK 200705 13.25 043967XXXX 043967XXXX MY BOOK 200706 88 043967XXXX 043967XXXX MY BOOK 200707 53 043967XXXX 043967XXXX MY BOOK 200708 54 043967XXXX 043967XXXX MY BOOK 200709 8.08 043967XXXX 043967XXXX MY BOOK 200710 11.84 043967XXXX 043979XXXX MY BOOK2 200705 6.5 043967XXXX 043979XXXX MY BOOK2 200706 135 043967XXXX 043979XXXX MY BOOK2 200707 82 043967XXXX 043979XXXX MY BOOK2 200708 83 043967XXXX 043979XXXX MY BOOK2 200709 80.64 043967XXXX 043979XXXX MY BOOK2 200710 112.16 ISBN Parent_ISBN Title FMonth Forecast FMonth Forecast FMonth Forecast FMonth Forecast FMonth Forecast FMonth Forecast 043967XXXX 043967XXXX MY BOOK 200705 13.25 200706 88 200707 53 200708 54 200709 8.08 200710 11.84 043967XXXX 043979XXXX MY BOOK2 200705 6.5 200706 135 200707 82 200708 83 200709 80.64 200710 112.16 (Desired) RESULTS

I have a table with multiple rows per ISBN. I would like to create a table with one line per ISBN adding columns. I dont want to concatenate any columns.

My goal is to dump this into a spreadsheet through Intregration Services 2005. I am not the DBA so I can't redesign the main table. Thanks for any help.

Here you go..

Code Snippet

--Drop table #books

Create Table #books (

[ISBN] Varchar(100) ,

[Parent_ISBN] Varchar(100) ,

[Title] Varchar(100) ,

[FMonth] Varchar(100) ,

[Forecast] Varchar(100)

);

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200705','13.25');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200706','88');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200707','53');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200708','54');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200709','8.08');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200710','11.84');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200705','6.5');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200706','135');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200707','82');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200708','83');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200709','80.64');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200710','112.16');

Select

[ISBN]

,[Parent_ISBN]

,[Title]

,Max(Case [FMonth] When 200705 Then 200705 End) as FMonth

,Max(Case [FMonth] When 200705 Then Forecast End) as Forecast

,Max(Case [FMonth] When 200706 Then 200706 End) as FMonth

,Max(Case [FMonth] When 200706 Then Forecast End) as Forecast

,Max(Case [FMonth] When 200707 Then 200707 End) as FMonth

,Max(Case [FMonth] When 200707 Then Forecast End) as Forecast

,Max(Case [FMonth] When 200708 Then 200708 End) as FMonth

,Max(Case [FMonth] When 200708 Then Forecast End) as Forecast

,Max(Case [FMonth] When 200709 Then 200709 End) as FMonth

,Max(Case [FMonth] When 200709 Then Forecast End) as Forecast

,Max(Case [FMonth] When 200710 Then 200710 End) as FMonth

,Max(Case [FMonth] When 200710 Then Forecast End) as Forecast

From

#books

Group By

[ISBN]

,[Parent_ISBN]

,[Title]

|||The only problem with this is that I have several thousand ISBNs which all appear 6 times in the table. From each set of 6 rows I need to create one row. Is there anyway that I can do this by setting parameters? Thanks|||

Yes. You can . here it is..

Code Snippet

--Drop table #books

Create Table #books (

[ISBN] Varchar(100) ,

[Parent_ISBN] Varchar(100) ,

[Title] Varchar(100) ,

[FMonth] Varchar(100) ,

[Forecast] Varchar(100)

);

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200705','13.25');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200706','88');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200707','53');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200708','54');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200709','8.08');

Insert Into #books Values('043967XXXX','043967XXXX','MYBOOK','200710','11.84');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200705','6.5');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200706','135');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200707','82');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200708','83');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200709','80.64');

Insert Into #books Values('043967XXXX','043979XXXX','MYBOOK2','200710','112.16');

Declare @.Start as Varchar(10);

Set @.Start = '200707'

Select

[ISBN]

,[Parent_ISBN]

,[Title]

,@.Start as FMonth

,Max(Case [FMonth] When @.Start Then Forecast End) as Forecast

,Substring(Convert(varchar,DateAdd(MM,1,Cast(@.Start + '01' as datetime)),112),1,6) as FMonth

,Max(Case [FMonth] When Substring(Convert(varchar,DateAdd(MM,1,Cast(@.Start + '01' as datetime)),112),1,6) Then Forecast End) as Forecast

,Substring(Convert(varchar,DateAdd(MM,2,Cast(@.Start + '01' as datetime)),112),1,6) as FMonth

,Max(Case [FMonth] When Substring(Convert(varchar,DateAdd(MM,2,Cast(@.Start + '01' as datetime)),112),1,6) Then Forecast End) as Forecast

,Substring(Convert(varchar,DateAdd(MM,3,Cast(@.Start + '01' as datetime)),112),1,6) as FMonth

,Max(Case [FMonth] When Substring(Convert(varchar,DateAdd(MM,3,Cast(@.Start + '01' as datetime)),112),1,6) Then Forecast End) as Forecast

,Substring(Convert(varchar,DateAdd(MM,4,Cast(@.Start + '01' as datetime)),112),1,6) as FMonth

,Max(Case [FMonth] When Substring(Convert(varchar,DateAdd(MM,4,Cast(@.Start + '01' as datetime)),112),1,6) Then Forecast End) as Forecast

,Substring(Convert(varchar,DateAdd(MM,5,Cast(@.Start + '01' as datetime)),112),1,6) as FMonth

,Max(Case [FMonth] When Substring(Convert(varchar,DateAdd(MM,5,Cast(@.Start + '01' as datetime)),112),1,6) Then Forecast End) as Forecast

From

#books

Where

[FMonth] Between @.Start And Substring(Convert(varchar,DateAdd(MM,5,Cast(@.Start + '01' as datetime)),112),1,6)

Group By

[ISBN]

,[Parent_ISBN]

,[Title]

|||

For 2005:

Code Snippet

Droptable #books

CreateTable #books (

[ISBN] Varchar(100),

[Parent_ISBN] Varchar(100),

[Title] Varchar(100),

[FMonth] Varchar(100),

[Forecast] money

);

InsertInto #books Values('043967XXXX','043967XXXX','MYBOOK','200705','13.25');

InsertInto #books Values('043967XXXX','043967XXXX','MYBOOK','200706','88');

InsertInto #books Values('043967XXXX','043967XXXX','MYBOOK','200707','53');

InsertInto #books Values('043967XXXX','043967XXXX','MYBOOK','200708','54');

InsertInto #books Values('043967XXXX','043967XXXX','MYBOOK','200709','8.08');

InsertInto #books Values('043967XXXX','043967XXXX','MYBOOK','200710','11.84');

InsertInto #books Values('043967XXXX','043979XXXX','MYBOOK2','200705','6.5');

InsertInto #books Values('043967XXXX','043979XXXX','MYBOOK2','200706','135');

InsertInto #books Values('043967XXXX','043979XXXX','MYBOOK2','200707','82');

InsertInto #books Values('043967XXXX','043979XXXX','MYBOOK2','200708','83');

InsertInto #books Values('043967XXXX','043979XXXX','MYBOOK2','200709','80.64');

InsertInto #books Values('043967XXXX','043979XXXX','MYBOOK2','200710','112.16');

;with cte as

(

select isbn +'-'+ parent_isbn as rkey, forecast, row_number()over(partitionby isbn, parent_isbn orderby fmonth)as rn

from #books

), cte2 as

(

select rkey, [1] as F1, [2] as F2, [3] as F3, [4] as F4, [5] as F5, [6] as F6

from cte

pivot

(

sum(forecast)

FOR rn IN([1], [2], [3], [4], [5], [6])

)as p

), cte3 as

(

select isbn +'-'+ parent_isbn as rkey, fmonth, row_number()over(partitionby isbn, parent_isbn orderby fmonth)as rn

from #books

), cte4 as

(

select rkey, [1] as FM1, [2] as FM2, [3] as FM3, [4] as FM4, [5] as FM5, [6] as FM6

from cte3

pivot

(

min(fmonth)

FOR rn IN([1], [2], [3], [4], [5], [6])

)as p

)

select books.isbn, books.parent_isbn, books.title,

cte4.fm1 as FMonth, cte2.f1 as Forecast,

cte4.fm2 as FMonth, cte2.f2 as Forecast,

cte4.fm3 as FMonth, cte2.f3 as Forecast,

cte4.fm4 as FMonth, cte2.f4 as Forecast,

cte4.fm5 as FMonth, cte2.f5 as Forecast,

cte4.fm6 as FMonth, cte2.f6 as Forecast

from cte2

innerjoin cte4

on cte2.rkey = cte4.rkey

innerjoin

(selectdistinct isbn, parent_isbn, title

from #books books

)as books

onleft(cte2.rkey,charindex('-', cte2.rkey)-1)= books.isbn

andright(cte2.rkey,len(cte2.rkey)-charindex('-', cte2.rkey))= books.parent_isbn

|||This was exactly what i was looking for...thanks...the only problem was having a static date in:

Declare @.Start as Varchar(10);

Set @.Start = '200707'

I wanted to use current yyyymm.