I'm a newbie to SSRS Reporting Services. My report runs great except that it doesn't show null or blank cells. I know there are some. What should I add to this query to show the blank or null in the DistributionOwnerId? Thanks in advance!
SELECT Id,
CONVERT(varchar(10), DistributionDate, 101) AS DistributionDate,
DistributionDate AS OriginalDistributionDate,
MedDrugName AS DrugName,
MedDistributionSessionDescription AS Session,
DistributionOwnerId,
CASE a.[Status]
WHEN 0 THEN 'Success'
WHEN 1 THEN 'Refused By Inmate'
WHEN 2 THEN 'Inmate Did Not Show'
WHEN 3 THEN 'Inmate Not In Cell'
WHEN 4 THEN 'Security Lockdown'
WHEN 5 THEN 'Medication Held (State Reason)'
WHEN 6 THEN 'Medication Out Of Stock'
END AS Status,
Notes,
UserName,
(SELECT NoteText + ';' AS Expr1
FROM MARDistAddNotes AS c
WHERE (a.Id = MARDistributionId) FOR XML PATH('')
) AS AdditionalNotes
, InmateLastName
, InmateFirstName
, InmateNumber
FROM MARDistribution AS a
WHERE (Status > 0)
ORDER BY DistributionDate, Session
SELECT CONVERT (DATE, GETDATE()) 'Date Part Only'
Related
I have taken reference from the internet about one user-defined function to locate 'nth occurrence of a string to do the sort column name in the database. I am using MySQL 5.5 version, not the latest version. Here is my sample database link https://dbfiddle.uk/?rdbms=mysql_5.5&fiddle=bcb32a6b47d0d5b061fd401d0888bdc3
My problem is I want to sort column name in the database follow the prefix number, but I am using below the SQL query, it doesn't work.
select t.id,t.name
from
(
select t.*, cast((case when col1_col2_ref > 0
then
substring_index(modified_name,'-',1)
else
modified_name
end
) as unsigned) col1
, cast((case when col1_col2_ref > 0
and col3_ref > 0
then
substr(modified_name,(col1_col2_ref + 1),(col3_ref - (col1_col2_ref + 1)))
when col1_col2_ref > 0
then
substr(modified_name,(col1_col2_ref + 1))
end) as unsigned) col2
, cast((case when col3_ref > 0
and col4_ref > 0
then
substr(modified_name,(col3_ref + 1),(col4_ref - (col3_ref + 1)))
when col3_ref > 0
then
substr(modified_name,(col3_ref + 1))
end) as unsigned) col3
, cast((case when col4_ref > 0
then
substr(modified_name,(col4_ref + 1))
end) as unsigned) col4
from
(
select t.*,substring_index(name,' ',1) modified_name
,locate('-',name,1) col1_col2_ref
,locate('/',name,1) col3_ref
,locate('/',name,locate('/',name,1)+1) col4_ref
from filing_code_management t
) t
) t
order by col1,col2,col3,col4
It shows me below the result, it cannot sort properly.
Output 1
Actually I want the output sample like below:
Output 2
Output 3
This is before I can sort the column name link, https://dbfiddle.uk/?rdbms=mysql_5.5&fiddle=6b12a4d42359cb30f27a5bfb9d0c8210. After I am inserted into new data, it cannot work for me. Maybe an example in new data like this error (R)100-6-2-2 Mesyuarat Majlis Kerajaan Negeri (MMKN) JKK if I put () in front. Or in new data like this error 100-1-1 Penggubalan/Penyediaan/Pindaan Undang-Undang/Peraturan if I put / in between the word.
Hope someone can guide me to solve this problem. Thanks.
You should be able to adapt the following code to your needs (tested at your DB Fiddle!). I've used the file_name column instead of the name column to slightly simplify building the sort fields, as it seems the file name is always repeated in the first part of the name field anyway.
This would be quite a bit simpler using regular expression support, but I note that the version of MySQL you are using doesn't have this feature (I think it arrives in SQL 8.0, if I'm not mistaken).
SELECT id,
num_hyphens,
CAST(SUBSTRING_INDEX(CONCAT(file_name_adj,'-'), '-', 1) AS UNSIGNED) AS sort1,
CAST(CASE WHEN num_hyphens = 0
THEN '0'
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(file_name_adj,'-', 2), '-',-1)
END AS UNSIGNED) AS sort2,
CAST(CASE WHEN num_hyphens <= 1
THEN '0'
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(file_name_adj,'-', 3), '-',-1)
END AS UNSIGNED) AS sort3,
CAST(CASE WHEN num_hyphens <= 2
THEN '0'
ELSE SUBSTRING_INDEX(file_name_adj, '-', -1)
END AS UNSIGNED) AS sort4,
file_name,
name
FROM (
SELECT id, name, MID(file_name, instr(file_name, ')') + 1) AS file_name_adj, file_name,
LENGTH(file_name) - LENGTH(REPLACE(file_name, '-', '')) AS num_hyphens
FROM filing_code_management
) t1
ORDER BY sort1, sort2, sort3, sort4
I need to create an SSIS Package that provides me the count of workdoneby (contractor/company).
Input table from sql server db:
I need to count no of orders by contractor and company for a particular day + station + worktype + accountno.
My output should look like this.
Can someone help me how to create a package to get the desired output?
Since the data is in a table, you can ask the database engine to do the calculation logic.
Setup
I created a temporary table and populated it with the supplied data.
CREATE TABLE
#Source
(
[Date] date
, Station char(3)
, worktype char(2)
, Accountno varchar(10)
, workdoneby varchar(10)
)
INSERT INTO
#Source
(
Date
, Station
, worktype
, Accountno
, workdoneby
)
VALUES
('2018-06-24', 'RMS', 'RH', 'I.145.001', 'Company')
, ('2018-06-24', 'RMS', 'PH', 'I.145.001', 'Contractor')
, ('2018-06-24', 'RMS', 'PH', 'I.145.002', 'Company')
, ('2018-06-24', 'RMS', 'PH', 'I.145.002', 'Contractor');
Query time
Now let's query! I find it is helpful to break these problems down into smaller pieces. The first thing I want to do is break out the workdoneby column into two columns with a 1 or 0
SELECT
S.Date
, S.Station
, S.worktype
, S.Accountno
, CASE S.workdoneby
WHEN 'Contractor' THEN 1
ELSE 0
END AS contractorCount
, CASE S.workdoneby
WHEN 'Company' THEN 1
ELSE 0
END AS companyCount
FROM
#Source AS S
Running that let's me look at the results and see I still have 4 rows and I get the correct entity counted.
The next step is to collapse/summarize/roll-up the values. You indicate we should group by date/station/worktype/accountno so that's exactly what we're going to to do.
I find it easier to debug if I take that first query and make it a derived table so the basic form now becomes SELECT * FROM (ORIGINAL QUERY HERE) AS D thus
SELECT
D.Date
, D.Station
, D.worktype
, D.Accountno
, D.contractorCount
, D.companyCount
FROM
(
SELECT
S.Date
, S.Station
, S.worktype
, S.Accountno
, CASE S.workdoneby
WHEN 'Contractor' THEN 1
ELSE 0
END AS contractorCount
, CASE S.workdoneby
WHEN 'Company' THEN 1
ELSE 0
END AS companyCount
FROM
#Source AS S
) D
Now that you can see it's giving the same original results, we're going to use the SUM function on the contractorCount and companyCount columns and GROUP BY date/station/worktype/accountno
SELECT
D.Date
, D.Station
, D.worktype
, D.Accountno
, SUM(D.contractorCount) AS contractor
, SUM(D.companyCount) AS company
FROM
(
SELECT
S.Date
, S.Station
, S.worktype
, S.Accountno
, CASE S.workdoneby
WHEN 'Contractor' THEN 1
ELSE 0
END AS contractorCount
, CASE S.workdoneby
WHEN 'Company' THEN 1
ELSE 0
END AS companyCount
FROM
#Source AS S
) D
GROUP BY
D.Date
, D.Station
, D.worktype
, D.Accountno;
SSIS
Now that we have data looking as expected, within SSIS you need to do something with it. Your question doesn't specify what you need to do but likely you're going to use a Data Flow Task to push this aggregated data from one place to another destination (different server, Excel, etc) or you're going to push this data into a table on the same server in which case you're going to use an Execute SQL Task
I'm trying to run a Report in SSRS 2012, it's set up, and all the code runs fine in SSMS. But in SSRS I get the following:
Fill in all the parameters
Press View Report
The following appears
As you can see, the ToDate Parameter field is asking for a value again.
The following is the code for the parameters:
Fiscal Parameter
SELECT
FiscalPeriodId
,FiscalPeriodName + ' - ' + [Status] + ' ' + Convert(varchar(15),ENDDate, 101) AS FiscalPeriodName
,FiscalPeriodId AS OrderId
FROM fin.FiscalPeriod
WHERE Status = 'Closed'
AND GeneralLedgerGroupId = #Location
UNION
SELECT
FiscalPeriodId
,'Current'
,9999999 AS OrderId
FROM fin.FiscalPeriod
WHERE Status IS NULL
AND GeneralLedgerGroupId = #Location
UNION
SELECT
0 AS FiscalPeriodId
,'AS of Specified Date' AS FiscalPeriodName
,10000000 AS OrderId
ORDER by OrderId DESC
Date Parameter
SELECT
CASE
WHEN fp.ENDDate IS NULL
THEN GETDATE()
ELSE fp.ENDDate
END AS ToDate
FROM fin.FiscalPeriod fp
WHERE FiscalPeriodID = #FP
UNION
SELECT
CASE
WHEN #FP IS NULL
THEN GETDATE()
ELSE NULL
END AS ToDate
ORDER BY ToDate DESC
I've searched the web and found one question on Stackoverflow which seemed to be the same question, but there was no answer to it (2012). Any suggestions?
I asked a similar question but it has become a larger problem.
The below question catered for 2 options, but not if a single option was stored in the database. See: Use SUBSTRING_INDEX(). Answer by Joyal George:
MYSQL SELECT multiple values between "" in Column
I have a form in WordPress which captures info on people who want to receive updates in certain areas. They can select 1 or more areas.
I then have a reporting plugin which only accepts SQL to retrieve the data for the report. No application layer, only SQL queries to a MYSQL database
If someone only selects 1 area, I need to extract only that area. If they select more than 1 area I need to extract each area separated by a comma. They can select up to 9 areas.
Data in the column is as follows:
1 area:
Western Cape
Multiple areas:
a:3:{i:0;s:10:"North-West";i:1;s:12:"Western Cape";i:2;s:13:"Northern Cape";}
I am using a case statement (previous issue with this database structure)
select
a.entry_id,
MAX(case when field_id = 74 then entry_value end) as FirstName,
MAX(case when field_id = 75 then entry_value end) as LastName,
MAX(case when field_id = 76 then entry_value end) as Email,
MAX(case when field_id = 78 then entry_value end) as Phone,
MAX(case when field_id = 79 then
(select concat(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
entry_value,
'"',
4
),
'"',
2
),
'"',
-1
),
",",
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
entry_value,
'"',
4
),
'"',
4
),
'"',
-1
)
))
end) as InterestedIn,
MAX(case when field_id = 81 then entry_value end) as Province from ch_arf_entry_values a GROUP BY a.entry_id
I need to adjust the 'as InterestedIn' to cater for only 1 input value.
I need to find a solution for the last case 'as Province'
Any assistance would be greatly appreciated.
You should find a better way to represent what you want, but I think the following comes close:
select concat_wc(',', substring_index(substring_index(entry_value, '"', 2), '"' -1),
substring_index(substring_index(entry_value, '"', 4), '"' -1),
. . .
)
You might have to put a stop condition based on the number of values in the number of values in the string, resulting in something like:
select concat_ws(',',
case when num_entry_values >= 1 then substring_index(substring_index(entry_value, '"', 2), '"' -1) end,
case when num_entry_values >= 2 then substring_index(substring_index(entry_value, '"', 4), '"' -1) end,
. . .
)
If you don't have this number, you can calculate it by counting the number of double quotes in the string.
EDIT:
To count the number of entries, count the ":
from (select aev.*,
(length(entry_value) = length(replace(entry_value, '"', '')) ) / 2 as num_entry_values
from ch_arf_entry_values aev
) aev
I am trying to populate a table with phone number from a temp table. I have wrote the query with no problem. but my peoblem here is to know if the company already has a primary number or not
so I select 2 fields from my temp table called "cvsnumbers" 1) company_code (id) and the phone number.
I need to add a case statement to change the value of a main_number field. so if the number already has a number with main_number = 1 then I need to insert 0 for the new phone number but if there is no main_number then I need to insert 1 for the new phone number making it a primary phone number for the account.
this is my query
SELECT ac.account_id,
REPLACE(REPLACE(REPLACE(REPLACE(ta.phone_number, '-', ''), ' ', ''), ')', ''),'(','') AS Phone,
IFNULL(ta.ext, '') AS extention,
IFNULL(ta.main_number, 0) AS MainNumber,
ta.type AS contact_type,
'2' AS created_by
FROM cvsnumbers AS ta
INNER JOIN accounts AS ac ON ac.account_id = ta.company_code
WHERE LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(ta.phone_number, '-', ''), ' ', ''), ')', ''),'(','') ) = 10
AND REPLACE(REPLACE(REPLACE(REPLACE(ta.phone_number, '-', ''), ' ', ''), ')', ''),'(','') NOT IN (SELECT contact_number FROM contact_numbers)
My issue is
`IFNULL(ta.main_number, 0) AS MainNumber,`
I want to change that to some what a case statment to check if a company_code already has a main_number or not.
How can I change this?
Thanks
I am still not sure exactly how your query should look like, but how about something along theese lines?
SELECT CASE
WHEN EXISTS (SELECT *
FROM contact_numbers
WHERE main_number = 1
AND contact_number =
<insert contact number from outer expression here>)
THEN 1
ELSE 0
END AS MainNumber
FROM ...