I have a code in the stored procedure and it needs to join on a comma-deli string column. I tried but it is failing to recognized by sql server. Please let me know the right approach for it. Here is the code from the sproc:
INSERT INTO CPE_EXT_Downloaded (FileName, LocalServerID, WaitingACK, CreationDate, FileSize, MD5, ErrorMsgTime, LocationID, OfferID)
SELECT
cip.FileName, LS.LocalServerID, cip.WaitingACK, cip.CreationDate,
cip.FileSize, cip.MD5, cip.ErrorMsgTime, ILV.LocationID, cip.OfferID
FROM
#WorkSet ws
INNER JOIN
CPE_IncentiveDLBuffer_Pending cip WITH (NOLOCK) ON cip.POID = ws.POID
INNER JOIN
CPE_IncentiveLocationsView AS ILV WITH (NOLOCK) ON ILV.IncentiveID = cip.OfferID
INNER JOIN
LocalServers AS LS WITH (NOLOCK) ON LS.LocationID = ILV.LocationID
CROSS APPLY on
dbo.split(cip.LocationGroups, ',') AS ST ON st.items = ILV.LocationID
WHERE
LS.MustIPL = 0
Have you considered doing a regular join instead?
FROM #WorkSet ws
INNER JOIN CPE_IncentiveDLBuffer_Pending cip WITH (NOLOCK)
on cip.POID = ws.POID
inner join CPE_IncentiveLocationsView as ILV WITH (NOLOCK)
on ILV.IncentiveID= cip.OfferID and
','+cip.LocationGroups+',' like '%,'+ILV.LocationId+',%'
Inner Join LocalServers as LS with (NoLock) on LS.LocationID=ILV.LocationID
Related
I am writing an equivalent logic in BQ as in my source system. In source SQL server side it is working fine. But in Big query it is failing with the OR condition in the last left outer join condition. If I am moving the OR condition in the where clause it is giving wrong count. Need help to fix this issue. How can I re write the below query ?
SELECT count(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON (DIM.diSet = PRO.diSet)
INNER JOIN DQConfig CFG
ON (CFG.ConSet = PRO.ConSet)
LEFT OUTER JOIN AgSt CCT
ON (CCT.StSet = PRO.StSet)
INNER JOIN stprof SummPRO
ON (SummPRO.diSet = DIM.SummdiSet AND
SummPRO.dIntervalStart = PRO.dIntervalStart AND
SummPRO.SiteId = PRO.SiteId AND
SummPRO.nDuration = PRO.nDuration)
LEFT OUTER JOIN AgSt SummCCT
ON (SummCCT.StSet = SummPRO.StSet)
LEFT OUTER JOIN AgentStatus SummSTS
ON (
SummSTS.StSet = SummPRO.StSet
OR
SummSTS.StSet = PRO.StSet)
WHERE DIM.cType = 'A'
You can replace LEFT JOIN with CROSS JOIN and move condition from ON clause to WHERE clause as in below example
#standardSQL
SELECT COUNT(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON DIM.diSet = PRO.diSet
INNER JOIN DQConfig CFG
ON CFG.ConSet = PRO.ConSet
LEFT OUTER JOIN AgSt CCT
ON CCT.StSet = PRO.StSet
INNER JOIN stprof SummPRO
ON SummPRO.diSet = DIM.SummdiSet
AND SummPRO.dIntervalStart = PRO.dIntervalStart
AND SummPRO.SiteId = PRO.SiteId
AND SummPRO.nDuration = PRO.nDuration
LEFT OUTER JOIN AgSt SummCCT
ON SummCCT.StSet = SummPRO.StSet
CROSS JOIN AgentStatus SummSTS
WHERE DIM.cType = 'A'
AND (
SummSTS.StSet = SummPRO.StSet
OR SummSTS.StSet = PRO.StSet
)
What is the best join strategy/indexes for this query:
SELECT
kwk.*, an.AuftragDatum, an.AbgabeDatum, an.BezahltDatum, an.AuftragStatus
FROM
KundenWerbenKunden kwk
INNER JOIN
Auftrag an ON an.AuftragNummer = kwk.AuftragNummer
WHERE
kwk.Deleted = 0
Table KundenWerbenKunden has 103950 rows with 103646 Deleted = 0 ones.
Table Auftrag has 3826552 rows.
In my real query I make some more joins:
INNER JOIN
Filiale fn WITH (NOLOCK) ON an.FilialeID = fn.FilialeID
INNER JOIN
Kunde kn ON an.KundeID = kn.KundeID
OUTER APPLY
(SELECT DISTINCT KSKNr
FROM KdZuordnung
WHERE KundeID = kn.KundeID) zn
LEFT JOIN
Anrede ann WITH (NOLOCK) ON kn.Anrede = ann.Anrede
INNER JOIN
AuftragArt aa WITH (NOLOCK) ON an.AuftragArtID = aa.AuftragArtID
INNER JOIN
AuftragGrund ag WITH (NOLOCK) ON an.AuftragGrundID = ag.AuftragGrundID
INNER JOIN
AuftragType at WITH (NOLOCK) ON an.AuftragTypeID = at.AuftragTypeID
For this query:
SELECT *
FROM KundenWerbenKunden kwk INNER JOIN
Auftrag an
ON an.AuftragNummer = kwk.AuftragNummer
WHERE kwk.Geloescht = 0;
And not knowing anything about the distribution of Geloescht, I would first try indexes on KundenWerbenKunden(Geloescht, AuftragNummer) and Auftrag(AuftragNummer).
Every time I try to run the following in Microsoft SQL Server Management Studio, I get an error message
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
Is there anyway at all to run this in one statement?
The reason why I'm calling a function in this statement (dbo.ufn_ConvertJambDepthFractionToNumber) is because the dbo.CDS_Shipments_Door_Overall_Jamb_Depth.InstructionValue AS [Jamb Depth] column spits out values like 4 1/6", 3 1/4", 6 9/16". I'm using that function to try to convert the varchar values (4 1/6", 3 1/4", 6 9/16") into decimals.
If there is a better way to do that, I'm all ears.
use BeechworthProdWTS
declare #jamb_depth as varchar
SELECT DISTINCT
dbo.QuoteShippingAddress.Name AS [CDS Location],
dbo.QuoteShippingAddress.Address1 AS [Quote Shipping Address1],
dbo.CDS_Shipments_Door_Overall_Jamb_Depth.InstructionValue AS [Jamb Depth],
dbo.CDS_Shipments_Door_Count_view.Doors,
dbo.CDS_Shipments_Screen_Count_view.Screens,
dbo.CDS_Shipments_Windows_Count_view.Windows,
dbo.LineItemMaster.LineNumber,
#jamb_depth = dbo.CDS_Shipments_Door_Overall_Jamb_Depth.InstructionValue,
dbo.ufn_ConvertJambDepthFractionToNumber(#jamb_depth)
FROM
dbo.Quotes
INNER JOIN
dbo.QuoteShippingAddress WITH (NOLOCK) ON dbo.Quotes.QuoteID = dbo.QuoteShippingAddress.QuoteID
INNER JOIN
dbo.Clients WITH (NOLOCK) ON dbo.Quotes.ClientID = dbo.Clients.ClientID
INNER JOIN
dbo.CustomerProjectInformation WITH (NOLOCK) ON dbo.Quotes.QuoteID = dbo.CustomerProjectInformation.QuoteID
INNER JOIN
dbo.LineItemMaster WITH (NOLOCK) ON dbo.Quotes.QuoteID = dbo.LineItemMaster.QuoteID
INNER JOIN
dbo.LineItems WITH (NOLOCK) ON dbo.LineItemMaster.LineItemMasterID = dbo.LineItems.LineItemMasterID
INNER JOIN
dbo.WorkOrders WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.WorkOrders.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Door_Count_view WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Door_Count_view.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Door_Overall_Jamb_Depth WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Door_Overall_Jamb_Depth.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Screen_Count_view WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Screen_Count_view.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Windows_Count_view WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Windows_Count_view.LineItemID
Since you say you are pretty new to SQL I reformatted this using aliases so you can see how much difference it make visually. This doesn't affect performance but it does keep the developer sane. Also, not really sure what you are trying to do with your scalar but maybe trying to get the value for each row?
Cleaned up your query might look something like this.
SELECT DISTINCT
qsa.Name AS [CDS Location],
qsa.Address1 AS [Quote Shipping Address1],
sdojd.InstructionValue AS [Jamb Depth],
sdcv.Doors,
sscv.Screens,
swcv.Windows,
lim.LineNumber,
--#jamb_depth = sdojd.InstructionValue, --not really sure what you are trying to do here
--dbo.ufn_ConvertJambDepthFractionToNumber(#jamb_depth)
JambDepth = dbo.ufn_ConvertJambDepthFractionToNumber(sdojd.InstructionValue)
FROM dbo.Quotes q
INNER JOIN dbo.QuoteShippingAddress qsa ON q.QuoteID = qsa.QuoteID
INNER JOIN dbo.Clients c ON q.ClientID = c.ClientID
INNER JOIN dbo.CustomerProjectInformation cpi ON q.QuoteID = cpi.QuoteID
INNER JOIN dbo.LineItemMaster lim ON q.QuoteID = lim.QuoteID
INNER JOIN dbo.LineItems li ON lim.LineItemMasterID = li.LineItemMasterID
INNER JOIN dbo.WorkOrders wo ON li.LineItemID = wo.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Door_Count_view sdcv ON li.LineItemID = sdcv.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Door_Overall_Jamb_Depth sdojd ON li.LineItemID = sdojd.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Screen_Count_view sscv ON li.LineItemID = sscv.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Windows_Count_view swcv ON li.LineItemID = swcv.LineItemID
-- Insert statements for procedure here
IF EXISTS
(SELECT S.SCHEDULER_ID FROM Scheduler S WITH (NOLOCK)
INNER JOIN CaseDetails_Scheduler_Mapping CDSM WITH (NOLOCK) ON (S.SCHEDULER_ID=CDSM.SCHEDULER_ID)
INNER JOIN CaseDetails CD WITH (NOLOCK) ON (CDSM.CASE_DETAIL_ID = CD.CASE_DETAIL_ID)
WHERE S.HEARD_BEFORE_ID=#HEARD_BEFORE_ID AND S.SCHEDULE_START_TIME=#SCHEDULE_START_TIME AND S.SCHEDULE_DATE=#SCHEDULE_DATE)
BEGIN
SELECT '4' AS 'STATUS', (SELECT S.SCHEDULER_ID FROM Scheduler S WITH (NOLOCK)
INNER JOIN CaseDetails_Scheduler_Mapping CDSM WITH (NOLOCK) ON (S.SCHEDULER_ID=CDSM.SCHEDULER_ID)
INNER JOIN CaseDetails CD WITH (NOLOCK) ON (CDSM.CASE_DETAIL_ID = CD.CASE_DETAIL_ID)
WHERE S.HEARD_BEFORE_ID=#HEARD_BEFORE_ID AND S.SCHEDULE_START_TIME=#SCHEDULE_START_TIME AND S.SCHEDULE_DATE=#SCHEDULE_DATE) AS 'SCHEDULER_ID',
(SELECT CD.CASE_NO FROM Scheduler S WITH (NOLOCK)
INNER JOIN CaseDetails_Scheduler_Mapping CDSM WITH (NOLOCK) ON (S.SCHEDULER_ID=CDSM.SCHEDULER_ID)
INNER JOIN CaseDetails CD WITH (NOLOCK) ON (CDSM.CASE_DETAIL_ID = CD.CASE_DETAIL_ID)
WHERE S.HEARD_BEFORE_ID=#HEARD_BEFORE_ID AND S.SCHEDULE_START_TIME=#SCHEDULE_START_TIME AND S.SCHEDULE_DATE=#SCHEDULE_DATE) AS 'CASE_NO'
END
I know this code are correct , but is there a way to simplify my sql code? i try did select 2 column in 1 table but it will appear "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS" error..... Sorry if i asking such beginner question..
I think you can remove the IF EXISTS part. Here is a simplified version of your THEN part:
SELECT
'4' AS [STATUS],
S.SCHEDULER_ID AS [SCHEDULER_ID],
CD.CASE_NO AS [CASE_NO]
FROM Scheduler S WITH (NOLOCK)
INNER JOIN CaseDetails_Scheduler_Mapping CDSM WITH (NOLOCK)
ON (S.SCHEDULER_ID = CDSM.SCHEDULER_ID)
INNER JOIN CaseDetails CD WITH (NOLOCK)
ON (CDSM.CASE_DETAIL_ID = CD.CASE_DETAIL_ID)
WHERE
S.HEARD_BEFORE_ID = #HEARD_BEFORE_ID
AND S.SCHEDULE_START_TIME = #SCHEDULE_START_TIME
AND S.SCHEDULE_DATE = #SCHEDULE_DATE
Note that putting NOLOCK everywhere is considered a bad habit.
I am trying to troubleshoot someone else's MS Access query and keep getting an invalid operation error. The Help doesn't seem to apply as I am just running a query. It all works as INNER JOINS but when I switch back to the LEFT JOIN the error.
SELECT *
FROM ((((orders
INNER JOIN orders_customers ON orders.CUST_ORDER_ID = orders_customers.ID)
LEFT JOIN quoted_theory ON orders.PART_ID = quoted_theory.PART_ID)
LEFT JOIN conversions ON orders.PART_ID = conversions.PART_ID)
LEFT JOIN dbo_WO_Header ON orders.CUST_ORDER_ID = dbo_WO_Header.PPC_Number)
INNER JOIN lines_qry ON orders.CUST_ORDER_ID = lines_qry.WORKORDER_BASE_ID
I can get one level of LEFT JOIN, but each time I add a second LEFT JOIN the error pops up.
Access' db engine frequently balks when mixing INNER and LEFT joins. If that query works without the last inner join ...
SELECT *
FROM
(((orders INNER JOIN orders_customers
ON orders.CUST_ORDER_ID = orders_customers.ID)
LEFT JOIN quoted_theory
ON orders.PART_ID = quoted_theory.PART_ID)
LEFT JOIN conversions
ON orders.PART_ID = conversions.PART_ID)
LEFT JOIN dbo_WO_Header
ON orders.CUST_ORDER_ID = dbo_WO_Header.PPC_Number
... then you could try that part as a subquery and inner join lines_qry to the subquery. It might get past the error.
SELECT *
FROM
(
SELECT *
FROM
(((orders INNER JOIN orders_customers
ON orders.CUST_ORDER_ID = orders_customers.ID)
LEFT JOIN quoted_theory
ON orders.PART_ID = quoted_theory.PART_ID)
LEFT JOIN conversions
ON orders.PART_ID = conversions.PART_ID)
LEFT JOIN dbo_WO_Header
ON orders.CUST_ORDER_ID = dbo_WO_Header.PPC_Number
) AS sub
INNER JOIN lines_qry
ON sub.CUST_ORDER_ID = lines_qry.WORKORDER_BASE_ID
If any other table besides orders includes a field named CUST_ORDER_ID, you will need something other than SELECT * within the subquery to avoid ambiguity.
SELECT *
FROM
(
SELECT *
FROM
(((orders INNER JOIN orders_customers
ON orders.CUST_ORDER_ID = orders_customers.ID)
LEFT JOIN quoted_theory
ON orders.PART_ID = quoted_theory.PART_ID)
LEFT JOIN conversions
ON orders.PART_ID = conversions.PART_ID)
LEFT JOIN dbo_WO_Header
ON orders.CUST_ORDER_ID = dbo_WO_Header.PPC_Number
) AS sub
INNER JOIN lines_qry
ON sub.CUST_ORDER_ID = lines_qry.WORKORDER_BASE_ID