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.
Related
This works:
SELECT DISTINCT
"public".visit.visit_id,
"public".visit.visit_name,
"public".visit.visit_admit_date,
"public".visit.visit_disch_date,
"public".visit.visit_stay_type,
"public".visit.visit_ins,
"public".ip_visit_1.ipv1_room,
"public".ip_visit_1.ipv1_ad_init,
CASE WHEN (SELECT digital_signature_images.dsigimg_acct FROM digital_signature_images where "public".visit.visit_id = digital_signature_images.dsigimg_acct AND digital_signature_images.dsigimg_title = 'IMPORTANT LETTER FROM MEDICARE') IS NOT NULL THEN 'YES'
WHEN (SELECT patient_images.patimg_acct FROM patient_images where visit.visit_id = patient_images.patimg_acct AND patient_images.patimg_title = 'IMPORTANT LETTER FROM MEDICARE') IS NOT NULL THEN 'YES' ELSE 'NOT ON FILE' END AS IMFM_ON_FILE
FROM
"public".digital_signature_images
INNER JOIN "public".visit ON "public".visit.visit_id = "public".digital_signature_images.dsigimg_acct
INNER JOIN "public".ip_visit_1 ON "public".visit.visit_id = "public".ip_visit_1.ipv1_num
INNER JOIN "public".patient_images ON "public".patient_images.patimg_acct = "public".visit.visit_id
WHERE
"public".visit.visit_ins LIKE 'M%' AND
"public".visit.visit_stay_type = '1' AND
"public".visit.visit_disch_date = '2022-07-01'
ORDER BY
"public".ip_visit_1.ipv1_room
When I try to do the previous month only in the WHERE portion, I get told "More than one row returned by a subquery used as an expression:
SELECT DISTINCT
"public".visit.visit_id,
"public".visit.visit_name,
"public".visit.visit_admit_date,
"public".visit.visit_disch_date,
"public".visit.visit_stay_type,
"public".visit.visit_ins,
"public".ip_visit_1.ipv1_room,
"public".ip_visit_1.ipv1_ad_init,
CASE WHEN (SELECT digital_signature_images.dsigimg_acct FROM digital_signature_images where "public".visit.visit_id = digital_signature_images.dsigimg_acct AND digital_signature_images.dsigimg_title = 'IMPORTANT LETTER FROM MEDICARE') IS NOT NULL THEN 'YES'
WHEN (SELECT patient_images.patimg_acct FROM patient_images where visit.visit_id = patient_images.patimg_acct AND patient_images.patimg_title = 'IMPORTANT LETTER FROM MEDICARE') IS NOT NULL THEN 'YES' ELSE 'NOT ON FILE' END AS IMFM_ON_FILE
FROM
"public".digital_signature_images
INNER JOIN "public".visit ON "public".visit.visit_id = "public".digital_signature_images.dsigimg_acct
INNER JOIN "public".ip_visit_1 ON "public".visit.visit_id = "public".ip_visit_1.ipv1_num
INNER JOIN "public".patient_images ON "public".patient_images.patimg_acct = "public".visit.visit_id
WHERE
"public".visit.visit_ins LIKE 'M%' AND
"public".visit.visit_stay_type = '1' AND
ip_visit_1.ipv1_dis_date >= DATE_TRUNC('MONTH', CURRENT_DATE - INTERVAL '1 MONTH') AND
ip_visit_1.ipv1_dis_date < DATE_TRUNC('MONTH', CURRENT_DATE)
ORDER BY
"public".ip_visit_1.ipv1_room
How can I return the previous month?
use EXISTS (subquery) to determine if a subquery returns at least one row. It returns a boolean, so you can put it directly after the WHEN.
**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)
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'
How can I find what was the available stock on a specific date in the past?
I know how to get OnHand for any date in the past:
SELECT LocType, LocCode, ItemCode, SUM(InQty-OutQty) [OnHand]
From OIVL
Where DocDate <= '7/7/2017'
AND ITEMCODE = 'xyz'
Group by LocType, LocCode, ItemCode
But this does not account for what was committed. It shows what was on hand, but not what was actually available (OnHand - Committed = Available). So, how could I get to this "Available" number for dates in the past?
Thanks!
In case this helps anyone else.... here is an example of how to get your item quantity and cost history in SAP BusinessOne. Pass the date and item you want to see history for into the function. If you omit item code it returns all items.
CREATE FUNCTION [dbo].[ufnStockAndCostHistory]
(
#EndDate DATETIME, #ItemCode NVARCHAR(255) = 'All'
)
RETURNS TABLE
AS
RETURN
(
WITH Cost AS (
SELECT
OINM.TransNum,
OINM.ItemCode,
OINM.Warehouse,
OINM.Balance,
OINM.CreateDate
FROM OINM (NOLOCK)
JOIN OITM (NOLOCK) ON OITM.ItemCode = OINM.ItemCode
JOIN OITB (NOLOCK) ON OITM.ItmsGrpCod=OITB.ItmsGrpCod
WHERE OINM.CreateDate <= #EndDate
),
FinalCostByWarehouse AS (
SELECT *
FROM Cost
WHERE TransNum = (
SELECT MAX(TransNum)
FROM Cost sub
WHERE Cost.ItemCode = sub.ItemCode
AND Cost.Warehouse = sub.Warehouse)
),
Quantity AS (
SELECT
OINM.ItemCode,
OINM.Warehouse,
SUM(OINM.InQty) - SUM(OINM.OutQty) [Qty]
FROM OINM (NOLOCK)
JOIN OITM (NOLOCK) ON OITM.ItemCode = OINM.ItemCode
JOIN OITB (NOLOCK) ON OITM.ItmsGrpCod=OITB.ItmsGrpCod
WHERE OINM.CreateDate <= #EndDate
group by OINM.ItemCode, OINM.Warehouse
)
SELECT
OITW.ItemCode,
OITW.WhsCode,
c.Balance [Cost],
q.Qty [Quantity]
FROM OITW
LEFT JOIN FinalCostByWarehouse c ON OITW.ItemCode = c.ItemCode
AND OITW.WhsCode = c.Warehouse
LEFT JOIN Quantity q ON OITW.ItemCode = q.ItemCode
AND OITW.WhsCode = q.Warehouse
WHERE (OITW.ItemCode = #ItemCode
OR #ItemCode = 'All')
AND (ISNULL(c.Balance,0) > 0
OR ISNULL(q.Qty,0) > 0)
)
Usage would look like:
SELECT *
FROM ufnStockAndCostHistory('9/15/2017','B107A-12HB')
I want to place this working code within a SQL Statement, OR do I need to perform a UDF.
The result set is a one line concatenation, and I want it to be place in every one of the overall result set lines.
----
MAIN QUERY
SELECT
H.CONNECTION_ID,
H.SEQUENTIAL_NO,
H.INVOICE_NUMBER,
H.INVOICE_DATE,
H.LAST_INVOICE_NUMBER,
H.LAST_INVOICE_DATE,
CAST(CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN '-'
ELSE CAST(H.PAYMENT_DUE_DATE AS NVARCHAR(20))
END AS SMALLDATETIME) AS PAYMENT_DUE,
H.JOB_NUMBER,
H.CUST_JOB_NUMBER,
HDR.SALES_PERSON,
H.INSIDE_SALES_PERSON,
H.IS_LAST_INVOICE,
CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN 'CASH'
ELSE H.PAYMENT_TERMS_DESCRIPTION
END AS PAYMENT_TERMS,
H.PRINTED,
H.NOTES,
CUR.ID,
CUR.CODE,
CASE CUR.CODE
WHEN 'USD' THEN '001-106624-211'
WHEN 'EUR' THEN '001-106624-101'
WHEN 'GBP' THEN '001-106624-100'
ELSE '001-106624-001'
END AS BANK_ACCT,
CUR.EXCHANGE_RATE,
H.BILL_CONTACT,
H.CUST_ACCOUNT,
H.CUST_NAME,
H.CUST_ADDR1,
H.CUST_ADDR2,
H.CUST_CITY,
H.CUST_STATE,
H.CUST_ZIP,
H.CONTACT_PHONE_NUMBER,
H.CONTACT_PHONE_NUMBER2,
H.ORDERED_BY_CONTACT,
H.SHIP_TO_NAME,
H.SHIP_TO_ADDR1,
H.SHIP_TO_ADDR2,
H.SHIP_TO_CITY,
H.SHIP_TO_STATE,
H.SHIP_TO_ZIP,
H.SITE_PHONE_NUMBER,
H.SITE_PHONE_NUMBER2,
H.OFFICE_NAME,
H.OFFICE_ADDR1,
H.OFFICE_ADDR2,
H.OFFICE_CITY,
H.OFFICE_STATE,
H.OFFICE_ZIP,
H.OFFICE_PHONE_NUMBER,
H.OFFICE_FAX_NUMBER,
H.DELIVERY_TICKET_NUMBER,
H.PO_NUMBER,
H.DUMMY_INVOICE_TEXT,
(SELECT MESSAGE FROM REPORT_MESSAGES WHERE CODE = 'INVOICE') ADVERT_MESSAGE,
(SELECT MAX(DISCOUNT_PERCENTAGE) FROM PRTINVITEM I2 WHERE I2.CONNECTION_ID = H.CONNECTION_ID AND I2.INVOICE_NUMBER = H.INVOICE_NUMBER) AS MAX_DISCOUNT,
I.ITEM,
I.DESCRIPTION,
I.QUANTITY,
I.UNIT_OF_MEASURE,
I.MINIMUM_CHARGE,
I.WEEKLY_CHARGE,
I.MONTHLY_CHARGE,
I.START_OF_BILLING_PERIOD,
I.END_OF_BILLING_PERIOD,
I.DAYS_USED,
I.WEEKS_USED,
I.DISCOUNT_PERCENTAGE,
I.TAX_CODE_FOR_ITEM,
I.INVENTORY_TYPE,
I.BILLING_LOGIC_TYPE,
I.ACTUAL_WEEKLY_CHARGE_USED,
I.DAYS_IN_ACTUAL_WEEKLY_CHARGE,
II.CHARGEABLE_DAYS,
II.CHARGEABLE_WEEKS,
II.CHARGEABLE_MONTHS,
II.FREE_DAYS_THIS_INVOICE,
CNV.TOTAL_NET_VALUE,
CNV.TOTAL_TAX_VALUE,
CNV.TOTAL_GROSS_VALUE,
CNV.TOTAL_GROSS_VALUE_NS,
CNV.NET_LINE_VALUE,
CMP.EMAIL_ADDRESS
FROM (PRTINVHDR H INNER JOIN PRTINVITEM I ON H.CONNECTION_ID = I.CONNECTION_ID AND H.INVOICE_NUMBER = I.INVOICE_NUMBER)
INNER JOIN INVOICEHDR HDR ON I.INVOICE_NUMBER = HDR.INVNO
INNER JOIN CUSTOMERS CST ON H.CUST_ACCOUNT = CST.CUSTNUM
INNER JOIN JOB JOB ON H.JOB_NUMBER = JOB.JOBNUM
INNER JOIN CURRENCY CUR ON HDR.CURRENCY_ID = CUR.ID
INNER JOIN VWCURRENCYCONVERSION CNV ON I.CONNECTION_ID = CNV.CONNECTION_ID AND I.INVC_UCOUNTER = CNV.INVC_UCOUNTER
INNER JOIN COMPANY CMP ON H.OFFICE_CODE = CMP.OFFICE
INNER JOIN INVOICEITEM II ON I.INVOICE_NUMBER = II.INVNO AND I.INVC_UCOUNTER = II.INVC_UCOUNTER
ORDER BY
H.SEQUENTIAL_NO,
I.PRINT_SEQUENCE
ASC
----
COALESCE QUERY
DECLARE
#DTICKET NVARCHAR(20),
#PUMPCATEGORYNAME NVARCHAR(3999)
SET #DTICKET = ''
SET #PUMPCATEGORYNAME = NULL
(SELECT
#DTICKET = DTICKET,
#PUMPCATEGORYNAME = COALESCE(#PUMPCATEGORYNAME + ', ', '' ) + PUMPCATEGORYNAME
FROM (SELECT
BHDR.DTICKET,
SCD.PUMPCATEGORYNAME
FROM PRTTICKHDR PHDR
INNER JOIN BIDHDR BHDR ON PHDR.DELIV_TICKET_NUMBER = BHDR.DTICKET
INNER JOIN PRTTICKITEM PITM ON PHDR.CONNECTION_ID = PITM.CONNECTION_ID AND PHDR.DELIV_TICKET_NUMBER = PITM.DELIV_TICKET_NUMBER
LEFT JOIN SUBCATEGORYDESCRIPTION SCD ON PITM.ITEM = SCD.PUMPCATEGORY
WHERE SCD.PUMPCATEGORYNAME IS NOT NULL)
SUBCATEGORYDESCRIPTION)
SELECT #DTICKET, #PUMPCATEGORYNAME
Not really sure what you are asking for but you can doing something along the lines of
Select col1 + ', ' + col2 + ', ' + col3 etc....