question

excelsql987 avatar image
excelsql987 asked

query from a very complex existed query (goal : to minimize script)

Can I select few fields from an existed complex query (maybe by saving the complex query with a short name) my goal; I don't want to reflect so much script

sql-server-2012
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jon Crawford avatar image
Jon Crawford answered

I think we need more info, are you trying to re-use an existing script but you don't need to use everything it returns? Or are you using a stored procedure/function?

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jon Crawford avatar image Jon Crawford commented ·

an example if you can swing it, would be very helpful here

0 Likes 0 ·
excelsql987 avatar image
excelsql987 answered
---Below is the MAIN query (join 3-4 different tables)
---Out of this MAIN query I am trying to pull very few fields
---so for example; Select 'CoCode'  FROM  MAIN query

SELECT be.[FY],be.[SAP Doc Number],
CONVERT(NVARCHAR(10),be.[Due Date],101) as 'Due Date',
CONVERT(nvarchar(10),ft.[EndDate],101) as 'End Date', 
DATEDIFF (DAY, ft.[EndDate],be.[Due Date])as Aged,
be.[SAP Customer ID],
be.[SAP Customer Name],mpc.[New_Div],mpc.[BU],mpc.[Company FY15] as 'CoCode', 
sum(be.[Invoice Amount]) 'Total Amount',
sum(be.[Current]) as 'Current',
sum(be.[Aged 1-30]) as '1-30',
sum(be.[Aged 31-60]) as '31-60',
sum(be.[Aged 61-90]) as '61-90',
sum (be.[Aged 91-120]) as '91-120',
sum (be.[Over 120]) as 'Over 120',
ISNULL(mrc.[Long Text],'') as 'RC category',ISNULL(mrc.Category,'') as 'Category'
FROM ([dbo].[FiscalDateTable] as ft
INNER JOIN ([dbo].[TB_DATA_All] as be
LEFT JOIN [dbo].[Master Reason Codes List] AS mrc
ON be.[Reason Code] = mrc.[Reason Code])
ON ft.[Fiscal YYYY-MM] = be.FY)
INNER JOIN [dbo].[Master Profit Center List_All Divisions_New] as mpc
ON be.[Profit Center] = mpc.[Profit Centers]
AND be.CoCd = mpc.[Company FY15]
WHERE (be.[Reconciliation Account]) LIKE'%24%' 
GROUP BY be.[FY], be.[SAP Doc Number],be.[Due Date],ft.EndDate,be.[SAP Customer ID],
be.[SAP Customer Name],mpc.New_Div,mpc.BU,mpc.[Company FY15],
mrc.[Long Text],mrc.Category
HAVING mpc.New_Div ='DI'
ORDER BY [SAP Doc Number]
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

excelsql987 avatar image
excelsql987 answered
---I created a SP but have no idea how to pull a field from the SP

USE [CCS]
GO
DECLARE@return_value int
EXEC@return_value = [dbo].[spMAIN]
SELECT'Return Value' = @return_value
GO
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.