Select Distinct Sum - GROUP BY - sql

SELECT DISTINCT [SC Vermorel SRL$Production Order].No_,
[SC Vermorel SRL$Production Order].[Replan Ref_ No_],
[SC Vermorel SRL$Production Order].Description,
[SC Vermorel SRL$Production Order].[Source No_],
[SC Vermorel SRL$Production Order].[Routing No_],
[SC Vermorel SRL$Production Order].Quantity,
[SC Vermorel SRL$Production Order].[Old Prod_ Order No_],
[SC Vermorel SRL$Routing Line].[Run Time],
[SC Vermorel SRL$Routing Line].[Run Time] * [SC Vermorel SRL$Production Order].Quantity AS TotalOre,
[SC Vermorel SRL$Prod_ Order Line].[Finished Quantity],
SUM([SC Vermorel SRL$ProductieZilnica].[Cantitate sudura]) AS [Cantitate sudura],
SUM([SC Vermorel SRL$ProductieZilnica].[Cantitate montaj]) AS [Cantitate montaj]
FROM [SC Vermorel SRL$Production Order]
INNER JOIN [SC Vermorel SRL$Routing Line]
ON [SC Vermorel SRL$Production Order].[Routing No_] = [SC Vermorel SRL$Routing Line].[Routing No_]
INNER JOIN [SC Vermorel SRL$Prod_ Order Line]
ON [SC Vermorel SRL$Production Order].No_ = [SC Vermorel SRL$Prod_ Order Line].[Prod_ Order No_]
INNER JOIN [SC Vermorel SRL$ProductieZilnica]
ON [SC Vermorel SRL$Production Order].No_ = [SC Vermorel SRL$ProductieZilnica].RPO
WHERE ([SC Vermorel SRL$Production Order].Status = 3)
AND ([SC Vermorel SRL$Production Order].[Location Code] = 'MACH FIN')
AND ([SC Vermorel SRL$Production Order].[Gen_ Prod_ Posting Group] = 'FINISHED G')
AND ([SC Vermorel SRL$Routing Line].No_ = '150')
GROUP BY [SC Vermorel SRL$Production Order].No_
I need to attach the sum of all the entries from another table ([SC Vermorel SRL$ProductieZilnica]) which corresponds to each distinct No_ from [SC Vermorel SRL$Production Order] table.
I cant for the love of God make it work. Can someone point me in the right direction?

At first, use table aliases - it will make your query more readable.
If you are using GROUP BY you need to add all columns, except one that are in aggregation function (you got SUM) like:
SELECT PO.No_,
PO.[Replan Ref_ No_],
PO.[Description],
PO.[Source No_],
PO.[Routing No_],
PO.Quantity,
PO.[Old Prod_ Order No_],
RL.[Run Time],
RL.[Run Time] * PO.Quantity AS TotalOre,
POL.[Finished Quantity],
SUM(PZ.[Cantitate sudura]) AS [Cantitate sudura],
SUM(PZ.[Cantitate montaj]) AS [Cantitate montaj]
FROM [SC Vermorel SRL$Production Order] PO
INNER JOIN [SC Vermorel SRL$Routing Line] RL
ON PO.[Routing No_] = RL.[Routing No_]
INNER JOIN [SC Vermorel SRL$Prod_ Order Line] POL
ON PO.No_ = POL.[Prod_ Order No_]
INNER JOIN [SC Vermorel SRL$ProductieZilnica] PZ
ON PO.No_ = PZ.RPO
WHERE (PO.Status = 3) AND (PO.[Location Code] = 'MACH FIN') AND
(PO.[Gen_ Prod_ Posting Group] = 'FINISHED G') AND (RL.No_ = '150')
GROUP BY PO.No_,
PO.[Replan Ref_ No_],
PO.[Description],
PO.[Source No_],
PO.[Routing No_],
PO.Quantity,
PO.[Old Prod_ Order No_],
RL.[Run Time],
PO.Quantity,
POL.[Finished Quantity]

Related

SUMIF SQL MS Access

SELECT [doc type], [Open Amount],
[customer number],
COUNT([customer number]) As CountCustomerNumber,
SUM(IIF([Open Amount]>'0', [Open Amount], 0)) AS sum_open_amount_pos,
SUM(IIF([Open Amount]<'0', [Open Amount], 0)) As sum_open_amount_neg
FROM
(
SELECT d.[customer number] & d.[membership number] AS CustMemb,
d.[customer number],
agg.[Open Amount],
agg.[doc type],
SUM(agg.[TotalSubOpenAmount]) AS SumOpenAmount
FROM
(
SELECT [doc type],
[customer number],
SUM([Open Amount]) AS TotalSubOpenAmount
FROM data
WHERE [doc type] = 'RU'
GROUP BY [doc type], [customer number], [Open Amount]
) agg
INNER JOIN [data] d
ON d.[customer number] = agg.[customer number]
GROUP BY d.[customer number] & d.[membership number],
d.[customer number],
agg.[doc type],
agg.[Open Amount]
) AS sub
GROUP BY [doc type], [customer number]
HAVING COUNT([customer number]) = 1
I am getting a Parameter Input on MS Access - Open Amount.
How do I reference the open amount in the from query?

"The multi-part identifier could not be bound" for update statement from 'inner' set to 'update' set

Error
'The multi-part identifier "ar_update.Id" could not be bound.'
as line denoted in sql script.
I also tried other columns for the errant join to try to prompt different errors and gives same error results.
Why/How to qualify the 'inner' processing set back to the 'update' set?
update ar_update
set ar_update.[GLEntry_Amount] = ar_sum.[GLEntry_Amount_sum]
from [dbo].[ASL_Result_VGO] as ar_update
join (select ar.[Id] as [Id]
,ar.[Company Id] as [Company Id]
,ar.[Schedule Name] as [Schedule Name]
,ar.[Line No_] as [Line No_]
,ar.[Row No_] as [Row No_]
,ar.[Posting Date] as [Posting Date]
,ar.[Business Centre Code] as [Business Centre Code]
,ar.[Site Locations Code] as [Site Locations Code]
,GLEntry_Amount_sum =
(select sum(are.[GLEntry_Amount])
from [dbo].[ASL_Result_VGO] as are
where are.[Company Id] = ar.[Company Id] and
are.[Schedule Name] = ar.[Schedule Name] and
are.[Posting Date] = ar.[Posting Date] and
are.[Business Centre Code] = ar.[Business Centre Code] and
are.[Site Locations Code] = ar.[Site Locations Code] and
are.[Row No_] in ( select * from dbo.udf_range_expand_VGO(ar.[Totaling]) )
)
from [dbo].[ASL_Result_VGO] as ar
where ar.[Id] = ar_update.[Id] -- The multi-part identifier "ar_update.Id" could not be bound.
) ar_sum on ar_update.[Id] = ar_sum.[Id]
where ar_update.[Totaling Type] = 2 and -- formulas
len(trim(ar_update.[Totaling])) > 0 and
charindex('..', ar_update.[Totaling]) > 0 -- P ranges
this happened because on where ar.[Id] = ar_update.[Id] because ar_update.[Id] is not a value it is a multivalued type, it's like you were comparing a list with an integer, try this
update ar_update
set ar_update.[GLEntry_Amount] = ar_sum.[GLEntry_Amount_sum]
from [dbo].[ASL_Result_VGO] as ar_update
join (select ar.[Id] as [Id]
,ar.[Company Id] as [Company Id]
,ar.[Schedule Name] as [Schedule Name]
,ar.[Line No_] as [Line No_]
,ar.[Row No_] as [Row No_]
,ar.[Posting Date] as [Posting Date]
,ar.[Business Centre Code] as [Business Centre Code]
,ar.[Site Locations Code] as [Site Locations Code]
,GLEntry_Amount_sum = (select sum(are.[GLEntry_Amount])
from [dbo].[ASL_Result_VGO] as are
where are.[Company Id] = ar.[Company Id] and
are.[Schedule Name] = ar.[Schedule Name] and
are.[Posting Date] = ar.[Posting Date] and
are.[Business Centre Code] = ar.[Business Centre Code] and
are.[Site Locations Code] = ar.[Site Locations Code] and
are.[Row No_] in ( select * from dbo.udf_range_expand_VGO(ar.[Totaling]) )
)
from [dbo].[ASL_Result_VGO] as ar
) ar_sum on ar_update.[Id] = ar_sum.[Id]
where ar_update.[Totaling Type] = 2 and -- formulas
len(trim(ar_update.[Totaling])) > 0 and
charindex('..', ar_update.[Totaling]) > 0

Filtering by an ISNULL statement within SELECT statement

I'm stuck on a portion of code where I can not figure out how to use ISNULL correctly.
I'm trying to find all sales prices for an item by location and by variant code. The issue is that not all items with variants are priced in the sales price table at the variant level. I'm trying to say if there is no variant match, find where variant = ''.
SELECT [Item Ledger].[Location Code], [Item Ledger].[Item No_] AS "No_", [Item Ledger].[Variant Code]
, (SELECT [Item].[Description] FROM [Wings$Item] AS "Item" WHERE [Item].[No_] = [Item Ledger].[Item No_]) AS "Description"
, (SELECT [Item Variant].[Variant Dimension 1] FROM [Wings$Item Variant Registration] AS "Item Variant" WHERE [Item Variant].[Item No_] = [Item Ledger].[Item No_]
AND [Item Variant].[Variant] = [Item Ledger].[Variant Code]) AS "Color"
, (SELECT [Item Variant].[Variant Dimension 2] FROM [Wings$Item Variant Registration] AS "Item Variant" WHERE [Item Variant].[Item No_] = [Item Ledger].[Item No_]
AND [Item Variant].[Variant] = [Item Ledger].[Variant Code]) AS "Size"
, (SELECT [Item].[Vendor No_] FROM [Wings$Item] AS "Item" WHERE [Item].[No_] = [Item Ledger].[Item No_]) AS "Vendor No."
, (SELECT [Item].[Division Code] FROM [Wings$Item] AS "Item" WHERE [Item].[No_] = [Item Ledger].[Item No_]) AS "Division Code"
, (SELECT [Item].[Item Category Code] FROM [Wings$Item] AS "Item" WHERE [Item].[No_] = [Item Ledger].[Item No_]) AS "Item Category Code"
, (SELECT [Item Category].[Description] FROM [Wings$Item Category] AS "Item Category" WHERE [Item Category].[Code] = [Item Ledger].[Item Category Code]) AS "Item Cat. Description"
, (SELECT [Item].[Product Group Code] FROM [Wings$Item] AS "Item" WHERE [Item].[No_] = [Item Ledger].[Item No_]) AS "Product Group Code"
, (SELECT [Product Group].[Description] FROM [Wings$Product Group] AS "Product Group" WHERE [Product Group].[Code] = [Item Ledger].[Product Group Code]) AS "Prod. Group Description"
, (SELECT CAST([Item].[Last Direct Cost] AS decimal(18,2)) FROM [Wings$Item] AS "Item" WHERE [Item].[No_] = [Item Ledger].[Item No_]) AS "Last Cost"
, (SELECT DISTINCT CAST(MAX([Sales Price].[Unit Price]) as decimal(18,2))
FROM [Wings$Sales Price] AS "Sales Price", [Wings$Store Price Group] AS "Store Price"
WHERE EXISTS
(SELECT DISTINCT [Store Price].[Store]
, [Sales Price].[Item No_]
, (SELECT [Item].[Description] FROM [Wings$Item] AS "Item" WHERE [Item].[No_] = [Sales Price].[Item No_]) AS "Description"
, [Sales Price].[Variant Code]
, [Sales Price].[Sales Code]
, CAST([Sales Price].[Unit Price] as decimal(18,2)) AS "Unit Price"
FROM [Wings$Sales Price] AS "Sales Price" JOIN [Wings$Picking _ Receiving lines] AS "Picking Lines" ON [Sales Price].[Item No_] = [Picking Lines].[Item No_]
JOIN [Wings$Store Price Group] AS "Store Price" ON [Store Price].[Price Group Code] = [Sales Price].[Sales Code]
AND [Store Price].[Store] = [Picking Lines].[Shortcut Dimension 1 Code] WHERE [Sales Price].[Starting Date] > [Sales Price].[Ending Date])
AND [Store Price].[Store] = [Item Ledger].[Location Code] AND [Sales Price].[Item No_] = [Item Ledger].[Item No_]
AND [Sales Price].[Variant Code] = [Item Ledger].[Variant Code] AND [Store Price].[Price Group Code] = [Sales Price].[Sales Code]) AS "Retail Price"
, (SELECT MIN([Purch Rec].[Posting Date]) FROM [Wings$Purch_ Rcpt_ Line] AS "Purch Rec" WHERE [Purch Rec].[No_] = [Item Ledger].[Item No_] AND [Purch Rec].[Variant Code] = [Item Ledger].[Variant Code])
AS "First Rec Date"
, (SELECT MAX([Purch Rec].[Posting Date]) FROM [Wings$Purch_ Rcpt_ Line] AS "Purch Rec" WHERE [Purch Rec].[No_] = [Item Ledger].[Item No_] AND [Purch Rec].[Variant Code] = [Item Ledger].[Variant Code])
AS "Last Rec Date"
, (SELECT MAX([Sales Line].[Date]) FROM [Wings$Trans_ Sales Entry] AS "Sales Line" WHERE [Sales Line].[Item No_] = [Item Ledger].[Item No_] AND [Sales Line].[Variant Code] = [Item Ledger].[Variant Code]
AND [Sales Line].[Store No_] = [Item Ledger].[Location Code]) AS "Last Sold Date"
, (SELECT CAST(SUM([Value Entry].[Item Ledger Entry Quantity]) AS decimal(18,0)) FROM [Wings$Value Entry] AS "Value Entry" WHERE [Value Entry].[Item No_] = [Item Ledger].[Item No_]
AND [Value Entry].[Variant Code] = [Item Ledger].[Variant Code] AND [Value Entry].[Location Code] = [Item Ledger].[Location Code] AND [Value Entry].[Posting Date] <= '20161201') AS "Beg. Balance"
, (SELECT CAST(SUM([Transfer Shipment].[Quantity]) AS decimal(18,0)) FROM [Wings$Transfer Shipment Line] AS "Transfer Shipment" WHERE [Transfer Shipment].[Item No_] = [Item Ledger].[Item No_]
AND [Transfer Shipment].[Variant Code] = [Item Ledger].[Variant Code] AND [Transfer Shipment].[Transfer-from Code] = [Item Ledger].[Location Code]) AS "Outbound Transfers"
, (SELECT CAST(SUM([Transfer Receipt].[Quantity]) AS decimal(18,0)) FROM [Wings$Transfer Receipt Line] AS "Transfer Receipt" WHERE [Transfer Receipt].[Item No_] = [Item Ledger].[Item No_]
AND [Transfer Receipt].[Variant Code] = [Item Ledger].[Variant Code] AND [Transfer Receipt].[Transfer-to Code] = [Item Ledger].[Location Code]) AS "Inbound Transfers"
, (SELECT CAST(SUM([Purch. Rec.].[Quantity]) AS decimal(18,0)) FROM [Wings$Purch_ Rcpt_ Line] AS "Purch. Rec." WHERE [Purch. Rec.].[No_] = [Item Ledger].[Item No_]
AND [Purch. Rec.].[Variant Code] = [Item Ledger].[Variant Code] AND [Purch. Rec.].[Location Code] = [Item Ledger].[Location Code]) AS "YTD Purchase Receipts"
, (SELECT CAST(SUM([Transaction Register].[Quantity]) as decimal(18,0)) FROM [Wings$Trans_ Sales Entry] AS "Transaction Register"
WHERE [Item Ledger].[Item No_] = [Transaction Register].[Item No_] AND [Item Ledger].[Variant Code] = [Transaction Register].[Variant Code]
AND [Item Ledger].[Location Code] = [Transaction Register].[Store No_]) AS "YTD Sales"
, (SELECT CAST(SUM([Transaction Register].[Quantity]) as decimal(18,0)) FROM [Wings$Trans_ Sales Entry] AS "Transaction Register"
WHERE [Item Ledger].[Item No_] = [Transaction Register].[Item No_] AND [Item Ledger].[Variant Code] = [Transaction Register].[Variant Code]
AND [Item Ledger].[Location Code] = [Transaction Register].[Store No_] AND [Transaction Register].[Date] >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -1, 0)
AND [Transaction Register].[Date] < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) , 0)) AS "Last Month Sales"
, (SELECT CAST(SUM([Transaction Register].[Quantity]) as decimal(18,0)) FROM [Wings$Trans_ Sales Entry] AS "Transaction Register"
WHERE [Item Ledger].[Item No_] = [Transaction Register].[Item No_] AND [Item Ledger].[Variant Code] = [Transaction Register].[Variant Code]
AND [Item Ledger].[Location Code] = [Transaction Register].[Store No_] AND [Transaction Register].[Date] >= DATEADD(DAY, 1-DAY(GETDATE()), GETDATE())
AND [Transaction Register].[Date] < EOMONTH(GETDATE())) AS "Current Month Sales"
, (SELECT CAST(SUM([Transfer Line].[Outstanding Quantity]) AS decimal(18,0)) FROM [Wings$Transfer Line] AS "Transfer Line" WHERE [Transfer Line].[Item No_] = [Item Ledger].[Item No_]
AND [Transfer Line].[Variant Code] = [Item Ledger].[Variant Code] AND [Transfer Line].[Transfer-to Code] = [Item Ledger].[Location Code]) AS "Current In-Transit to Store"
, (SELECT CAST(SUM([Item Ledger].[Quantity]) as decimal(18,0))) AS "Current on Hand"
, (SELECT CAST(SUM([Value Entry].[Item Ledger Entry Quantity]) as decimal(18,0)) FROM [Wings$Value Entry] AS "Value Entry" WHERE [Value Entry].[Item No_] = [Item Ledger].[Item No_]
AND [Value Entry].[Variant Code] = [Item Ledger].[Variant Code] AND [Value Entry].[Location Code] = [Item Ledger].[Location Code] AND [Value Entry].[Location Code] LIKE '9%') AS "Whse Inv."
, (SELECT CAST(SUM([Value Entry].[Item Ledger Entry Quantity]) as decimal(18,0)) FROM [Wings$Value Entry] AS "Value Entry" WHERE [Value Entry].[Item No_] = [Item Ledger].[Item No_]
AND [Value Entry].[Variant Code] = [Item Ledger].[Variant Code] AND [Value Entry].[Location Code] = [Item Ledger].[Location Code] AND [Value Entry].[Location Code] NOT LIKE '9%') AS "Store Inv."
, (SELECT CAST(SUM([Purchase Line].[Outstanding Quantity]) as decimal(18,0)) FROM [Wings$Purchase Line] AS "Purchase Line" WHERE [Item Ledger].[Item No_] = [Purchase Line].[No_]
AND [Purchase Line].[Location Code] = [Item Ledger].[Location Code] AND [Purchase Line].[Variant Code] = [Item Ledger].[Variant Code]) AS "Qty. on PO"
FROM [Wings$Item Ledger Entry] AS "Item Ledger", [Wings$Store Price Group] AS "Store Price" WHERE [Item Ledger].[Location Code] = [Store Price].[Store] AND [Item Ledger].[Location Code] = [Store Price].[Store]
GROUP BY [Item Ledger].[Location Code], [Item Ledger].[Item No_], [Item Ledger].[Variant Code], [Item Ledger].[Item Category Code], [Item Ledger].[Product Group Code], [Store Price].[Price Group Code], [Store Price].[Store]

Combobox on DataGridView

I have a combobox on a datagridview. I am able to populate the values of the combobox, but what I am failing to do is to populate the data base value into the combobox "text" when the datagridview is loading.
I hope this makes sense. Basically I just want the data returned to reflect in the combobox Display.
Your assistance will be highly appreciated.
Dim cbo As DataGridViewComboBoxColumn
If GlobalVariables.UsrSite = 0 Then
SQL.ReadQuery("Select LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down]
from UDOData where status = 'Down' order by [Days Down] Desc")
Else
SQL.ReadQuery("Select LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down] from UDOData
Where SiteID = " & GlobalVariables.UsrSite & " and Status = 'Down' order by [Days Down] Desc")
End If
dgvUDO.DataSource = SQL.SQLDS.Tables(0)
cbo.DataSource = SQL.SQLDS.Tables(0)
cbo.DisplayMember = "Logs Update"
cbo.ValueMember = "Logs Update"
cbo.DataPropertyName = "Logs Update"
I get exception error saying "logs Update" does not exist, but it loads in the datagridview.
I ended up doing this, which worked for me.
Dim cbo As DataGridViewComboBoxColumn
cbo = dgvUDO.Columns("cboLogsUDO")
Dim AltDb As New DataTable
SQL.ReadQuery("Select Descrip from dbo.UDOLogs")
AltDb = SQL.SQLDS.Tables(0)
'LOAD DATA GRID VIEW DATA
If GlobalVariables.UsrSite = 0 Then
SQL.ReadQuery("Select ID, LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down]
from UDOData where status = 'Down' order by [Days Down] Desc")
Else
SQL.ReadQuery("Select ID, LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down] from UDOData
Where SiteID = " & GlobalVariables.UsrSite & " and Status = 'Down' order by [Days Down] Desc")
End If
'SET DATA GRIDVIEW DATA SOURCE
dt = SQL.SQLDS.Tables(0)
dgvUDO.DataSource = dt
dgvUDO.Rows(0).Selected = True
'LOAD UPDATE COMMAND FOR DATA GRIDVIEW CHANGES
SQL.SQLDA.UpdateCommand = New SqlClient.SqlCommandBuilder(SQL.SQLDA).GetUpdateCommand
'HIDE UNWANTED COLUMNS
dgvUDO.Columns("Logs Update").Visible = False
dgvUDO.Columns("ID").Visible = False
'SET COMBOBOX ATTRIBUTES
cbo.DataSource = AltDb
cbo.DisplayMember = "Descrip"
cbo.ValueMember = "Descrip"
cbo.DataPropertyName = "Logs Update"
This gives me the ability to call the value in the returned db for the datagridview as the combobox "text" value, but show the drop down items as the items in another table.

SQL Conversion Error on Inner Join Statement - Not Converting Anything

I've used this site for research for years but can't figure this one out from my research. Getting the following error:
Conversion failed when converting the varchar value '38221.0' to data type int.
This '38221.0' correlates to the 'CP.DNCP_PATIENTID' from the below code:
SELECT PAT.V51_PRSNPERSONNUMBER AS PSNNMBR,
PAT.V51_PRSNFIRSTNAME AS FIRSTNAME,
PAT.V51_PRSNMIDDLENAME AS MIDDLENAME,
PAT.V51_PRSNLASTNAME AS LASTNAME,
ADDR.V8_PERSONHOMEADDRLINE1 AS ADDRESS,
ADDR.V8_PERSONHOMEADDRLINE2 AS ADDRESS2,
ADDR.V8_PERSONHOMEADDRCITY AS CITY,
ADDR.V8_PERSONHOMEADDRSTATE AS STATE,
ADDR.V8_PERSONHOMEADDRZIP AS ZIP,
PAT.V51_PRSNDOB AS DOB,
PAT.V51_PRSNSEX AS SEX,
PAT.V51_PRSNSSN AS SSN,
ADDR.V8_PERSONHOMEPHONENUMBER AS HOMEPHONE,
ADDR.V8_PERSONMOBILEPHONENUMBER AS MOBILEPHONE,
CV.V9_DNPLANSORTNAME AS INSPLAN,
CV.V9_DNCARRIERNAME AS INSCARRIER,
COORD.PCCUSERNAME AS CARECOORDINATOR
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
LEFT OUTER JOIN IPATIENT IPAT
ON (CP.OFFICE_ID = IPAT.OFFICE_ID AND CP.DNCP_PATIENTID = IPAT.PATDNPERSONNUMBER)
LEFT OUTER JOIN I_VPATCARECOORD COORD
ON (IPAT.OFFICE_ID = COORD.OFFICE_ID AND IPAT.PATCARECOORDINATORID = COORD.PCCMMUSERSID)
INNER JOIN (
SELECT CP.DNCP_PATIENTID AS PSNNMBR,
MAX(CP.DNCP_FROMSERVICEDATE) AS SVCDT
--MIN(CP.DNCP_AMOUNT_CHARGE) AS CHGAMT
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
WHERE CP.OFFICE_ID = 'EDQV'
AND CP.DNCP_ACTIVITYTYPE = 'CHG'
AND (CP.DNCP_PROCEDURECODE LIKE '99490' OR CP.DNCP_PROCEDURECODE LIKE 'CCM')
AND (DATEDIFF(DAY,CP.DNCP_POSTDATE,GETDATE())) < 180
GROUP BY CP.DNCP_PATIENTID
) AS MAXSVCDATE
ON (CP.DNCP_PATIENTID = MAXSVCDATE.PSNNMBR AND CP.DNCP_FROMSERVICEDATE = MAXSVCDATE.SVCDT) --AND CP.DNCP_AMOUNT_CHARGE = MAXSVCDATE.CHGAMT)
WHERE CP.OFFICE_ID = 'EDQV'
As I'm not converting anything, I'm unsure why I'm getting this error. I've used a variation of this coding on many projects and never received the error. I appreciate any help. Thanks!
Sorry, I got it. I was thinking it was in my INNER JOIN but it was in one of the other joins. I was joining a PERSONNUMBER to a PERSONID. I fixed it and everything works smoothly now. A case of staring at it too long and not seeing what's right in front of you. Thank you for your time.
Try CONV(N,from_base,to_base),it converts numbers between different number bases. It returns a string representation of the number N, converted
from base from_base to to_base