SSRS Using an expression to specify values for parameters - sql

I am trying to have a parameter that either filters out interns or includes interns and every other job title using a column that holds 'Yes' or 'No' whether that job title is an intern.
The basics of my code are like this:
SELECT
Date
,BillHours
,OrgArea
,Jobtitle
,InternYesNo
FROM(
SELECT
Date
,BillHours
,OrgArea
,Jobtitle
,CASE WHEN LEFT(jobtitle, 6) = 'Intern' THEN 'Yes' ELSE 'No' END AS 'InternYesNo') MainQuery
WHERE InternYesNo IN(#includeinterns)
So I'm thinking I want to specify the available values in the #includeinterns parameter, but I think I would need to have one of the values be an expression so that it includes both 'Yes' and 'No' rows based on the InternYesNo column.
My question is how do I write an expression for specifying values so that one of the available values includes both 'Yes' and 'No' rows? Do I need an additional dataset to hold those values? Or is there better way to accomplish this?

I think I have figured out a solution. I updated my code to this:
SELECT
Date
,BillHours
,OrgArea
,Jobtitle
,InternYesNo
FROM(
SELECT
Date
,BillHours
,OrgArea
,Jobtitle
,CASE WHEN LEFT(jobtitle, 6) = 'Intern' THEN 'Yes' ELSE 'No' END AS 'InternYesNo') MainQuery
WHERE (InternYesNo = #includeinterns OR #includeinterns = 'abc')
Then I specify available values with one labeled as 'No' and the value is 'No' and the other is labeled as 'Yes' with a value of 'abc'.

Related

Query that detects difference between accounts/loads from TODAY and YESTERDAY

GOAL: DETECT any difference between yesterday's table loads and today's loads. Each load loads values of data that are associated with bank accounts. So I need a query that returns each individual account that has a difference, with the value in the column name.
I need data from several columns that are located from two different tables. AEI_GFXAccounts and AEI_GFXAccountSTP. Each time the table is loaded, it has a "run_ID" that is incremented by one. So it needs to be compared to MAX(run_id) and MAX(run_id) -1.
I have tried the following queries. All this query does is return all the columns I need. I now need to implement logic that runs these queries WHERE runID = MAX(runID). Then run it again where run_ID = Max(runID) -1. Compare the two tables, show the differences that can be displayed under columns like SELECT AccountBranch WHERE MAX(Run_ID) -1 AS WAS. etc. and another custom named column as 'IS NOW' etc for each column.
SELECT AEI_GFXAccounts.AccountNumber,
AccountBranch,
AccountName,
AccountType,
CostCenter,
TransactionLimit,
ClientName,
DailyCumulativeLimit
FROM AEI_GFXAccounts
JOIN AEI_GFXAccountSTP
ON (AEI_GFXAccounts.feed_id = AEI_GFXAccountSTP.feed_id
and AEI_GFXAccounts.run_id = AEI_GFXAccountSTP.run_id)
I use something similar to this to detect changes for a logging system:
WITH data AS (
SELECT
a.run_id,
a.AccountNumber,
?.AccountBranch,
?.AccountName,
?.AccountType,
?.CostCenter,
?.TransactionLimit,
?.ClientName,
?.DailyCumulativeLimit
FROM
AEI_GFXAccounts a
INNER JOIN AEI_GFXAccountSTP b
ON
a.feed_id = b.feed_id and
a.run_id = b.run_id
),
yest AS (
SELECT * FROM data WHERE run_id = (SELECT MAX(run_id)-1 FROM AEI_GFXAccounts)
),
toda AS (
SELECT * FROM data WHERE run_id = (SELECT MAX(run_id) FROM AEI_GFXAccounts)
)
SELECT
CASE WHEN COALESCE(yest.AccountBranch, 'x') <> COALESCE(toda.AccountBranch, 'x') THEN yest.AccountBranch END as yest_AccountBranch,
CASE WHEN COALESCE(yest.AccountBranch, 'x') <> COALESCE(toda.AccountBranch, 'x') THEN toda.AccountBranch END as toda_AccountBranch,
CASE WHEN COALESCE(yest.AccountName, 'x') <> COALESCE(toda.AccountName, 'x') THEN yest.AccountName END as yest_AccountName,
CASE WHEN COALESCE(yest.AccountName, 'x') <> COALESCE(toda.AccountName, 'x') THEN toda.AccountName END as toda_AccountName,
...
FROM
toda INNER JOIN yest ON toda.accountNumber = yestaccountNumber
Notes:
You didn't say which table some of your columns are from. I've prefixed them with ?. - replace these with a. or as. respectively (always good practice to fully qualify all your column aliases)
When you're repeating out the pattern in the bottom select (above ...) choose data for the COALESCE that will not appear in the column. I'm using COALESCE as a quick way to avoid having to write CASE WHEN a is null and b is not null or b is null and a is not null or a != b, but the comparison fails if accountname (for example) was 'x' yesterday and today it is null, because the null becomes 'x'. If you pick data that will never appear in the column then the check will work out because nulls will be coalesced to something that can never appear in the real data, and hence the <> comparison will work out
If you don't care when a column goes to null today from a value yesterday, or was null yesterday but is a value today, you can ditch the coalesce and literally just do toda.X <> yest.X
New accounts today won't show up until tomorrow. If you want them to show up do toda LEFT JOIN yest .... Of course all their properties will show as new ;)
This query returns all the accounts regardless of whether any changes have been made. If you only want a list of accounts with changes you'll need a where clause that is similar to your case whens:
WHERE
COALESCE(toda.AccountBranch, 'x') <> COALESCE(yest.AccountBranch, 'x') OR
COALESCE(toda.AccountName, 'x') <> COALESCE(yest.AccountName, 'x') OR
...
Do you have a date field? If so you can use Row_Number partitioned by your accounts. Exclude all accounts that have a max of 1 row 'New accounts", and then subtract the Max(rownumber) of each account's load by the Max(rownumber)-1's load. Only return accounts where this returned load is >0.You can also use the lag function to grab the previous accounts load instead of Max(rownumber)-1

CASE GROUPING in which I need to manually add in as null to a value

I have one student grade of null that I manually need to add in the below as an f (to match a grade received and reported previously). I am trying to find a way to do this in SQL Server without having to do it in Excel.
Here is what I have in the select statement for the grades portion (also showing that I am doing the group by roll up at the end):
SELECT
CASE grouping (STC_GRADE)
WHEN 1 THEN 'total' ELSE STC_GRADE
END AS 'MARK ANALYSIS'....
GROUP BY ROLLUP (STC_GRADE)...
How would I add into that select statement that if the STC_GRADE IS NULL to count it as an F so that the results show as:
'38' `F`'s
instead of '1' null and '37' F's?
To replace a NULL with a non-NULL value, you use this:
SELECT ISNULL(stc_grade, 'F') AS stc_grade
FROM your_table

How can I return a range of values using two drop down parameters in SSRS?

I understand that in order to use drop down parameters in SSRS, a second data set is required.
Here is my main, original query:
--DECLARE #BegSP VARCHAR = 'JC-A'
--DECLARE #EndSP VARCHAR = 'LK-F'
SELECT DISTINCT co.cust_num,
custaddr.name,
co.order_date,
coitem.due_date,
co.Uf_Slsman5,
co.price,
co.co_num,
co.datefld AS Cncl_Date,
CASE
WHEN co.credit_hold = 0
THEN 'No'
ELSE 'Yes'
END AS Credit_Hold
FROM co,
custaddr,
coitem
WHERE co.cust_num = custaddr.cust_num
AND co.cust_seq = custaddr.cust_seq
AND co.co_num = coitem.co_num
AND co.Uf_Slsman5 BETWEEN #BegSP AND #EndSP
AND coitem.stat = 'O'
ORDER BY 7 ASC
This allows me to view a range of SalesPerson numbers by typing the numbers in, however this is not what I want to do.
Obviously, I can't have Declared parameters in that query, because that would negate using an additional query to get the drop-down lists I'm wanting, so I took my declarations out to get this:
SELECT DISTINCT co.cust_num,
custaddr.name,
co.order_date,
coitem.due_date,
co.Uf_Slsman5,
co.price,
co.co_num,
co.datefld AS Cncl_Date,
CASE
WHEN co.credit_hold = 0
THEN 'No'
ELSE 'Yes'
END AS Credit_Hold
FROM co,
custaddr,
coitem
WHERE co.cust_num = custaddr.cust_num
AND co.cust_seq = custaddr.cust_seq
AND co.co_num = coitem.co_num
AND coitem.stat = 'O'
ORDER BY 7 ASC
Ideally, I would like to have two drop down parameter boxes that allow me to return a range of SalesPerson numbers (co.Uf_Slsman5)
This is the second query I would normally use to provide a drop down parameter:
SELECT DISTINCT co.Uf_Slsman5
FROM co
ORDER BY Uf_Slsman5
Herein lies my problem: This query only provides ONE drop down list and only returns ONE SalesPerson number. What can I add/fix to be able to use two drop-down lists, returning a range of SalesPerson numbers?
Any help is greatly appreciated!

Using SELECT with a display condition

SELECT DISTINCT Invoice.InvNo, Invoice.OrderNo, Part.PartNo,
orders.orddate AS Order_Date, Invoice.InvDate AS Bill_Date,
MiscChg.Descr, MiscChg.RegFee, Invoice.InvAmt,
Orders.ClaimNo, Firm.FirmName AS Ordering_Firm,
**oppatty.attyid(WHERE oppatty.attyfor = 13)**, Location.Name1 AS Location
The bolded section is the part I'm having trouble with. I know what I have isn't right, but it demonstrates what I would like to accomplish. In the oppatty table, there could be several items listed. I want it to only display "AttyID for the entry that has an ATTYFOR = 13".
Hope this make sense, thanks
Jack
You need to add a CASE WHEN to the select statement.
SELECT DISTINCT
Invoice.InvNo,
Invoice.OrderNo,
Part.PartNo,
orders.orddate AS Order_Date,
Invoice.InvDate AS Bill_Date,
MiscChg.Descr,
MiscChg.RegFee,
Invoice.InvAmt,
Orders.ClaimNo,
Firm.FirmName AS Ordering_Firm,
CASE WHEN oppatty.AttyFor = 13
THEN oppatty.AttyId
ELSE '' END AS attyfor,
Location.Name1 AS Location
FROM
.........
This will display the AttyId field when the row's AttyFor field is equal to 13 and show an empty string when it's not.
Your query has no from or where clause and your question is a bit jumbled, but even so, I think I understand what you want to do. Assuming it's acceptable to fill the "AttyID" values with null where "AttyFor" isn't equal to 13, then you could just use a case statement. Try something like this
select
stuff.things,
case
where oppatty.attyfor <> 13 then null
else oppatty.attyid
end as attyid,
stuff.others
from
oppatty
join stuff on oppatty.ID = stuff.ID
If that's not your desired result, and you'd rather entirely exclude rows where "AttyFor" isnt equal to 13, then just use a where clause.
select
stuff.things,
oppatty.attyid,
stuff.others
from
oppatty
join stuff on oppatty.ID = stuff.ID
where
oppatty.attyfor = 13

changing positions of sql query results

The title is not claryifying my problem but this is how i could describe it.
I have a query which returns the following result :
and i was wondering if there is a way to reduce the number of lines from three to one having all the three no null values ( 400, 1000 and 21820 in one line ) with banquet as description.
Thank you for reading.
PS: this is just a capture of a part of the query results and there are a lot of duplicated lines. i can post my query if it would be helpful. i'm using some select case there..
EDIT:
THANK YOU guys but i solved that by copying the results of the main query to input of another one and adding distinct and sum clauses
SELECT description, MAX(number1) AS number1, MAX(number2) AS number2)
FROM myTable
GROUP BY description
At last in Oracle, you can use "When"
Eg:
SELECT
DESCRIPTION,
CASE WHEN SUMPRICE1 IS NULL THEN
CASE WHEN SUMPRICE2 IS NULL THEN
CASE WHEN SUMPRICE3 IS NULL THEN
0
ELSE SUMPRICE3 END
ELSE SUMPRICE2 END
ELSE SUMPRICE1 END AS SUMPRICE
FROM MY_TABLE
GROUP BY DESCRIPTION, SUMPRICE
Off course this is useble just if you have a static number of columns.
EDIT: I think I don't get the problem, but, if you don't want to merge the columns, you can use:
SELECT DESCRIPTION,
MAX(SUMPRICE1) AS SUMPRICE1,
MAX(SUMPRICE2) AS SUMPRICE2,
MAX(SUMPRICEN) AS SUMPRICEN
FROM MY_TABLE
GROUP BY DESCRIPTION
Or you can use the case to avoid the null, in the case of any of rows don't have a value:
SELECT DESCRIPTION,
CASE WHEN MAX(SUMPRICE1) IS NULL THEN 0 ELSE WHEN MAX(SUMPRICE1) END AS SUMPRICE1,
CASE WHEN MAX(SUMPRICE2) IS NULL THEN 0 ELSE WHEN MAX(SUMPRICE2) END AS SUMPRICE2,
CASE WHEN MAX(SUMPRICEN) IS NULL THEN 0 ELSE WHEN MAX(SUMPRICEN) END AS SUMPRICEN
FROM MY_TABLE
GROUP BY DESCRIPTION