I have a fairly complicated report that I'm trying to edit. Below is a screenshot of the report:
I need to filter out all rows where "Number not in Groups" is n/a or 0, I've tried filtering out using the filter option in tablix properties, but it wont let me due to the error:
aggregate functions cannot be used in dataset filters.
Below is the code for the expression that makes up the field "Number not in Groups":
=iif(sum(iif(left(Fields!crs_group.Value,1) = "G",1,0),"GrpCourse")=0,
"n/a",
sum(iif(Fields!crs_group.Value="Enrolled on Course",1,0)) - sum(iif(left(Fields!crs_group.Value,1) = "G",1,0)))
I have also tried to filter out those rows in the SQL query but haven't had much luck. Below is my attempt at filtering out the rows in the query:
SELECT
sub.course ,
sub.crs_group ,
m.m_reference ,
me.e_status ,
me.e_id ,
s.s_studentreference ,
p.p_forenames ,
p.p_surname ,
pcd.p_surname + ',' + pcd.p_forenames AS course_dir ,
ISNULL(COUNT(*) - SUM(CASE WHEN LEFT(crs_group, 1) = 'G' THEN 1
END), 1) AS NumberNotInGroups
FROM
msql.unitesnapshot.dbo.capd_moduleenrolment AS me
INNER JOIN msql.unitesnapshot.dbo.capd_module AS m
ON me.e_module = m.m_id
LEFT JOIN msql.unitesnapshot.dbo.capd_staff scd
ON m.m_modulesupervisor = scd.s_id
LEFT JOIN msql.unitesnapshot.dbo.capd_person pcd
ON scd.s_id = pcd.p_id
INNER JOIN msql.unitesnapshot.dbo.capd_student s
ON me.e_student = s.s_id
INNER JOIN msql.unitesnapshot.dbo.capd_person p
ON s.s_id = p.p_id
INNER JOIN (
SELECT
m.m_id ,
CASE WHEN m.m_reference NOT LIKE '%G_'
THEN m.m_reference
ELSE LEFT(m.m_reference,
CHARINDEX('G', m.m_reference) - 1)
END AS course ,
CASE WHEN m.m_reference NOT LIKE '%G_'
THEN 'Enrolled on Course'
ELSE RIGHT(m.m_reference, 2)
END AS crs_group
FROM
unitesnapshot.dbo.capd_module m
) sub
ON sub.m_id = me.e_module
WHERE
me.e_status = 'A'
AND LEFT(m.m_reference, 2) = '12'
AND SUBSTRING(m.m_reference, 7, 2) IN ( 'VF', 'AB', 'FB' )
GROUP BY
sub.course ,
sub.crs_group ,
m.m_reference ,
me.e_status ,
me.e_id ,
s.s_studentreference ,
p.p_forenames ,
p.p_surname ,
pcd.p_surname + ',' + pcd.p_forenames
HAVING
COUNT(*) - SUM(CASE WHEN LEFT(crs_group, 1) = 'G' THEN 1
ELSE 0
END) <> 0
ORDER BY
p.p_surname ,
p.p_forenames
However this doesn't work because it includes rows that would have the n/a value in the report. A group is defined by a course code that ends in G_, however some course's don't have groups so these appear as 'n/a'.
Sorry if none of this makes sense, I just really need some help with it.
Thanks
I haven't entirely understood your query, but seems like you could do this in the query with a Common Table Expression (CTE):
;
WITH
CourseIsGroupCourse
AS
(
SELECT
m.m_id ,
CASE WHEN m.m_reference NOT LIKE '%G_'
THEN m.m_reference
ELSE LEFT(m.m_reference,
CHARINDEX('G', m.m_reference) - 1)
END AS course ,
SUM(...[Something I haven't figured out yet...]) as IsGroupCourse
FROM
unitesnapshot.dbo.capd_module m
GROUP BY
m.m_id,
CASE WHEN m.m_reference NOT LIKE '%G_'
THEN m.m_reference
ELSE LEFT(m.m_reference,
CHARINDEX('G', m.m_reference) - 1)
END
)
[Now back to your big query...]
SELECT
sub.course ,
sub.crs_group ,
m.m_reference ,
...
FROM
msql.unitesnapshot.dbo.capd_moduleenrolment AS me
INNER JOIN msql.unitesnapshot.dbo.capd_module AS m
ON me.e_module = m.m_id
LEFT JOIN
...
WHERE
me.e_status = 'A'
AND LEFT(m.m_reference, 2) = '12'
AND SUBSTRING(m.m_reference, 7, 2) IN ( 'VF', 'AB', 'FB' )
-- Here's the key bit...
AND sub.course in (SELECT course FROM CourseIsGroupCourse where IsGroupCourse = 1)
(When struggling with a SQL problem, I find it often helps to name tables with more descriptive names, then you can start to see what you've got where, and piece things together appropriately.)
Related
This is the function I created:
CREATE OR REPLACE FUNCTION NS_REPORTS.AP."COUPA_GET_EXCH_RATE"("from_curr_id" NUMBER(38,0), "to_curr_id" NUMBER(38,0), "date" DATE)
RETURNS FLOAT
LANGUAGE SQL
AS '
SELECT
COALESCE((
SELECT
RATE
FROM
(
SELECT
ROW_NUMBER() OVER (PARTITION BY DATE(RATE_DATE) ORDER BY RATE_DATE DESC) ROW_NUM
, RATE
FROM
CONNECTORS.COUPA.EXCHANGE_RATE
WHERE
FROM_CURRENCY_ID = from_curr_id
AND TO_CURRENCY_ID = to_curr_id
AND DATE(RATE_DATE) = date
) R
WHERE
ROW_NUM = 1
), 1)
';
I'm using the ROW_NUMBER function because the RATE_DATE field is actually datetime and so there are multiple records per date.
When I call the function by itself, it works fine. However, when I try to use it in a view, I get the unsupported subquery type error. The view works fine without it. Can anyone think of what I can do to either fix the error or work around it by rewriting the query?
EDIT 1: View code and exact error message
CREATE OR REPLACE VIEW COUPA_REQUISITION
AS
SELECT
RH.ID REQ_NUM
, RL.LINE_NUM REQ_LINE_NUM
, OH.PO_NUMBER
, REPLACE(REPLACE(OH.CUSTOM_FIELDS:"legacy-po-number", '"', ''), '.0', '') LEGACY_PO_NUMBER
, S."NAME" SUPPLIER
, OH.STATUS
, UR.FULLNAME REQUESTED_BY
, UC.FULLNAME CREATED_BY
, OL.RECEIVED
, DATE(RH.SUBMITTED_AT) ORDER_DATE
, DATE(RH.NEED_BY_DATE) NEEDED_BY_DATE
, RL."DESCRIPTION" ITEM
, CAST(NULL AS VARCHAR) CHART_OF_ACCOUNTS
, REPLACE(OH.CUSTOM_FIELDS:"purchase-type", '"', '') PURCHASE_TYPE
, COM."NAME" COMMODITY
, ACT.NS_SUB_NAME SUBSIDIARY
, ACT.NS_ACCT_NAME_FULL "ACCOUNT"
, ACT.NS_DEPT_NAME_FULL DEPARTMENT
, ACT.NS_L3_DEPT_NAME L3_DEPARTMENT
, ACT.NS_LOC_NAME "LOCATION"
, RL.QUANTITY QTY
, OL.LINE_NUM ORDER_LINE_NUM
, RL.TOTAL * NS_REPORTS.AP.COUPA_GET_EXCH_RATE(RL.CURRENCY_ID, 1, DATE(RH.SUBMITTED_AT)) LINE_TOTAL
, RL.TOTAL - OL.INVOICED UNINVOICED_AMOUNT
, OL.INVOICED INVOICED_TOTAL
, RLSUM.TOTAL TOTAL
, REPLACE(IL.CUSTOM_FIELDS:"amortization-schedule"."name", '"', '') AMORTIZATION_SCHEDULE
, CASE WHEN COALESCE(IL.CUSTOM_FIELDS:"amortization-start-date", '') <> '' THEN DATE(REPLACE(IL.CUSTOM_FIELDS:"amortization-start-date", '"', '')) ELSE NULL END AMORTIZATION_START_DATE
, CASE WHEN COALESCE(IL.CUSTOM_FIELDS:"amortization-end-date", '') <> '' THEN DATE(REPLACE(IL.CUSTOM_FIELDS:"amortization-end-date", '"', '')) ELSE NULL END AMORTIZATION_END_DATE
, CASE WHEN COALESCE(OH.CUSTOM_FIELDS:"contract-start-date", '') <> '' THEN DATE(REPLACE(OH.CUSTOM_FIELDS:"contract-start-date", '"', '')) ELSE NULL END CONTRACT_START_DATE
, CASE WHEN COALESCE(OH.CUSTOM_FIELDS:"contract-end-date", '') <> '' THEN DATE(REPLACE(OH.CUSTOM_FIELDS:"contract-end-date", '"', '')) ELSE NULL END CONTRACT_END_DATE
FROM
CONNECTORS.COUPA.REQUISITION_HEADER RH
JOIN CONNECTORS.COUPA.REQUISITION_LINE RL ON RL.REQUISITION_HEADER_ID = RH.ID
JOIN NS_REPORTS.AP.COUPA_ACCOUNT ACT ON ACT.COUPA_ACCT_ID = RL.ACCOUNT_ID
JOIN CONNECTORS.COUPA."USER" UR ON UR.ID = RH.REQUESTED_BY_ID
JOIN CONNECTORS.COUPA."USER" UC ON UC.ID = RH.CREATED_BY_ID
JOIN (
SELECT
REQUISITION_HEADER_ID
, SUM(TOTAL) TOTAL
FROM
CONNECTORS.COUPA.REQUISITION_LINE
GROUP BY
REQUISITION_HEADER_ID
) RLSUM ON RLSUM.REQUISITION_HEADER_ID = RH.ID
LEFT JOIN CONNECTORS.COUPA.ORDER_LINE OL ON OL.ID = RL.ORDER_LINE_ID
LEFT JOIN CONNECTORS.COUPA.ORDER_HEADER OH ON OH.ID = OL.ORDER_HEADER_ID
LEFT JOIN CONNECTORS.COUPA.COMMODITY COM ON COM.ID = OL.COMMODITY_ID
LEFT JOIN CONNECTORS.COUPA.SUPPLIER S ON S.ID = OH.SUPPLIER_ID
LEFT JOIN CONNECTORS.COUPA.INVOICE_LINE IL ON IL.ORDER_LINE_ID = OL.ID
Error message:
SQL Error [2031] [42601]: SQL compilation error:
Unsupported subquery type cannot be evaluated
The error is a correlated subquery and the are not supported (beyond some tiny toy examples)
But the basic form is
SELECT a.a
(select b.b from b where b.a = a.a order by b.y limit 1)
FROM a;
in effect for each row, a sub-query is run on another table to get a value. There are many tricks done in other DB's to make this "work" but in effect there is work done on each row. Snowflake does not types of for each row operations.
The good news is there are other patterns that are effectively the same, that snowflake does support, the two patterns are really the same use a CTE or join to a sub-select which is the same thing.
so the above becomes:
WITH subquery AS (
SELECT b.a, b.b FROM b
QUALIFY row_number() over (partition by b.a order by b.y) = 1
)
SELECT a.a
sq.b
FROM a
JOIN subquery AS sq
ON sq.a = a.a
So we first process/shape "all records" from the other/sub table, and we only keep the rows that have the count/shape we want, and then join to that result. The is very parallelizable, so performs well. The reason Snowflake does not auto translate a sub-query for you, is it rather easy to get it wrong, and they presently are spending there development efforts working on features that do not exist at all, etc etc, and it can be rewritten by you, given you understand your model.
What if you move this to the FROM clause? I would phrase this as:
SELECT COALESCE(MAX(er.rate), 1)
FROM (SELECT er.*
FROM CONNECTORS.COUPA.EXCHANGE_RATE er
WHERE er.FROM_CURRENCY_ID = in_from_curr_id AND
er.TO_CURRENCY_ID = in_to_curr_id AND
DATE(er.RATE_DATE) = in_date
ORDER BY RATE_DATE DESC
LIMIT 1
) er;
Notice that I changed the names of the parameters so they are more obviously input parameters.
I have created an ABAP CDS view using ACDOCA, BSID, MARA, MAKT and such tables.
Now my CDS view contains list of customers along with their outstanding (FI) documents.
Against these customers, FI documents and certain more parameters, I am maintaining certain data in a custom table.
Now using left outer join I want to connect this CDS view with custom table.
The problem now is that this join is working like inner join, so if my custom table is blank my output is displaying without values whereas it should be displaying CDS view as it is, and custom table fields as blank.
Now based on similar issues on web I did research and found some useful points:
all conditions on right table are included at most in ON join condition
tried to include null values from right side custom table as well.
tried using just one parameter on join condition to see the result (still working as inner join)
Here is my complete select statement, where:
ZPD1 is my ABAP CDS view of ACDOCA, BSDI ... tables
ZPD_HSL is an ABAP CDS view for customer wise GL amount calculation.
ZFI_PDRATE_MAP is my custom table which contains rate for customer / FI docs combination.
What could be a misstep in this statement?
SELECT
FROM ZPD1( PBUKRS = #BUKRS-LOW ,
POSTFROM = #COL_DATE-LOW ,
POSTTO = #COL_DATE-HIGH ) AS A
LEFT OUTER JOIN ZFI_PDRATE_MAP AS F
ON A~PROD_CATEGORY = F~PROD_CATEGORY
AND A~OD_DATE GE F~POST_DT_FROM
AND A~OD_DATE LE F~POST_DT_TO
AND F~COL_DT_FROM GE #COL_DATE-LOW
AND F~COL_DT_TO LE #COL_DATE-HIGH
AND F~SALES_OFFICE = #VKBUR-LOW
AND CASE WHEN A~ARREAR_DAYS <= 0 THEN 'NOD'
WHEN A~ARREAR_DAYS > 0 THEN 'OD'
END = F~OD
INNER JOIN KNA1 AS H
ON H~KUNNR = A~KUNNR
INNER JOIN TVKBT AS I
ON I~VKBUR = F~SALES_OFFICE
AND I~SPRAS = #SY-LANGU
LEFT OUTER JOIN SKAT AS J
ON J~SAKNR = A~HKONT
AND J~SPRAS = 'E'
AND J~KTOPL = '1000'
LEFT OUTER JOIN MAKT AS K
ON K~MATNR = A~MATNR
AND K~SPRAS = 'E'
LEFT OUTER JOIN T001W AS L
ON L~WERKS = A~WERKS
FIELDS
A~RBUKRS AS BUKRS,
A~KUNNR,
H~NAME1,
F~SALES_OFFICE,
I~BEZEI,
A~BELNR AS COL_BELNR,
A~GJAHR AS COL_GJAHR,
A~HSL AS COL_AMT,
A~BUDAT AS COL_DATE,
A~KEY_DATE AS KEY_OS_DATE,
A~OD_DOC AS OS_BELNR,
A~OD_YR AS OS_GJAHR,
A~BLART ,
A~OD_DATE AS OS_DATE,
CASE WHEN A~SHKZG = 'H' THEN CAST( A~WRBTR * -1 AS CURR( 12, 2 ) )
ELSE A~WRBTR
END AS OS_AMT ,
A~DUE_DATE,
A~ARREAR_DAYS,
CASE WHEN A~ARREAR_DAYS <= 0 THEN 'NOD'
WHEN A~ARREAR_DAYS > 0 THEN 'OD'
ELSE ' '
END AS OD_IND,
A~HKONT,
J~TXT50,
A~PROD_CATEGORY,
A~MATNR,
K~MAKTX,
A~WERKS ,
L~NAME1,
A~BWTAR,
A~PROFIT_CENTRE AS PRCTR,
COALESCE( F~PD_RATE , 0 ) AS RATE,
0 AS PD_AMT,
CASE WHEN A~ARREAR_DAYS > 0 THEN
CAST(
DIVISION(
( CAST( A~WRBTR * F~PD_RATE AS CURR( 15 ,2 ) ) ) ,
100 ,
3 )
AS CURR( 15, 2 ) )
* -1
ELSE
DIVISION(
( CAST( A~WRBTR * F~PD_RATE AS CURR( 15 , 2 ) ) ) ,
100 ,
3 )
END AS ITEM_PD_AMT, "DISCOUNT_RATE,
0 AS ITEM_PD_AMT1,
A~MAIN_TYPE
WHERE A~KUNNR IN #KUNNR
AND NOT EXISTS (
SELECT *
FROM ZFI_PAYMENT_DISC
WHERE BELNR = A~BELNR
AND GJAHR = A~GJAHR
AND BUKRS = A~RBUKRS
AND SALES_OFFICE = #VKBUR-LOW
AND A~KUNNR IN #KUNNR
AND OS_BELNR EQ A~OD_DOC )
AND A~ARREAR_DAYS <= #OD
AND ( F~COL_AMT_FROM LE (
SELECT SUM( COLLECT )
FROM ZPD_HSL( BUKRS = #BUKRS-LOW ,
BUDATFROM = #COL_DATE-LOW ,
BUDATO = #COL_DATE-HIGH )
WHERE KUNNR = A~KUNNR )
AND F~COL_AMT_TO GE (
SELECT SUM( COLLECT )
FROM ZPD_HSL( BUKRS = #BUKRS-LOW ,
BUDATFROM = #COL_DATE-LOW ,
BUDATO = #COL_DATE-HIGH )
WHERE KUNNR = A~KUNNR )
)
ORDER BY A~KUNNR, A~BELNR ,A~DUE_DATE ASCENDING
INTO TABLE #PROV_POST.
This is a mere guess, as your complex setup makes it hard to reproduce a working example.
My assumption is that the INNER JOIN TVKBT AS I ON I~VKBUR = F~SALES_OFFICE is getting in the way. It inner-joins to your custom table, aliased as F, and might lead to an overreduction of records if the join order is interpreted wrong.
I'd recommend to remove that join and see whether that fixes the basic join result.
In any way, I would recommend to add parentheses and reorder the join clauses to make clear what the "left" side of the equation is, and to remove any ambiguity, both for the interpreter, and the reader.
My query doesn't give the right output.
I tried with different way such as with CTE as well but nothing help.
I'm trying to write View to display available products with quantity but as some data is not available in StockCloseInventory table it displaying me 0 even though records are available for some products.
Here is my query:
SELECT TOP (100) PERCENT
SOI.InStockPrdID
, SOI.ProdName
, SOI.TradeDrugId
, ISNULL(SOI.Quantity, 0) AS INQTY
, ISNULL(SCI.QuantitySold, 0) AS OUTQTY
, ISNULL(SOI.Quantity, 0) - ISNULL(SCI.QuantitySold, 0) AS AQTY
, SOI.UnitPrice
FROM dbo.StockInventory AS SOI
LEFT OUTER JOIN dbo.StockCloseInventory AS SCI
ON SOI.InStockPrdID = SCI.InStockPrdID
AND SOI.SoldOut <> 1
LEFT OUTER JOIN dbo.vwTradeDrug AS VTD
ON SOI.TradeDrugId = VTD.TradeDrugId
ORDER BY SOI.ProdName
And this is the output of my View:
And here is my Tables
Table: StockInventory
Table: StockCloseInventory
vwTradeDrug view:
CREATE VIEW [dbo].[vwTradeDrug]
AS
SELECT
T.TradeDrugId
, T.GenericDrugId
, T.TradeName
, G.GenericProprietaryName
, G.DosageType
, G.Strength
, T.TradeName + ',' + G.GenericProprietaryName + ',' + G.DosageType
+ ',' + G.Strength AS TradeFullName
FROM dbo.TradeDrug AS T
INNER JOIN Inventory.GenericDrug AS G
ON T.GenericDrugId = G.GenericId
GO
The problem is with SOI.SoldOut <> 1
First, as Pouya Kamyar pointed out, it is more conventional to include this in a WHERE clause than in a JOIN.
However, the issue is that the value of this field is mostly NULL.
What you want is something more like this:
WHERE SOI.SoldOut IS NULL
OR SOI.SoldOut <> 1
I Suggest bring "AND SOI.SoldOut <> 1" in WHERE clause not in join terms
Either change the null data in sold out to be 0 or 1 OR...
handle the nulls so that the boolean compare results in a true or false instead of "NULL" as below.
SELECT TOP (100) PERCENT
SOI.InStockPrdID
, SOI.ProdName
, SOI.TradeDrugId
, ISNULL(SOI.Quantity, 0) AS INQTY
, ISNULL(SCI.QuantitySold, 0) AS OUTQTY
, ISNULL(SOI.Quantity, 0) - ISNULL(SCI.QuantitySold, 0) AS AQTY
, SOI.UnitPrice
FROM dbo.StockInventory AS SOI
LEFT OUTER JOIN dbo.StockCloseInventory AS SCI
ON SOI.InStockPrdID = SCI.InStockPrdID
AND ISNULL(SOI.SoldOut,0) <> 1 -- this should do the trick
LEFT OUTER JOIN dbo.vwTradeDrug AS VTD
ON SOI.TradeDrugId = VTD.TradeDrugId
ORDER BY SOI.ProdName
Remember Boolean compares on Null values result in NULL (a third value in a Boolean compare!) so SOI.SoldOut <> 1 when SOI.SoldOut is null will result in NULL instead of a true/false you're expecting.
I am trying to join 3 reports into one, taking parts out of each report.
This is my script that works with 2 of the reports:
ALTER VIEW [dbo].[v_JB2] AS
SELECT R.*, I.ENTERED, I.PROBLEM_CODE, I.WORKORDER
FROM DALE.DBO.V_JBTRB R
JOIN REPORTS.DBO.V_TC_ANALYSIS_JEREMY I
ON R.HUBID = I.HUB
AND R.NODEID = I.NODE
AND CAST(R.CREATE_DATE AS DATE) = I.ENTERED_DATE
GO
and I want to add this field A.CREATE_DATE-O.ACTUAL_START_DATE AS ASSIGN_SECS from what should be DALE.DBO.V_MTTA
Your join of DALE.DBO.V_MTTA has no ON condition...
Try this:
SELECT R.*, I.ENTERED, I.PROBLEM_CODE, I.WORKORDER,
A.CREATE_DATE-O.ACTUAL_START_DATE as ASSIGN_SECS
FROM
DALE.DBO.V_JBTRB R JOIN
REPORTS.DBO.V_TC_ANALYSIS_JEREMY I
ON R.HUBID = I.HUB AND R.NODEID = I.NODE AND
CAST(R.CREATE_DATE AS DATE) = I.ENTERED_DATE
JOIN DALE.DBO.V_MTTA A ON A.CREATE_DATE-O.ACTUAL_START_DATE = ??? (what do you want this to be joined on? I don't know how your tables are related, but you need a valid ON statement for this join to work)
so the right answer was and i don't think anyone would have been able to tell based on the code was instead of joining a third query i added to my trb query and got the same data not sure why i didn't think of it sooner.
ALTER VIEW [dbo].[v_JBTRB] AS
SELECT SINGLE_USER, RECORD_ID, DIVISION, CREATE_DATETIMEINNEW, OUTAGESTARTDATE, STATUS_DESC, CAST(HUBID AS VARCHAR(255)) AS HUBID, CAST(NODEID AS VARCHAR(255)) AS NODEID, CATEGORY, STATUS, ASSIGN_SECS
FROM OPENQUERY(REMEDY_BI,
'
SELECT
T.RECORD_ID AS Record_ID
, T.DIVISION AS Division
, T.CREATE_DATE_ET AS Create_Date
, T.TIME_IN_NEW AS TimeInNew
, O.ACTUAL_START_DATE_ET AS OutageStartDate
, T.STATUS_DESC
, T.FACILITY AS HubID
, T.NODE AS NodeID
, T.CATEGORY
, T.STATUS
, T.SINGLE_USER
, T.CREATE_DATE-O.ACTUAL_START_DATE AS ASSIGN_SECS
FROM ARNEUSER.VW_BASE_TROUBLE T
JOIN ARNEUSER.VW_BASE_OUTAGE O
ON O.INSTANCE_ID = T.OUTAGE_INSTANCE_ID
AND O.SUBMITTED_BY = T.SUBMITTER
JOIN ARNEUSER.VW_BASE_TROUBLE_TKT_ASGN_HIS A
ON A.TROUBLE_ID = T.TROUBLE_ID
AND A.CREATE_DATE = ( SELECT MIN(CREATE_DATE)
FROM ARNEUSER.VW_BASE_TROUBLE_TKT_ASGN_HIS
WHERE TROUBLE_ID=T.TROUBLE_ID
AND STATUS_NUM=1
AND CREATE_DATE>=O.ACTUAL_START_DATE
AND SUBMITTER=T.SUBMITTER )
WHERE T.STATUS > 3
AND T.REGION = ''Carolina''
AND T.CREATE_DATE >= DATE_TO_UNIX_TZ(TRUNC(SYSDATE)-14)
AND T.CATEGORY IN (''HFC'',''CRITICAL INFRASTRUCTURE'',''VIDEO DIGITAL'',''VIDEO ANALOG'',''DIGITAL PHONE'',''HEADEND/HUB'',''METRO/REGIONAL NETWORK'',''NATIONAL BACKBONE'',''NETWORK'')
')
I added this part the rest was already there if that helps this is one of the reports i was originally joining. I was trying to join another report but it came from the same data base.
, T.CREATE_DATE-O.ACTUAL_START_DATE AS ASSIGN_SECS
AND A.CREATE_DATE = ( SELECT MIN(CREATE_DATE)
FROM ARNEUSER.VW_BASE_TROUBLE_TKT_ASGN_HIS
WHERE TROUBLE_ID=T.TROUBLE_ID
AND STATUS_NUM=1
AND CREATE_DATE>=O.ACTUAL_START_DATE
AND SUBMITTER=T.SUBMITTER )
this is the other query that was being joined for anyone curious the 8 **** replace some sensitive data
ALTER VIEW [dbo].[v_TC_ANALYSIS_JEREMY] AS
SELECT *, cast(entered_date + ' ' + entered_time as datetime) as ENTERED FROM OPENQUERY(ICOMS_H,'
SELECT
W.WONUM AS WORKORDER,
W.WOTYC AS TYPE,
CVGDT2DATE(W.WOEDT) AS ENTERED_DATE,
W.WOQCD AS QUEUE_CODE,
W.WOETM AS TIME_ENTERED,
W.WOPB1 AS PROBLEM_CODE,
TRIM(H.HOAAEQ) AS HUB,
TRIM(H.HONODE) AS NODE,
H.HOZIP5 AS ZIPCODE,
W.WOPOL AS POOL,
P.EXTXD0 AS AREA,
CVGTM2TIME(W.WOETM) AS ENTERED_TIME
FROM
********.WOMHIPF W
JOIN ******.HOSTPF H ON H.HONUM = W.WOHNUM
JOIN CF83PF P ON P.EXPLLL = W.WOPOL
WHERE
W.WOEDT >= REPLACE(CHAR(CURRENT_DATE - 14 DAYS,ISO),''-'','''')-19000000
AND ((WOTYC =''SR'' AND WOQCD IN (''O'',''M'')) OR (WOTYC =''TC''))
AND WOPOL IN (''1'',''2'',''3'',''4'',''6'',''7'',''E'',''M'',''R'')
I had a query that was returning member transaction information. This query has an aggregate function to calculate the amount. All is working fine according to its grouping. Now what I need to do is to add two more columns from different tables. I did try to add them unfortunately they are giving me duplicated information with tons number of records.
Can anyone help me I just want to be able to include the two fields on the query and not include them in the group by clause. And also ensure that data returned is not a duplicate
See below is the query I used.
DECLARE #LastMonthExtractID Int = 11
SELECT x.*
,lstmnth.Submission ---added
,lm_subt.SubmissionTypeDescription ---added
FROM (
SELECT MemberRef --unique key
, SiteName
, ChargePeriod
, SUM(Amount) AS Amount
, TransactionMap
, PackageCode
FROM (
SELECT MemberRef
, SiteName
, ChargePeriod
, Amount
, PackageCode
, CASE WHEN map.TransactionMap = 'JoinFee' AND lstmnth.ChargeDate <> lstmnth.JoinDate THEN 'PayPlan'
WHEN map.TransactionMap = 'MemberFee' AND lstmnth.PackageCode LIKE 'PV%' AND lstmnth.SiteID <> 15 THEN 'VitalityMF' -- must use Package and not CURRENT PACKAGE
WHEN map.TransactionMap = 'MemberFee' AND lstmnth.PackageCode LIKE 'PV%' AND lstmnth.SiteID = 15 THEN 'PlatVitalityMF' -- PLATINUM
WHEN map.TransactionMap = 'MemberFee' AND lstmnth.PackageCode LIKE 'Z%' THEN 'ZContract'
WHEN map.TransactionMap IS NULL THEN 'Other'
ELSE map.TransactionMap END AS TransactionMap
--, lstmnth.Submission
--, lm_subt.SubmissionTypeDescription --added
FROM dbo.CCX_Billing lstmnth
LEFT JOIN dbo.TransactionMap map on lstmnth.TransactionType = map.TransactionType
AND lstmnth.TransactionDescription = map.TransactionDescription
AND ISNULL (lstmnth.AnalysisCode, '') = map.AnalysisCode
WHERE lstmnth.ExtractID = #LastMonthExtractID
) l
GROUP BY SiteName, MemberRef, ChargePeriod, PackageCode, TransactionMap
) x
INNER JOIN dbo.CCX_Billing lstmnth ON lstmnth.MemberRef = x.MemberRef
LEFT JOIN dbo.CCX_Billing_PSubmission lm_sub on lstmnth.SubmissionID = lm_sub.ID
INNER JOIN dbo.CCX_Billing_SubmissionType lm_subt on lm_sub.SubmissionTypeID = lm_subt.SubmissionID --added