Causes of duplicate results appearing in Crystal Reports - crystal-reports

I have a crystal report using the SQL command below.
I am wondering if anyone can assist me with the reason why I am getting duplicate people appearing in my report.
At the moment I get multiple lines for a sales account manager (SAM),
I want one line per SAM.
My 1st Group Header is based on the Sector Code,
2nd group header is on command.sector,
3rd group header is on SAM
SELECT DISTINCT
'Actuals' as [Trans Type],
ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO as [Order Date],
ER101_ACCT_ORDER_DTL.ER101_ENT_DATE_ISO as [Ent Date],
year(dateadd(month,6,ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYR],
MONTH(DATEADD(M, 6, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYP],
DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO), 0) as [PStart],
DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO) + 1, 0)) as [Pend],
Revenue =
Case
When ER101_ACCT_ORDER_DTL.ER101_PHASE = '1' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N'
then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG
When ER101_ACCT_ORDER_DTL.ER101_PHASE = '5' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N'
then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG
Else 0
End,
isnull(MM405_USER_NAME,'No SAM') AS [SAM],
ER101_ACCT_ORDER_DTL.ER101_DESC as [Desc],
EV200_EVENT_MASTER.EV200_EVT_ID as [EventID],
isnull(SALESACCT.EV870_TYPE,'NoSec') as [Sector],
isnull(CCRPF_ACCT_TYPE_DESC,'ZZ - No Sector') as [Sector Name],
EV130_STATUS_MASTER.EV130_STATUS_DESC as [Event Status Desc],
isnull(EV800_USER_FLD_6X,'N') as [Budgeted]
FROM ER101_ACCT_ORDER_DTL WITH (NOLOCK)
LEFT OUTER JOIN EV700_FUNC_MASTER WITH (NOLOCK)
ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV700_FUNC_MASTER.EV700_ORG_CODE
AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV700_FUNC_MASTER.EV700_EVT_ID
AND ER101_ACCT_ORDER_DTL.ER101_FUNC_ID = EV700_FUNC_MASTER.EV700_FUNC_ID
LEFT OUTER JOIN EV800_SPACE_MASTER WITH (NOLOCK)
ON EV700_FUNC_MASTER.EV700_SPACE = EV800_SPACE_MASTER.EV800_SPACE_CODE
AND EV700_FUNC_MASTER.EV700_ORG_CODE = EV800_SPACE_MASTER.EV800_ORG_CODE
LEFT OUTER JOIN EV200_EVENT_MASTER WITH (NOLOCK)
ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV200_EVENT_MASTER.EV200_EVT_ID
LEFT OUTER JOIN EV870_ACCT_MASTER WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV870_ACCT_MASTER.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_SLSPER = EV870_ACCT_MASTER.EV870_ACCT_CODE
LEFT OUTER JOIN EV870_ACCT_MASTER SALESACCT WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = SALESACCT.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = SALESACCT.EV870_ACCT_CODE
LEFT OUTER JOIN CCRPF_ACCT_TYPES WITH (NOLOCK)
ON SALESACCT.EV870_TYPE = CCRPF_ACCT_TYPE
LEFT OUTER JOIN MM405_USER_MASTER_EXT WITH (NOLOCK)
ON EV870_ACCT_MASTER.EV870_USERID = MM405_USER_MASTER_EXT.MM405_USER_ID
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
LEFT OUTER JOIN EV370_RES_MASTER WITH (NOLOCK)
ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV370_RES_MASTER.EV370_ORG_CODE
AND ER101_ACCT_ORDER_DTL.ER101_NEW_RES_TYPE = EV370_RES_MASTER.EV370_NEW_RES_TYPE
AND ER101_ACCT_ORDER_DTL.ER101_RES_CODE = EV370_RES_MASTER.EV370_RES_CODE
WHERE EV200_ORG_CODE = '10' and EV200_EVT_STATUS in ('33', '34', '36', '49','50', '52') and NOT(EV200_EVT_TYPE IN ('MOS','MP','GB'))
and ER101_RES_CLASS <> '8' and ER101_ORD_TYPE = 'SO'
and not(EV370_RES_MASTER.EV370_MAJOR_GROUP in ('SRVCHR', 'EBOND', 'SCHARG','GIFTC'))
UNION
Select 'Targets' as [Trans Type],
MM012_PERIOD_STDATE as [Order Date],
GL010_ENT_STAMP as [Ent Date],
MM012_FISCAL_YR as [FYR],
MM012_FISCAL_PERIOD as [FYP],
MM012_PERIOD_STDATE as [PStart],
MM012_PERIOD_ENDATE as [Pend],
GL010_BUD_PTD as [Revenue],
'Budget SAM' as [SAM],
GL010_ACCOUNT as [Desc],
null as [Event ID],
GL001_SHORT_DESC as [Sector],
[Sector Name] =
Case
When CCRPF_ACCT_TYPE_DESC is null then 'OLD - ' + GL001_DESC
Else CCRPF_ACCT_TYPE_DESC
End,
'Budget Event Status Desc' as [Event Status Desc],
'Targets' as [Budgeted]
FROM GL010_BALANCES WITH (NOLOCK)
LEFT OUTER JOIN GL001_ACCOUNT_MASTER
WITH (NOLOCK) ON GL010_ORG_CODE = GL001_ORG_CODE
AND GL010_ACCOUNT = GL001_ACCOUNT
LEFT OUTER JOIN MM012_FISCAL_PERIODS
WITH (NOLOCK) ON GL010_ORG_CODE = MM012_ORG_CODE
AND GL010_FISCAL_YR = MM012_FISCAL_YR
AND GL010_FISCAL_PERIOD = MM012_FISCAL_PERIOD
LEFT OUTER JOIN dbo.CCRPF_ACCT_TYPES
WITH (NOLOCK) ON GL001_SHORT_DESC = CCRPF_ACCT_TYPE
Where GL001_ACCT_TYPE = 'SS' and LEFT(GL001_ACCOUNT_MASTER.GL001_ACCOUNT,3)='SEC'

Related

Prompts not working correctly in Crystal Reports

I have a report which I would like to create prompts for both VENUE and DATE RANGE, and after having these prompts bring back the incorrect data, I have deleted both and tried to start again from the beginning.
To begin with, I have a Crystal Report based upon the below SQL:
To summarise the code - it uses a common table expression broken separating the query out into revenue, space and event, finally joining this together at the end.
I had to convert the data type of the 'booking date' to DATE, because the value is stored as 2018-01-01 12:00:00:00.
WITH Revenue as
(
SELECT EV200_EVENT_MASTER.EV200_EVT_ID as [Event],
Revenue =
SUM(Case
When Orddtl.ER101_PHASE = '1' and ORDDTL.ER101_COMPL_STS = 'N'
then ORDDTL.ER101_EXT_CHRG
When ORDDTL.ER101_PHASE = '5' and ORDDTL.ER101_COMPL_STS = 'N'
then ORDDTL.ER101_EXT_CHRG
Else 0
End),
SM.EV800_SPACE_DESC,
SM.EV800_SPACE_CODE
FROM EV200_EVENT_MASTER WITH (NOLOCK)
LEFT OUTER JOIN EV870_ACCT_MASTER PrimCoord WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = PrimCoord.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_1 = PrimCoord.EV870_ACCT_CODE -- Event manager
LEFT OUTER JOIN EV870_ACCT_MASTER FloorMgr WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = FloorMgr.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_2 = FloorMgr.EV870_ACCT_CODE -- Floor manager
LEFT OUTER JOIN EV870_ACCT_MASTER EventAccount WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EventAccount.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = EventAccount.EV870_ACCT_CODE -- The person who made the booking
LEFT OUTER JOIN EV215_EVT_TYPE WITH (NOLOCK) -- get the event type
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV215_EVT_TYPE.EV215_ORG_CODE
AND EV200_EVENT_MASTER.EV200_EVT_TYPE = EV215_EVT_TYPE.EV215_EVT_TYPE
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) -- this might not be necessary. Only if we want to show status = completed, etc
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
LEFT OUTER JOIN EV802_SPACE_BKD SPBK
ON SPBK.EV802_ORG_CODE = EV200_ORG_CODE AND SPBK.EV802_EVT_ID = EV200_EVT_ID
LEFT OUTER JOIN ER101_ACCT_ORDER_DTL ORDDTL
ON ORDDTL.ER101_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND ORDDTL.ER101_EVT_ID = EV200_EVENT_MASTER.EV200_EVT_ID
LEFT OUTER JOIN EV800_SPACE_MASTER SM
ON Sm.EV800_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND SM.EV800_SPACE_CODE = SPBK.EV802_BKD_SPACE
WHERE EV200_EVENT_MASTER.EV200_ORG_CODE = '10'
AND EV200_EVENT_MASTER.EV200_EVT_STATUS >= 30 /* only confirmed bookings */
AND EV200_EVENT_MASTER.EV200_EVT_STATUS <= 52
AND Not(EV200_EVENT_MASTER.EV200_EVT_TYPE = 'GB') -- exclude group bookings
GROUP BY EV200_EVENT_MASTER.EV200_EVT_ID,SM.EV800_SPACE_DESC,EV800_SPACE_CODE
), eventdetails as
(
SELECT EV200_EVENT_MASTER.EV200_EVT_ID as [Event],
EV215_EVT_TYP_DESC as [Event Type],
EV200_event_master.EV200_EVT_DESC,
EV200_EVENT_MASTER.EV200_PLN_ATTEND,
--EventAccount.EV870_NAME AS [Account],
PrimCoord.EV870_FIRST_NAME + ' ' + PrimCoord.EV870_LAST_NAME AS [Event Manager],
--FloorMgr.EV870_FIRST_NAME + ' ' + FloorMgr.EV870_LAST_NAME AS [Floor Manager]
EV130_STATUS_MASTER.EV130_STATUS_DESC AS 'Status'
FROM EV200_EVENT_MASTER WITH (NOLOCK)
LEFT OUTER JOIN EV870_ACCT_MASTER PrimCoord WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = PrimCoord.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_1 = PrimCoord.EV870_ACCT_CODE -- Event manager
LEFT OUTER JOIN EV870_ACCT_MASTER FloorMgr WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = FloorMgr.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_2 = FloorMgr.EV870_ACCT_CODE -- Floor manager
LEFT OUTER JOIN EV870_ACCT_MASTER EventAccount WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EventAccount.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = EventAccount.EV870_ACCT_CODE -- The person who made the booking
LEFT OUTER JOIN EV215_EVT_TYPE WITH (NOLOCK) -- get the event type
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV215_EVT_TYPE.EV215_ORG_CODE
AND EV200_EVENT_MASTER.EV200_EVT_TYPE = EV215_EVT_TYPE.EV215_EVT_TYPE
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) -- this might not be necessary. Only if we want to show status = completed, etc
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
WHERE EV200_EVENT_MASTER.EV200_ORG_CODE = '10'
AND EV200_EVENT_MASTER.EV200_EVT_STATUS >= 30 /* only confirmed bookings */
AND EV200_EVENT_MASTER.EV200_EVT_STATUS <= 52
AND Not(EV200_EVENT_MASTER.EV200_EVT_TYPE = 'GB') -- exclude group bookings
), spacedetails as
(
SELECT distinct
SPDTL.EV803_BKD_SPACE,
SPDTL.EV803_EVT_ID,
SM.EV800_SPACE_DESC,
CONVERT(DATE,SPDTL.EV803_BKG_DATE) as bkg_date,
spdtl.EV803_START_TIME,
spdtl.EV803_END_TIME,
SPDTL.EV803_BKG_START_TIME,
SPDTL.EV803_BKG_END_TIME,
SPDTL.EV803_USAGE,
EV800_NOTE_1 AS [VENUE]
FROM EV803_SPACE_BKD_DTL SPDTL
INNER JOIN EV800_SPACE_MASTER SM ON SPDTL.EV803_BKD_SPACE = SM.EV800_SPACE_CODE
)
select distinct eventdetails.Event,
[Event Type],
eventdetails.EV200_EVT_DESC as 'Description',
eventdetails.EV200_PLN_ATTEND as 'PAX',
eventdetails.[Event Manager],
ss.EV800_SPACE_DESC as 'Space',
ss.bkg_date as 'Booking Date',
ss.VENUE,
eventdetails.Status,
/*spacedetails.
(CASE
WHEN spacedetails.EV803_USAGE = 'IN' THEN ''
WHEN spacedetails.EV803_USAGE = 'ET' THEN 'EVENT'
WHEN spacedetails.EV803_USAGE = 'OUT' THEN 'BUMP OUT'
END) as 'Booking Type',
*/
/*this doesnt work because it will be evaluated one row at a time, and one row will not satisfy in,et,out
CAST((CASE WHEN spacedetails.ev803_usage = 'IN' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Bump in Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'IN' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Bump in End Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'ET' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Event Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'ET' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Event End Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'OUT' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Bump Out Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'OUT' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Bump Out End Time',
*/
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'IN'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-in Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'IN'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-in End Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'ET'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Event Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'ET'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Event End Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'OUT'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-out Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'OUT'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-out End Time',
revenue.revenue
from eventdetails
inner join spacedetails ss on eventdetails.Event = ss.EV803_EVT_ID
inner join Revenue on ss.EV803_EVT_ID = revenue.Event and ss.EV803_BKD_SPACE = Revenue.EV800_SPACE_CODE
I created a Dynamic Parameter on the 'Booking date' setting allow for 'range values',
but when i try to run the report (it is deployed in Ungerboeck Event Management) I only get the prompt for a non range value.
I was able to resolve this issue.
In my original query, I referenced two datetime fields, and converted one to a date (shown below) but failed to explicitly convert the second.
FIRST > CONVERT(DATE,SPDTL.EV803_BKG_DATE) as bkg_date,
SECOND > ss.bkg_date as 'Booking Date',
After making the change to convert the second to date, the prompt worked as it should have.

Need to replace case statement in where clause

**The below query is working badly, and I'm planning to replace case stmt in where clause with temp table. Pls suggest to me some option to rewrite this query
Is there a way to simplify this into multiple SQLs
Also explain me how this case stmt works in where clause**
select DISTINCT
ph.order_type_cd,
c.order_num,
ph.created_date,
os.order_status_desc,
ph.facility_name,
PH.vendor_order_num,
l.lic_nm,
(u.last_nm + ', ' + u.first_nm )as requestor_nm,
bl.billing_location_desc,
ph.ship_to_name,
dm.dispatch_method_name_desc as courier,
PH.po_last_updated_dt as last_updated_date,
(u1.last_nm + ', ' + u1.first_nm )as last_updated_by,
ph.right_grp_id,
c.line_id,
a.material_product_id,
a.parent_material_product_id
from avt_po_detail c
inner join avt_po_hdr ph on ph.order_num = c.order_num
left outer join avt_material_product AS a on a.material_product_id=c.material_product_id
INNER JOIN #entity_ids po on po.entity_id = c.order_num --or #select_pos = 0 )
LEFT OUTER JOIN dbo.ert_product AS pc ON pc.product_id = a.product_id
left outer join ert_product_type as pt on pt.product_type_cd = pc.product_type_cd
LEFT OUTER JOIN dbo.ert_product_xref as xref on xref.product_id = a.parent_product_id AND xref.version_id = 1
left outer join avt_order_status AS OS ON OS.order_status_cd = PH.order_status_cd
left outer join avt_licensee l on l.lic_id = ph.lic_id
left outer join avt_po_from pf on pf.material_product_id = a.material_product_id and pf.line_id = c.line_id
left outer join avt_po_audio_detail pad on pad.line_id = c.line_id
left outer join avt_materials_trans_material MTM on mtm.sequence_no = c.video_seq_no
left outer join avt_user u on u.userid = ph.requestor_id
left outer join avt_user u1 on u1.userid = ph.po_last_updated_by
left outer join avt_billing_location bl on bl.billing_location_cd = PH.billing_location_cd
left outer join avt_dispatch_method_name dm on dm.dispatch_method_name_id = PH.dispatch_method_name_id
left outer join avt_right_group arg on arg.right_grp_id = ph.right_grp_id
left outer join #prod product on product.product_id=a.product_id and (product.version_id=c.dist_version_id OR product.version_id = 1)
where (pf.from_desc in ( select from_desc from #from_desc ) or #select_form = 0)
and (case WHEN c.dist_version_id IS NULL THEN c.UCS_prod_no
ELSE
CASE
WHEN isnull(pt.version_control_ind, 'N') = 'Y' THEN xref.rss_prod_no
WHEN
isnull(pt.version_control_ind, 'N') = 'N' AND
xref.rss_prod_no IS NULL AND
xref.rss_film_id IS NULL THEN xref.rss_titleid
WHEN
isnull(pt.version_control_ind, 'N') = 'N' AND
xref.rss_prod_no IS NOT NULL AND
xref.rss_film_id IS NULL THEN substring(pc.glacct, 24, 5) -- ASR 3290687
WHEN isnull(pt.version_control_ind, 'N') = 'N' AND xref.rss_film_id IS NOT NULL THEN isnull(substring(pc.glacct, 24, 5), -- ASR 3290687
(
SELECT substring(dbo.ert_product.glacct, 24, 5) -- ASR 3290687
FROM dbo.ert_product
WHERE dbo.ert_product.product_id = a.parent_product_id
))
END
END in (select prod_no from #prod_no) or #prod_no = 0)

Case Condition to join columns

Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'CASE'
I am trying to join columns based on the column value.
If case conditon 1 is true then i want to join
ON im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4)
if Case condition 2 is true then
ON tr.ItemNumber = im.LegacyItemNumber
else
tr.ItemNumber = im.ItemNumber
SELECT
im.Category as RootCategory,GETDATE() AS LoadDate,tr.*
into transactionreport_stage_testNN
FROM transaction tr
LEFT OUTER JOIN ALLDW_ora.dbo.Location(NOLOCK) loc ON tr.location=loc.location
CASE
when
right(tr.itemnumber,4) = '_old' then
LEFT OUTER JOIN [Inventory](NOLOCK) im
ON im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4)
when
tr.itemnumber <> im.ItemNumber
then
LEFT OUTER JOIN [Inventory](NOLOCK) im
ON tr.ItemNumber = im.LegacyItemNumber
else
LEFT OUTER JOIN [Inventory](NOLOCK) im
ON tr.ItemNumber = im.itemnumber
end
AND im.Location=loc.LocationNo
Schema
You should probably just use AND's and OR's
LEFT OUTER JOIN [Inventory](NOLOCK) im ON im.Location=loc.LocationNo
AND (
(right(tr.itemnumber,4) = '_old' AND im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4))
OR
(tr.itemnumber <> im.ItemNumber AND tr.ItemNumber = im.LegacyItemNumber)
OR
(tr.ItemNumber = im.itemnumber)
)
it may just be as simple as
SELECT im.Category AS RootCategory,
GETDATE() AS LoadDate,
tr.*
INTO #transactionreport_stage_testNN
FROM [transaction] tr
LEFT OUTER JOIN ALLDW_ora.dbo.Location (NOLOCK) loc ON tr.location = loc.location
LEFT OUTER JOIN [Inventory] (NOLOCK) im ON im.Location = loc.LocationNo
AND REPLACE(tr.itemnumber, '_old', '') IN (im.itemnumber, im.legacyitemnumber)

Error converting data type varchar to float when using Top clause in sql query

Why I get converting error when I use top clause in my query but when I use select * in my query everything is ok.
I use SQL server 2000
I use a view that is created from below code
select sum(g.qty) as qty,g.PartCode,g.PartName,g.arz,g.t,g.OrdNo,max(g.id) as id,g.Val from
(SELECT sum(iv.FinalQty) AS qty, p.PartCode, p.PartName, CAST(RIGHT(p.PartCode, 4) AS float) AS arz,case when isnumeric(LEFT(RIGHT(p.PartCode, 7), 3))=1 then CAST(LEFT(RIGHT(p.PartCode, 7), 3) AS float) / 100 else CAST(LEFT(RIGHT(p.PartCode, 7), 4) AS float)/100 end AS t, ord.OrdNo,
id.id,ctrl.val
FROM INV.InvVchHdr AS hv WITH (nolock) INNER JOIN
INV.InvVchItm AS iv WITH (nolock) ON hv.VchHdrID = iv.VchHdrRef INNER JOIN
inv.Part p WITH (nolock) ON p.Serial = iv.PartRef INNER JOIN
INV.InvRqstItm AS i WITH (nolock) ON i.RqstItmID = iv.RefNum INNER JOIN
INV.InvRqstHdr AS h WITH (nolock) ON h.RqstHdrID = i.HdrRef INNER JOIN
PRD.PrdOrdPlan AS po WITH (nolock) ON po.OrdPlnId = h.OrdPlnBase INNER JOIN
PRD.PrdOrdItem AS oi WITH (nolock) ON oi.OrdItmId = po.OrdItmRef INNER JOIN
PRD.PrdOrder AS ord WITH (nolock) ON ord.OrdID = oi.OrderRef JOIN
inv.InvVchItmCtrl ctrl WITH (nolock) ON ctrl.VchItmRef = iv.VchItmID
join USR.idgen_coil_trace id WITH (nolock) on id.vchitmid=iv.VchItmID
WHERE (hv.VchType = 51) AND (iv.BaseVchType <> 0) and iv.VchItmID>981336159
and isnumeric(LEFT(RIGHT(p.PartCode, 7), 3))=1
AND hv.VchHdrID IN
(SELECT max(i.vchhdrref)
FROM inv.InvVchItm i WITH (nolock) JOIN
inv.InvVchItmCtrl c WITH (nolock) ON c.VchItmRef = i.VchItmID
WHERE i.VchType = 51
GROUP BY i.PartRef, c.Val)
GROUP BY p.PartCode, p.PartName, CAST(RIGHT(p.PartCode, 4) AS float)
, CAST(LEFT(RIGHT(p.PartCode, 7), 3) AS float) / 100
, ord.OrdNo, ctrl.Val,id.id) g
--join USR.idgen_coil_trace id on id.vchitmid=g.VchItmID
group by g.PartCode, g.PartName, g.arz
, g.t, g.OrdNo, g.Val
UNION
select sum(g.qty),g.PartCode,g.PartName,g.SizeW,g.SizeH,g.OrdNo,max(g.id),g.Val from(SELECT sum(iv.FinalQty) AS qty, p.PartCode, p.PartName, cp.SizeW, cp.Sizeh, ord.OrdNo,id.id, ctrl.val
FROM INV.InvVchHdr AS hv WITH (nolock) INNER JOIN
INV.InvVchItm AS iv WITH (nolock) ON hv.VchHdrID = iv.VchHdrRef INNER JOIN
INV.Part AS p WITH (nolock) ON p.Serial = iv.PartRef INNER JOIN
INV.InvRqstItm AS i WITH (nolock) ON i.RqstItmID = iv.RefNum INNER JOIN
INV.InvRqstHdr AS h WITH (nolock) ON h.RqstHdrID = i.HdrRef INNER JOIN
PRD.PrdOrdPlan AS po WITH (nolock) ON po.OrdPlnId = h.OrdPlnBase INNER JOIN
PRD.PrdOrdItem AS oi WITH (nolock) ON oi.OrdItmId = po.OrdItmRef INNER JOIN
PRD.PrdOrder AS ord WITH (nolock) ON ord.OrdID = oi.OrderRef JOIN
inv.InvVchItmCtrl ctrl WITH (nolock) ON ctrl.VchItmRef = iv.VchItmID JOIN
inv.PartCmplmnt cp WITH (nolock) ON cp.PartRef = p.Serial
join USR.idgen_coil_trace id WITH (nolock) on id.vchitmid=iv.VchItmID
WHERE (hv.VchType = 57) AND (iv.BaseVchType <> 0) and iv.VchItmID>981336159
AND PO.PartRef IN (SELECT SERIAL FROM INV.Part P WHERE P.PartName LIKE '%لاف%')
GROUP BY p.PartCode, p.PartName, cp.SizeW, cp.Sizeh, ord.OrdNo,id.id, ctrl.Val)g
--join USR.idgen_coil_trace id on id.vchitmid=g.VchItmID
group by g.PartCode, g.PartName, g.SizeW
, g.SizeH, g.OrdNo, g.Val
UNION
--SELECT *FROM usr.aghili WITH (nolock)
--UNION
--SELECT *
--FROM usr.moradi WITH (nolock)
SELECT qty, PartCode, PartName, arz, t, OrdNo, id, val FROM USR.COIL_TRACE_BAK WITH (nolock)
union
select qty,PartCode,PartName,arz,t,OrdNo,id,Val from usr.unregister_coil_trace WITH (nolock)
this query is correct > select * from usr.coil_trace
this query is not correct > select top 100 * from usr.coil_trace
Top clause should work in sql server 2000.
Though I do not have sql 2000 with me but this link should help you.
Also have a look at answers at this stackover flow link

Eliminating NULL rows in TSQL query [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to eliminate NULL fields in TSQL
I am using SSMS 2008 R2 and am developing a TSQL query. I want just 1 record / profile_name. Because some of these values are NULL, I am currently doing LEFT JOINS on most of the tables. But the problem with the LEFT JOINs is that now I get > 1 record for some profile_names!
But if I change this to INNER JOINs then some profile_names are excluded entirely because they have NULL values for these columns. How do I limit the query result to just one record / profile_name regardless of NULL values? And if there are non-NULL values then I want it to choose the record with non-NULL values. Here is initial query:
select distinct
gp.group_profile_id,
gp.profile_name,
gp.license_number,
gp.is_accepting,
case when gp.is_accepting = 1 then 'Yes'
when gp.is_accepting = 0 then 'No '
end as is_accepting_placement,
mo.profile_name as managing_office,
regions.[region_description] as region,
pv.vendor_name,
pv.id as vendor_id,
at.description as applicant_type,
dbo.GetGroupAddress(gp.group_profile_id, null, 0) as [Office Address],
gsv.status_description
from group_profile gp With (NoLock)
inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = #agency_id and gp.is_deleted = 0
inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id
left outer join payor_vendor pv With (NoLock) on gp.payor_vendor_id = pv.payor_vendor_id
left outer join applicant_type at With (NoLock) on gp.applicant_type_id = at.applicant_type_id and at.is_foster_home = 1
inner join group_status_view gsv With (NoLock) on gp.group_profile_id = gsv.group_profile_id and gsv.status_value = 'OPEN' and gsv.effective_date =
(Select max(b.effective_date) from group_status_view b With (NoLock)
where gp.group_profile_id = b.group_profile_id)
left outer join regions With (NoLock) on isnull(mo.regions_id, gp.regions_id) = regions.regions_id
left join enrollment en on en.group_profile_id = gp.group_profile_id
join event_log el on el.event_log_id = en.event_log_id
left join people client on client.people_id = el.people_id
As you can see, the results of the above query is 1 row / profile_name:
group_profile_id profile_name license_number is_accepting is_accepting_placement managing_office region vendor_name vendor_id applicant_type Office Address status_description Cert Date2
But now watch what happens when I add in 2 LEFT JOINs and 1 additional column:
select distinct
gp.group_profile_id,
gp.profile_name,
gp.license_number,
gp.is_accepting,
case when gp.is_accepting = 1 then 'Yes'
when gp.is_accepting = 0 then 'No '
end as is_accepting_placement,
mo.profile_name as managing_office,
regions.[region_description] as region,
pv.vendor_name,
pv.id as vendor_id,
at.description as applicant_type,
dbo.GetGroupAddress(gp.group_profile_id, null, 0) as [Office Address],
gsv.status_description,
ri.[description] as race
from group_profile gp With (NoLock)
inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = #agency_id and gp.is_deleted = 0
inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id
left outer join payor_vendor pv With (NoLock) on gp.payor_vendor_id = pv.payor_vendor_id
left outer join applicant_type at With (NoLock) on gp.applicant_type_id = at.applicant_type_id and at.is_foster_home = 1
inner join group_status_view gsv With (NoLock) on gp.group_profile_id = gsv.group_profile_id and gsv.status_value = 'OPEN' and gsv.effective_date =
(Select max(b.effective_date) from group_status_view b With (NoLock)
where gp.group_profile_id = b.group_profile_id)
left outer join regions With (NoLock) on isnull(mo.regions_id, gp.regions_id) = regions.regions_id
left join enrollment en on en.group_profile_id = gp.group_profile_id
join event_log el on el.event_log_id = en.event_log_id
left join people client on client.people_id = el.people_id
left join race With (NoLock) on el.people_id = race.people_id
left join race_info ri with (nolock) on ri.race_info_id = race.race_info_id
The above query results in all of the same profile_names, but some with NULL race values:
group_profile_id profile_name license_number is_accepting is_accepting_placement managing_office region vendor_name vendor_id applicant_type Office Address status_description Cert Date2 race
Unfortunately it complicates matters that I need to join in 2 additional tables for this one additional field value (race). If I simply change the last two LEFT JOINs above to INNER JOINs then I eliminate the NULL rows above. But I also eliminate some of the profile_names:
group_profile_id profile_name license_number is_accepting is_accepting_placement managing_office region vendor_name vendor_id applicant_type Office Address status_description Cert Date2 race
Hopefully I have provided all of the details that you need for this question.
Not the most elegant solution, but one that will work:
select [stuff]
from group_profile gp With (NoLock)
inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = #agency_id and gp.is_deleted = 0
inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id
join payor_vendor pv on ISNULL(gp.payor_vendor_id, 'THISVALUEWILLNEVEROCCUR') = ISNULL(pv.payor_vendor_id, 'THISVALUEWILLNEVEROCCUR')
...etc...
Biggest issue with what I posted is that you'll be doing a whole lot of table scans.