I need to fetch the data from SQL Server based on the below condition.  In the database there are columns Ticketnumber and comments. For a Ticketnumber there are multiple records, for the same ticket number there are multiple comments stored in different records. Whenever I retrieve the ticketnumber and comments columns, the comments should be concatenated in the single record.   Let's look at an example:  Ticketnumber       Comments         ------------------------------- 1002               Case raised     1003               Case raised 1002               Processing, 1003               resolved 1002               resolved   Expected output should be:  Ticketnumber       Comments ----------------------------------------------- 1002               Case raise,processing,resolved, 1003               case raised,resolved     All the comments should be concatenated for single ticketnumber as above.  Please provide the query. Thanks in advance
1770
Consider a database table holding names, with three rows:  Peter Paul Mary   Is there an easy way to turn this into a single string of Peter, Paul, Mary?
24101
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters that belong to a stored procedure on a SQL Server before I actually execute it?  I have an a "multi-environment" scenario where there are multiple versions of the same database schema. Examples of environments might be "Development", "Staging", &amp; "Production". "Development" is going to have one version of the stored procedure and "Staging" is going to have another.  All I want to do is validate that a parameter is going to be there before passing it a value and calling the stored procedure. Avoiding that SqlException rather than having to catch it is a plus for me.  Joshua
107229
Lately, I have been trying to figure out the most generic query for getting tracking data into one table. As suggested in the , I have been using this join:  ... FROM _Sent s LEFT JOIN _Job as j ON s.JobID = j.JobID  LEFT JOIN _Open o ON s.JobID = o.JobID and s.ListID = o.ListID and s.BatchID = o.BatchID and s.SubscriberID = o.SubscriberID and o.IsUnique = 1 LEFT JOIN _Click c ON s.JobID = c.JobID  and s.ListID = c.ListID and s.BatchID = c.BatchID and s.SubscriberID = c.SubscriberID and c.IsUnique = 1 LEFT JOIN _bounce b ON s.JobID = b.JobID and s.ListID = b.ListID and s.BatchID = b.BatchID and s.SubscriberID = b.SubscriberID and b.IsUnique = 1   My results show the same email send, with the same JobID, sent exactly at the same time, to the same subscriber, but with different batch numbers. On top of that, only the first record has an open date:    According to documentation, BatchID   is "The batch ID number for any batches used in the send" - but what does it mean, really? And how should the  BatchID be treated in queries?   Thanks!
67080
I have a table with 5 columnsID,CPT4,CPT4Mod,ItemCharge,Eff_Date.   Deletion Condition:  When rows exists with duplicate CPT4and CPT4Mod field values but different ItemCharge and Eff_Date values, exclude all but the most recent Eff_Date records.  I need a query which can achieve this in two scenarios.   When CPT4Mod is given by the user. When  CPT4Mod is not given by the user.   Later I need to achieve the same using LINQ.
107777
This might be a silly question, and my suspicion is that I can't do this, but is there a construct in SQL that would allow me to do something like the following:  SELECT whatever WHERE col1,col2 IN ((val1, val2), (val1, val2), ...)   I want to select data where two columns are in a set of pairs.  I'd like to avoid using a subquery, if possible.
78798
I have a table structured as so (Simplified)  Name, EMail, LastLoggedInAt   I have a user in SQL Server (RemoteUser) that should only be able to see data (Via a select query) where the LastLoggdInAt field is not null.   It looks like I can do this? Is it possible?
86723
The SQL...  UPDATE Threads t  SET t.Content = (     SELECT GROUP_CONCAT(a.Content ORDER BY a.PageID SEPARATOR '&lt;!-- pagebreak --&gt;')      FROM MSarticlepages a     WHERE a.ArticleID = t.MSthreadID GROUP BY a.ArticleID )   As you can see it takes all of an article's pages (which are each stored as longtext in separate rows) and GROUP_CONCATs them into a single longtext row. The problem is the results are only so many characters and then it gets completely truncated, losing about 90% of the contents. Does CONCAT not handle longtext very well or is there something else I am doing wrong?
2711
I have this code:  DECLARE @MyTable AS TABLE (    [Month] INT,    Salary INT );  INSERT INTO @MyTable VALUES (1,2000), (1,3100);  SELECT [Month], Salary FROM @MyTable;   Output:    I want to concat the Salary (grouping by month) so that it will be NVARCHAR like this: '2000,3100'  How would I do this efficiently?
37349
I have a query who return something like it :  Name   Gender Job         date of hire John   M      mechanic    2012-05-08 John   M      electrician 2010-01-01 Vicky  F      scientific  2012-11-11 Bob    M      NULL        NULL   I need to have the name, gender and jobname of first job of each person. But i don't know how. My query look like this :  select name,gender,jobname,hiredate from person p left join job j on p.personid = j.personid   I'm on Microsoft SQL Server 2000  I need this as result :  Name   Gender Job          John   M      electrician Vicky  F      scientific  Bob    M      NULL
107704
I have data like multiples of 40. 40 80 120 160 200 and so on.    I would like to label every record that is a multiple of 100.  I have tried a couple of methods and can't get them to work either as a query in the label or as a select statement.  "STATION" =  %00   RIGHT ("STATION", 2) =  00     I get invalid SQL statement. Does anyone see what I am doing wrong?    Respectfully I did search and did not see any of the example questions you presented.  However it still does not answer my question, re-stated, what am I doing wrong?
104872
I'm looking for an Access 2007 equivalent to SQL Server's COALESCE function.  In SQL Server you could do something like:  Person  John Steve Richard   SQL  DECLARE @PersonList nvarchar(1024) SELECT @PersonList = COALESCE(@PersonList + ',','') + Person FROM PersonTable  PRINT @PersonList   Which produces: John, Steve, Richard  I want to do the same but in Access 2007.  Does anyone know how to combine rows like this in Access 2007?
23656
I have a simple query:  select * from countries   with the following results:  country_name ------------ Albania Andorra Antigua .....   I would like to return the results in one row, so like this:  Albania, Andorra, Antigua, ...   Of course, I can write a PL/SQL function to do the job (I already did in Oracle 10g), but is there a nicer, preferably non-Oracle-specific solution (or may be a built-in function) for this task?  I would generally use it to avoid multiple rows in a sub-query, so if a person has more then one citizenship, I do not want her/him to be a duplicate in the list.  My question is based on the similar question on .  UPDATE: My function looks like this:  CREATE OR REPLACE FUNCTION APPEND_FIELD (sqlstr in varchar2, sep in varchar2 ) return varchar2 is ret varchar2(4000) := ''; TYPE cur_typ IS REF CURSOR; rec cur_typ; field varchar2(4000); begin      OPEN rec FOR sqlstr;      LOOP          FETCH rec INTO field;          EXIT WHEN rec%NOTFOUND;          ret := ret || field || sep;      END LOOP;      if length(ret) = 0 then           RETURN '';      else           RETURN substr(ret,1,length(ret)-length(sep));      end if; end;
48977
I have a table with several columns which I want to SELECT:  SELECT his_name , her_name, other_name FROM foo;   Bu, I instead I want to combine the results all into a single column. As an example, I can do this with UNION ALL as   SELECT her_name AS name FROM foo UNION ALL SELECT his_name AS name FROM foo UNION ALL SELECT other_name AS name FROM foo   Is there more elegant way to do this operation?
6644
I've got two tables:  TableA ------ ID, Name  TableB ------ ID, SomeColumn, TableA_ID (FK for TableA)   The relationship is one row of TableA - many of TableB.  Now, I want to see a result like this:  ID     Name      SomeColumn  1.     ABC       X, Y, Z (these are three different rows) 2.     MNO       R, S   This won't work (multiple results in a subquery):  SELECT ID,        Name,         (SELECT SomeColumn FROM TableB WHERE F_ID=TableA.ID) FROM TableA   This is a trivial problem if I do the processing on the client side. But this will mean I will have to run X queries on every page, where X is the number of results of TableA.   Note that I can't simply do a GROUP BY or something similar, as it will return multiple results for rows of TableA.   I'm not sure if a UDF, utilizing COALESCE or something similar might work?
119927
How do I page results in SQL Server 2005?  I tried it in SQL Server 2000, but there was no reliable way to do this. I'm now wondering if SQL Server 2005 has any built in method?  What I mean by paging is, for example, if I list users by their username, I want to be able to only return the first 10 records, then the next 10 records and so on.  Any help would be much appreciated.
124140
I'm getting some really weird results from my QUERY() Here is an image with an example:    I'm querying for the name where No Show = "No Show". It find the correct name Name 5 but also returns a concatenated list of names separated by a space.   Why is this happening? How do I fix it?
43671
I have written below inner join query to get the data from my database tables. But  output of this data is coming up with almost 140+ lines. Output is below. I need data in only one row with the most recent value by "TimeSampled"    select DisplayName, ObjectName, CounterName, InstanceName, SampleValue, max(TimeSampled) as TimeSampled from PerformanceDataAllView pdv with (NOLOCK) inner join PerformanceCounterView pcv on pdv.performancesourceinternalid = pcv.performancesourceinternalid inner join BaseManagedEntity bme on pcv.ManagedEntityId = bme.BaseManagedEntityId where ObjectName = 'Processor'  AND TimeSampled &gt;= DATEADD(MINUTE,-5,GETDATE()) AND DisplayName Like 'BLRW1ETST01.jcp.com' AND CounterName = '% Processor Time' GROUP BY DisplayName, ObjectName, CounterName, InstanceName, SampleValue ORDER BY  TimeSampled desc   How I can retrive results with only last Time Sampled row with respect to DisplayName. I have multiple servers (DisplayName) like this to retrieve the data.  Thanks, Ramu Chittiprolu