I have a "receipt Information Report". For instance
ID Name Date Createtor Payment Date
1 Bob 12.12.2012 bb 01.01.2013
2 Smith 15.01.2010 smt 15.02.2011
3 Peter 21.02.2011 ptr null
4 Sarah 18.06.2012 srh 23.07.2012
I want to add a parameter into this report like that;
List all the receipts(Then it will list the report above)
List all the receipts which have been created by Sarah
List all the receipt which have been paid.
List all the receipt which haven't been paid.
I have created the report without the parameter. How may I adjust these parameters into my report?
You could parameterize your query as follows:
IF #PAYMENTSTATUS = 'ALL'
BEGIN
SELECT ID, NAME, [DATE], CREATOR, PAYMENTDATE FROM RECEIPT
WHERE NAME = CASE #NAME WHEN 'ALL' THEN NAME ELSE #NAME END
END
IF #PAYMENTSTATUS = 'PAID'
BEGIN
SELECT ID, NAME, [DATE], CREATOR, PAYMENTDATE FROM RECEIPT
WHERE NAME = CASE #NAME WHEN 'ALL' THEN NAME ELSE #NAME END
AND PAYMENTDATE IS NOT NULL
END
IF #PAYMENTSTATUS = 'UNPAID'
BEGIN
SELECT ID, NAME, [DATE], CREATOR, PAYMENTDATE FROM RECEIPT
WHERE NAME = CASE #NAME WHEN 'ALL' THEN NAME ELSE #NAME END
AND PAYMENTDATE IS NULL
END
Really it could be simpler if not that I could not figure how to case a date column correctly in SQL where clause, then you would not need 3 if statements.
Next, you can use a different dataset, in parameter properties, get values from a query, to populate your #Name parameter e.g.
select 'ALL'
UNION
SELECT DISTINCT NAME FROM RECEIPT
Lastly, you can simply use available values for the #PAYMENTSTATUS parameter and specify values 'ALL', 'PAID', 'UNPAID'.
Related
I have table as data_attributes with a column data_type
SELECT * FROM DATA_ATTRIBUTES;
DATA_TYPE
----------
NAME
MOBILE
ETHINICITY
CC_INFO
BANK_INFO
ADDRESS
Bank_info, CC_info classified as Risk1,
Mobile, Ethinicity classified as Risk2,
Name, Address classified as Risk3
I should get the Risk classification as output,
For eg: If any of the row contains Risk1 type then output should be Risk1,
else if any of the row contains Risk2 type then output should be Risk2,
else if any of the row contains Risk3 type then output should be Risk3
I wrote below query for this
SELECT COALESCE(COL1,COL2,COL3) FROM
(SELECT
CASE WHEN DATATYPE IN ('BANK_INFO','CC_INFO') THEN 'RISK1' ELSE NULL END AS COL1,
CASE WHEN DATATYPE IN ('MOBILE','ETHINICITY') THEN 'RISK2' ELSE NULL END AS COL2,
CASE WHEN DATATYPE IN ('NAME','ADDRESS') THEN 'RISK3' ELSE NULL END AS COL3
FROM DEMO.TPA_CLASS1) A;
The required output is: Risk1 ( Only 1 value )
Please give some idea to achieve this.
You can use conditional aggregation:
SELECT
CASE
WHEN MAX(DATATYPE IN ('BANK_INFO','CC_INFO')) = 1 THEN 'RISK1'
WHEN MAX(DATATYPE IN ('MOBILE','ETHINICITY')) = 1 THEN 'RISK2'
WHEN MAX(DATATYPE IN ('NAME','ADDRESS')) = 1 THEN 'RISK3'
END AS RISK
FROM DEMO.TPA_CLASS
I need to pivot rows to columns in SSIS.
I am using Integration Services in the Microsoft Visual Studio version 2010.
I have a flat file with the following info:
column 0 column1 column2
-------------------------------------
d-5454-s34 name Frans
d-5454-s34 sd xyh
d-5454-s34 description Group zen
d-5454-s34 member xxxx
d-5454-s34 member yyyy
d-5454-s34 member zzzzz
d-5454-s34 member uuuuu
d-5454-s45 name He-man
d-5454-s45 sd ygh
d-5454-s45 description Group Comics
d-5454-s45 member eeee
d-5454-s45 member ffffff
e-3434-t45 name Calvin
e-3434-t45 sd trdg
and the final output should be
id name sd description member
---------------------------------------------------------------------------
d-5454-s34 Frans xyh Group zen xxxx; yyyy; zzzzz; uuuuu
d-5454-s45 He-man ygh Group Comics eeee; ffffff
e-3434-t45 Calvin trdg NULL NULL
I have used the flat file component and the result is the same as you see BEFORE the final output (check above).
If I setup with the pivot component in SSIS as follows:
I set the PIVOT KEY as column 1 (it contains rows Name, sd, description and member - this last is repeated....) , the SET KEY as column 0 as we have the id that should not be repeated. :) and finally the pivot value as column 2. Afterwards I have set pivot output columns as C_NAME, C_sd, C_description, C_member... but as member is repeated in several rows it is throwing this error... Duplicate key value "member" ... how to overcome this?
Just to test i have deleted all remaining Members leaving only one member, in this way it works. Now I need to get a way to aggregate the several rows with MEMBER duplicated (column 0). How to use the aggregate function of SSIS to group only the member in column 1 and connecting all the different values for member in column 2 separated by ; as shown in the last table. Thank you.
[
You would need to change your approach a bit and transform (aggregate) your data before you are actually doing the pivot operation.
Built a sample package to demonstrate the solution -
As per the package the data needs to be sorted first as the job would be comparing records with each other. Next we need a script component (type transformation). Select all the required input and create the necessary output columns. The data type of the output columns would be same as input just make sure to increase the size of the last column(column3). Also, make sure the script component is asynchronous because it throws out a different number of rows than there are incomming.
Use the below code in script component which would be checking the previous row value and appending the data as a semi-colon separated list of related records.
bool initialRow = true; // Indicater for the first row
string column0 = "";
string column1 = "";
string column2 = "";
public override void Input0_ProcessInput(Input0Buffer Buffer)
{
// Loop through buffer
while (Buffer.NextRow())
{
// Process an input row
Input0_ProcessInputRow(Buffer);
// Change the indicator after the first row has been processed
initialRow = false;
}
// Check if this is the last row
if (Buffer.EndOfRowset())
{
// Fill the columns of the existing output row with values
// from the variable before closing this Script Component
Output0Buffer.Column0 = column0;
Output0Buffer.Column1 = column1;
Output0Buffer.Column2 = column2;
}
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (initialRow)
{
// This is for the first input row only
// Create a new output row
Output0Buffer.AddRow();
// Now fill the variables with the values from the input row
column0 = Row.column0;
column1 = Row.column1;
column2 = Row.column2;
}
else if ((!initialRow) & ((column0 != Row.column0) || (column1 != Row.column1)))
{
// This isn't the first row, but either the column1 or column2 did change
// Fill the columns of the existing output row with values
// from the variable before creating a new output row
Output0Buffer.Column0 = column0;
Output0Buffer.Column1 = column1;
Output0Buffer.Column2 = column2;
// Create a new output row
Output0Buffer.AddRow();
// Now fill the variables with the values from the input row
column0 = Row.column0;
column1 = Row.column1;
column2 = Row.column2;
}
else if ((!initialRow) & (column0 == Row.column0) & (column1 == Row.column1) & (column1 == "member"))
{
// This isn't the first row, and the column (member) did not change
// Concatenate the studentsname to the variable
column2 += ";" + Row.column2;
}
}
Reference: link
SSIS provides a lot of transformations, but most of time, insert data into a temp table and write a simple query can save a lot of time and performance may be better.
for example:
with #tempTable as (
select 'd-5454-s34' column0, 'name' column1, 'Frans' column2
union all select 'd-5454-s34', 'sd ', 'xyh'
union all select 'd-5454-s34', 'description', 'Group zen'
union all select 'd-5454-s34', 'member', 'xxxx'
union all select 'd-5454-s34', 'member', 'yyyy'
union all select 'd-5454-s34', 'member', 'zzzzz'
union all select 'd-5454-s34', 'member', 'uuuuu'
union all select 'd-5454-s45', 'name', 'He-man'
union all select 'd-5454-s45', 'sd', 'ygh '
union all select 'd-5454-s45', 'description', 'Group Comics'
union all select 'd-5454-s45', 'member', 'eeee'
union all select 'd-5454-s45', 'member', 'ffffff'
union all select 'e-3434-t45', 'name', 'Calvin'
union all select 'e-3434-t45', 'sd', 'trdg'
)
SELECT column0
, [name]
, sd
, description
, member
FROM ( SELECT column0,column1, column2 , STUFF(( SELECT '; ' + column2
FROM #tempTable T1
WHERE T1.column0 = t2.column0
AND column1 = 'member'
FOR XML PATH('') ),1, 1, '') member
FROM #tempTable t2 ) t
PIVOT ( MAX(t.column2) FOR t.column1 IN ([name], sd, description)) AS pivotable
I have the following SQL query in an SSRS dataset:
SELECT c.Id
, c.LastName + ', ' + c.FirstName AS CustomerName
, r.PurchaseDate
FROM tblCustomer c
JOIN Receipt r ON r.CustomerId = c.Id
WHERE StoreId = #storeId
I also have three parameters for the report: start date, end date, and customer. The workflow is: select a start date, select an end date, then the above dataset is filtered to only show the customer names in the multi-select dropdown parameter that have a receipt date within the start and end dates. The problem is, when customer has multiple receipts within the date range, the customer shows up more than once in the dropdown parameter. I copied the VB code that filters out the duplicates:
Public Shared Function RemoveDuplicates(parameter As Parameter) As String()
Dim items As Object() = parameter.Value
System.Array.Sort(items)
Dim k As Integer = 0
For i As Integer = 0 To items.Length - 1
If i > 0 AndAlso items(i).Equals(items(i - 1)) Then
Continue For
End If
items(k) = items(i)
k += 1
Next
Dim unique As [String]() = New [String](k - 1) {}
System.Array.Copy(items, 0, unique, 0, k)
Return unique
End Function
which works great, except that it only shows the Customer ID in the dropdown.
How do I get the multi-select dropdown to have the CustomerName as the label and the Customer Id as the value?
You can get around this by doing a cascading parameters.
I don't see where the start_date and end_date parameters are being used..
You can create another dataset, lets call it customers.
Your customer dateset query will be:
SELECT DISTINCT
c.Id
, c.LastName + ', ' + c.FirstName AS CustomerName
FROM tblCustomer c
JOIN Receipt r ON r.CustomerId = c.Id
WHERE StoreId = #storeId
-- and Receipt.somedate between #start_date and #end_date
Set your customer parameter to source it's data from this query. You will only ever have customers from the above select..
Go to the customer parameter.. available value -> customer data set
Set the values in there - value field will be ID and Label field will be name
Of course, your main dataset need to have #customerID filter along with #start_date and #end_date
I am working on this peculiar sql stored proc where the business case is as follows:
Business Case:
Table Specialties contains all the Specialties and there is a bit field for each record telling if it's Active or Inactive. We always display only the active records from that table in form of dropdown. Users may select a Specialty which can later on be deactivated. New requirement is to be able to pull that Inactive record along with all the active records in the result set.
Here's how I thought I should do this:
If no specialty is assigned to the person I am pulling up then the dropdown is going to be populated by all active records.
If there is a inactive specialty associated with the person I am pulling up then I send that specialtyID in stored proc as a parameter and return that inactive records along with active records to populate the dropdown.
Below is what I got so far:
So far if I dont pass in any specialtyId then I am returning active specialty records which is working. When I send in a specialtyId parameter then it just returns that one inactive record but not rest of the other active records. I need the rest of the active records too along with that one inactive record.
DECLARE #specialtyId INT = null;
BEGIN
IF isnull(#specialtyId,'')=''
BEGIN
SELECT SpecialtyID AS Id, Specialty AS Name
FROM dbo.Specialties
WHERE IsActive = 1
ORDER BY Specialty;
END
ELSE
BEGIN
SET #specialtyId = #specialtyId ;
SELECT s.SpecialtyID AS Id, s.Specialty AS Name
FROM dbo.Specialties s
WHERE specialtyId = #specialtyId
GROUP BY s.Specialty, s.SpecialtyID
HAVING (Specialty IS NOT NULL)
AND (max(SpecialtyID) IS NOT NULL)
ORDER BY Name;
END
END
It seems to me that this can be done with no IF whatsoever:
SELECT s.SpecialtyID AS Id,
s.Specialty AS Name
FROM dbo.Specialties s
WHERE specialtyID = #specialtyId
OR IsActive = 1;
You can do this all in the where clause. In the case below it checks to see if the #specId is pass in and selects only that ID or Inactive records OR if the #specId is 0 then just select the active records.
IF OBJECT_ID('tempdb..#tmptest') IS NOT NULL
DROP TABLE #tmptest
CREATE TABLE #tmptest
(
SpecialtyID INT IDENTITY(1,1) NOT NULL PRIMARY KEY
, SpecialtyName VARCHAR(50) NOT NULL
, IsActive BIT NOT NULL DEFAULT (0)
)
INSERT INTO #tmptest
VALUES
('Peditrician', 1)
, ('Rad Tech', 1)
, ('Surg Nurse', 1)
, ('Peds Nurse', 1)
, ('Cardio Doctor', 0)
, ('Cardio Nurse', 1)
, ('Test Doctor', 1)
DECLARE #SpecID INT = 0
SELECT *
FROM #tmptest
WHERE
(
(#SpecID > 0 AND (SpecialtyID = #SpecID OR IsActive = 0))
OR
(#SpecID = 0 AND IsActive = 1)
)
So I am using Kentico CMS Desk 7 to generate reports for my company. In Kentico you create parameters and then create a table using sql and those parameters with the # symbol so whatever the user enters into that parameter, it will be the value of a parameter variable like #Status. I am wanting to add the ability for the user to either enter in one value, multiple values, or no values into the parameters, but I do not know how to implement the multiple values. I am a little new to SQL so bear with me. This is the SQL code I have right now:
select
ClaimNumber as 'Claim Number',
CustomerName as 'Customer Name',
DollarAmount as 'Dollar Amount',
[ReasonCode] as 'Reason code',
rt.[ReasonTypeName] as 'Reason type',
PlantNumber as 'Selling Company',
Status as 'Status'
from TABLE1 as c
join TABLE2 as u on u.UserID = c.DocumentCreatedByUserID
left join TABLE3 as rt on rt.ItemId = c.ReasonType
where ClaimDate between #FromDate and #ToDate
and ReasonCode like #ReasonCode
and ReasonType like #ReasonType
and (#SellingCompany = '' or PlantNumber = #SellingCompany)
and Status like #Status
order by ClaimNumber;
The parameter that I am trying to do this with is the selling company parameter denoted as #SellingCompany. Right now, this works for users not entering in any value and users entering in only one value, but I would like for users to have the ability to input multiple values separated by commas. I feel like an IN operator might work, but I am inexperienced in SQL and I don't know how I would implement this. I can't publish the data obviously because there is customer information, but this statement works as it is and I just need to know how to implement what I'm wanting to do. Thanks guys!
Have you tried this?
select
ClaimNumber as 'Claim Number',
CustomerName as 'Customer Name',
DollarAmount as 'Dollar Amount',
[ReasonCode] as 'Reason code',
rt.[ReasonTypeName] as 'Reason type',
PlantNumber as 'Selling Company',
Status as 'Status'
from TABLE1 as c
join TABLE2 as u on u.UserID = c.DocumentCreatedByUserID
left join TABLE3 as rt on rt.ItemId = c.ReasonType
where ClaimDate between #FromDate and #ToDate
and ReasonCode like #ReasonCode
and ReasonType like #ReasonType
and (#SellingCompany = '' or PlantNumber IN (#SellingCompany))
and Status like #Status
order by ClaimNumber;
I use this SQL function specifically when I need to cast a delimited string to a table value to use with the IN operator.
CREATE FUNCTION [dbo].[ParseIDListToTable]
(#vc_Ids nvarchar(MAX))
RETURNS #Id_table TABLE
(ID nvarchar(15))
BEGIN
DECLARE #in_Index1 AS INT, --Used to store ID delimiter(',') position in string
#vc_ID AS NVARCHAR(15)
/* initialize working variables */
SET #in_Index1 = CHARINDEX(',',#vc_Ids)
/* loop through ids in delimited string */
WHILE (#in_Index1 > 0 OR LEN(#vc_Ids) > 0)
BEGIN
/* parse out single id for processing */
IF #in_Index1 > 0
BEGIN
SET #vc_ID = Left(#vc_Ids,#in_Index1 - 1)
SET #vc_Ids = Right(#vc_Ids,Len(#vc_Ids) - #in_Index1)
END
ELSE
BEGIN
SET #vc_ID = #vc_Ids
SET #vc_Ids = ''
END
INSERT #Id_table (ID)
VALUES(#vc_ID)
/* prepare to loop */
SET #in_Index1 = CHARINDEX(',',#vc_Ids)
END
/* return the ids */
RETURN
END
Then I use it in my SELECT statement like so
WHERE PlantNumber IN (SELECT * FROM dbo.ParseIDListToTable('Microsoft,Apple,Dell'))
This should return the results you're looking for.