SSRS - Refine WHERE Criteria in T-SQL Query - tsql

I am developing a report that should return results depending on user-specified parameters. There are 3 report parameters, one is a drop down with 'Locations', and the other two are text parameters to search within 'user_id' and users' first or last name. This is what I am currently using:
SELECT *
FROM UserTable1
WHERE Location = #Location
AND (user_id LIKE '%'+#SearchUserID+'%'
AND first_name LIKE '%'+#SearchUserName+'%'
OR last_name LIKE '%'+#SearchUserName+'%')
If the #SearchUserID parameter field is left blank by the user, all user_ids should be returned (that meet the other 2 parameters). If specified, it will return user_ids that have that substring in the user_id. Same thing goes for the #SearchUserName parameter field. If left blank, it should return all users that meet the other parameters. If specified, it should return all users that have that substring in their first_name or last_name (but only results that meet the other parameters). If both are left blank, it should return all users.
The query I have above is not working. It seems to just return results meeting the #SearchUserName parameter, but disregards the #SearchUserID parameter. Is there a way to fix this? I had put the #SearchUserID filter on the tablix itself, using the expression:
="" & Parameters!SearchUserID.Value & ""
But this has resulted in severely hampered performance..

Try this. You must pass NULL if parameter is blank(empty).
SELECT *
FROM UserTable1
WHERE Location = #Location
AND (user_id LIKE '%'+#SearchUserID+'%'
OR #SearchUserID IS NULL
)
AND (first_name LIKE '%'+#SearchUserName+'%'
OR last_name LIKE '%'+#SearchUserName+'%'
OR #SearchUserName IS NULL
)
"....but disregards the #SearchUserID parameter." What data type of user_id?

Related

Select query Prisma Client on Postgres view returning 0 rows when it shouldn't

I am having a problem when trying to select rows from a Postgres view filtering by name.
The result from the query should be a company in company_portfolio named 'any company 2'. However, instead of returning the company, it returns nothing. By nothing I mean an empty array of companies instead of an array containing the company with the name 'any company 2'.
I have created a Postgres view that looks something like this:
CREATE OR REPLACE view company_portfolio as
SELECT company.id,
company.name,
...
FROM "Company" company
LEFT JOIN
(SELECT DISTINCT ... FROM
(SELECT DISTINCT ... FROM "Contract") a
GROUP BY 1) as smpLists
ON company.... = smpLists...
I have created a company with the name: 'any company 2'.
I am querying the table with my prisma client like this:
if (params.name) {
filters = `${filters} AND "name" = '${params.name}'`;
}
return this.prisma.$queryRaw(
`
SELECT * FROM "company_portfolio"
WHERE id IS NOT NULL ${filters}
ORDER BY ${orderBy}
LIMIT ${params.limit}
OFFSET ${params.offset}
`
);
So the end query is like this:
SELECT * FROM "company_portfolio"
WHERE id IS NOT NULL AND name = 'any company 2'
ORDER BY name ASC
LIMIT 1
OFFSET 1
It is returning an empty array, so it is not properly selected the company with the name = 'any company 2'.
I have executed a similar query on the database directly and it returns the desired company.
Does anyone spot what could be wrong here?
Solved it.
I was setting an offset = 1 and a limit = 1. This returns the second row from the select query result.
Since there was only 1 record meeting the criteria of the select query, there was not a second record, thus I was seeing an empty array.

AR Query for jsonb attribute column

I'm using Postgres for my db, and I have a column in my Auction model as a jsonb column. I want to create a query that searches the Auction's json column to see whether a user's name OR email exists in any Auction instance's json column.
Right now I have #auctions = Auction.where('invitees #> ?', {current_user.name.downcase => current_user.email.downcase}.to_json), but that only brings back exact key => value matches I want to know whether the name OR the email exists.
You're using the #> operator. The description for it is this:
“Does the left JSON value contain the right JSON path/value entries
at the top level?”
You probably want the ? operator if you want to return a row when the key (name in your case) matches.
There's not a good way to search for values only in a JSON column (see this answer for more details), but you could check if the key exists alone or the key and value match exists.
The same ActiveRecord methods and chaining apply as when using non-JSON columns, namely where and where(…).or(where(…)):
class Auction
def self.by_invitee(user)
name = user.name.downcase
json = { name => user.email } # note: you should be downcasing emails anyways
where('invitee ? :name', name: name).or(
where('invitee #> :json', json: json)
)
end
end
This is just a temporary patch until I add an Invite model, but casting the invitee column to text I can search the columns. Like so
SELECT * FROM auctions
WHERE data::text LIKE "%#{string I'm searching for}%"
So, AR:
Auction.where('data::text LIKE ?', "%#{string I'm searching for}%")

it is possible to concatenate one result set onto another in a single query?

I have a table of Verticals which have names, except one of them is called 'Other'. My task is to return a list of all Verticals, sorted in alpha order, except with 'Other' at the end. I have done it with two queries, like this:
String sqlMost = "SELECT * from core.verticals WHERE name != 'Other' order by name";
String sqlOther = "SELECT * from core.verticals WHERE name = 'Other'";
and then appended the second result in my code. Is there a way to do this in a single query, without modifying the table? I tried using UNION
(select * from core.verticals where name != 'Other' order by name)
UNION (select * from core.verticals where name = 'Other');
but the result was not ordered at all. I don't think the second query is going to hurt my execution time all that much, but I'm kind of curious if nothing else.
UNION ALL is the usual way to request a simple concatenation; without ALL an implicit DISTINCT is applied to the combined results, which often causes a sort. However, UNION ALL isn't required to preserve the order of the individual sub-results as a simple concatenation would; you'd need to ORDER the overall UNION ALL expression to lock down the order.
Another option would be to compute an integer order-override column like CASE WHEN name = 'Other' THEN 2 ELSE 1 END, and ORDER BY that column followed by name, avoiding the UNION entirely.

SphinxQL - how to filter behind match

I'm working on a project where I use Sphinx searchengine. But - as I realized - the Sphinx documentation is big but hard to understand.
So I was not able to find any information on how to use the WHERE clause to filter behind a MATCH-statement. What I tried yet is:
"SELECT *, country FROM all_gebrauchte_products WHERE MATCH('#searchtext (".$searchQuery.")') AND country='".$where."' ORDER BY WEIGHT() DESC LIMIT ".$page.", ".$limit." OPTION ranker=expr('sum(lcs)')"
If I use it without the country=$where clause, I get back many GUIDs but from different countries. So somehow I have to filter the country column;
If I use the above statement, I get error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 index all_gebrauchte_products: parse error: unknown column: country'
But I set the index like this:
sql_query_range = SELECT MIN(gebr_id), MAX(gebr_id) FROM all_gebrauchte_products
sql_range_step = 10000
sql_query = \
SELECT a.gebr_id AS guid, 'products' AS data_type, a.gebr_products AS products, a.gebr_user AS username, a.gebr_date AS datadate, CONCAT(a.gebr_hersteller,' ', a.gebr_modell,' ', a.gebr_ukat,' ', a.gebr_kat,' ', a.gebr_bemerkung) AS searchtext, a.gebr_bild1 AS image1, a.gebr_bild2 AS image2, a.gebr_bild3 AS image3, a.gebr_bild4 AS image4, a.gebr_bild5 AS image5, b.h_land AS country, b.h_web AS weblink, b.h_firmenname AS company, b.h_strasse AS street, b.h_plz AS zipcode, b.h_ort AS city, a.gebr_aktiv AS active \
FROM all_gebrauchte_products a, all_haendler b \
WHERE a.gebr_user = b.h_loginname AND a.gebr_id>=$start AND a.gebr_id<=$end
sql_attr_uint = active
Can anybody tell me what is going wrong? Or how do I have to filter for country?
Thnx. in advance for your help.
Any columns in the sql_query you dont make an ATTRIBUTE, is automatically a FIELD (except the first column is always the document-id).
FIELDs are 'full-text' indexed, they are what you can match in the query - ie the MATCH(...) clause.
ATTRIBUTES are what can be 'filtered' in WHERE, sorted by in ORDER BY, grouped in GROUP BY, or retrieved in the SELECT (or even used in ranking expressions).
So you need country to be an ATTRIBUTE to be able use it in WHERE filter
You don't say but guess it's a string. You can use sql_field_string to make a column BOTH a FIELD and ATTRIBUTE, if you are still interested in being able to full-text query the column too.
(also because its a string, need a very recent version of sphinx. Sphinx only recently gained ability to filter by strings attributes)

Updating records in Postgres using nested sub-selects

I have a table where I have added a new column, and I want to write a SQL statement to update that column based on existing information. Here are the two tables and the relevant columns
'leagues'
=> id
=> league_key
=> league_id (this is the new column)
'permissions'
=> id
=> league_key
Now, what I want to do, in plain English, is this
Set leagues.league_id to be permissions.id for each value of permissions.league_key
I had tried SQL like this:
UPDATE leagues
SET league_id =
(SELECT id FROM permissions WHERE league_key =
(SELECT distinct(league_key) FROM leagues))
WHERE league_key = (SELECT distinct(league_key) FROM leagues)
but I am getting an error message that says
ERROR: more than one row returned by a subquery used as an expression
Any help for this would be greatly appreciated
Based on your requirements of
Set leagues.league_id to be permissions.id for each value of permissions.league_key
This does that.
UPDATE leagues
SET league_id = permissions_id
FROM permissions
WHERE permissions.league_key = leagues.league_key;
When you do a subquery as an expression, it can't return a result set. Your subquery must evaluate to a single result. The error that you are seeing is because one of your subqueries returns more than one value.
Here is the relevant documentation for pg84: