SQL Sum Case Statement - group by case - reporting-services

I'm simply trying to write an sql query to sum a group of fields.
I have 2 columns, one of which is Account Name, the other is Amount
I'd only like to sum two of the account names:
=sum(case when [Account Name] like Salaries or 'Other Income', Fields!Amount.Value)
the following works fine,
=sum(Fields!Amount.Value)
but I'm trying to narrow down the number of account names that are included in the sum. I'm trying to figure out how to use a case statement then group by that case statement.

I'm going to take a stab in the dark here because I think from your example that you're using SQL Server Reporting Services (SSRS), and the syntax is quite different to T-SQL or ANSI SQL, which is what most people think of when you say "SQL".
If that's the case, and I have no experience with this so this may be completely wrong, I'm guessing you might need to try something using either iif or switch like this:
=sum(iif(Fields![Account Name].Value = 'Salaries'
or Fields![Account Name].Value = 'Other Income', Fields!Amount.Value, 0))

Related

my sql of multiple conditions is not working

delete from tab_1 where lastname = "Ahmad"
and INSERT INTO tab_1(firstname, lastname, title)VALUES("Kashif" , "Younis" , "I am working in GSK")
AND update tab_1 set firstname = "Kh. Mohsin" WHERE id= 4;
You are using 'AND' in a context where it means something entirely different. You want to say "do this query and then to this query and then do another query", but that is not supported in the syntax of the language yo are using.
AND has a very specific meaning in a WHERE clause; you are creating a list of requirements that must be met. Putting the AND on a new line means nothing to MySQL.
I'm not sure how you are making this database request, but you have three distinct queries and you need to make them one at a time.

Case statement between two columns in two tables

Im trying to write a case statement (Or use any statement)in MySQL that will print out a value if the variable = true.
Basically, in my select statement, if the value in one table is 10, then display a value within a new column
I was thinking to use something like this:
CASE (table1.payment WHEN 10 THEN table2.type END) AS plan
The above does not work and has syntax errors. What can I do to achieve my desired results?
In your case I think it is optimal to use IF, because it is one option to check (it is 10 or not) and IF is faster for such a check.
IF(table1.payment=10,table2.type,table1.payment) AS plan
https://www.w3schools.com/sql/func_mysql_if.asp For more information.
Option using case looks like:
(CASE
WHEN table1.payment=10 THEN table2.type
ELSE table1.payment
END) AS plan
https://www.w3schools.com/sql/func_mysql_case.asp For more information.
Execution time comparison for ONE check IF and CASE queries:
Bigger result
Smaller result

Access query amazing

When I do that on access, SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN (1,2,5))
GROUP BY RMonturesImp.N°Fac;
but when I do this
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN Requête2)
GROUP BY RMonturesImp.N°Fac;
it doesn't work (it shows 1,2,5 indeed) although the result of Requête2 (which is a query) is also (1,2,5). I can't understand this!
Thanks in advance
It's quite easy. The IN (1,2,5)) must be explicit as SQL will not evaluate an expression not to say a function to obtain the values for IN.
So build your SQL in code creating the string, or pull the values from a (temp) table.
Try this:
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE RMonturesImp.N°Fac NOT IN (Select N°Fac From Requête2)
GROUP BY RMonturesImp.N°Fac;

SQL Like Statement is showing all results

I have used LIKE statements before and have been spending ages rewriting the statement and not sure what I have done wrong. When the query is ran, it displays all records in the database when it should be showing a more narrow list.
The reason for using a LIKE statement is to make my advanced search facility more efficient by allow part of a "property name".
SQL Statement:
SELECT
*
FROM
properties
WHERE
PropertyName LIKE '%$PropertyName%'
OR PropertyLocation LIKE '%$PropertyLocation%'
OR PropertyType LIKE '%$PropertyType%'
OR PropertyBeds='$PropertyBeds'
OR PropertyRate >= '$PropertyRate1'
AND PropertyRate <= '$PropertyRate2'
Please note: The statement does work without using like and wildcards.
Your conditions are:
WHERE PropertyName LIKE '%$PropertyName%' or
PropertyLocation LIKE '%$PropertyLocation%' or
PropertyType LIKE '%$PropertyType%' or
PropertyBeds = '$PropertyBeds' or
PropertyRate >= '$PropertyRate1' and PropertyRate <= '$PropertyRate2'
If PropertyName, PropertyLocation, or PropertyType are empty strings, then you will return all the rows. That is my first guess on what is happening.
Perhaps you want AND as a connector rather than OR.

Combine 'like' and 'in' in a SqlServer Reporting Services query?

The following doesn't work, but something like this is what I'm looking for.
select *
from Products
where Description like (#SearchedDescription + %)
SSRS uses the # operator in-front of a parameter to simulate an 'in', and I'm not finding a way to match up a string to a list of strings.
There are a few options on how to use a LIKE operator with a parameter.
OPTION 1
If you add the % to the parameter value, then you can customize how the LIKE filter will be processed. For instance, your query could be:
SELECT name
FROM master.dbo.sysobjects
WHERE name LIKE #ReportParameter1
For the data set to use the LIKE statement properly, then you could use a parameter value like sysa%. When I tested a sample report in SSRS 2008 using this code, I returned the following four tables:
sysallocunits
sysaudacts
sysasymkeys
sysaltfiles
OPTION 2
Another way to do this that doesn't require the user to add any '%' symbol is to generate a variable that has the code and exceute the variable.
DECLARE #DynamicSQL NVARCHAR(MAX)
SET #DynamicSQL =
'SELECT name, id, xtype
FROM dbo.sysobjects
WHERE name LIKE ''' + #ReportParameter1 + '%''
'
EXEC (#DynamicSQL)
This will give you finer controller over how the LIKE statement will be used. If you don't want users to inject any additional operators, then you can always add code to strip out non alpha-numeric characters before merging it into the final query.
OPTION 3
You can create a stored procedure that controls this functionality. I generally prefer to use stored procedures as data sources for SSRS and never allow dynamically generated SQL, but that's just a preference of mine. This helps with discoverability when performing dependency analysis checks and also allows you to ensure optimal query performance.
OPTION 4
Create a .NET code assembly that helps dynamically generate the SQL code. I think this is overkill and a poor choice at best, but it could work conceivably.
Have you tried to do:
select * from Products where Description like (#SearchedDescription + '%')
(Putting single quotes around the % sign?)
Dano, which version of SSRS are you using? If it's RS2000, the multi-parameter list is
not officially supported, but there is a workaround....
put like this:
select *
from tsStudent
where studentName like #SName+'%'
I know this is super old, but this came up in my search to solve the same problem, and I wound up using a solution not described here. I'm adding a new potential solution to help whomever else might follow.
As written, this solution only works in SQL Server 2016 and later, but can be adapted for older versions by writing a custom string_split UDF, and by using a subquery instead of a CTE.
First, map your #SearchedDescription into your Dataset as a single string using JOIN:
=JOIN(#SearchedDedscription, ",")
Then use STRING_SPLIT to map your "A,B,C,D" kind of string into a tabular structure.
;with
SearchTerms as (
select distinct
Value
from
string_split(#SearchedDescription, ',')
)
select distinct
*
from
Products
inner join SearchTerms on
Products.Description like SearchTerms.Value + '%'
If someone adds the same search term multiple times, this would duplicate rows in the result set. Similarly, a single product could match multiple search terms. I've added distinct to both the SearchTerms CTE and the main query to try to suppress this inappropriate row duplication.
If your query is more complex (including results from other joins) then this could become an increasingly big problem. Just be aware of it, it's the main drawback of this method.