For background: this works fine without the addition of the 'E' Table in production.
I am trying to execute the below query but am getting the error:
Msg 8114, Level 16, State 5, Line 10
Error converting data type varchar to numeric.
The odd thing is that it executes fine on our test DB - not seeing anything trying to convert to numeric so a bit baffled. Not really sure what would cause that difference as I assume data types are the same in the test DB and the production DB - any ideas?
Thanks.
/* - adding po balance qty causes duplicates
- adding stock category causes duplicates
- added PO comments using MAX function to return only first comment - gets rid of duplicates*/
/* Using NEW employee table as of 11/2 */
SELECT
S.team_member_name AS [ASSIGNED EMPLOYEE], --Using new employee table - only on bus unit as of 11/2
H.po_type AS [ PO TYPE],
G.order_no AS [GPS PO #],
P.po_number AS [SAP PO NUMBER],
M.department AS [DEPARTMENT],
G.order_qty AS [GPS QTY],
SUM(P.po_ordered_quantity) AS [SAP QUANTITY],
(SUM(P.po_ordered_quantity) - G.order_qty) AS [DIFFERENCE],
G.last_conf_date_cst AS [LAST CONFIRMED DATE],
K.business_unit_desc AS [BU],
M.[description] AS [DESCRIPTION],
P.material AS [MATERIAL],
MAX(P.comment) AS [PO COMMENT],
MIN(E.date) AS [FIRST SHOWN ON RPT]
FROM
(SELECT
order_no, order_qty, order_status,
last_conf_date_cst
FROM
asagdwpdx_prod.dbo.SimoxOrder1
UNION ALL
SELECT
order_no, order_qty, order_status,
last_conf_date_cst
FROM
asagdwpdx_prod.dbo.SimoxOrder2
UNION ALL
SELECT
order_no, order_qty, order_status,
last_conf_date_cst
FROM
asagdwpdx_prod.dbo.SimoxOrder3) G
JOIN
pdx_sap_user.dbo.vw_po_header H ON G.order_no = h.ahag_number
JOIN
pdx_sap_user.dbo.vw_po_item P ON H.po_number = P.po_number
JOIN
pdx_sap_user.dbo.vw_mm_material M ON P.material = M.material
JOIN
adi_user_maintained.dbo.SCM_PO_Employee_Name S ON P.po_number = S.po_number
JOIN
pdx_sap_user.dbo.vw_kd_business_unit K ON M.business_unit_code = K.business_unit_code
JOIN
adi_user_maintained.dbo.scm_po_error_tracking E ON E.po_number = P.po_number
WHERE
M.business_segment_code NOT IN ('421', '420', '422', '424') --exclude adi golf
AND E.report_source = 'gpsvssap_qty'
GROUP BY
G.order_no, -- GROUP BY needed on aggregate function in SELECT
G.order_qty,
G.order_status,
P.po_number,
P.material,
P.del_indicator,
H.po_created_by,
M.[description],
M.department,
S.team_member_name,
K.business_unit_desc,
G.last_conf_date_cst,
H.po_type
HAVING
G.order_qty <> SUM(P.po_ordered_quantity)
AND G.order_status NOT IN ('60', '90') -- excluding GPS cancelled (90) & shipped (60) - do we need to exclude other status'?
AND P.del_indicator <> 'L'
You might want to look at 'MAX(P.comment)'. You cant find Max of string. Unless your comment is numeric
Related
This is my code for a query to produce a specific report. This works, however when I added the last select statement
(Select Name
from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10) AS [Application]
The RI1.Cust_10 column holds multiple values delimited by commas. How can I get it so that the look up table pulls each value and provides the correct name for that value? I cannot create or modify the tables within this database.
select
RI1.incident_id as [Project Incident #],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_02) as [Business Name],
RI1.NAME as [Project Name],
RI1.INCIDENT_STATUS_NAME as [Phase],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_09) as [Key Milestone Name],
convert(nvarchar(10), RI1.CUST_26,103) as [Key Milestone Date], -- leave as date
convert(nvarchar(10), RI1.CUST_29,103) as [Target Completion Date], -- leave as date
RI1.SEVERITY_NAME as [Status Color],
RI1.CUST_01 as [Status Summary],
RI1.OWNER_NAME as [IT Owner],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10) AS [Application]
from
RPT_INCIDENTS RI1
where
RI1.PROJECT_ID = 445
and RI1.IS_DELETED = 0
and (RI1.INCIDENT_STATUS_NAME <> '5.1-Cancelled' and RI1.INCIDENT_STATUS_NAME <> '5.2-Completed')
My output should be, however, the last column should have names not values. The values are from the Lookup table and I need a way to pull that data so that the values are now names.
Report Output
I can't test without your data, but hopefully it will work for you:
select RI1.incident_id as [Project Incident #]
, [Business Name] = s1.Name
,RI1.NAME as [Project Name]
,RI1.INCIDENT_STATUS_NAME as [Phase]
, [Key Milestone Name] = s2.Name
,convert(nvarchar(10), RI1.CUST_26,103) as [Key Milestone Date] -- leave as date
,convert(nvarchar(10), RI1.CUST_29,103) as [Target Completion Date] -- leave as date
,RI1.SEVERITY_NAME as [Status Color]
,RI1.CUST_01 as [Status Summary]
,RI1.OWNER_NAME as [IT Owner]
, [Application] = LEFT(s3.App,LEN(APP) - SIGN(LEN(s3.APP)))
From RPT_INCIDENTS AS RI1
OUTER APPLY (Select TOP 1 i.Name from RPT_CUSTOM_LIST_VALUES as i where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_02) as s1
OUTER APPLY (Select TOP 1 i.Name from RPT_CUSTOM_LIST_VALUES as i where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_09) as s2
OUTER APPLY (SELECT App = (
Select i.Name + ','
from RPT_CUSTOM_LIST_VALUES as i
where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10
FOR XML PATH(''))
) as s3
where RI1.PROJECT_ID = 445
and RI1.IS_DELETED = 0
and (RI1.INCIDENT_STATUS_NAME <> '5.1-Cancelled' and RI1.INCIDENT_STATUS_NAME <> '5.2-Completed')
I want to group by Quote ID, I am not able to get it to work though. I've been banging my head on this one for a while.
EDIT: I'm trying to group the Quantity items into one column, so for example if a Quote ID column has 3 items with the same Quote ID but different quantities, I want to display them in the same row in this fashiong - $QuoteID, $QuantityFromRow1, $QuantityFromRow2, $QuantityFromRow3.
SELECT TOP(100)
PPV8.dbo.CUSTOMER.ID As [Customer ID]
, PPV8.dbo.CUSTOMER.NAME As [Customer]
, PPV8.dbo.CUSTOMER.CITY As City, Null As [PM]
, PPV8.dbo.CUSTOMER.STATE As State
, PPV8.dbo.QUOTE_PRICE.QUOTE_ID As [Quote ID]
, FLOOR(PPV8.dbo.QUOTE_PRICE.QTY) As Quantity
, PPV8.dbo.QUOTE_LINE.PART_ID As [Part ID]
, SUBSTRING(PPV8.dbo.QUOTE_LINE.CUSTOMER_PART_ID, 6, 12) As [Style #]
, PPV8.dbo.QUOTE_LINE.DESCRIPTION As Description
, PPV8.dbo.QUOTE_PRICE.UNIT_PRICE As [Unit Price]
, PPV8.dbo.QUOTE_LINE.CREATE_DATE As [Create Date]
, PPV8.dbo.QUOTE.SALESREP_ID As [Rep]
, PPV8.dbo.QUOTE.STATUS As [Quote Status]
, PPV8.dbo.QUOTE.FOLLOWUP_DATE As [Follow Up]
, PPV8.dbo.QUOTE.EXPECTED_WIN_DATE As [Due Date]
FROM
CUSTOMER
INNER JOIN
QUOTE ON PPV8.dbo.CUSTOMER.ID = PPV8.dbo.QUOTE.CUSTOMER_ID
INNER JOIN
QUOTE_LINE ON PPV8.dbo.QUOTE.ID = PPV8.dbo.QUOTE_LINE.QUOTE_ID
INNER JOIN
QUOTE_PRICE ON PPV8.dbo.QUOTE_LINE.QUOTE_ID = PPV8.dbo.QUOTE_PRICE.QUOTE_ID
AND PPV8.dbo.QUOTE_LINE.LINE_NO = PPV8.dbo.QUOTE_PRICE.QUOTE_LINE_NO
WHERE
PPV8.dbo.QUOTE.STATUS = 'A'
AND PPV8.dbo.QUOTE.EXPECTED_WIN_DATE > '20170101'
ORDER BY
PPV8.dbo.QUOTE.EXPECTED_WIN_DATE ASC
All (non aggregate) columns in the select statement must be included in the group by clause
So you are missing the point of group by
I need a query for [Contribution]. I used this query:
with ttt as
(
select
(DYG.U_StyleId)[DYG Style]
,Max(O1.CardCode) [Party Group Code],
MAX(O1.CardName) [Party Group Name]
,MAX(OR1.DocDate) [Date]
,sum(CONVERT(NUMERIC(15,2),(RDR1.PriceBefDi*RDR1.Quantity))) [JobAmount]
,CONVERT(NUMERIC(15,2),SUM(RDR1.Quantity)) [Mtr]
,CONVERT(NUMERIC(15,2),SUM(RDR1.U_Pcs))[Pcs]
,(select sum(RDR1.PriceBefDi*RDR1.Quantity) from RDR1) tqty
from
ORDR OR1
left join RDR1 on RDR1.DocEntry = OR1.DocEntry
left join OITM on RDR1.ItemCode = oitm.ItemCode
LEFT JOIN OCRD ON OCRD.CardCode = OR1.CardCode
LEFT JOIN OCRG ON OCRG.GroupCode = OCRD.GroupCode
LEFT JOIN OCRD O1 ON O1.U_BCode = OCRD.U_GrpCod
LEFT JOIN
( SELECT U_StyleId FROM RDR1 WHERE U_StyleId in
('BLOOM','BLOOMING','DYD','DYD-R','DYED','Ex.CLR.','RAINBOW'))
DYG ON DYG.U_StyleId = RDR1.U_StyleId
group by
DYG.U_StyleId
)
select
Style, [Party Group Code],
[Party Group Name], JobAmount,
(sum(JobAmount) / tqty * 100) [Contribution],
[Date], [Pcs]
from
ttt
group by
Style
I need Sum of last jobamount to divide it with above tqty.
But it shows this error.
'Column 'ttt.Party Group Code' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.'
Please help me with the query to get right [Contribution] amount.
Try this:
select Style,[Party Group Code],[Party Group Name],JobAmount,[Date],[Pcs],
100.0 * (sum(JobAmount) OVER (PARTITION BY Style))/tqty [Contribution]
from ttt;
I have the following code to insert data from a table in another database, but how can I insert into a primary key column by incrementing the last record in ID column value by 1? In my case [WORK ORDER #] is a primary key it doesn't allow null.
[WORK ORDER #] is nvarchar(10)
INSERT INTO DB1.dbo.WORKORDERS ([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
SELECT *
FROM OPENQUERY([DB29],
'SELECT DISTINCT
NULL, --need to set auto increment value here
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference)
FROM Customers
INNER JOIN JrnlHdr ON Customers.CustomerRecordNumber = JrnlHdr.CustVendId
WHERE JrnlHdr.JrnlKey_Journal = 11
AND JrnlHdr.TransactionDate = CURDATE()
-------------------// i tried as follows-----
--> You only do this one time...not with each query
create sequence dbo.WorkOrderSequence
as int
start with 43236
--> I took out the part that failed (you got option 1 and 3 kinda
--> mashed together)
insert DB1.dbo.WORKORDERS
([WORK ORDER #],[CUSTOMER],[SO DATE],[SO NUMBER],[ASSY PN-S],[CUSTOMER PN],[SHIP VIA],[PROMISED DATE],[COMMENTS],[PO #],[WO Notes])
select
convert(varchar(10), next value for DB1.dbo.WorkOrderSequence ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference,
x.ItemID,
x.PartNumber,
x.WhichShipVia,
x.ShipByDate,
x.Comment2,
x.CustomerInvoiceNo,
x.SalesDescription
from
openquery
([DB29],
'select distinct
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference,
LineItem.ItemID ,
LineItem.PartNumber,
Customers.WhichShipVia,
JrnlHdr.ShipByDate,
JrnlHdr.Comment2,
JrnlHdr.CustomerInvoiceNo,
LineItem.SalesDescription
FROM Customers
INNER JOIN JrnlHdr
ON Customers.CustomerRecordNumber = JrnlHdr.CustVendId
LEFT OUTER JOIN Address
ON Customers.CustomerRecordNumber = Address.CustomerRecordNumber
INNER JOIN JrnlRow
ON JrnlHdr.PostOrder = JrnlRow.PostOrder
INNER JOIN LineItem
ON JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber
WHERE JrnlHdr.JrnlKey_Journal = 11 AND JrnlHdr.TransactionDate = CURDATE()
AND JrnlHdr.PostOrder = JrnlRow.PostOrder
AND JrnlHdr.CustVendId = Customers.CustomerRecordNumber
AND JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber
AND JrnlHdr.POSOisClosed = 0'
) as x
Option 1
If you're on at least SQL Server 2012 (you didn't mention a specific version), you have a general sequence number generator that you can use. I like it a lot for this kind of scenario. In the DB1 database, you'd add your sequence like this:
create sequence dbo.WorkOrderSequence
as int
start with 5002230 --> pick a starting number greater
--> than any existing [WorkOrder #]
Then, you can just get the next number(s) in your insert-for-select statement:
insert DB1.dbo.WORKORDERS
([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
select
convert(varchar(10), next value for DB1.dbo.WorkOrderSequence ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference
from
openquery
([DB29],
'select distinct
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference
from
Customers
inner join
JrnlHdr on
Customers.CustomerRecordNumber = JrnlHdr.CustVendId
where
JrnlHdr.JrnlKey_Journal = 11
and
JrnlHdr.TransactionDate = CURDATE()'
) as x
The sequence is a standalone auto-incrementing number. Every time you use the next value for dbo.WorkOrderSequence, it auto-increments. This way, you don't have to modify any table definitions.
Option 2
Alternatively, you could alter the DB1.dbo.WORKORDERS table so that the default value to use the expression...
alter table dbo.WORKORDERS
alter column [Work Order #] nvarchar(10) not null
default( convert( nvarchar(10), next value for dbo.WorkOrderSequence ) )
If you do this, then you can completely omit inserting the [Work Order #] altogether and let the default do the magic.
Option 3
If you're not on 2012, but on at least 2008, you can still get there...but it's a little trickier because you have to get the current starting [Work Order #]:
insert DB1.dbo.WORKORDERS
([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
select
convert(varchar(10), x.RowNum + y.MaxOrderNum ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference
from
openquery
([DB29],
'select distinct
row_number() over( order by JrnlHdr.TransactionDate ) as RowNum,
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference
from
Customers
inner join
JrnlHdr on
Customers.CustomerRecordNumber = JrnlHdr.CustVendId
where
JrnlHdr.JrnlKey_Journal = 11
and
JrnlHdr.TransactionDate = CURDATE()'
) as x
cross join
(
select
convert( int, max( [Work Order #] ) ) as MaxOrderNum
from
Db1.dbo.WORKORDERS
) as y
Option 4
If you're on something earlier than 2008...you'll probably want a stored procedure to do the insert in two steps: inserting the work orders into a temporary table (one with an auto-increment [Work Order #] starting at the current table's max( [Work Order #] ) + 1 )...and then step 2 would insert the temp table into WORKORDERS with a convert.
I've dabbled a little with SQL but not to create tables as much. I have used postgreSQL along with knex to do so and I believe the solution that should fit for your needs as well is using the value unique. I apologize if that's not correct since I am junior to coding, but hopefully looking at this will help or looking at the SQL docs :) difference between primary key and unique key
I have a query that should return the MAX CloseDate from a table that has many close dates, grouped by the ShipmentNumber column.
I seem to be getting more than one ShipmentNumber in the results when I include the Notes column.
table WorkOrders
WorkOrderID, ProjectNumber, ShipmentNumber, CloseDate, WorkOrderNotes
1, 884, 123-01, 2016-04-01, Note A
2, 884, 123-01, 2016-04-02, Note B
3, 884, 123-02, 2016-04-01, Note A
table Projects
ProjectNumber, Name
884, Project A
Query:
SELECT p.id, p.name, WO.ShipmentNumber,MAX(WO.CloseDate) AS CloseDate,
CAST(WO.WorkOrderNotes AS NVARCHAR(100)) AS WorkOrderNotes
FROM DA.dbo.WorkOrders AS WO
LEFT JOIN Projects.dbo.project_primary AS p ON p.id = WO.ProjectNumber
WHERE WO.CloseDate >= '2016-03-01'
AND WO.CloseDate IS NOT NULL
GROUP BY WO.ShipmentNumber, p.name, p.id, CAST(WO.WorkOrderNotes AS NVARCHAR(100)) ;
Results:
884, Project A, 123-01, 2016-04-01, Note A <-- Should not be here
884, Project A, 123-01, 2016-04-02, Note B
884, Project A, 123-02, 2016-04-01, Note A
#Irb got this in the comments; you're grouping by WO.ShipmentNumber and have different values for this for the first and third line; thus getting both.
If you want only the WO record with the max close date, try this:
SELECT p.id
, p.name
, WO.ShipmentNumber
, WO.CloseDate
FROM (
select ProjectNumber
, ShipmentNumber
, CloseDate
, CAST(WorkOrderNotes AS NVARCHAR(100)) AS WorkOrderNotes
, row_number() over (partition by ProjectNumber order by CloseDate desc) r
from DA.dbo.WorkOrders
WHERE WO.CloseDate >= '2016-03-01'
AND WO.CloseDate IS NOT NULL
) AS WO
LEFT JOIN Projects.dbo.project_primary AS p
ON p.id = WO.ProjectNumber
WHERE WO.r = 1 --only return the record with the most max CloseDate
GROUP BY p.id
, p.name
, WO.ShipmentNumber
, WO.WorkOrderNotes