Not performing a select when a condition is not satisfied - sql

I'd like to perform a SELECT only when a condition is satisfied.
So far, the most I've come up with is the following:
SELECT id,
CASE WHEN country = 'Germany'
THEN (SELECT population)
END
FROM table;
However, the query returns an empty column named "case", as shown in the image:
Note that I don't want to make the field false or null, I don't even want this column to appear in the resulting table, as I need to use this same query for different databases. For example, the same query that I will use in the database "Germany", where the column "population" exists, should also be used in the database "Italy", where the column "population" does not even exist.
In other words:
If the country is "Germany", I would like to return "id" and "population".
Else, I would like to return only "id".
Note: It needs to be the same query. There is no possibility for me to make a different query for each country.

Related

How to replace entire values in a specific column?

I am trying to replace and entire value in a column (pet). The value ‘dog’ is repeated 85 times in that same column. If I wanted to replace it with ‘cat’ how would I do that but just in a that single column (pet)?
I tried with…
SELECT pet REPLACE(pet, ‘dog’, ‘cat’)
FROM Animals
Error message
REPLACE() in SQL will replace all instances of a specific sub-string within a string column value. But when used in a SELECT it only affects the output for that query, it will not update the source table.
The following should return all pets, and in the second column the dogs should be cats:
SELECT pet, REPLACE(pet, ‘dog’, ‘cat’) as newPet
FROM Animals
But REPLACE() is really designed for replacing 'dog' inside of a longer string, for instance to change thequickbrownfoxjumpsoverthelazydog to thequickbrownfoxjumpsoverthelazycat
It should still work for your simple scenario but it will change a dogfish into a catfish and that might not be appropriate.
The other way to do a conditional replacement is with CASE
SELECT pet
, CASE pet
WHEN ‘dog’ THEN ‘cat’
ELSE pet
END as newPet
FROM Animals
In these examples the replacement is shown as a separate column, just to demonstrate the output side-by-side with the source column, but you can use the column alias to return the calculated column instead of pet
SELECT CASE pet
WHEN ‘dog’ THEN ‘cat’
ELSE pet
END as pet
FROM Animals
If you want to permanently update the values in the source table, then combine either of these options with an UPDATE statement, the simplest form would be this
UPDATE Animals SET
pet = 'cat'
WHERE pet = 'dog'

SQL select statement dout

I have a table in which it contains the Name column contains some names in Capital letters and some data is in small letters in my WHERE clause if i give (where name='Syed') it will give only matching records because it is case sensitive but i want my output should display like(SYED,Syed,syed) how to do that please help me
SELECT Name
FROM Persons
WHERE UPPER(Name) = 'SYED'
This will return the Name in whatever case it exists as in the table, but will return all instances of it.

DAX query to exclude records from dataset

I have to write a query to exclude records using DAX. Now I am not sure how would I exclude records based on a particular condition.
For example, I have to filter data and display employee data for a company in all states except New York. How would I achieve that?
It seems like I can only apply a filter to just display specific data and not for exclusion as you would do in SQL. In SQL we can just use a NOT IN (...) clause to do that. Is there something similar in DAX?
Any help would be greatly appreciated. Thanks!!
EVALUATE
CALCULATETABLE(
<table expression>
,<table>[State] <> "New York"
)
The first argument need not be a table literal, but could be a function that returns a table.
The second argument should be on the table which contains the [State] field, and we simply exclude "New York". CALCULATETABLE() takes 1-N arguments. Arguments 2-N are all filters, which can be tables or simple predicates like in the example above. All filter arguments are evaluated in a logical and.
It looks like you only need a filter if the state is New York, but if you need something equivalent of the SQL NOT IN you can use nested AND functions. For example
EVALUATE
CALCULATETABLE(
'EMPLOYEE',
AND('EMPLOYEE'[STATE] <> "New York", AND('EMPLOYEE'[STATE] <>
"VIRGINIA", 'EMPLOYEE'[STATE] <> "MARYLAND"))
)

T-SQL/SQL- Copy Value From One Column to Another in View Depending on Condition

I have a problem. I will try to show it on Northwind database.
https://northwinddatabase.codeplex.com
I want to make a view on "Customer" table, where the "Region" column has an alias of "Country", but I will have values from "Region" column in "Country" column. I show it with "ContactName" column.
I made it like this:
CREATE VIEW test AS
SELECT ContactName, Region AS Country FROM Customers
Now i want to make a modification. I want to "copy" value from "Region" to "Country" if it is not null/not empty. If it is I want to keep value from Country.
I don't know if it's possible to make in view and using aliases and case.
You are looking for ISNULL or COALESCE. ISNULL will look at the first argument and if it is NULL, goes to the second argument.
SELECT ContactName, ISNULL(Region,Country) AS Country FROM Customers

insert made instead of update resulting in duplicate rows. how to fix?

I have a database where is supposed to have only one register with 2 value columns that can be filled by my web application. The values to one of the columns where given to me in an excel and we had to put it into the database. The person who did that, should had used an "update if exists else insert" but he didn't. Now, we have for some data, duplicate lines, one having just the column "valor_realizado_oficial" filled (with the column adt_login filled with 'talend' and with the key columns filled too), and another with the other column filled by the application.
So:
If exists two lines, I would like to copy the value of the column "valor_realizado_oficial" from the line with adt_login like 'talend' to the other line and delete this line.
If exists just one line, do nothing.
I tried to perform the copy part with:
update indicador_val iv
set valor_realizado_oficial=carga.valor_realizado_oficial
from (
select valor_realizado_oficial, ano, municipio_fk, indicador_fk, und_federativa_fk
from indicador_val
where adt_login like 'talend' and ano=2013 ) carga
where iv.ano=2013 and iv.municipio_fk=carga.municipio_fk
and iv.indicador_fk=carga.indicador_fk and iv.ano=carga.ano
and iv.und_federativa_fk = carga.und_federativa_fk;
But 0 rows where affected. Here's an example of a pair of lines:
id; adt_login; ano; valor_estimado, valor_realizado_oficial, indicador_fk, municipio_fk, und_federativa_fk
313885; "talend";2013;;888;2;2202;
291998;"suagenda";2013;900;;2;2202;
And I would like to have just the second, with values:
291998;"suagenda";2013;900;888;2;2202;
What I did wrong? Thanks.
In the sample data, there are empty columns. For example, the last one: und_federativa_fk.
Some of these columns are used in your joining conditions in the correlated UPDATE. If these empty values translate to the SQL NULL value, you should realize that NULL=NULL as a condition is going to be NULL, which means false in this context.
That alone might explain why no row get updated.
The solution is to use IS NOT DISTINCT FROM as the comparison operator for values that may be null.
Example:
where iv.ano=2013 and iv.municipio_fk IS NOT DISTINCT FROM carga.municipio_fk
and iv.indicador_fk IS NOT DISTINCT FROM carga.indicador_fk
etc...