Incorrect results using parameter in SSRS - sql

I have an SSRS report for which I created two datasets for. One for the actual report and one for the parameter.
Dataset 1 is the report
Dataset 2 simply pulls the parameter that I want to report on from the table where it is stored.
When I run the report and select the parameter, it shows me all the values, but when I select one, I still get every record rather than just the records based on the parameter.
Dataset 1
SELECT stk.ItemCode AS ItemCode, stk.warehouse AS warehouse,
stk.Description AS Description, stk.Assortment AS Assortment,
Items.Class_02, stk.ItemGroup AS ItemGroup, stk.ItemStatus AS ItemStatus,
stk.ItemUnit AS ItemUnit, ISNULL((SELECT TOP 1 dbo.bacoSalesPrice(p.ID,
0) FROM staffl p WHERE ({d '2019-05-27'} BETWEEN validfrom AND
ISNULL(validto,{d '9999-12-31'}))
AND prijslijst = 'SALESPRICE' AND Items.ItemCode = artcode ORDER BY
artcode, validfrom desc),0) AS SalesPackagePrice,
ROUND((stk.Stock/stk.SalesPkg),2) AS Stock, ROUND((stk.Stock +
stk.QtyToBeReceived - stk.QtyToBeDelivered)/stk.SalesPkg,2) AS
AvailableStock, stk.SearchCode AS SearchCode FROM Items INNER JOIN (
SELECT
v.magcode AS warehouse,
MAX(i.itemcode) AS ItemCode,
MAX(i.description_0) AS Description,
i.SearchCode AS SearchCode,
MAX(ia.Assortment) AS Assortment,
MAX(ia.Description_0) AS ItemGroup,
MAX(CASE
WHEN i.condition = 'A' THEN 'Active'
WHEN i.condition = 'B' THEN 'Blocked'
WHEN i.condition = 'D' THEN 'Discontinued'
WHEN i.condition = 'E' THEN 'Inactive'
WHEN i.condition = 'F' THEN 'Future'
END) AS ItemStatus,
ISNULL(MAX(a.purchasepackage), '-') AS ItemUnit,
MAX(i.SalesPackagePrice) AS SalesPackagePrice,
MAX(v.bestniv) AS Minimum,
MAX(v.maxvrd) AS Maximum,
MAX(i.lev_crdnr) AS Supplier,
MAX(a.DeliveryTimeInDays) AS DeliveryTime,
MAX(ISNULL(a.SlsPkgsPerPurPkg,1)) AS SalesPkg,
ISNULL(Actual.Quantity,0) AS Stock
,ISNULL(SUM(QtyToReceived),0) AS QtyToBeReceived
,ISNULL(SUM(QtyToBeDelivered),0) AS QtyToBeDelivered
FROM items i
JOIN voorrd v ON i.itemcode=v.artcode
LEFT JOIN itemaccounts a ON i.itemcode=a.itemcode and i.lev_crdnr=a.crdnr
AND i.itemcode IS NOT NULL AND a.itemcode IS NOT NULL AND i.lev_crdnr IS N
NOT NULL AND a.crdnr IS NOT NULL
JOIN ItemAssortment ia ON ia.Assortment = i.Assortment
LEFT OUTER JOIN
dbo.TempToBeReceivedDelivered#3E5BBBE6#FB7B#4309#9CE1#560885B9BF94# budget
ON v.magcode = budget.warehouse AND i.ItemCode = budget.artcode
AND v.magcode IS NOT NULL AND budget.warehouse IS NOT NULL AND i.ItemCode IS NOT NULL AND budget.artcode IS NOT NULL
LEFT OUTER JOIN
(SELECT GX.artcode, GX.warehouse,
GX.Quantity,
GX.AmtActualStock, GX.TotalCnt,
CASE WHEN GX.FreeQuantity > GX.CurrentQuantity THEN (CASE WHEN GX.CurrentQuantity < 0 THEN 0 ELSE GX.CurrentQuantity END) ELSE
(CASE WHEN GX.FreeQuantity < 0 THEN 0 ELSE GX.FreeQuantity END) END As FreeQuantity
FROM (
SELECT sb.ItemCode AS artcode, SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.Quantity END) as Quantity,
ROUND(SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.StockAmount END),2) as AmtActualStock,
SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.Quantity END) as CurrentQuantity,
SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.FreeStock END) AS FreeQuantity, sb.Warehouse,
SUM(sb.GbkmutCount) AS TotalCnt
FROM StockBalances sb WITH (NOLOCK) JOIN Items ON sb.ItemCode = Items.ItemCode JOIN ItemAssortment ON Items.Assortment = ItemAssortment.Assortment
--WHERE Items.Class_02 in #Customer
GROUP BY sb.ItemCode, sb.WareHouse, Items.Class_02
HAVING SUM(sb.GbkmutCount) > 0) GX) AS Actual
ON Actual.artcode = i.ItemCode AND Actual.warehouse = v.magcode AND Actual.artcode IS NOT NULL AND i.ItemCode IS NOT NULL AND Actual.warehouse IS NOT NULL AND v.magcode IS NOT NULL
WHERE i.type IN ('S', 'B')
AND i.ItemCode BETWEEN '0030122186' AND 'XS45000'
AND i.Condition IN ('A')
AND ((i.IsSalesItem <> 0)) AND i.Type IN ('S', 'B')
AND 1=1
GROUP BY v.magcode , i.itemcode, i.SearchCode, Actual.Quantity
) stk ON items.ItemCode = stk.ItemCode
INNER JOIN grtbk ON Items.GLAccountDistribution = grtbk.reknr
ORDER BY stk.ItemCode
Dataset 2
SELECT Class_02, ItemCode FROM Items
Parameter in SSRS is set to Get values from Query, Value field: Class_02
Label field: Class_02

Related

Using an array in conditional summation with grouping

I have the sql query thats work fine
SELECT
CAST(L.CreationUtcDateTime AS DATE),
SUM(L.Profit),
SUM(CASE WHEN (L.Network = 0 OR L.Network = 1) THEN L.Profit ELSE 0 END),
SUM(CASE WHEN (L.Network != 0 AND L.Network != 1) THEN L.Profit ELSE 0 END)
FROM
[dbo].[Leads] L WITH (NOLOCK)
LEFT JOIN [dbo].[Transactions] S WITH (NOLOCK) ON L.TransactionId = S.Id
GROUP BY
CAST(L.CreationUtcDateTime AS DATE);
I need to make it more generic by using variables instead of hard-coded constants.
I changed the query to:
DECLARE #matchNetworks TABLE (id int)
INSERT #matchNetworks(id) VALUES (0),(1)
SELECT
CAST(L.CreationUtcDateTime AS DATE),
SUM(L.Profit),
SUM(CASE WHEN (L.Network in (SELECT ID from #matchNetworks)) THEN L.Profit ELSE 0 END),
SUM(CASE WHEN (L.Network not in (SELECT ID from #matchNetworks)) THEN L.Profit ELSE 0 END)
FROM
[dbo].[Leads] L WITH (NOLOCK)
LEFT JOIN [dbo].[Transactions] S WITH (NOLOCK) ON L.TransactionId = S.Id
GROUP BY
CAST(L.CreationUtcDateTime AS DATE);
And now i have an error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
How can I use the predefined array of network ids in my query to avoid an error?
Move the comparison to the FROM clause using a LEFT JOIN:
SELECT CAST(L.CreationUtcDateTime AS DATE),
SUM(L.Profit),
SUM(CASE WHEN mn.ID IS NOT NULL THEN L.Profit ELSE 0 END),
SUM(CASE WHEN mn.ID IS NULL THEN L.Profit ELSE 0 END)
FROM [dbo].[Leads] L LEFT JOIN
[dbo].[Transactions] S
ON L.TransactionId = S.Id LEFT JOIN
#matchNetworks mn
ON mn.ID = l.Network
GROUP BY CAST(L.CreationUtcDateTime AS DATE);

Complex Case Statement Issue - Oracle SQL

Wrote the query below, but am getting multiplied amounts because the aggregation needs to occur before the case statements. Would love some advice on the best way to structure this.
Select Store, CUSTID, CUST.ID_CUST,
Sum(
CASE
WHEN Cust_Gift.Code_Status = 'C' AND Gift_Item.FLAG_STORE_LOC = 'N'
THEN Cust_Gift.AMT_PAID ELSE 0
END) GiftAmt,
Sum(
CASE WHEN Cust_Gift.Code_Status = 'C' AND Gift_Item.FLAG_STORE_LOC = 'Y'
THEN Cust_Gift.AMT ELSE 0
END) CustGiftAmt,
Sum(
CASE WHEN Cust_Coupon.Code_Status = 'C'
THEN Cust_Coupon.AMT
ELSE 0
END) CouponAmt,
Sum(CASE WHEN Cust_Sports.Status = 'C'
THEN Cust_Sports.AMT
ELSE 0
END) SportsAmt
FROM CUST
LEFT OUTER JOIN CUST_GIFT
ON CUST.ID_CUST = CUST_GIFT.ID_CUST
LEFT OUTER JOIN CUST_COUPON
ON CUST.ID_CUST = CUST_COUPON.ID_CUST
LEFT OUTER JOIN CUST_SPORTS
ON CUST.ID_CUST = CUST_SPORTS.ID_CUST
INNER JOIN GIFT_ITEM
ON CUST_GIFT.ID_GIFT_ITEM = GIFT_ITEM.ID_GIFT_ITEM
WHERE (STORE = 'M669098' OR STORE = 'M66923434' )
Group by CustID, Store, CUST.ID_CUST
This is one way you could do it:
SELECT cust.store,
cust.custid,
cust.id_cust,
gift.giftamt,
gift.custgift,
cpn.couponamt,
sprt.sportsamt
FROM cust
LEFT OUTER JOIN (SELECT id_cust,
SUM(CASE WHEN cg.code_status = 'C' AND gi.flag_store_loc = 'N' THEN cg.amt_paid END) giftamt,
SUM(CASE WHEN cg.code_status = 'C' AND gi.flag_store_loc = 'Y' THEN cg.amt_paid END) custgiftamt
FROM cust_gift cg
INNER JOIN gift_item gi ON cg.id_gift_item = gi.id_gift_item) gift ON cust.id_cust = gift.id_cust
LEFT OUTER JOIN (SELECT id_cust,
SUM(CASE WHEN code_status = 'C' THEN amt END) couponamt
FROM cust_coupon) cpn ON cust.id_cust = cpn.id_cust
LEFT OUTER JOIN (SELECT id_cust,
SUM(CASE WHEN status = 'C' THEN amt END) sportsamt
FROM cust_sports) sprt ON cust.id_cust = sprt.id_cust
WHERE (STORE = 'M669098' OR STORE = 'M66923434');

SQL Server 2012 - is there a better way to do this as when there are duplicates it counts them more than once?

This is not accurate as the count can be wrong so is there a better way using exists? I want to identify if one case of each course exists.
SELECT
IdentityCourses.IdentityID AS ID,Identities.LastName AS LastName,
Identities.FirstNames AS FirstName,Units.UnitID, Units.Description AS Unit
FROM
dbo.UnitIdentities
INNER JOIN
dbo.IdentityCourses ON dbo.UnitIdentities.IdentityID = dbo.IdentityCourses.IdentityID
INNER JOIN
dbo.COCSourceCourses ON dbo.IdentityCourses.CourseID = dbo.COCSourceCourses.CBESCourseID
INNER JOIN
dbo.Identities ON dbo.UnitIdentities.IdentityID = dbo.Identities.IdentityID
INNER JOIN
dbo.Units ON dbo.UnitIdentities.UnitID = dbo.Units.UnitID
WHERE
(dbo.UnitIdentities.IsActiveMember = 1)
GROUP BY
IdentityCourses.IdentityID, Identities.LastName, Identities.FirstNames,
Units.Description, Units.UnitID
HAVING
(SUM((CASE WHEN COCSourceCourses.COCID = 10048 then 1 else 0 end)+
(CASE WHEN COCSourceCourses.COCID = 10049 then 1 else 0 end)+
(CASE WHEN COCSourceCourses.COCID = 10050 then 1 else 0 end)+
(CASE WHEN COCSourceCourses.COCID = 10051 then 1 else 0 end)+
(CASE WHEN COCSourceCourses.COCID = 10063 then 1 else 0 end)+
(CASE WHEN COCSourceCourses.COCID = 10073 then 1 else 0 end))) = 6
AND IdentityCourses.IdentityID NOT IN (SELECT IdentityID
FROM IdentityQualifications
WHERE QualificationID IN (1012, 1014, 1025))
ORDER BY
Units.UnitID
Try using count(distinct ..):
SELECT (..columns..)
FROM dbo.UnitIdentities UI
LEFT JOIN IdentityQualifications IQ
ON IQ.IdentityID = UI.IdentityID
AND IQ.QualificationID IN (1012, 1014, 1025)
INNER JOIN dbo.IdentityCourses IC
ON IC.IdentityID = dbo.UnitIdentities.IdentityID
INNER JOIN dbo.COCSourceCourses COC
ON COC.CBESCourseID = IC.CourseID
AND COC.COCID IN (10048, 10049, 10050, 10051, 10063, 10073)
(..two more table joins on identities and units..)
WHERE IQ.IdentityID IS NULL
GROUP BY (..columns..)
HAVING COUNT(DISTINCT COC.COCID) = 6
ORDER BY Units.UnitID
When you are only interested in certain records, then why don't you use the WHERE clause? Only select the COCIDs you are interested in and then count distinct results.
You don't need any GROUP BY and HAVING by the way, as you only display identities/units, so you can count associated courses in a subquery in your WHERE clause.
select
i.identityid as id,
i.lastname as lastname,
i.firstnames as firstname,
u.unitid,
u.description as unit
from dbo.identities i
join dbo.unitidentities ui on ui.identityid = i.identityid and ui.isactivemember = 1
join dbo.units u on u.unitid = ui.unitid
where i.identityid not in
(
select iq.identityid
from identityqualifications iq
where iq.qualificationid in (1012, 1014, 1025)
)
and
(
select count(distinct sc.cocid)
from dbo.cocsourcecourses sc
join dbo.identitycourses ic on ic.courseid = sc.cbescourseid
where sc.cocid in (10048, 10049, 10050, 10051, 10063, 10073)
and ic.identityid = i.identityid
) = 6
order by u.unitid;

SQL SELECT Statement - Select record based on number of matches from second table

This is my first question so sorry in advance for breaking any unspoken rules. I currently am trying to retrieve categories from a InventoryCategory table using the primary key from the InventoryItem table.
Since an item can have one or more categories, the InventoryCategory table holds the ItemCode, CategoryCode, and IsPrimary (to define which category is primary category and is TRUE/FALSE type)
I really need to use the main category for all items with only one category and the non-primary category for items with two categories. I am trying to use a CASE statement to output the right value as a named ParentCategory field but having tough time.
Currently, I just have WHERE IC.IsPrimary = 1 since I haven't been able to figure out how to query the ItemCode to return # of CategoryCodes and select non-primary category based on that. I tried doing a COUNT but it always returns 1 and keep striking out.
Code:
SELECT
BillToCode, POSWorkstationID,
InvoiceDate, ItemName,
CategoryCode,
SUM(QtySales) QtySales,
SUM(Sales) Sales,
SUM(QtyReturns) QtyReturns,
SUM([Returns]) [Returns],
WarehouseCode
FROM
(SELECT
CI.BillToCode, CI.WarehouseCode,
CI.POSWorkstationID, CI.InvoiceDate,
IC.CategoryCode, ii.ItemName,
CASE
WHEN CI.Type IN ('Invoice', 'Opening Invoice')
THEN CID.QuantityShipped
ELSE 0
END AS QtySales,
CASE
WHEN CI.Type IN ('Invoice', 'Opening Invoice')
THEN CID.ExtPriceRate
ELSE 0
END AS Sales,
CASE
WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate')
THEN CID.QuantityShipped
ELSE 0
END AS QtyReturns,
CASE
WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate')
THEN CID.ExtPriceRate
ELSE 0
END AS [Returns]
FROM
CustomerInvoice CI
INNER JOIN
Customer C ON CI.BillToCode = C.CustomerCode
INNER JOIN
CustomerInvoiceDetail CID ON CI.InvoiceCode = CID.InvoiceCode
INNER JOIN
InventoryItem ii ON CID.ItemCode = ii.ItemCode
INNER JOIN
InventoryCategory IC ON CID.ItemCode = IC.ItemCode
WHERE
(CI.IsBatch = 0 OR CI.IsBatch IS NULL)
AND CI.IsPosted = 1
AND CI.[Type] = 'Invoice'
AND CI.IsVoided = 0
AND IC.IsPrimary = 1) Items
GROUP BY
POSWorkstationID, BillToCode, ItemName, InvoiceDate, CategoryCode, WarehouseCode
If the IsPrimary field is 0 for the non-primary row, you could do a SELECT MIN statement. See below:
SELECT BillToCode
, POSWorkstationID
, InvoiceDate
, ItemName
, CategoryCode
, SUM(QtySales) QtySales
, SUM(Sales) Sales
, SUM(QtyReturns) QtyReturns
, SUM([Returns]) [Returns]
, WarehouseCode
FROM (
SELECT CI.BillToCode
, CI.WarehouseCode
, CI.POSWorkstationID
, CI.InvoiceDate
, IC.CategoryCode
, ii.ItemName
, CASE WHEN CI.Type IN ('Invoice', 'Opening Invoice') THEN CID.QuantityShipped ELSE 0 END AS QtySales
, CASE WHEN CI.Type IN ('Invoice', 'Opening Invoice') THEN CID.ExtPriceRate ELSE 0 END AS Sales
, CASE WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate') THEN CID.QuantityShipped ELSE 0 END AS QtyReturns
, CASE WHEN CI.Type IN ('Credit Memo', 'Opening Credit', 'Gift Card', 'Gift Certificate') THEN CID.ExtPriceRate ELSE 0 END AS [Returns]
FROM CustomerInvoice CI
INNER JOIN Customer C ON CI.BillToCode = C.CustomerCode
INNER JOIN CustomerInvoiceDetail CID ON CI.InvoiceCode = CID.InvoiceCode
INNER JOIN InventoryItem ii ON CID.ItemCode = ii.ItemCode
INNER JOIN InventoryCategory IC ON CID.ItemCode = IC.ItemCode
WHERE (CI.IsBatch = 0 OR CI.IsBatch IS NULL)
AND CI.IsPosted = 1
AND CI.[Type] = 'Invoice' AND CI.IsVoided = 0
AND IC.IsPrimary = (SELECT MIN(IsPrimary) FROM InventoryCategory ICi WHERE ICi.ItemCode = IC.ItemCode)
) Items
GROUP BY POSWorkstationID, BillToCode, ItemName, InvoiceDate, CategoryCode, WarehouseCode
Assuming your InventoryCategory data looks something like this:
ItemCode CategoryCode IsPrimary
abc def 1
abc xyz 0
abc 123 0
Instead of joining to that table, used a "derived table" (subquery) such as this:
inner join (
select
ItemCode
, max(case when IsPrimary = 1 then CategoryCode end) PrimCategory
, max(case when IsPrimary = 0 then CategoryCode end) SecCategory
, min(CategoryCode) as tertiaryCategory
from InventoryCategory
group by ItemCode
) IC ON CID.ItemCode = IC.ItemCode
Then in your main select clause you can use this (or a case expression perhaps):
COALESCE(IC.PrimCategory,IC.SecCategory,IC.tertiaryCategory) as CategoryCode

How can I rewrite this query without repeating SELECT subqueries

I have written a query like this. It is working and giving me the result which I wanted.
I have a SELECT query to get CAR_AMOUNT and HOTEL_AMOUNT.
But, I have to repeat the same SELECT query to get the SUM of both. I am not able to use alias name. How can I avoid this?
SELECT B.EMPLOYEE_ID,
(select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'CAR'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED') as CAR_AMOUNT,
(select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'HOTEL'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED') as HOTEL_AMOUNT,
NVL((select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'CAR'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED'),0)
+NVL((select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'HOTEL'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED'),0) as TOTAL
FROM TRAVEL_REQUEST_ITEM A
LEFT OUTER JOIN TRAVEL_REQUEST B
ON (B.TRAVEL_REQUEST_NO= A.TRAVEL_REQUEST_SR_NO)
select b.employee_id
, sum(case when c.type = 'CAR' then c.amount end) car_amount
, sum(case when c.type = 'HOTEL' then c.amount end) hotel_amount
, sum(c.amount) total
from travel_request_item a
left outer join travel_request b
on (b.travel_request_no= a.travel_request_sr_no)
left outer join travel_reimbursements_items c
on ( c.travel_request_no = b.travel_request_no
and c.type in ('CAR','HOTEL')
and c.status = 'APPROVED'
)
group by b.employee_id
Regards,
Rob.
This should work in oracle.
SELECT B.EMPLOYEE_ID,
SUM(CASE WHEN C.type = 'CAR' and C.STATUS='APPROVED' THEN C.AMOUNT ELSE 0 END) as CAR_AMOUNT,
SUM(CASE WHEN C.type = 'HOTEL' and C.STATUS='APPROVED' THEN C.AMOUNT ELSE 0 END) as HOTEL_AMOUNT,
SUM(CASE WHEN C.type IN ('CAR', 'HOTEL') and C.STATUS='APPROVED' THEN C.AMOUNT ELSE 0 END) as TOTAL
FROM TRAVEL_REQUEST_ITEM A
LEFT OUTER JOIN TRAVEL_REQUEST B ON (B.TRAVEL_REQUEST_NO= A.TRAVEL_REQUEST_SR_NO)
LEFT OUTER JOIN TRAVEL_REIMBURSEMENTS_ITEMS C ON (C.TRAVEL_REQUEST_NO = A.TRAVEL_REQUEST_SR_NO)
GROUP BY B.EMPLOYEE_ID
Just do a conditional sum based on the item type.