How can I split a T-SQL Command in
SELECT FROM WHERE GROUP
For example i need split this command
Text To lcSQLCommand TextMerge NoShow Flags 1 Pretext 15
SELECT a.HabitacionID, a.TipoID, b.AlquilerID, b.Desde, b.Hasta, IFNULL(b.Estado, 0) Estado,
c.Habitacion, c.Precio1 Precio
FROM habitaciones a
LEFT JOIN (SELECT HabitacionID, AlquilerID, Desde, Hasta, Estado
FROM alquiler
WHERE (Estado = 2 AND Desde <= CURDATE()) OR Estado = 1
) b ON a.HabitacionID = b.HabitacionID
LEFT JOIN habitaciones_tipo c ON a.TipoID = c.TipoID
EndText
In
lcSQLSelect = "SELECT a.HabitacionID, a.TipoID, b.AlquilerID, b.Desde, b.Hasta, IFNULL(b.Estado, 0) Estado, c.Habitacion, c.Precio1 Precio"
lcSQLFrom = "FROM habitaciones a LEFT JOIN (SELECT HabitacionID, AlquilerID, Desde, Hasta, Estado FROM alquiler WHERE (Estado = 2 AND Desde <= CURDATE()) OR Estado = 1) b ON a.HabitacionID = b.HabitacionID LEFT JOIN habitaciones_tipo c ON a.TipoID = c.TipoID"
lcWhere = "" && No Where in this command
lcGroup = "" && No Group un this command
Best regards
Ze Roberto
Text To lcSQLCommand TextMerge NoShow Flags 1 Pretext 15
SELECT a.HabitacionID, a.TipoID, b.AlquilerID, b.Desde, b.Hasta, IFNULL(b.Estado, 0) Estado,
c.Habitacion, c.Precio1 Precio
FROM habitaciones a
LEFT JOIN (SELECT HabitacionID, AlquilerID, Desde, Hasta, Estado
FROM alquiler
WHERE (Estado = 2 AND Desde <= CURDATE()) OR Estado = 1
) b ON a.HabitacionID = b.HabitacionID
LEFT JOIN habitaciones_tipo c ON a.TipoID = c.TipoID EndText
In
lcSQLSelect = "SELECT a.HabitacionID, a.TipoID, b.AlquilerID, b.Desde, b.Hasta, IFNULL(b.Estado, 0) Estado, c.Habitacion, c.Precio1 Precio"
lcSQLFrom = "FROM habitaciones a LEFT JOIN (SELECT HabitacionID, AlquilerID, Desde, Hasta, Estado FROM alquiler WHERE (Estado = 2 AND Desde <= CURDATE()) OR Estado = 1) b ON a.HabitacionID = b.HabitacionID LEFT JOIN habitaciones_tipo c ON a.TipoID = c.TipoID"
lcWhere = "" && No Where in this command
lcGroup = "" && No Group un this command
Take a look at the STREXTRACT() function. It lets you pull strings apart by specifying beginning and ending delimiters. So, for example, to get the field list, you'd use:
lcFieldList = STREXTRACT(lcSQLCommand, "SELECT", "FROM")
It gets trickier for the rest of what you need because you'll have to check for the presence of the WHERE and GROUP BY keywords to know exactly what delimiters you have, but the whole thing shouldn't be more than about 15 or 20 lines.
Note: I'm assuming you want to do this in VFP.
Related
I have an select:
SELECT
tb_dim_equipe.no_equipe "EQUIPE",
tb_dim_profissional.no_profissional "PROFISSIONAL",
no_cidadao "CIDADÃO",
a.nu_cns "CNS",
sum(case when co_dim_tempo >= 20190100 and co_dim_tempo <= 20200131 then 1 else 0 end) as "TOTAL"
from (
SELECT no_cidadao,
tb_fat_cad_individual.nu_cns,
tb_fat_cad_individual.co_dim_profissional,
tb_fat_cad_individual.co_dim_equipe from tb_fat_cidadao_pec
join tb_fat_cad_individual on tb_fat_cad_individual.nu_cns = tb_fat_cidadao_pec.nu_cns
join tb_fat_cidadao on tb_fat_cad_individual.co_seq_fat_cad_individual = tb_fat_cidadao.co_fat_cad_individual
where st_mudou = 0
and st_vivo = 1
and st_gestante = 1
and tb_fat_cidadao.co_dim_tempo_validade = 30001231) a
left join (
SELECT tb_fat_atendimento_individual.nu_cns,
tb_fat_atendimento_individual.co_dim_tempo
from tb_fat_atendimento_individual
join tb_dim_tempo on tb_dim_tempo.co_seq_dim_tempo = tb_fat_atendimento_individual.co_dim_tempo
where co_seq_dim_tempo >= 20190100
and co_seq_dim_tempo <= 20200131
and ds_filtro_ciaps like '%ABP001%'
union SELECT tb_fat_proced_atend.nu_cns,
tb_fat_proced_atend.co_dim_tempo
from tb_fat_proced_atend
join tb_dim_tempo on tb_dim_tempo.co_seq_dim_tempo = tb_fat_proced_atend.co_dim_tempo
where co_seq_dim_tempo >= 20190100
and co_seq_dim_tempo <= 20200131
and ds_filtro_procedimento like '%0301010110%') b
on a.nu_cns = b.nu_cns
join tb_dim_equipe on tb_dim_equipe.co_seq_dim_equipe = a.co_dim_equipe
join tb_dim_profissional on tb_dim_profissional.co_seq_dim_profissional = a.co_dim_profissional
group by no_equipe, no_profissional, no_cidadao, a.nu_cns
order by no_equipe, no_profissional, no_cidadao
This return to me five columns EQUIPE, PROFISSIONAL, CIDADÃO, CNS and TOTAL
I want to create a new select command like this:
SELECT sum(c.TOTAL) from *query above* c
but I receive the error that column not exists.
How I can use a select with the names of alias columns EQUIPE, PROFISSIONAL, CIDADÃO, CNS and TOTAL?
Just like in the alias, you need to quote the column identifier if it is all-uppercase:
SELECT sum(c."TOTAL") from /*query above*/ c
-- ^ ^
I've been tasked to create a Powershell script that will create the following view which works currently within Microsoft SQL Studio. I've made a copy of the working script and would like to to apply it to this Microsoft example. My question is - can you take the known working query below and insert it in the TextBody property below?
# Set the TextHeader and TextBody property to define the view.
$myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"
$myview.TextBody ="SELECT h.SalesOrderID, d.OrderQty FROM
Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON
h.SalesOrderID = d.SalesOrderID"
CREATE VIEW [dbo].[PROV] AS
SELECT drname.dr_id, drname.create_dt, dr_fname, dr_iname, dr_lname,
drtitle, drsuffix, gender, birthdate, marital, ethnic, drname.email,
org_name, dr_ofname, dr_oiname, dr_olname, nationalid, drname.medicare,
drname.medicaid,
CASE
WHEN EXISTS(SELECT 1 FROM stfstatu act WHERE act.dr_id =
drname.dr_id AND fac_cd NOT IN('SMC','WAR','CCS') AND act.active123 IN
('1','3','N','G','6','S','U','CA','TPRO')) THEN 'Y'
ELSE 'N'
END as Active_Status,
a.sch_type as Addr_Type_sch_type, a.addr, a.addr2, a.city,
a.state, a.zip, a.phone, a.fax, a2.tax_id,
(select top 1 rtrim(d.dr_lname) from drname d join address aa
on d.dr_id = aa.dr_id join address2 aa2 on aa.link = aa2.l_address where
d.verfac = 1 and aa2.tax_id = a2.tax_id and drname.verfac = 0) as
Addr_DrPGp,
lan_tab.txt as lan_tab_txt, dr_lan.lan_cd as dr_lan_cd, CASE
WHEN dr_spec.specorder = 1 THEN dr_spec.specialty ELSE '' END as dr_spec,
CASE WHEN dr_spec.PrimSpec = 1 THEN 'P' ELSE '' END as PrimSpec, school.cd
as school_cd, school.txt as school_txt, educate.fdate as educate_fdate,
educate.sch_type, educ_arr.txt as school_type_txt, educate.degree,
educ_tab.txt
as degree_txt, educate.tdate as educate_tdate, CASE WHEN dr_spec.specorder =
1
THEN spec_tab.txt ELSE '' END as spec_Tab_txt,
CASE WHEN stfstatu.pcp_spec = 'B' then 'Yes' WHEN
stfstatu.pcp_spec = 'P' then 'Yes' ELSE 'No' END as PCP,
case when stfstatu.pcp_spec = 'B' then 'PCP and Specialist'
else case when stfstatu.pcp_spec = 'P' then 'PCP'
else case when stfstatu.pcp_spec = 'S' then
'Specialist'
else ''
END
END
END as PCPSpec, taxonomy.cd as tax_cd
FROM drname
JOIN stfstatu on drname.dr_id = stfstatu.dr_id
LEFT JOIN address1 a on drname.dr_id = a.dr_id AND a.sch_type = 'G' AND
a.link = (SELECT TOP 1 link FROM address1 WHERE sch_type = 'G' AND
address1.dr_id = drname.dr_id ORDER BY LINK DESC)
LEFT JOIN address2 a2 on a2.l_address = a.link
LEFT JOIN dr_lan on drname.dr_id = dr_lan.dr_id
LEFT JOIN lan_tab on lan_tab.cd = dr_lan.lan_cd
LEFT JOIN dr_spec on drname.dr_id = dr_spec.dr_id
LEFT JOIN spec_tab on dr_spec.specialty = spec_tab.cd
LEFT JOIN educate on educate.dr_id = drname.dr_id
LEFT JOIN school on educate.cd = school.cd
LEFT JOIN educ_arr on educ_arr.cd = educate.sch_type
LEFT JOIN facility on facility.fac_cd = educate.fac_cd
LEFT JOIN educ_tab on educate.degree = educ_tab.cd
LEFT JOIN taxonomy on taxonomy.dr_id = drname.dr_id
--Where LTRIM(RTRIM(dr_spec.Fac_Cd)) = ''
did you try using the here string (https://www.safaribooksonline.com/library/view/windows-powershell-cookbook/9780596528492/ch05s03.html]) for TextBody?
# Set the TextHeader and TextBody property to define the view.
$myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"
$myview.TextBody = #"
SELECT h.SalesOrderID, d.OrderQty FROM
Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON
h.SalesOrderID = d.SalesOrderID
CREATE VIEW [dbo].[PROV] AS
SELECT drname.dr_id, drname.create_dt, dr_fname, dr_iname, dr_lname,
drtitle, drsuffix, gender, birthdate, marital, ethnic, drname.email,
org_name, dr_ofname, dr_oiname, dr_olname, nationalid, drname.medicare,
drname.medicaid,
CASE
WHEN EXISTS(SELECT 1 FROM stfstatu act WHERE act.dr_id =
drname.dr_id AND fac_cd NOT IN('SMC','WAR','CCS') AND act.active123 IN
('1','3','N','G','6','S','U','CA','TPRO')) THEN 'Y'
ELSE 'N'
END as Active_Status,
a.sch_type as Addr_Type_sch_type, a.addr, a.addr2, a.city,
a.state, a.zip, a.phone, a.fax, a2.tax_id,
(select top 1 rtrim(d.dr_lname) from drname d join address aa
on d.dr_id = aa.dr_id join address2 aa2 on aa.link = aa2.l_address where
d.verfac = 1 and aa2.tax_id = a2.tax_id and drname.verfac = 0) as
Addr_DrPGp,
lan_tab.txt as lan_tab_txt, dr_lan.lan_cd as dr_lan_cd, CASE
WHEN dr_spec.specorder = 1 THEN dr_spec.specialty ELSE '' END as dr_spec,
CASE WHEN dr_spec.PrimSpec = 1 THEN 'P' ELSE '' END as PrimSpec, school.cd
as school_cd, school.txt as school_txt, educate.fdate as educate_fdate,
educate.sch_type, educ_arr.txt as school_type_txt, educate.degree,
educ_tab.txt
as degree_txt, educate.tdate as educate_tdate, CASE WHEN dr_spec.specorder =
1
THEN spec_tab.txt ELSE '' END as spec_Tab_txt,
CASE WHEN stfstatu.pcp_spec = 'B' then 'Yes' WHEN
stfstatu.pcp_spec = 'P' then 'Yes' ELSE 'No' END as PCP,
case when stfstatu.pcp_spec = 'B' then 'PCP and Specialist'
else case when stfstatu.pcp_spec = 'P' then 'PCP'
else case when stfstatu.pcp_spec = 'S' then
'Specialist'
else ''
END
END
END as PCPSpec, taxonomy.cd as tax_cd
FROM drname
JOIN stfstatu on drname.dr_id = stfstatu.dr_id
LEFT JOIN address1 a on drname.dr_id = a.dr_id AND a.sch_type = 'G' AND
a.link = (SELECT TOP 1 link FROM address1 WHERE sch_type = 'G' AND
address1.dr_id = drname.dr_id ORDER BY LINK DESC)
LEFT JOIN address2 a2 on a2.l_address = a.link
LEFT JOIN dr_lan on drname.dr_id = dr_lan.dr_id
LEFT JOIN lan_tab on lan_tab.cd = dr_lan.lan_cd
LEFT JOIN dr_spec on drname.dr_id = dr_spec.dr_id
LEFT JOIN spec_tab on dr_spec.specialty = spec_tab.cd
LEFT JOIN educate on educate.dr_id = drname.dr_id
LEFT JOIN school on educate.cd = school.cd
LEFT JOIN educ_arr on educ_arr.cd = educate.sch_type
LEFT JOIN facility on facility.fac_cd = educate.fac_cd
LEFT JOIN educ_tab on educate.degree = educ_tab.cd
LEFT JOIN taxonomy on taxonomy.dr_id = drname.dr_id
--Where LTRIM(RTRIM(dr_spec.Fac_Cd)) = ''
"#
I'm very new to coding but I am trying to work out how I can (or if I can) concat the code below ideally excluding any null returns separated with commas.
So an example would be:-
Germany, Latvia, Norway
Great Britain, France, Latvia
Any help and explanations would be greatly appreciated.
Select
*,
CASE
WHEN PT_ID1 != 0 THEN 'Germany'
WHEN PT_ID2 != 0 THEN 'Great Britain'
WHEN PT_ID3 != 0 THEN 'France'
WHEN PT_ID4 != 0 THEN 'Latvia'
WHEN PT_ID5 != 0 THEN 'Norway'
END AS Country
FROM CTE2 C1
FULL JOIN CTE C ON C.PT_ID1 = C1.PT_ID2
FULL JOIN CTE3 D ON D.PT_ID3 = C1.PT_ID2
FULL JOIN CTE4 E ON E.PT_ID4 = C1.PT_ID2
FULL JOIN CTE5 F ON F.PT_ID5 = C1.PT_ID2
order by MRN asc
Use different CASE statements for each country
Select
*,
STUFF(CASE WHEN PT_ID1 != 0 THEN ', Germany' ELSE '' END
+ CASE WHEN PT_ID2 != 0 THEN ', Great Britain' ELSE '' END
+ CASE WHEN PT_ID3 != 0 THEN ', France' ELSE '' END
+ CASE WHEN PT_ID4 != 0 THEN ', Latvia' ELSE '' END
+ CASE WHEN PT_ID5 != 0 THEN ', Norway' ELSE '' END, 1, 2, '') AS Country
FROM CTE2 C1
FULL JOIN CTE C ON C.PT_ID1 = C1.PT_ID2
FULL JOIN CTE3 D ON D.PT_ID3 = C1.PT_ID2
FULL JOIN CTE4 E ON E.PT_ID4 = C1.PT_ID2
FULL JOIN CTE5 F ON F.PT_ID5 = C1.PT_ID2
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
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....