Showing posts with label figure. Show all posts
Showing posts with label figure. Show all posts

Tuesday, March 6, 2012

Aggregate Function to Concatenate Columns Data into a single Row

Hi all,

I have a scenario which I am not able to figure out how to do it better for quite some time.

Assume I have a few rows of data :

RunningID Date WOid

1234 1/23/2007 23

1236 1/24/2007 23

1239 1/2/2007 24

1222 1/4/2007 23

1321 2/4/2007 22

My objective is to merge all RunningID into a single cell when WOid is the same (this will most probably use a "group by" to get the different WOid out). Maybe some aggregate function that can do it as:

select ReturnConca(RunningID, "#") as RunningID_str, max(Date) as MaxDate, max(WOid) as WO from tableXXX

group by WOid

Results:

RunningID_str MaxDate WO

1234#1236#1222 1/24/2007 23

1239 1/2/2007 24

1321 2/4/2007 22

Any advise would be much appreciated.

If you use SQL server 2005,

Code Snippet

Create Table #data (

[RunningID] int ,

[Date] datetime ,

[WOid] int

);

Insert Into #data Values('1234','1/23/2007','23');

Insert Into #data Values('1236','1/24/2007','23');

Insert Into #data Values('1239','1/2/2007','24');

Insert Into #data Values('1222','1/4/2007','23');

Insert Into #data Values('1321','2/4/2007','22');

Select

[RunningIDs],

Max([Date]) [Date],

[WoId]

From

(

select

Substring((Select '#' + cast([RunningID] as varchar) as [text()] from #data sub

where sub.[Woid] = main.[Woid] for xml path('')

),2,8000) as [RunningIDs],

[Date],

[WoId]

from

#data main

) as data

Group By

[RunningIDs],[WoId]

Order By

[RunningIDs]

|||

Thanks for your code.

However, this must be done on the fly and there are many similar rows in a single selection and how do we encapsulate the above code into a function. If not, how do we insert the dynamic data into the temp table on the fly?

|||

Post your query.. I didn't understand the dynamic data / on the fly.. You can achive this without function.|||

Here is my query:

SELECT P.running as PRunning, P.[date], P.refWO, P.addWO,
W.id,W.running as WRunning,W.[date] as WO_Date
FROM WorkOrder W
RIGHT JOIN PurchaseOrder P ON (P.refWO=W.id)
where not W.id is null
UNION ALL

SELECT P.running as PRunning, P.[date], P.refWO, P.addWO,
W.id,W.running as WRunning, W.[date] as WO_Date
FROM WorkOrder W
RIGHT JOIN PurchaseOrder P ON (P.addWO like ('%#' + cast(W.id as varchar) + ':%'))
where not W.id is null
order by W.id, W.[date], W.running

The PRunning and P.date will have a few rows to one P.refWO. his might be occuring a few times over the result.

|||

May be something like this,

Code Snippet

SELECT P.running as PRunning, P.[date], P.refWO, P.addWO,

W.id,W.running as WRunning,W.[date] as WO_Date into #temp

FROM WorkOrder W

RIGHT JOIN PurchaseOrder P ON (P.refWO=W.id)

where not W.id is null

UNION ALL

SELECT P.running as PRunning, P.[date], P.refWO, P.addWO,

W.id,W.running as WRunning, W.[date] as WO_Date

FROM WorkOrder W

RIGHT JOIN PurchaseOrder P ON (P.addWO like ('%#' + cast(W.id as varchar) + ':%'))

where not W.id is null

order byW.id, W.[date], W.running

Select

PRunning

,Max([date])

,refWO

,addWO

,WRunning

,max(WO_Date)

From

(

Select

(Select '#' + PRunning as [text()] from #temp sub where sub.id = main.id For xml path('')) as PRunning

,[date]

,(Select '#' + refWO as [text()] from #temp sub where sub.id = main.id For xml path('')) as refWO

,(Select '#' + addWO as [text()] from #temp sub where sub.id = main.id For xml path('')) as addWO

,(Select '#' + WRunning as [text()] from #temp sub where sub.id = main.id For xml path('')) as WRunning

,WO_Date

from

#temp

) as Data

Group By

PRunning

,refWO

,addWO

,WRunning

|||

Hi I am using SQL 2000 and I suppose i need some minor tweating to the code. When i run the code, It reported invalid for "For XML Path('')'. So i took those out.

Another issue is where does the alias "main" referring to?

Monday, February 13, 2012

After Install, "Running sqlcmd -S Server\Instance" Fails

I'm trying to figure out how to set up SQL Express so I can start learning SQL. I've installed SQL Express, but when I type "sqlcmd -S Server\SQLExpress," I receive the following error:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Username>sqlcmd -S Server\SQLExpress
HResult 0xFFFFFFFF, Level 16, State 1
SQL Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establi
shing a connection to the server. When connecting to SQL Server 2005, this failu
re may be caused by the fact that under the default settings SQL Server does not
allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.


Hi,

see the screencast on my site for eabling remote connections.

HTH, Jens K. Suessmeyer.

-
http://www.sqlserver2005.de
-

|||Thanks, but when I got to http://www.sqlserver2005.de/Screencast/Screencast.aspx?ScreencastId=1, nothing happened.

I've found how to enable remote connections in the KB. So I have to enable remote connections to access SQL Server on a local computer? Want I want to do is get some sort of IDE up to learn in. Thanks.|||The screencast is a flash file, so you will need to have flash installed on your computer.

If you navigate through the Surface Area configuration (presented in the program menu) > Services and connections, you can change the setting, make sure you restart the service afterwards.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||Thanks. I will point out that Flash is installed in my computer though, and about:plugins shows "Shockwave Flash." I've tested Flash on other sites, it pulls up fine.|||Not working. Using http://support.microsoft.com/kb/914277/en-us, I've tried enable remote connections, stopping, starting the Database engine, still get errors. Disabled remote connections, set the SQL Server Browser to automatic, stopped, started the Database engine, no change. Enable Remote Connections, first Named Pipes, then both TCP/IP and Names Pipes, in conjunction with the SQL Server Browser set to automatic, still get errors. So what do I do?|||*bump*|||So no one really knows how to install the thing? :-(|||

I was having the same issues until I changed the way I was trying to connect:

sqlcmd -S .\SQLEXPRESS.

This was after an install as part of visual studio 2005. I hope this helps.

After Install, "Running sqlcmd -S Server\Instance" Fails

I'm trying to figure out how to set up SQL Express so I can start learning SQL. I've installed SQL Express, but when I type "sqlcmd -S Server\SQLExpress," I receive the following error:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Username>sqlcmd -S Server\SQLExpress
HResult 0xFFFFFFFF, Level 16, State 1
SQL Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establi
shing a connection to the server. When connecting to SQL Server 2005, this failu
re may be caused by the fact that under the default settings SQL Server does not
allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.


Hi,

see the screencast on my site for eabling remote connections.

HTH, Jens K. Suessmeyer.

-
http://www.sqlserver2005.de
-

|||Thanks, but when I got to http://www.sqlserver2005.de/Screencast/Screencast.aspx?ScreencastId=1, nothing happened.

I've found how to enable remote connections in the KB. So I have to enable remote connections to access SQL Server on a local computer? Want I want to do is get some sort of IDE up to learn in. Thanks.|||The screencast is a flash file, so you will need to have flash installed on your computer.

If you navigate through the Surface Area configuration (presented in the program menu) > Services and connections, you can change the setting, make sure you restart the service afterwards.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||Thanks. I will point out that Flash is installed in my computer though, and about:plugins shows "Shockwave Flash." I've tested Flash on other sites, it pulls up fine.|||Not working. Using http://support.microsoft.com/kb/914277/en-us, I've tried enable remote connections, stopping, starting the Database engine, still get errors. Disabled remote connections, set the SQL Server Browser to automatic, stopped, started the Database engine, no change. Enable Remote Connections, first Named Pipes, then both TCP/IP and Names Pipes, in conjunction with the SQL Server Browser set to automatic, still get errors. So what do I do?|||*bump*|||So no one really knows how to install the thing? :-(|||

I was having the same issues until I changed the way I was trying to connect:

sqlcmd -S .\SQLEXPRESS.

This was after an install as part of visual studio 2005. I hope this helps.