Lookup using textbox value as source showing too many rows + SSRS - reporting-services

I did a lookup function in ssrs where the source is a reportItem reference. I want to return the value from the table that I am looking up based on the reportItem reference. The report is retrieving the correct values, but and I'm getting repeated rows and I'd like to know if there's a way to eliminate that. My parameters in the tablix is based on a ticket number.
The underlying data has 3 transactions but 9 rows are currently being returned.
In SSRS, my query is:
Select
ticketno, name, control, value
from ticket a
inner join details b on a.ticketno = b.ticketno
where control like 'LS%' and ticket = 'ED08'
The return result contains 4 rows transactions
ie:
Ticket
Name
Control
Value
ED08
Eng
LS1
A
ED08
Acct
LS2
B
ED08
Med
LS3
C
In SSRS, I used a table and hard coded the Name as it's possible that there will be no values.
I hard coded Eng, Acct, Med, Dent for names.
I entered an expression on each individual row with an expression
=lookup(ReportItems!textbox.Value,Fields!Name.Value,Fields!Value.Value, "UDF_Det")
However, when I run the report, I get extra rows.
The transactions retrieved from the ticket in SQL only retrieved 3 rows, so I would have expected that. Is there a way to filter on row specific data?
I have looked at this post Adding values to a Report when there is no Data in query SSRS but since I am not doing any calculations I'm not sure why I am getting repeat rows.
My design looks like this:
Output looks like this:

Acually, now I've edited your question I understand the problem. :)
You can just get a list of Name and left join from it to your existing query.
You maybe able to get the list from an existing table (hopefully) or you could hardcode one (avoid if possible).
Assuming all the Names you need are in your ticket table you could use something like this...
SELECT DISTINCT [Name] FROM ticket
(if name comes from another table, just change the query to suit)
then left join your existing query to this, something like
SELECT n.[Name], t.ticketno, t.control, t.value
FROM (SELECT DISTINCT [Name] FROM ticket) n
LEFT JOIN (
Select ticketno, name, control, value
from ticket a
inner join details b on a.ticketno = b.ticketno
where control like 'LS%' and ticket = 'ED08'
) t
ON n.[Name] = t.[Name]
which should give you something like
Name
Ticket
Control
Value
Eng
ED08
LS1
A
Acct
ED08
LS2
B
Med
ED08
LS3
C
Dent
NULL
NULL
NULL
Then you can simply have one row in the detail group in your table to output the results.
If this does not help, post some sample data from your database tables and show the full report design including row groups etc

Related

Querying multiple SQL tables

I'm having trouble understanding joins and subqueries and when to use each. I'm sure that one of them is appropriate here.
I have a table ("owners") of (to keep things simple) unit numbers, names and email addresses.
I have another table ("widgets") of unit numbers and the number of widgets assigned to each unit. Each unit has 0, 1 or 2 widgets.
I need to send an email to each unit depending on whether they have 0, 1 or 2 widgets. In other words (and in plain English, not even remotely an attempt at semi-correct SQL):
select numwidgets from widgets where unit=x
then where owners.unit = widgets.unit
select unit, name, email
The data that I need to pass to my script will look like this:
unit name email widgets
1 Bob Smith bob#example.com 2
I can visualise in my mind the data that I need, but it's extracting it from two different tables that is the problem. The "owners" table is a permanent table, and the "widgets" table is a temporary one for tracking a specific issue that is being addressed in the email I'm sending. I don't need help sending the email, just creating the SQL I need to use to extract the data (numwidgets, name, email) for one email.
Thanks.
EDIT:
Input data:
owners table:
unit, name, email
1,Bob Smith, bob#example.com
widgets table:
unit,widgets
1,2
try this, a inner join selects all rows from both tables as long as there is a match between the columns in both tables.
Subqueries (also known as inner queries or nested queries) are a tool for performing operations in multiple steps. For example, if you wanted to take the sums of several columns, then average all of those values, you’d need to do each aggregation in a distinct step.
select owners.unit, name, email, widgets.numwidgets
from owners
inner join widgets On owners.unit = widgets.unit
where owners.unit = x
For your case you need an inner join. To understand that you need to see the concept of keys which is pretty simple.
In your tables unitnumber is the common column in both tables. So a join has to be applied based on this column.
Subqueries are used when the output of one query is given as input to another query i.e to perform related operations
Select o.unit,o.name,o.email ,w.numwidgets from owners o inner join widgets w on o.unit=w.unit where w.unit=X
In above query pass X = 0,1,2 as per the result you want
Thanks
I think you want:
select o.*, w.widgets
from owners o
inner join widget w
on o.unit = w.unit
where o.unit = 123;

mysql query using column values as parameter in query phpMyAdmin

I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.

SSRS report - add extra table and chart to show occurence of character string in one of the main report's columns

I have an SSRS report based on stored procedure dataset. The report shows employees and their performance rating and uses bunch of parameters to filter the data.
Now I would like to add a table below that would dynamicaly count and show occurence of given mark in the main report. The table data should update according to what is visible in the main report after filtering it.
I wanted also to add a chart that would visualize this.
It would be feasible to do if the extra table and chart could run from the same dataset as the main report. This however seems impossible as this dataset does not always contain all the possible marks. It can happen that some marks are missing (when filtered or missing at all), and I would like to show the mark with zero value (and zero value bar in the chart) instead of just skipping it.
So far I was able to produce the table by hardcoding the headers and using SUM(IIF...) expressions under each header
Here is the expression for the "C" column.
=Sum(IIf(Fields!current_performance_rating.Value = "C", 1, 0))
It shows correctly the number of "C" marks appearing in the main report.
Now I am stuck with creating a chart that would show this.
I am not able to hardcode expressions similar to the ones in the table
and can't make the chart run from the main dataset, because the categories
would be missing after filtering the report (and not showing zero).
I tried linking datasets with Lookup function, but that did not work.
Which way should I go now? What is the best practice in such case?
Thank you for any hints!
Thanks trubs.
I have right joined a view that contains
all the marks and that solves the issue
of missing categories.
The join is on something like
tb.current_performance_rating = vw.performance_rating_code
I can now add the value series that counts
the occurence of current_performance_rating
per category. This works all fine.
However there is another table joined
(on employee_id) that stores last year's rating.
This rating obviously may differ to the current one.
On the same chart I would like to add another
series that counts last year's rating per category.
The category is there already, joined to the current
rating.
So you can have row like:
curren rating | last year's rating | category
C | H | C
So I am stuck, because when SSRS groups per category
it counts last year's H rating and displays
i the C category, while it should display it in H.
Sorry I can't post any pictures, seems like I
need more reputation points.
Hope you can understand what I mean.
Regards!
You're likely better off changing your query to return results for marks where there are no values.
So instead of inner joining to your Performance Rating table, use a Right Join or Full Outer Join, so that all the data is always available.
eg: Instead of...
SELECT p.PersonId, PersonName, g.Grade
FROM Person p
INNER JOIN PersonGrades pg ON p.PersonId = pg.PersonId
INNER JOIN Grades g ON pg.GradeId = pg.GradeId
Use
SELECT p.PersonId, PersonName, g.Grade
FROM Grades g
LEFT JOIN PersonGrades pg ON pg.GradeId = g.GradeId
LEFT JOIN Person p ON p.PersonId = pg.PersonId
If that's not clear, post your query and we could help get the data in the right format.
The way to go was to simplify the dataset that the main report was based on.
Then I took the query from that dataset, used COUNT and GROUB BY to count number of occurence of each current year mark in the main report. Then I right joined (thx trubs!) with the view that contains all the possible marks (so that my extra table headers/chart categories would not disappear if they do not exist). As a result I got the number of occurence in current year per mark (table A).
Then I did almost exactly the same for the last year's rating, just used last year's mark. I got the number of occurence of last year's mark per mark (table B).
I inner joined table A and B on common column (the mark, which will always appear thanks to the RIGHT joins). This gave me a table (dataset eventualy) where I had:
mark | current year mark count | last year mark count.
Making a table and chart basing on this was really easy then.
There was some more fun of adding all the parameters from the main report to both the count queries, so that the counts would change when report is filtered. I also needed to make sure that count works not only when my filter criteria (in WHERE stetement) equal the parameter provided from the report, but also when they are NULLs (so I added OR column_i_filter_on IS NULL).
This works smoothly - the table content and chart changes when filtering changes (although runs slowly, as parameters are passed to two big dataset, one of which uses them twice).
Thanks for all the help!!
psh

SQL Query/Procedure with user input to view all persons meeting a criteria

I have an SQL database which I need to create a procedure or query that is linked to a web front end where the user can type in a value which will be in one of the columns of the database. The user types in the value, submits the command (just normal text entry in a textbox in the website), a query is ran in the background to view all persons who have this value in the column under their entry.
User entry to determine the outcome for a view of specific people.
SQL Side I have currently but not certain it is correct, works but not as intended!
I have a table with people in it and a model of interest value column.
Each model of interest is just a number 1-7 which relates to a product range in another table. So 1 = a Corsa, 2 = Astra, 3 = Vectra etc.
So in the persons table, they will have purely a 1 or a 2 etc. Now I need it to be linked to the model table so if it sees 1 it looks for Corsa rather than the number 1.
A user types in the frontend, select all users interested in a Corsa, it will look up the word corsa and match it to the value 1, then search for 1 in the model of interest column in the person table.
So far I have the following query. Any suggestions as I'm stumped for today. Will try again tomorrow.
SELECT TOP 100 [ld_idno]
,[ld_company]
,[ld_decisionmaker]
,[ld_decisionmaker_workphone]
,[ld_decisionmaker_mobile]
,[ld_decisionmaker_email]
,[ld_discussion_model]
FROM [FMLive204].[dbo].[tblLeads]
SELECT a.po_word
FROM dbo.tblPopulation a
INNER JOIN dbo.tblLeads b
ON ld_discussion_model = po_idno
WHERE [po_word]='Corsa'
SELECT TOP 100 Unique Records
company name
customer name
customer workphone]
customer mobile]
customer email]
[ld_discussion_model] Model of interest = 1
FROM [FMLive204].[dbo].[tblLeads]
SELECT a.po_word
FROM dbo.tblPopulation a (table a)
INNER JOIN dbo.tblLeads b (table b)
ON ld_discussion_model = po_idno (if the model of interest 1 is the same as an entry in the other table, pick the value in the next columne (po_word)
WHERE [po_word]='Corsa'
Daniel,
Below I've outlined the stored procedure which would require the userdata (e.g. Corsa entered from the website) and return all fields from tblpopulation where 'po_idno' is in the list matched against the tblLeads. Not having the tblpopulation definition I'm guessing for field name 'po_idno' is the field which has the value 1-7 in it.
Create PROCEDURE [dbo].[sp_find_person] (#UserData varchar(6))
AS
BEGIN
SELECT Top 100 * FROM [FMLive204].[dbo].tblpopulation
where po_idno in (SELECT po_idno FROM dbo.tblLeads b
JOIN dbo.tblPopulation a
ON ld_discussion_model = po_idno
WHERE [po_word]=#UserData)
Once you create the procedure you'll have to execute it which more information can be found # http://msdn.microsoft.com/en-us/library/ms189915.aspx

Access 2010 DLookUp

Working with MS Access for the first time and coming across a few problems if someone could please point me in the right direction.
So I'm doing a mock database (so it looks silly) just to learn the ins and outs and need some help with DLookUp at the moment.
My database has two tables, with the following fields:
C_ID the PK in Courses and FK in Student
tblCourse: C_ID, Title, Subject
tblStudent: S_ID, C_ID, Name, EnrollDATE
As I said this is just for testing/learning. So what I want is to have a filter that gives me a list of C_ID's based on which EnrollDates are NULL.
so filter is:
Expr1: DLookUp("[tblStudent]![C_ID]","tblStudent","isNull([tblStudent]![EnrollDATE])")
I have also tried with the criteria being
[tblStudent]![EnrollDATE] = Null
Currently I get just blank fields returned. Any help is greatly appreciated, and please ask me to elaborate if my explanation is off.
Thank You!
The correct syntax looks like this:
DLookup("C_ID", "tblStudent", "EnrollDate is null")
You don't need to include the table name when you specify the columns
In Access, you check for Null by using xxx is null or xxx is not null
Note that DLookup only returns one value (if the criteria matches more than one row, the value is taken from any row), so you can't use it to get a list of C_IDs back.
EDIT:
What you actually want to do is select data from one table, and filter that based on data from the other table, correct?
Like, selecting all courses where at least one student has an empty EnrollDATE?
If yes, you don't need the DLookup at all, there are two different ways how to do that:
1) With a sub-select:
select *
from tblCourse
where C_ID in
(
select C_ID
from tblStudents
where EnrollDATE is null
)
2) By joining the tables:
select tblCourse.*
from tblCourse
inner join tblStudent on tblCourse.C_ID = tblStudent.C_ID
where tblStudent.EnrollDATE is null
This is SQL, so you need to switch to SQL View in your query in Access.