I am having trouble converting this Pervasive sql query to Oracle. The errors that oracle is giving me does not help. It is giving me an error at SQL_Date.
This is what I have in Pervasive:
SELECT
(CASE
WHEN joefdt = 0
THEN NULL
WHEN joefdt > 0
THEN CONVERT(LEFT(CONVERT(joefdt, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(joefdt, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(joefdt, SQL_CHAR),2),SQL_DATE) END) AS EFFECTIVEDATE,
(CASE
WHEN jopost = 0
THEN NULL
WHEN jopost > 0
THEN CONVERT(LEFT(CONVERT(jopost, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(jopost, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(jopost, SQL_CHAR),2),SQL_DATE) END) AS POSTDATE,
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 3 AND cdnumb = joetyp) AS TYPE,
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 5 AND cdnumb = jodelm) AS DELIVERYMETHOD,
joissd AS SHARESISSUED, jocand AS SHARESCANCELLED , joprsn AS BROKERID, joprsn AS TRANSACID
FROM money.jour
WHERE joiss = 1 AND joetyp in (2,3,4) AND (JOISSD > 0 OR JOCAND > 0)
AND joefdt >= '19920127' AND joefdt <= '20040726'
ORDER BY joefdt;
This is what I have tried, but it doesn't work:
SELECT
(CASE
WHEN joefdt = 0
THEN NULL
WHEN joefdt > 0
THEN CONVERT(substr(CONVERT(joefdt, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(joefdt, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(joefdt, SQL_CHAR),2),SQL_DATE) END) AS EFFECTIVEDATE,
(CASE
WHEN jopost = 0
THEN NULL
WHEN jopost > 0
THEN CONVERT(substr(CONVERT(jopost, SQL_CHAR),4) + '-' + RIGHT(LEFT(CONVERT(jopost, SQL_CHAR),6),2) + '-' + RIGHT(CONVERT(jopost, SQL_CHAR),2),SQL_DATE) END) AS POSTDATE, ----Error at SQL_DATE
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 3 AND cdnumb = joetyp) AS TYPE,
(SELECT ltrim(rtrim(cdname)) from money.code WHERE cdtabl = 5 AND cdnumb = jodelm) AS DELIVERYMETHOD,
joissd AS SHARESISSUED, jocand AS SHARESCANCELLED , joprsn AS BROKERID, joprsn AS TRANSACID
FROM money.jour
WHERE joiss = 1 AND joetyp in (2,3,4) AND (JOISSD > 0 OR JOCAND > 0)
AND joefdt >= '20000302' AND joefdt <= '20000302'
ORDER BY joefdt;
In Oracle CONVERT function translates character string from one character set to another. It cannot be used to convert something to date (does Pervasive means this?) or something. Oracle does not know identifiers SQL_DATE and SQL_CHAR, so you cannot use them in this manner. And last, conditions like joefdt >= '20000302' are doubtful. Maybe it means something like joefdt >= DATE '2000-03-02'
Related
SELECT DISTINCT
ATB.AcountCountDesc,
TB.LastFirstName,
N.EMAIL,
TB.AccountNumber,
TB.OpenShareCount,
TB.MemberOpenDate,
TB.OpenMemberCount,
TB.OpenShareBalance,
SH.ShareType,
FORMAT(SH.ShareOpenDate, 'MM/dd/yyyy') AS "ShareOpenDate",
SH.ShareCreatedByUser,
SH.ShareCreatedByUserName,
SH.ShareBranchName,
SH.ShareBranch,
cast(month(SH.ShareOpenDate) as varchar) + '/' + cast(year(SH.ShareOpenDate) as varchar) as 'Open Period',
CONCAT(SH.ShareCreatedByUser, '-',SH.ShareCreatedByUserName) 'Opened By'
FROM
arcu.vwARCUOperationMemberTrialBalance as TB
INNER JOIN arcu.vwARCUOperationMemberAccountTrialBalance as ATB ON TB.MemberSuppID = ATB.MemberID
and TB.ProcessDate = ATB.PDate
and TB.MemberStatus = 0 -- Account count for open Members only
and TB.AccountStatus <> 1
INNER JOIN arcu.vwARCUShare AS SH ON TB.ProcessDate = SH.ProcessDate
AND TB.AccountNumber = SH.AccountNumber
INNER JOIN NAME AS N ON TB.AccountNumber = N.PARENTACCOUNT
WHERE
ATB.AcountCountDesc = 1
AND TB.OpenShareCount >= 1
AND SH.ShareType = '00'
AND SH.ShareID != '40'
AND SH.ShareOpenDate >= DATEADD(s, 1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), -2))
AND TB.MemberOpenDate = SH.ShareOpenDate
AND N.EMAIL <> ''
AND N.EMAIL is not null
order by MemberOpenDate Desc;
Issue: the MemberOpen Date starts on 08/02/2021.
Desired outcome: I need it to start on 08/01/2021
What I have tried: I have tried declaring a variable and calculating the start of the month by using datediff and still get the 08/02/2021 outcome.
I tried changing the => to = or <= and the issue persists. Please help.
You could calculate the 1st of the month using EOMONTH something like this
SELECT DISTINCT ATB.AcountCountDesc,TB.LastFirstName,N.EMAIL,TB.AccountNumber,TB.OpenShareCount,TB.MemberOpenDate,
TB.OpenMemberCount,TB.OpenShareBalance,SH.ShareType,FORMAT(SH.ShareOpenDate,'MM/dd/yyyy') AS "ShareOpenDate",
SH.ShareCreatedByUser,SH.ShareCreatedByUserName,SH.ShareBranchName,SH.ShareBranch,cast(month(SH.ShareOpenDate)
as varchar) + '/' + cast(year(SH.ShareOpenDate) as varchar)as 'Open Period', CONCAT(SH.ShareCreatedByUser,'-',SH.ShareCreatedByUserName)
'Opened By'
FROM arcu.vwARCUOperationMemberTrialBalance as TB
JOIN arcu.vwARCUOperationMemberAccountTrialBalance as ATB
ON TB.MemberSuppID = ATB.MemberID
and TB.ProcessDate = ATB.PDate
and TB.MemberStatus = 0 -- Account count for open Members only
and TB.AccountStatus <> 1
JOIN arcu.vwARCUShare AS SH
ON TB.ProcessDate = SH.ProcessDate
AND TB.AccountNumber = SH.AccountNumber
JOIN NAME AS N ON TB.AccountNumber = N.PARENTACCOUNT
WHERE
ATB.AcountCountDesc = 1 AND
TB.OpenShareCount >= 1 AND
SH.ShareType = '00' AND
SH.ShareID !='40' AND
SH.ShareOpenDate >= (select dateadd(day, 1, dateadd(month, -1, eomonth(getdate())))) AND
TB.MemberOpenDate = SH.ShareOpenDate AND
N.EMAIL <> '' AND
N.EMAIL is not null
order by MemberOpenDate Desc;
Hello two days I have tried to figure out why my code isn't working.
I am quite sure that it's a stupid mistake, but if anybody has time to check the code I would really appreciate.
I have tried to comment out this "SUM" case, but then everything else breaks
I know that this line screws with me, but I can't figure out how to fix this:
sum (Case when bm.CaseQTY IS NULL
then (k.postedqty / mm.CaseQTY)
else (k.postedqty / bm.CaseQTY)
End) as laoseiskaste,
sum (Case when bm.CaseQTY IS NULL
then ((k.postedqty-k.reservordered-k.reservphysical-k.picked+k.registered) / mm.CaseQTY)
else ((k.postedqty-k.reservordered-k.reservphysical-k.picked+k.registered) / bm.CaseQTY)
End) as vabakaste
from (select s.itemid, i.product, i.ItemMiscRefId, i.MMFItemProductGroupId, s.INVENTDIMID,
s.postedqty, s.registered, s.reservordered, s.reservphysical, s.picked, s.ordered,
s.postedvalue, s.received, i.PrimaryVendorId, i.standardPalletQuantity
from inventsum s WITH (INDEX(I_174CLOSEDITEMDIMIDX),NOLOCK),
SELECT k.itemid AS itemid,
(CASE WHEN ISNULL(bm.mmfitemid, '') != '' THEN bm.mmfitemid ELSE mm.mmfitemid END) AS mmfitemid,
(CASE WHEN ISNULL(bm.itembarcode, '') != '' THEN bm.itembarcode ELSE mm.itembarcode END) AS itembarcode,
e.name AS name, d.inventlocationid AS inventlocationid, d.inventbatchid AS inventbatchid,
LEFT(CONVERT(varchar, ib.proddate, 121), 10) AS proddate,
LEFT(CONVERT(varchar, ib.expdate, 121), 10) AS expdate,
d.wmslocationid AS wmslocationid, d.wmspalletid AS wmspalletid, p.pallettypeid AS pallettypeid,
xz.inputblockingcauseid AS inputblockingcauseid, xz.outputblockingcauseid AS outputblockingcauseid,
d.inventserialid AS inventserialid, d.inventownerid_via AS inventownerid_via,
d.inventlabelid_via AS inventlabelid_via, d.inventtaxstampid AS inventtaxstampid,
(CASE WHEN ISNULL(bm.CaseQTY, 0) != 0 THEN bm.CaseQTY ELSE mm.CaseQTY END) AS caseqty,
k.standardPalletQuantity AS palletqty,
(CASE WHEN ISNULL(bm.caseheight, 0) != 0 THEN bm.caseheight ELSE mm.caseheight END) AS caseheight,
(CASE WHEN ISNULL(bm.casewidth, 0) != 0 THEN bm.casewidth ELSE mm.casewidth END) AS casewidth,
(CASE WHEN ISNULL(bm.casedepth, 0) != 0 THEN bm.casedepth ELSE mm.casedepth END) AS casedepth,
(CASE WHEN ISNULL(bm.casebarcode, '') != '' THEN bm.casebarcode ELSE mm.casebarcode END) AS casebarcode,
(CASE WHEN ISNULL(bm.casebrutoweight, 0) != 0 THEN bm.casebrutoweight ELSE mm.casebrutoweight END) AS casebrutoweight,
(CASE WHEN ISNULL(bm.alcopct, 0) != 0 THEN bm.alcopct ELSE mm.alcopct END) AS alcopct,
(CASE WHEN ISNULL(bm.blockqty, 0) != 0 THEN bm.blockqty ELSE mm.blockqty END) AS blockqty,
(CASE WHEN ISNULL(bm.exciseproductregnum, '') != ''
THEN bm.exciseproductregnum
ELSE mm.exciseproductregnum
END) AS exciseproductregnum,
SUM(k.postedqty + k.received + k.registered) AS postedqty,
SUM(k.registered) AS registeredqty,
SUM(k.reservordered + k.reservphysical) AS reservedqty, SUM(k.picked) AS pickedqty,
SUM(k.postedqty - k.reservordered - k.reservphysical - k.picked + k.registered) AS freeqty,
SUM(k.ordered) AS ordered, SUM(k.postedvalue) AS postedvalue,
k.MMFItemProductGroupId AS mmfitemproductgroupid,
SUM(CASE WHEN bm.CaseQTY IS NULL
THEN (k.postedqty / mm.CaseQTY)
ELSE (k.postedqty / bm.CaseQTY)
END) AS laoseiskaste,
SUM(CASE WHEN bm.CaseQTY IS NULL
THEN ((k.postedqty - k.reservordered - k.reservphysical - k.picked + k.registered) / mm.CaseQTY)
ELSE ((k.postedqty - k.reservordered - k.reservphysical - k.picked + k.registered) / bm.CaseQTY)
END) AS vabakaste
FROM (SELECT s.itemid, i.product, i.ItemMiscRefId, i.MMFItemProductGroupId, s.INVENTDIMID,
s.postedqty, s.registered, s.reservordered, s.reservphysical, s.picked,
s.ordered, s.postedvalue, s.received, i.PrimaryVendorId, i.standardPalletQuantity
FROM inventsum s WITH (INDEX(I_174CLOSEDITEMDIMIDX), NOLOCK),
inventtable i WITH (INDEX([I_175ITEMIDX]), NOLOCK)
WHERE s.itemid = i.itemid
AND s.DATAAREAID = #Var1
AND i.DATAAREAID = #Var1
AND s.PARTITION = 5637144576
AND i.PARTITION = 5637144576
AND RIGHT(s.ITEMID, 2) = 'LA'
AND s.CLOSED = 0
AND (s.POSTEDQTY != 0
OR s.reservordered + s.reservphysical != 0
OR s.ordered != 0
)
) k
JOIN inventdim d WITH (INDEX(I_698DIMIDIDX), NOLOCK)
ON (k.INVENTDIMID = d.INVENTDIMID
AND d.DATAAREAID = #Var3
AND d.PARTITION = 5637144576)
LEFT OUTER JOIN EcoresProductTranslation e WITH (INDEX(I_6869PRODUCTLANGUAGEIDX))
ON (k.product = e.product
AND e.languageid = 'et'
AND e.PARTITION = 5637144576)
LEFT OUTER JOIN WMSPALLET p
ON (p.WMSPALLETID = d.WMSPALLETID
AND p.PARTITION = 5637144576
AND p.DATAAREAID = #Var4)
LEFT OUTER JOIN InventItemMisc mm WITH (INDEX(I_103616RecId))
ON (mm.RecId = k.ItemMiscRefId
AND mm.PARTITION = 5637144576)
LEFT OUTER JOIN InventBatch ib WITH (INDEX(I_752itemBatchIdx))
ON (ib.ItemId = k.ItemId
AND ib.InventBatchId = d.InventBatchId
AND ib.PARTITION = 5637144576
AND ib.DATAAREAID = #Var5)
LEFT OUTER JOIN InventItemMisc bm WITH (INDEX(I_103616RecId))
ON (bm.RecId = ib.ItemMiscRefId
AND bm.PARTITION = 5637144576)
JOIN WMSLOCATIOn xz WITH (INDEX(/*I672COORDINATEDIDX*/ I_672LOCATIONIDX))
ON (xz.inventlocationID = d.inventlocationID
AND xz.wmslocationid = d.wmslocationid
AND xz.partition = 5637144576
AND xz.dataareaid = 'EWH')
WHERE 1 = 1
GROUP BY k.itemid,
(CASE WHEN ISNULL(bm.mmfitemid, '') != '' THEN bm.mmfitemid ELSE mm.mmfitemid END),
(CASE WHEN ISNULL(bm.itembarcode, '') != '' THEN bm.itembarcode ELSE mm.itembarcode END),
e.name, d.inventlocationid, d.inventbatchid, LEFT(CONVERT(varchar, ib.proddate, 121), 10),
LEFT(CONVERT(varchar, ib.expdate, 121), 10), d.wmslocationid, d.wmspalletid,
p.pallettypeid, xz.inputblockingcauseid, xz.outputblockingcauseid, d.inventserialid,
d.inventownerid_via, d.inventlabelid_via, d.inventtaxstampid,
(CASE WHEN ISNULL(bm.CaseQTY, 0) != 0 THEN bm.CaseQTY ELSE mm.CaseQTY END),
k.standardPalletQuantity,
(CASE WHEN ISNULL(bm.caseheight, 0) != 0 THEN bm.caseheight ELSE mm.caseheight END),
(CASE WHEN ISNULL(bm.casewidth, 0) != 0 THEN bm.casewidth ELSE mm.casewidth END),
(CASE WHEN ISNULL(bm.casedepth, 0) != 0 THEN bm.casedepth ELSE mm.casedepth END),
(CASE WHEN ISNULL(bm.casebarcode, '') != '' THEN bm.casebarcode ELSE mm.casebarcode END),
(CASE WHEN ISNULL(bm.casebrutoweight, 0) != 0 THEN bm.casebrutoweight ELSE mm.casebrutoweight END),
(CASE WHEN ISNULL(bm.alcopct, 0) != 0 THEN bm.alcopct ELSE mm.alcopct END),
(CASE WHEN ISNULL(bm.blockqty, 0) != 0 THEN bm.blockqty ELSE mm.blockqty END),
(CASE WHEN ISNULL(bm.exciseproductregnum, '') != ''
THEN bm.exciseproductregnum
ELSE mm.exciseproductregnum
END),
k.MMFItemProductGroupId
ORDER BY k.itemid,
(CASE WHEN ISNULL(bm.mmfitemid, '') != '' THEN bm.mmfitemid ELSE mm.mmfitemid END),
(CASE WHEN ISNULL(bm.itembarcode, '') != '' THEN bm.itembarcode ELSE mm.itembarcode END),
e.name, d.inventlocationid, d.inventbatchid, LEFT(CONVERT(varchar, ib.proddate, 121), 10),
LEFT(CONVERT(varchar, ib.expdate, 121), 10), d.wmslocationid, d.wmspalletid,
p.pallettypeid, xz.inputblockingcauseid, xz.outputblockingcauseid,
d.inventserialid, d.inventownerid_via, d.inventlabelid_via, d.inventtaxstampid,
(CASE WHEN ISNULL(bm.CaseQTY, 0) != 0 THEN bm.CaseQTY ELSE mm.CaseQTY END),
k.standardPalletQuantity,
(CASE WHEN ISNULL(bm.caseheight, 0) != 0 THEN bm.caseheight ELSE mm.caseheight END),
(CASE WHEN ISNULL(bm.casewidth, 0) != 0 THEN bm.casewidth ELSE mm.casewidth END),
(CASE WHEN ISNULL(bm.casedepth, 0) != 0 THEN bm.casedepth ELSE mm.casedepth END),
(CASE WHEN ISNULL(bm.casebarcode, '') != '' THEN bm.casebarcode ELSE mm.casebarcode END),
(CASE WHEN ISNULL(bm.casebrutoweight, 0) != 0 THEN bm.casebrutoweight ELSE mm.casebrutoweight END),
(CASE WHEN ISNULL(bm.alcopct, 0) != 0 THEN bm.alcopct ELSE mm.alcopct END),
(CASE WHEN ISNULL(bm.blockqty, 0) != 0 THEN bm.blockqty ELSE mm.blockqty END),
(CASE WHEN ISNULL(bm.exciseproductregnum, '') != ''
THEN bm.exciseproductregnum
ELSE mm.exciseproductregnum
END),
k.MMFItemProductGroupId;
Error in SQL:Error in SQL:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Divide by zero error encountered.
In SSMS, hit Ctrl+F to open the Find & Replace dialogue box. Search the query for all instances of /. This will tell you everywhere that division is happening, plus some false positives.
In every single instance, the query is dividing by mm.CaseQTY or bm.CaseQTY. Those are the only two values, and the only two tables you have to worry about. One or the other of them is coming through as a zero.
We don't have access to your database, so we can't help you troubleshoot the data issue, but that's how I'd go about deciding where to start looking.
Im having a problem with this :( is this because of my select statement? or any join? derived table? or my CASE syntax?
The error occurs the time I put this CASE syntax: CASE WHEN dbo.AdditionalInfo.UserDefined3 = 1 THEN 2
because before it is just:
CASE WHEN WorkOrder.DateCreated < (CASE WHEN (DATEPART(dw,
dbo.ToBeScheduled_InProgress.Start) = 2) THEN (ToBeScheduled_InProgress.Start + 0.625) - 3 ELSE (ToBeScheduled_InProgress.Start + 0.625) - 1 END) THEN 1 ELSE 2 END AS ScheduleTime
SELECT Start, UserDefined3, ItemNo, Name, TotalQtyRequired, PreviouslyCounted, QtyLeftToPick, LocationCode, Location, Rack, QtyInRack, PickQty, CellCode, UDMaterial, UDMaterialSort, ScheduleTimeFROM (SELECT dbo.ToBeScheduled_InProgress.Start, dbo.ItemSpecs.ItemNo, dbo.ItemSpecs.Name, SUM(ISNULL(dbo.ToBeScheduled_InProgress.RemainingQty, 0) * ISNULL(dbo.ItemSpecFullStruc.TotalQtyPerRoot, 0))
AS TotalQtyRequired, ISNULL(RackedInventory.PreviouslyCounted, 0) AS PreviouslyCounted, SUM(ISNULL(dbo.ToBeScheduled_InProgress.RemainingQty, 0) * ISNULL(dbo.ItemSpecFullStruc.TotalQtyPerRoot, 0))
- ISNULL(RackedInventory.PreviouslyCounted, 0) AS QtyLeftToPick, RackedInventory.LocationCode, RackedInventory.Location, RackedInventory.Rack, ISNULL(RackedInventory.QtyInRack, 0) AS QtyInRack,
ISNULL(CASE WHEN SUM(ISNULL(dbo.ToBeScheduled_InProgress.RemainingQty, 0) * ISNULL(dbo.ItemSpecFullStruc.TotalQtyPerRoot, 0)) - ISNULL(RackedInventory.PreviouslyCounted, 0)
< RackedInventory.QtyInRack THEN SUM(ISNULL(dbo.ToBeScheduled_InProgress.RemainingQty, 0) * ISNULL(dbo.ItemSpecFullStruc.TotalQtyPerRoot, 0)) - ISNULL(RackedInventory.PreviouslyCounted, 0)
ELSE RackedInventory.QtyInRack END, 0) AS PickQty, dbo.ToBeScheduled_InProgress.CellCode, dbo.ItemSpecs.Userdefined2 AS UDMaterial, CASE WHEN dbo.ItemSpecs.Userdefined2 IN ('Foam', 'Wood',
'Plastic Inner', 'Rubber') THEN 0 ELSE 1 END AS UDMaterialSort, CASE WHEN dbo.AdditionalInfo.UserDefined3 = 1 THEN 2 WHEN WorkOrder.DateCreated < (CASE WHEN (DATEPART(dw,
dbo.ToBeScheduled_InProgress.Start) = 2) THEN (ToBeScheduled_InProgress.Start + 0.625) - 3 ELSE (ToBeScheduled_InProgress.Start + 0.625) - 1 END) THEN 1 ELSE 2 END AS ScheduleTime
FROM dbo.ToBeScheduled_InProgress INNER JOIN
dbo.OpenCalendarDaysTable AS OpenCalendarDays ON dbo.ToBeScheduled_InProgress.Start BETWEEN OpenCalendarDays.CalendarDateForwardRangeStart AND
OpenCalendarDays.CalendarDateForwardRangeEnd AND OpenCalendarDays.CalendarLinkID IS NULL INNER JOIN
dbo.OpenCalendarDaysTable AS PriorOpenCalendarDays ON OpenCalendarDays.CalendarOpenDayID = PriorOpenCalendarDays.CalendarOpenDayID + 1 AND OpenCalendarDays.CalendarLinkID IS NULL
INNER JOIN
dbo.ItemSpecFullStruc ON dbo.ToBeScheduled_InProgress.ItemSpecID = dbo.ItemSpecFullStruc.RootItemSpecID INNER JOIN
dbo.ItemSpecs ON dbo.ItemSpecFullStruc.ChildItemSpecID = dbo.ItemSpecs.ItemSpecID LEFT OUTER JOIN
dbo.WorkOrder ON dbo.ToBeScheduled_InProgress.WorkOrderID = dbo.WorkOrder.WorkOrderID LEFT OUTER JOIN
(SELECT InventoryByLocation.ItemID, InventoryByLocation.ItemNo, InventoryByLocation.ItemDescription, InventoryByLocation.LocationCode, InventoryByLocation.Location, InventoryByLocation.Rack,
InventoryByLocation.QtyInRack, SUM(PreviouslyCounted.QtyInRack) AS PreviouslyCounted
FROM (SELECT Rack, SUM(ISNULL(QtyToStock, ' ')) AS QtyInRack, ItemID, ItemSpecID, LocationID
FROM dbo.InventoryItems WITH (NOLOCK)
GROUP BY ItemID, ItemSpecID, LocationID, Rack) AS PreviouslyCounted RIGHT OUTER JOIN
(SELECT Items_1.ItemNo, Items_1.Name AS ItemDescription, Locations_1.LocationCode, Locations_1.DescriptionMed AS Location, InventoryItems_1.Rack,
SUM(dbo.Val(ISNULL(InventoryItems_1.QtyToStock, ''))) AS QtyInRack, UOMs_1.UOMCode AS StockUOM, InventoryItems_1.ItemID, InventoryItems_1.ItemSpecID,
Locations_1.LocationID
FROM dbo.Items AS Items_1 WITH (NOLOCK) INNER JOIN
dbo.Locations AS Locations_1 WITH (NOLOCK) INNER JOIN
dbo.InventoryItems AS InventoryItems_1 WITH (NOLOCK) LEFT OUTER JOIN
dbo.UOMs AS UOMs_1 WITH (NOLOCK) ON InventoryItems_1.StockUOMID = UOMs_1.UOMID ON Locations_1.LocationID = InventoryItems_1.LocationID ON
Items_1.ItemID = InventoryItems_1.ItemID
WHERE (Locations_1.LocationID = 7)
GROUP BY Locations_1.LocationCode, Items_1.ItemNo, Locations_1.DescriptionMed, Items_1.Name, UOMs_1.UOMCode, InventoryItems_1.ItemID, InventoryItems_1.ItemSpecID,
Locations_1.LocationID, InventoryItems_1.Rack
HAVING (SUM(dbo.Val(ISNULL(InventoryItems_1.QtyToStock, ''))) > 0)) AS InventoryByLocation ON PreviouslyCounted.Rack < InventoryByLocation.Rack AND
PreviouslyCounted.LocationID = InventoryByLocation.LocationID AND PreviouslyCounted.ItemID = InventoryByLocation.ItemID
GROUP BY InventoryByLocation.ItemNo, InventoryByLocation.ItemDescription, InventoryByLocation.LocationCode, InventoryByLocation.Location, InventoryByLocation.Rack, InventoryByLocation.QtyInRack,
InventoryByLocation.ItemID) AS RackedInventory ON dbo.ItemSpecs.ItemID = RackedInventory.ItemID
WHERE (dbo.ToBeScheduled_InProgress.ExcludeFromFiniteScheduling = 0) AND (ISNULL(dbo.ItemSpecs.Userdefined2, '') <> 'Covering') AND (dbo.ItemSpecs.InventoryTrackingID > 1)
GROUP BY dbo.ToBeScheduled_InProgress.Start, dbo.ItemSpecs.ItemNo, dbo.ItemSpecs.Name, RackedInventory.Rack, ISNULL(RackedInventory.PreviouslyCounted, 0), RackedInventory.Location,
RackedInventory.LocationCode, dbo.ToBeScheduled_InProgress.CellCode, dbo.ItemSpecs.Userdefined2,
CASE WHEN dbo.AdditionalInfo.UserDefined3 = 1 THEN 2 WHEN WorkOrder.DateCreated < (CASE WHEN (DATEPART(dw, dbo.ToBeScheduled_InProgress.Start) = 2) THEN (ToBeScheduled_InProgress.Start + 0.625)
- 3 ELSE (ToBeScheduled_InProgress.Start + 0.625) - 1 END) THEN 1 ELSE 2 END, RackedInventory.QtyInRack
HAVING (SUM(ISNULL(dbo.ToBeScheduled_InProgress.RemainingQty, 0) * ISNULL(dbo.ItemSpecFullStruc.TotalQtyPerRoot, 0)) - ISNULL(RackedInventory.PreviouslyCounted, 0) > 0)) AS _RackSort_Lean_AMPM ORDER BY Start, LocationCode, Rack
ALTER TABLE table_1
ADD [calc_column] AS
(
CASE
WHEN ([payment_1] IS NULL
AND [payment_2] IS NULL
AND [payment_3] IS NULL
AND [payment_4] IS NULL
AND [payment_5] IS NULL
AND [payment_6] IS NULL)
THEN NULL
ELSE (COALESCE([payment_1],(0)) + COALESCE([payment_2],(0)) +
CASE
WHEN (COALESCE([payment_3],(0)) + COALESCE([payment_7],(0)) + COALESCE([payment_4],(0)) > COALESCE([payment_5],(0)))
THEN (COALESCE([payment_1],(0)) + COALESCE([payment_4],(0)) + COALESCE([payment_3],(0)))
ELSE (COALESCE([payment_8],(0))) END) AS DECIMAL(13,2)
There are multiple errors, mostly caused by unbalanced parentheses and lack of end for the cases. Also, you don't need a specify a type.
So, I think this will work:
ALTER TABLE table_1
ADD [calc_column] AS (CASE WHEN [payment_1] IS NULL AND
[payment_2] IS NULL AND
[payment_3] IS NULL AND
[payment_4] IS NULL AND
[payment_5] IS NULL AND
[payment_6] IS NULL
THEN NULL
ELSE COALESCE([payment_1], 0) + COALESCE([payment_2], 0) +
(CASE WHEN COALESCE([payment_3], 0) + COALESCE([payment_7], 0) + COALESCE([payment_4], 0) > COALESCE([payment_5], 0)
THEN COALESCE([payment_1], 0) + COALESCE([payment_4], 0) + COALESCE([payment_3], 0)
ELSE COALESCE([payment_8], 0)
END)
END);
Is there another way I can sum up counts with less code?
I'm using a view (my only option) to try to find out if a customer spent money during any two of the last 5 calendar years
Table name:
V_PERSON
V_REVENUE
Columns:
V_PERSON.ID
V.REVENUE.PersonID
V.REVENUE.Year1 revenue for the year (currently 2016)
V.REVENUE.Year2 revenue for the year (currently, 2015)
V.REVENUE.Year3
V.REVENUE.Year4
V.REVENUE.Year5
Here's what I've tried:
SELECT V_PERSON.ID
FROM V_PERSON
WHERE
(
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year1 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year2 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year3 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year4 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year5 > 0
)
) >= 2
Here's one option using exists with multiple case statements:
select id
from person p
where exists (
select 1
from revenue r
where p.id = r.personid
and case when r.year1 > 0 then 1 else 0 end +
case when r.year2 > 0 then 1 else 0 end +
case when r.year3 > 0 then 1 else 0 end +
case when r.year4 > 0 then 1 else 0 end +
case when r.year5 > 0 then 1 else 0 end >= 2
)
How about a CASE statement:
SELECT V_PERSON.ID
FROM V_PERSON
WHERE
(
SELECT (case when V_REVENUE.Year1 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year2 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year3 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year4 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year5 > 0 then 1 else 0 end)
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID) >= 2