Cant choose multiple values in Report Builder - reporting-services

I made a logical Mistake in Report builder in below there is 2 Sequel Query that is connected to #Income Parameter, However in the report I get this result, which doesn't make sense because it just shows same income.
I think its because I can't choose multiple incomes. When I try to choose multiple values I get this error.
This query can be executed but does not work efficiently. I am also experiencing the same for Age Queries, I cant choose multiple age as you can see in the error picture.
(SELECT MIN(YearlyIncome) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) as MinIncome,
(SELECT MAX(YearlyIncome) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) as MaxIncome
This is my Stored Procedure (DataSet1)
CREATE PROC GET_TargetCustomer (
#Age INT,
#Income int,
#Buyer int
) as
BEGIN
SELECT DISTINCT
(SELECT AVG(Age) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) AS AVGAge,
(SELECT SUM(BikeBuyer) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) as TotalBuyers,
(SELECT MAX(Age) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) AS OldestCustomer,
(SELECT AVG(YearlyIncome) FROM vTargetCustomer WHERE(#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) as AVGIncome,
(SELECT MIN(Age) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) AS YoungestCustomer,
(SELECT MIN(YearlyIncome) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) as MinIncome,
(SELECT MAX(YearlyIncome) FROM vTargetCustomer WHERE (#Age = Age) AND (#Income = YearlyIncome) AND (#Buyer = BikeBuyer)) as MaxIncome
END
GO
I have Created Data Set For each Parameter
AgeDataSet for Age Parameter
SELECT DISTINCT Age FROM vTargetCustomer ORDER BY Age ASC
IncomeDS For Income Parameter
SELECT DISTINCT YearlyIncome FROM vTargetCustomer ORDER BY YearlyIncome ASC
BuyerDS for Buyer Parameter
SELECT DISTINCT BikeBuyer FROM vTargetCustomer ORDER BY BikeBuyer ASC
Can Someone help me to figure this out?

If you are using multiple values in your paramter, the query would need to be able to use them using IN instead of equals (=).
And is there a reason why you're not combining them all in a single SELECT?
SELECT AVG(Age) AVGAge, SUM(BikeBuyer) TotalBuyers, MAX(Age), MIN(Age)
FROM vTargetCustomer
WHERE (Age = #Age)
AND (Income IN (#YearlyIncome))
AND (BikeBuyer = #Buyer)

Related

Function that will return the updated table in PostgreSQL

I have these two subqueries which work perfectly fine.
I want these two subqueries in one function and give the result together.
The function will return the UPDATED table.
This query updates the product_Details TABLE1 where start_Date matches with the current_Date and time. This will ADD offer/promotional price by matching the id with the other TABLE2 named pricing. HERE, price_and_price_Type is JSONB TYPE object.
update product_Details
SET price_and_price_Type = price_and_price_Type ||
( pps.details::jsonb )
FROM (
select id, price_Details as details
from pricing
where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, details
) as pps
where product_Details.id = "pps".id;
The following query updates the product_Details TABLE1 where end_date matches with the current_Date and time. This will REMOVE offer/promotional price IF ALREADY EXISTS by matching the id with the other TABLE2 named pricing. HERE, price_and_price_Type is JSONB TYPE object.
update product_Details
set price_And_price_Type= price_And_price_Type - pps.price_details
FROM (
select id, jsonb_object_keys(price_Details) price_details
from pricing
where to_char(end_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, price_details
) as pps
where product_Details.id = "pps".id;
I tried to write this function which isnt working as I want to return product_Details table with the updated value. But, I'm getting errors as it wants column name which I cant provide as update isnt inserting new data, Its just an update!
The function is written as.
CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS TABLE()
AS
$$
BEGIN
return query
update product_Details
SET price_and_price_Type = price_and_price_Type ||
( pps.details::jsonb )
FROM (
select id, price_Details as details
from pricing
where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, details
) as pps
where product_Details.id = "pps".id;
update product_Details
set price_And_price_Type= price_And_price_Type - pps.price_details
FROM (
select id, jsonb_object_keys(price_Details) price_details
from pricing
where to_char(end_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, price_details
) as pps
where product_Details.id = "pps".id;
END;
$$
LANGUAGE PLPGSQL;
How to write this function to fit these two subqueries in the function and where I can just call the function and get the updated table.
Thanks!
As proposed by #Laurenz, adding a RETURNING clause would make the job if you had only one UPDATE in your function :
CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS setof record LANGUAGE PLPGSQL AS
$$
BEGIN
return query
update product_Details
SET price_and_price_Type = price_and_price_Type ||
( pps.details::jsonb )
FROM (
select id, price_Details as details
from pricing
where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, details
) as pps
where product_Details.id = "pps".id
RETURNING * ;
END;
$$;
But as you have two UPDATE in the same function, if you want to get the results from both, then you need to group them in a SELECT clause :
CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS setof record LANGUAGE PLPGSQL AS
$$
BEGIN
return query
WITH update1 AS (
update product_Details
SET price_and_price_Type = price_and_price_Type ||
( pps.details::jsonb )
FROM (
select id, price_Details as details
from pricing
where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, details
) as pps
where product_Details.id = "pps".id
returning *
), update2 AS (
update product_Details
SET price_And_price_Type= price_And_price_Type - pps.price_details
FROM (
select id, jsonb_object_keys(price_Details) price_details
from pricing
where to_char(end_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, price_details
) as pps
where product_Details.id = "pps".id
returning *
) SELECT * FROM update1 UNION ALL SELECT * FROM update2 ;
END;
$$ ;
The same function in sql language :
CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS setof record LANGUAGE sql VOLATILE AS $$
WITH update1 AS (
update product_Details
SET price_and_price_Type = price_and_price_Type ||
( pps.details::jsonb )
FROM (
select id, price_Details as details
from pricing
where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, details
) as pps
where product_Details.id = "pps".id
returning price_And_price_Type
), update2 AS (
update product_Details
SET price_And_price_Type= price_And_price_Type - pps.price_details
FROM (
select id, jsonb_object_keys(price_Details) price_details
from pricing
where to_char(end_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
group by id, price_details
) as pps
where product_Details.id = "pps".id
returning price_And_price_Type
) SELECT * FROM update1 UNION ALL SELECT * FROM update2 ;
$$ ;

how to sum amount from two different tables in one query without using union mysql

SELECT SUM(amount_paid) AS total_session FROM session
WHERE session.date = '2016-02-03' AND session.payment_status = 'PAID'
UNION
SELECT SUM(amount_paid) AS total_subscription FROM subscription
WHERE subscription.date_enrolled = '2016-02-03' AND subscription.payment_status = 'PAID'
I just want to display the result in two different columns with alias total_session and total_subscription. How can I do that using union?
Instead of a UNION, put each query as a subquery in a main SELECT
SELECT
(SELECT SUM(amount_paid) FROM session
WHERE session.date = '2016-02-03' AND session.payment_status = 'PAID') AS total_paid,
(SELECT SUM(amount_paid) FROM subscription
WHERE subscription.date_enrolled = '2016-02-03' AND subscription.payment_status = 'PAID') AS total_subscription
In that case, you can try doing a fake join like
SELECT xx.total_session, yy.total_subscription
FROM (
SELECT '1' as ID, SUM(amount_paid) AS total_session FROM session
WHERE session.date = '2016-02-03' AND session.payment_status = 'PAID') xx
JOIN (SELECT '1' as ID, SUM(amount_paid) AS total_subscription FROM subscription
WHERE subscription.date_enrolled = '2016-02-03' AND subscription.payment_status = 'PAID') yy
ON xx.ID = yy.ID;

Need to create an update from a Join with nested selects

I have this:
SELECT users.first_name,
users.last_name,
family_products.costs_obj
FROM users
JOIN family_products
ON users.family_id = family_products.family_id
WHERE users.family_id IN (SELECT family_id
FROM employer_families
WHERE employer_id = 117)
AND family_products.product_id IN (SELECT id
FROM market_products
WHERE type = "medicalplan")
AND users.first_name = 'alexandre'
And i need to be able to update cost_obj to = '' how would i run this select as an update?
Although I have not been able to test this, I think this is what you need:
UPDATE fp
SET fp.costs_obj = ''
FROM
users u
JOIN family_products fp ON u.family_id = fp.family_id
WHERE
u.family_id IN
(
SELECT
family_id
FROM
employer_families
WHERE
employer_id = 117
)
AND
fp.product_id IN
(
SELECT
id
FROM
market_products
WHERE
type = "medicalplan"
)
AND
u.first_name = 'alexandre';
Didn't test this since I don't know the schema, but you can try this:
UPDATE family_products
SET costs_obj = ''
WHERE costs_obj IN(
SELECT
family_products.costs_obj
FROM users
JOIN family_products
ON users.family_id = family_products.family_id
WHERE users.family_id IN (SELECT family_id
FROM employer_families
WHERE employer_id = 117)
AND family_products.product_id IN (SELECT id
FROM market_products
WHERE type = "medicalplan")
AND users.first_name = 'alexandre'
)

MySql: How to check field has data in MySql?

Here is my code and result :
SELECT DISTINCT
CAL.CarListingId,
(SELECT
IF(REPLACE(carImage.ImageUrl,'~','') IS NULL,'asdf',REPLACE(carImage.ImageUrl,'~',''))
FROM
carImage
WHERE
IsMainImage = 1 AND Status = 1
AND CarListingId = CAL.CarListingId) AS ImageUrl,
CAL.ListingNumber,
CAL.Caption,
CAL.Year,
CAL.Km,
CAL.Color,
CAL.Price,
CONCAT((SELECT Name FROM City WHERE CityId IN (SELECT CityId FROM County WHERE CountyId = CAL.CountyId)),'/', (SELECT Name FROM County WHERE CountyId = CAL.CountyId)) AS Region,
CAL.Creation
FROM
carlisting AS CAL
INNER JOIN
User AS U ON U.UserId = CAL.CreatedBy
INNER JOIN
carlistingcategory AS CLC ON CLC.CarListingId = CAL.CarListingId
LEFT JOIN CarImage AS CI ON CI.CarListingId = CAL.CarListingId
ORDER BY CAL.Creation;
I use this query as a subquery in another query. I need to check this query's result if it is `NULL`. But as you can see there is no data so `IS NULL` returns false. How can I check the sub query has data ?
try this query:
SELECT DISTINCT
CAL.CarListingId,
CAL.ListingNumber,
CAL.Caption,
CAL.Year,
CAL.Km,
CAL.Color,
CAL.Price,
CONCAT((SELECT Name FROM City WHERE CityId IN (SELECT CityId FROM County WHERE CountyId = CAL.CountyId)),'/', (SELECT Name FROM County WHERE CountyId = CAL.CountyId)) AS Region,
CAL.Creation,
( case when CI.ImageUrl IS NULL then 'asdf' else CI.ImageUrl
end)
FROM
carlisting AS CAL
LEFT JOIN CarImage AS CI ON CI.CarListingId = CAL.CarListingId
INNER JOIN User AS U ON U.UserId = CAL.CreatedBy
INNER JOIN carlistingcategory AS CLC ON CLC.CarListingId = CAL.CarListingId
ORDER BY CAL.Creation;

subquery returning multiple rows..how to deal with it?

SELECT requestID
FROM request
WHERE userId = (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept = (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
It would be much clearer to write this query using JOIN:
select distinct r.requestID
from
request r
join department d
on d.userId = r.userID
and desig = 'E'
join department d2
on d2.dept = d.dept
and d2.desig = 'FM'
and d2.userId = 'it18'
Alternately, You could simply replace the = with IN:
SELECT requestID
FROM request
WHERE userId IN (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept IN (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
They should return identical results, but try both to see if there's any difference in performance.
There are two ways of dealing with it:
Option 1: Change to WHERE ... IN (SELECT ...), like this:
SELECT requestID
FROM request
WHERE userId IN (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept IN (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
Option 2: Force only one result by using an aggregate function like MAX(), like this:
SELECT requestID
FROM request
WHERE userId = (
SELECT MAX(userID)
FROM department
WHERE desig = 'E'
AND dept = (
SELECT MAX(dept)
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);