In the report I'm working with, I need to display information of four columns of a database table. The first three columns of the table are SEX, AGE and NAME. The other N columns (N being like 100!) are questions, with every line of the table meaning that person's answer to that question:
SEX | AGE | NAME | Q1 | Q2 | Q3 | ... | Q100
In my report, I need to show four of these columns, where the first three are always the same and the fourth column varies according to the option selected by the user:
SEX | AGE | NAME | <QUESTION_COLUMN>
So far I've created a dropdown parameter (filled with "Q1", "Q2", "Q3", etc) where the user can select the question he wants to compare. I've tried to use the value of the selected option (for instance, "Q1") in the SELECT clause of my report query, without success:
SELECT sex, age, name, ${QUESTION} FROM user_answers
Pentaho Report Designer doesn't show any errors with that, it simply doesn't show any values for the question column (the other columns - sex, age and name - always return their values)
So, I would like your know:
Can I do this? I mean, use parameters in the SELECT clause?
Is there any other way have this "wildcard" column according to a parameter?
Thanks in advance!
Bruno Gama
you can use the pentaho report design to design.
First,you must bulid the param "QUESTION"on the paramers
eg: SELECT question FROM user_ansewers order by XXXX
you can use the sql
SELECT sex, age, name,question FROM user_answers
where question= ${QUESTION}
last ,you can see the "drop down" to realized the choose
I am using SQL server as database. This problem solves like this :
execute('SELECT sex, age, name, '+${QUESTION}+' as Q1 FROM user_answers')
Please note that ${QUESTION} must be a column name of user_answers. In this example I used a text box parameter name QUESTION where column name is given as input. You may need other coding if input parameter is not text box.
Related
Let’s say I am passing the input as input=2021-01-21,CGT for an sql query. CGT will be the common keyword in the database but the dates keep changing. I want the records which contain CGT and all the other dates except the date mentioned in the input parameter.
Don’t answer it as retrieve all the records that contain CGT and filter it out in Excel as the records for the particular date is huge in number. So I want the other dates which are less in count which can be handled.
Example query :-
select records from tablename where var_name=‘input’;
Based on your question, I assume that this is what you want.
Given:
| input |
---------------
2021-01-21,CGT
2021-01-22,CGT
2021-01-23,CGT
2021-01-25,CGT
2021-01-26,CGT
2021-01-27,CGT
2021-01-28,CGT
If you specify '2021-01-23' you expect to get:
| input |
---------------
2021-01-21,CGT
2021-01-22,CGT
2021-01-25,CGT
2021-01-26,CGT
2021-01-27,CGT
2021-01-28,CGT
You did not specify the database you are using. However, the concept should be similar regardless of the database platform.
SELECT *
FROM sample
WHERE SUBSTRING_INDEX(input,',',1) <> '2021-01-22'
[MySQL]
In [DB2], Given that you wish to pass in the input string and extract
the date, do the following:
SELECT *
FROM sample
WHERE SUBSTRING(input,1, (LOCATE(',',input) - 1)) <> '2021-01-22'
I was given a patient list with names and I am trying to match with a list already in our database and am having troubles given the format of the name field in the patient list. This list is taken from a web form so people can input names however they want so it does not match up well.
WEBFORM_NAME
PATIENT_NAME
JOHN SMITH
SMITH,JOHN L
SHANNON BROWN
BROWN,SHANNON MARIE
Is there a way to use a LIKE clause in an instance like this? All I really need is the LIKE clause to find the first name because I have joined on phone number and email address already. My issue is when households have the same phone number and email address (spouses for example) I just want to return the right person in the household.
Not sure if all you need is to get first name, here is the WIldCard expression to get first name
SELECT LEFT(WEBFORM_NAME,CHARINDEX(' ',WEBFORM_NAME)-1) AS FirstName1,
SUBSTRING(PATIENT_NAME,CHARINDEX(',',PATIENT_NAME)+1,(CHARINDEX(' ',PATIENT_NAME)-CHARINDEX(',',PATIENT_NAME))) AS FirstName2
FROM yourTable
The assumption here seems to be that the webform (where user would manually) type in the name would be of the format <First Name> [<optional middle Name(s)>] <Last Name>, where as the data stored in the table are of the form <Last Name>,<First Name> [<optional middle Name(s)>]. Its not an exact science, but since other criteria (like email, phone etc) have been matched best case
select *
from webform w, patient p
where
-- extract just the last name and match that
regexp_like(p.name,
'^' ||
regexp_extract(w.name,
'([^[:space:],][[:space:],])*([^[:space:],]+)', 1, 2))
and -- extract the first name and match it
regexp_like(p.name,
',[[:space:]]*' ||
regexp_extract(w.name, '(^[^[:space:],]+)'))
Since webform is free form user input, its hard to handle abbreviated middle name(s) and other variations so using the above will do first name and last name based matching which in addition to the matching you are already doing should help.
New to SQL please dont mind if this is a silly question..
My table looks like this
I want to send only one email to manager telling him that these employees in your group failed to fill timesheet.
currently i have pivoted the above table that looks like this
and sending emails by concatinating firstemp+secondemp+thirdemp+------
can this be done in any more easiest way..?
You can use CONCAT() function to retrieve a single row data in one column
SELECT M_EMAIL,
CONCAT(FIRSTEMP, SECONDEMP, THIRDEMP, FOURTHEMP, FIFTHEMP...)
FROM 'your_table';
CONCAT() replaces NULL values with an empty string.
Please don't pivot, as the concat is really ugly to maintain (and will break if a more capable manager pops up with more subordinates than your concat columns).
The syntax depends on what SQL server you use. For example, in MSSQL you could do this:
select manager, m_email, STRING_AGG(employee, ', ') as subordinates
from Employee
group by manager, m_email
This result has only 1 row per manager and fixed number of columns regardless how many subordinates the manager has:
manager | m_email | subordinates
----------------------------------
A | A#A.COM | b, D
D | D#D.COM | e, h
I | I#I.COM | j
Play with the example here: http://sqlfiddle.com/#!18/73bb5/5
Another option is just query relevant data to application layer and do the grouping there.
I have an RDLC report, I have a column chart whose X axis is shows PerformedBy person names,
Y axis shows the count of exams grouped by each PerformedBy person.
The data in the dataset contains case sensitive data.
For example, there are two PerformedBy names like 'john' and 'JOHN'.
john has Number of exams as 1 where as JOHN has 2. Currently in the chart it displays only JOHN.
The data for both 'john' and 'JOHN' are combined and shown under 'JOHN' as 3.
I want to display john with Number of exams as 1 and JOHN with Number of exams as 2.
How to handle this at RDLC level?
I have seen in some of the dicussions that I can select Data Options and then choose casesensitivity property.
But I am not seeing this option in Dataset Properties dialog.It shows only the option General.
I am using Visual studio 2010 , .NET 4.0 and SQL Server 2008 Express.
Another option I was thinking to add a Unique identifier field for the Performed By person in the dataset. In the chart
how do I group by Identifier but then display Performed by Name in X axis?
Note that I saw both records are coming in my output collection from sql query.
One way is to convert the string to ASCII code, and then put the number together
For instance 'john' => '106111104110' and John=> '74111104110'. And then group on that value
This link will explain how to convert a string to an ASCII values.
http://p2p.wrox.com/beginning-vb-6/56056-converting-set-string-its-ascii-value.html
I have a column called NAME, I have 2000 rows in that column that are filled with people's full names, e.g. ANN SMITH. How do I do a query that will list all the people whose first name is ANN? There are about 20 different names whose first name is ANN but the surname is different.
I tried
and (NAME = 'ANN')
but it returned zero results.
I have to enter the FULL name and (NAME = 'ANN SMITH') ANN SMITH to even get a result .
I just want to list all the people with there first name as ANN
Try in your where clause:
Where Name like 'ANN %'
Should work mate.
ANN% will find all results where ANN is first then anything after.
%ANN% will find the 3 letters ANN in any part of that rows field.
Hope it helps
Also usually Name is separated into First names and second name columns.
this will save Having to use wild cards in your SQL and provide A bit more normalized data.
SELECT NAME
FROM NAMES
WHERE NAME LIKE 'ANN %'
This should wildcard select anything that begins with 'ANN' followed by a space.