Not all items in Order by are in Select - tsql

SELECT DISTINCT f.FoodNumber,
f.FoodID,
fn.Name AS FoodName,
d.[Description],
substring(n.Note, CHARINDEX(']', n.Note) + 2,
LEN(n.Note)) AS FoodNote
FROM Food f
JOIN FoodName fn
ON fn.FoodNameID = f.FoodNameID
JOIN FoodPart fp
ON fp.FoodID = p.FoodID
JOIN [Application] a
ON a.ApplicationID = fn.ApplicationID
LEFT JOIN [Description] d
ON d.DescriptionID = ap.DescriptionID
JOIN Note n
ON n.NoteID = a.NoteID
JOIN FoodYear fy
ON fy.FoodYearID = a.FoodYearID
WHERE mmy.FoodlId = 33997332
ORDER BY CASE
WHEN substring(n.Note, 1, 1) = 'A' THEN 1
WHEN substring(n.Note, 1, 1) = 'Y' THEN 2
WHEN substring(n.Note, 1, 1) = 'D ' THEN 3
END,
f.FoodNumber,
f.FoodID,
fn.Name,
d.[description],
substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))
I keep getting an error saying not all the items in the order by are in the select list.
UPDATE
This works but I'm getting dup part numbers listed...I can't add distinct or it will complain that I don't have something in the select list for the order by
select ap.applicationID,
ap.NoteID,
f.FoodNumber,
n.Note as PartNote,
f.FoodID,
q.Quantity,
fn.Name as FoodName,
d.[Description]
from Food f
join FoodName fn on fn.FoodNameID = f.FoodNameID
join FoodPart fp on fp.partID = f.FoodID
join Quantity q on q.QuantityID = ap.QuantityID
join [Application] a on a.ApplicationID = ap.ApplicationID
left join [Description] d on d.DescriptionID = ap.DescriptionID
join Note n on n.NoteID = ap.NoteID
join Note n2 on n2.NoteID = a.NoteID
join FoodYear fy on fy.FoodYearID = a.FoodYearID
join Model mo on mo.ModelID = fy.ModelID
where fy.ModelId = #ModelId
order by
case when substring(f.FoodNumber, 1, 1) = 'T' then 1
when substring(f.FoodNumber, 1, 1) = 'R' then 2
when substring(f.FoodNumber, 1, 1) = 'C' then 3
else
substring(f.FoodNumber, 1, 1)
END,
f.FoodNumber asc,
f.FoodID,
fn.Name ,
d.[description],
substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))

You need to add SUBSTRING(n.Note, 1, 1) to the SELECT list or remove the DISTINCT.
You currently are doing DISTINCT on SUBSTRING(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note)) AS FoodNote but there can be multiple possible SUBSTRING(n.Note, 1, 1) values that all map to the same value for that.
WITH n(Note) AS
(
SELECT '' UNION ALL
SELECT 'A' UNION ALL
SELECT ']'
)
SELECT SUBSTRING(n.Note, CHARINDEX(']', n.Note) + 2,
LEN(n.Note)) AS FoodNote
,SUBSTRING(n.Note, 1, 1) AS SS
FROM n
Returns
FoodNote SS
-------- ----
A
]
You can also use GROUP BY instead of DISTINCT and ORDER BY MIN(SUBSTRING(n.Note, 1, 1)) so the expression becomes unambiguous in that event and is allowed.
i.e. something like
;WITH CTE AS
(
SELECT
f.FoodNumber,
f.FoodID,
fn.Name AS FoodName,
d.[Description],
substring(n.Note, CHARINDEX(']', n.Note) + 2,
LEN(n.Note)) AS FoodNote,
substring(n.Note, 1, 1) AS N1
FROM Food f
JOIN FoodName fn
ON fn.FoodNameID = f.FoodNameID
JOIN FoodPart fp
ON fp.FoodID = p.FoodID
JOIN [Application] a
ON a.ApplicationID = fn.ApplicationID
LEFT JOIN [Description] d
ON d.DescriptionID = ap.DescriptionID
JOIN Note n
ON n.NoteID = a.NoteID
JOIN FoodYear fy
ON fy.FoodYearID = a.FoodYearID
WHERE mmy.FoodlId = 33997332
)
SELECT FoodNumber,
FoodID,
FoodName,
[Description],
FoodNote
FROM CTE
GROUP BY FoodNumber,
FoodID,
FoodName,
[Description],
FoodNote
ORDER BY CASE
WHEN MIN(N1) = 'A' THEN 1
WHEN MIN(N1) = 'Y' THEN 2
WHEN MIN(N1) = 'D ' THEN 3
END,
FoodNumber,
FoodID,
FoodName,
[Description],
FoodNote

perhaps add
case when when substring(n.Note, 1, 1) = 'A' then 1
when substring(n.Note, 1, 1) = 'Y' then 2
when substring(n.Note, 1, 1) = 'D ' then 3
END,
to your select?
Though I dind't think an order by required something to be in the select.
Also: you don't have a else in the case for if note is not A Y or D.

Related

Convert multiple rows to one row

select
datum, naam,
to_char(verkocht::numeric/(verkocht+beschikbaar)*100, 'fm00D00%') rangen
from
(select
sr.namenl rang,
count((case when t.currentstatus is null then '1' end)) as beschikbaar,
count((case when t.currentstatus = 101 then '2' end)) as verkocht,
coalesce(e.namenl) || ' ' || coalesce(e.subtitlenl,'') naam,
e.startts datum,
sr.id seatrank
from
tm.ticket t
inner join
tm.tickettype tt on tt.id = t.tickettypeid
inner join
tm.event e on e.id = tt.eventid
inner join
tm.seatrank sr on sr.id = tt.seatrankid
where
e.id = 28744
group by
1, 4, 5, 6
order by
1 desc) sq1
group by
1, 2, 3
Current result (https://i.stack.imgur.com/1KPOi.png)
What I would like is to convert the 4 row result into 1 row. The 4 different outcomes in row "rangen" next to each other so that I have 1 row with 6 columns. Don't know if this is possible and I am fairly new to this as you can see :)
You can use string_agg function. For example, like as this query:
select
datum, naam,
string_agg(rangen, ', ') as rangen
from table
group by
datum, naam

Is there a way to merge these json aggregations?

I am trying to create json object from getting some info from one table, then creating interger arrays from some other tables' id's and adding n > 1 (2 or more) arrays to the json object. I am using Postgres version 10.7:
select json_build_object(
'id', bi.id,
'street', ba.street,
'features1', features1.f1_json_arr,
'features2', features2.f2_json_arr
)
from business.info bi
inner join business.address ba on bi.id = ba.location_id
left outer join (
select f1.location_id,
json_agg(f1_id) as f1_json_arr
from business.features1 as f1
group by f1.location_id
) features1 on bi.id = features1.location_id
left outer join (
select f2.location_id,
json_agg(f2_id) as f2_json_arr
from business.feature2 as f2
group by f2.location_id
) features2 on bi.id = features2.location_id
where bi.id='1234';
which gives me a result as I want, like so:
{
"id": "1234",
"street", "some street",
"features1": [
2,
1
],
"features2": [
3,
2,
1
]
}
Is there a cleaner way to do this? I tried this:
select json_build_object(
'id', bi.id,
'street', ba.street_name,
'features1', f1_and_f2.f1_json_arr,
'features2', f1_and_f2.f2_json_arr
)
from business.info bi
inner join business.address ba
on bi.id = ba.location_id
left outer join (
select f1.location_id,
json_agg(f1_id) as f1_json_arr,
json_agg(f2_id) as f2_json_arr
from business.feature1 as f1
inner join business.feature2 as f2 on f1.location_id = f2.location_id
group by f1.location_id
) f1_and_f2 on bi.id = f1_and_f2.location_id
where bi.id = '1234';
but got a result like this:
{
"id": "1234",
"street_name": "a street",
"features1": [
2,
2,
2,
1,
1,
1
],
"features2": [
3,
2,
1,
3,
2,
1
]
}
SELECT A.*, B.*, C_GROUPED.C_STUFF, D_GROUPED.D_STUFF
FROM A
INNER JOIN B ON B.A_ID = A.ID
LEFT JOIN ( SELECT A_ID, JSON_AGG(STUFF) AS C_STUFF FROM C GROUP BY A_ID ) AS C_GROUPED ON C_GROUPED.A_ID = A.ID
LEFT JOIN ( SELECT A_ID, JSON_AGG(OTHER_STUFF) AS D_STUFF FROM D GROUP BY A_ID ) AS D_GROUPED ON D_GROUPED.A_ID = A.ID
WHERE A.ID = 123;
should return the same result as
SELECT
A.*,
B.*,
( SELECT JSON_AGG(STUFF) FROM C WHERE C.A_ID = A.ID ) AS C_STUFF,
( SELECT JSON_AGG(OTHER_STUFF) FROM D WHERE D.A_ID = A.ID ) AS D_STUFF
FROM A
INNER JOIN B ON B.A_ID = A.ID
WHERE A.ID = 123
In fact, I would expect the second query be faster.
Ps - Since LEFT JOIN and LEFT OUTER JOIN are the same, I would suggest writing them in the same way in your query.

Select Query result not come when any one select query not execute or result empty?

I've written four SELECT queries to get quantities grouped by napkin type. The "b" and "c" query is NULL when cannot diverted or income the quantity. So we are not able to get the napkin type.
It's the problem of a.napkin_type=b.napkin_type and a.napkin_type=c.napkin_type.
WITH a AS (SELECT
COALESCE(SUM(quantity), 0) AS "receipt",
napkin_type
FROM hsc_receipt_entry
WHERE dvn_cd = '15' AND phc_cd = '012' AND hsc_cd = '05' AND trans_type = '1'
GROUP BY napkin_type),
b AS (SELECT
COALESCE(SUM(quantity), 0) AS "diverted_income",
napkin_type
FROM hsc_receipt_entry
WHERE dvn_cd = '15' AND phc_cd = '012' AND to_dvn = '05' AND trans_type = '2'
GROUP BY napkin_type),
c AS (SELECT
COALESCE(SUM(quantity), 0) AS "diverted_out",
napkin_type
FROM "hsc_receipt_entry"
WHERE "dvn_cd" = '15' AND "phc_cd" = '012' AND "from_dvn" = '05' AND trans_type = '2'
GROUP BY napkin_type),
d AS (SELECT
COALESCE(SUM(slsc_qty + non_slsc_qty + six_sc_qty + sev_sc_qty + hom_sc_qty + slst_qty + non_slst_qty +
six_st_qty + sev_st_qty + hom_st_qty + slot_qty + non_slot_qty + six_ot_qty + sev_ot_qty +
hom_ot_qty), 0) AS issue,
napkin_type
FROM vhn_issue
WHERE dvn_cd = '15' AND phc_cd = '012' AND hsc_cd = '05'
GROUP BY napkin_type)
SELECT *
FROM a, b, c, d
WHERE a.napkin_type = b.napkin_type AND a.napkin_type = c.napkin_type AND a.napkin_type = d.napkin_type;
If I undestand your problem correctly I belive you should change your final query to use LEFT OUTER JOINS instead of INNER JOINS (as you did). So in place of
SELECT *
FROM a, b, c, d
WHERE a.napkin_type = b.napkin_type
AND a.napkin_type = c.napkin_type
AND a.napkin_type = d.napkin_type;
use:
SELECT
a."receipt", a.napkin_type,
COALESCE(b."diverted_income", 0), a.napkin_type,
COALESCE(c."diverted_out", 0), a.napkin_type,
COALESCE(d.issue, 0), a.napkin_type,
FROM a
LEFT JOIN b ON a.napkin_type = b.napkin_type
LEFT JOIN c ON a.napkin_type = c.napkin_type
LEFT JOIN d ON a.napkin_type = d.napkin_type
;
Notice I've used a.napkin_type everywhere it's intentional since you join everything to table named a, this approach removes NULLs from napkin_type output.

set value from select for few select

I have select, iside select have 2 column. This column must be filled from same select, but I don't want use select twice for it. Is it possoble use select 1 time and after that set second column value from first?
Example:
insert into #temptable from
select
a = (select aa from table1 where quantity > 5)
b = (select aa from table1 where quantity > 5)
I need:
insert into #temptable from
select
a = (select aa from table1 where quantity > 5)
b = {value from a}
Update. I wrote bad example, I need set to BalancePrediction1 and BalancePrediction2 value from Balance
INSERT #tmpBalances
SELECT PA.ContractId AS 'ContractId',
Con.Name AS 'ContractName',
Bal.PortfolioAccountId AS 'PortfolioAccountId',
PA.Name AS 'PortfolioAccountName',
RA.GeneralId AS 'RegisterAccountGeneralId',
Bal.BalanceTypeId AS 'BalanceTypeId',
Bt.Name AS 'BalanceTypeName',
Bt.Type AS 'BalanceTypeType',
Bal.BalanceTimeType AS 'BalanceTimeType',
Bal.InstrumentId AS 'InstrumentId',
Ins.Name AS 'InstrumentName',
Ins.GeneralId AS 'InstrumentGeneralId',
(Bal.Balance -
(
SELECT COALESCE(SUM(Mov.Amount), 0)
FROM trd.Movements AS Mov
WHERE
Bal.InstrumentId = Mov.InstrumentId AND
Bal.PortfolioAccountId = Mov.PortfolioAccountId AND
Bal.BalanceTypeId = Mov.BalanceTypeId AND
Bal.BalanceTimeType = Mov.BalanceTimeType AND
DateDiff(DAY, #Date, Mov.Date) > 0 AND
-- Currency může být null a NULL = NULL nejde
COALESCE(Bal.CurrencyId, -1) = COALESCE(Mov.CurrencyId, -1)
)
) as Balance,
Balance AS 'BalancePrediction1',
Balance AS 'BalancePrediction2',
Bal.CurrencyId AS 'CurrencyId',
Ccy.Code AS 'CurrencyCode',
PA.PositionServiceType 'PositionServiceType',
Ccy.Name 'CurrencyName',
S.Nominal AS 'Nominal',
S.NominalCurrencyId AS 'NominalCurrencyId',
trd.GetCurrencyCode(S.NominalCurrencyId) AS 'NominalCurrencyCode'
FROM trd.Balances AS Bal
JOIN trd.PortfolioAccounts AS PA ON PA.Id = Bal.PortfolioAccountId
JOIN trd.Contracts AS Con ON Con.Id = PA.ContractId
JOIN trd.RegisterAccounts AS RA ON RA.Id = PA.RegisterAccountId
JOIN trd.BalanceTypes AS Bt ON Bt.Id = Bal.BalanceTypeId
JOIN trd.Instruments AS Ins ON Ins.Id = Bal.InstrumentId
LEFT OUTER JOIN trd.Currencies AS Ccy ON Ccy.Id = Bal.CurrencyId
LEFT JOIN trd.SecuritiesView S ON s.Id = Ins.Id AND DateDiff(d, S.ValidFrom, #Date) >= 0 AND (S.ValidTo IS NULL OR DateDiff(d, S.ValidTo, #Date) < 0)
AND S.InstrumentInstrumentTypePriceUnit = 1
You could do an update to the table variable after the insert.
update #tmpBalances
set BalancePrediction1 = Balance,
BalancePrediction2 = Balance
Or you can use cross apply to calculate the sum.
INSERT #tmpBalances
SELECT PA.ContractId AS 'ContractId',
Con.Name AS 'ContractName',
Bal.PortfolioAccountId AS 'PortfolioAccountId',
PA.Name AS 'PortfolioAccountName',
RA.GeneralId AS 'RegisterAccountGeneralId',
Bal.BalanceTypeId AS 'BalanceTypeId',
Bt.Name AS 'BalanceTypeName',
Bt.Type AS 'BalanceTypeType',
Bal.BalanceTimeType AS 'BalanceTimeType',
Bal.InstrumentId AS 'InstrumentId',
Ins.Name AS 'InstrumentName',
Ins.GeneralId AS 'InstrumentGeneralId',
(Bal.Balance - Mov.SumAmount) AS Balance,
(Bal.Balance - Mov.SumAmount) AS 'BalancePrediction1',
(Bal.Balance - Mov.SumAmount) AS 'BalancePrediction2',
Bal.CurrencyId AS 'CurrencyId',
Ccy.Code AS 'CurrencyCode',
PA.PositionServiceType 'PositionServiceType',
Ccy.Name 'CurrencyName',
S.Nominal AS 'Nominal',
S.NominalCurrencyId AS 'NominalCurrencyId',
trd.GetCurrencyCode(S.NominalCurrencyId) AS 'NominalCurrencyCode'
FROM trd.Balances AS Bal
JOIN trd.PortfolioAccounts AS PA ON PA.Id = Bal.PortfolioAccountId
JOIN trd.Contracts AS Con ON Con.Id = PA.ContractId
JOIN trd.RegisterAccounts AS RA ON RA.Id = PA.RegisterAccountId
JOIN trd.BalanceTypes AS Bt ON Bt.Id = Bal.BalanceTypeId
JOIN trd.Instruments AS Ins ON Ins.Id = Bal.InstrumentId
LEFT OUTER JOIN trd.Currencies AS Ccy ON Ccy.Id = Bal.CurrencyId
LEFT JOIN trd.SecuritiesView S ON s.Id = Ins.Id AND DateDiff(d, S.ValidFrom, #Date) >= 0 AND (S.ValidTo IS NULL OR DateDiff(d, S.ValidTo, #Date) < 0)
AND S.InstrumentInstrumentTypePriceUnit = 1
CROSS APPLY (SELECT COALESCE(SUM(Mov.Amount), 0)
FROM trd.Movements AS Mov
WHERE
Bal.InstrumentId = Mov.InstrumentId AND
Bal.PortfolioAccountId = Mov.PortfolioAccountId AND
Bal.BalanceTypeId = Mov.BalanceTypeId AND
Bal.BalanceTimeType = Mov.BalanceTimeType AND
DateDiff(DAY, #Date, Mov.Date) > 0 AND
-- Currency může být null a NULL = NULL nejde
COALESCE(Bal.CurrencyId, -1) = COALESCE(Mov.CurrencyId, -1)
) Mov(SumAmount)
SELECT aa AS a, aa AS b
FROM table1
WHERE quantity > 5
One way;
;with T (value) as (
select aa from table1 where quantity > 5
)
insert into #temptable
select value, value from T

T-SQL Nested Subquery

I want to place this working code within a SQL Statement, OR do I need to perform a UDF.
The result set is a one line concatenation, and I want it to be place in every one of the overall result set lines.
----
MAIN QUERY
SELECT
H.CONNECTION_ID,
H.SEQUENTIAL_NO,
H.INVOICE_NUMBER,
H.INVOICE_DATE,
H.LAST_INVOICE_NUMBER,
H.LAST_INVOICE_DATE,
CAST(CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN '-'
ELSE CAST(H.PAYMENT_DUE_DATE AS NVARCHAR(20))
END AS SMALLDATETIME) AS PAYMENT_DUE,
H.JOB_NUMBER,
H.CUST_JOB_NUMBER,
HDR.SALES_PERSON,
H.INSIDE_SALES_PERSON,
H.IS_LAST_INVOICE,
CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN 'CASH'
ELSE H.PAYMENT_TERMS_DESCRIPTION
END AS PAYMENT_TERMS,
H.PRINTED,
H.NOTES,
CUR.ID,
CUR.CODE,
CASE CUR.CODE
WHEN 'USD' THEN '001-106624-211'
WHEN 'EUR' THEN '001-106624-101'
WHEN 'GBP' THEN '001-106624-100'
ELSE '001-106624-001'
END AS BANK_ACCT,
CUR.EXCHANGE_RATE,
H.BILL_CONTACT,
H.CUST_ACCOUNT,
H.CUST_NAME,
H.CUST_ADDR1,
H.CUST_ADDR2,
H.CUST_CITY,
H.CUST_STATE,
H.CUST_ZIP,
H.CONTACT_PHONE_NUMBER,
H.CONTACT_PHONE_NUMBER2,
H.ORDERED_BY_CONTACT,
H.SHIP_TO_NAME,
H.SHIP_TO_ADDR1,
H.SHIP_TO_ADDR2,
H.SHIP_TO_CITY,
H.SHIP_TO_STATE,
H.SHIP_TO_ZIP,
H.SITE_PHONE_NUMBER,
H.SITE_PHONE_NUMBER2,
H.OFFICE_NAME,
H.OFFICE_ADDR1,
H.OFFICE_ADDR2,
H.OFFICE_CITY,
H.OFFICE_STATE,
H.OFFICE_ZIP,
H.OFFICE_PHONE_NUMBER,
H.OFFICE_FAX_NUMBER,
H.DELIVERY_TICKET_NUMBER,
H.PO_NUMBER,
H.DUMMY_INVOICE_TEXT,
(SELECT MESSAGE FROM REPORT_MESSAGES WHERE CODE = 'INVOICE') ADVERT_MESSAGE,
(SELECT MAX(DISCOUNT_PERCENTAGE) FROM PRTINVITEM I2 WHERE I2.CONNECTION_ID = H.CONNECTION_ID AND I2.INVOICE_NUMBER = H.INVOICE_NUMBER) AS MAX_DISCOUNT,
I.ITEM,
I.DESCRIPTION,
I.QUANTITY,
I.UNIT_OF_MEASURE,
I.MINIMUM_CHARGE,
I.WEEKLY_CHARGE,
I.MONTHLY_CHARGE,
I.START_OF_BILLING_PERIOD,
I.END_OF_BILLING_PERIOD,
I.DAYS_USED,
I.WEEKS_USED,
I.DISCOUNT_PERCENTAGE,
I.TAX_CODE_FOR_ITEM,
I.INVENTORY_TYPE,
I.BILLING_LOGIC_TYPE,
I.ACTUAL_WEEKLY_CHARGE_USED,
I.DAYS_IN_ACTUAL_WEEKLY_CHARGE,
II.CHARGEABLE_DAYS,
II.CHARGEABLE_WEEKS,
II.CHARGEABLE_MONTHS,
II.FREE_DAYS_THIS_INVOICE,
CNV.TOTAL_NET_VALUE,
CNV.TOTAL_TAX_VALUE,
CNV.TOTAL_GROSS_VALUE,
CNV.TOTAL_GROSS_VALUE_NS,
CNV.NET_LINE_VALUE,
CMP.EMAIL_ADDRESS
FROM (PRTINVHDR H INNER JOIN PRTINVITEM I ON H.CONNECTION_ID = I.CONNECTION_ID AND H.INVOICE_NUMBER = I.INVOICE_NUMBER)
INNER JOIN INVOICEHDR HDR ON I.INVOICE_NUMBER = HDR.INVNO
INNER JOIN CUSTOMERS CST ON H.CUST_ACCOUNT = CST.CUSTNUM
INNER JOIN JOB JOB ON H.JOB_NUMBER = JOB.JOBNUM
INNER JOIN CURRENCY CUR ON HDR.CURRENCY_ID = CUR.ID
INNER JOIN VWCURRENCYCONVERSION CNV ON I.CONNECTION_ID = CNV.CONNECTION_ID AND I.INVC_UCOUNTER = CNV.INVC_UCOUNTER
INNER JOIN COMPANY CMP ON H.OFFICE_CODE = CMP.OFFICE
INNER JOIN INVOICEITEM II ON I.INVOICE_NUMBER = II.INVNO AND I.INVC_UCOUNTER = II.INVC_UCOUNTER
ORDER BY
H.SEQUENTIAL_NO,
I.PRINT_SEQUENCE
ASC
----
COALESCE QUERY
DECLARE
#DTICKET NVARCHAR(20),
#PUMPCATEGORYNAME NVARCHAR(3999)
SET #DTICKET = ''
SET #PUMPCATEGORYNAME = NULL
(SELECT
#DTICKET = DTICKET,
#PUMPCATEGORYNAME = COALESCE(#PUMPCATEGORYNAME + ', ', '' ) + PUMPCATEGORYNAME
FROM (SELECT
BHDR.DTICKET,
SCD.PUMPCATEGORYNAME
FROM PRTTICKHDR PHDR
INNER JOIN BIDHDR BHDR ON PHDR.DELIV_TICKET_NUMBER = BHDR.DTICKET
INNER JOIN PRTTICKITEM PITM ON PHDR.CONNECTION_ID = PITM.CONNECTION_ID AND PHDR.DELIV_TICKET_NUMBER = PITM.DELIV_TICKET_NUMBER
LEFT JOIN SUBCATEGORYDESCRIPTION SCD ON PITM.ITEM = SCD.PUMPCATEGORY
WHERE SCD.PUMPCATEGORYNAME IS NOT NULL)
SUBCATEGORYDESCRIPTION)
SELECT #DTICKET, #PUMPCATEGORYNAME
Not really sure what you are asking for but you can doing something along the lines of
Select col1 + ', ' + col2 + ', ' + col3 etc....