Dax How to get distinct values from a column - ssas

This is the query I'm trying.
EVALUATE
SELECTCOLUMNS('MyTable',"col1",DISTINCT(VALUES('MyTable'[Email])))

If you are trying to simply create a new, single column table with the distinct values of an existing table, you can use the formula below.
Starting with data like this...
... simply create a new table with this formula to get a list of distinct values.
Locations = DISTINCT(Fruit[Location])

This will work:
Evaluate
VALUES('Table'[Column])

Related

Get column names stored in a different table

I have a table that has just a code for the individual column name like "A1G", "Z8H" etc.
This table is a really huge table. Therefore it would not help to manually add the alias name in the SELECT Statement like.
The description for each column code is stored in a different table:
Now I would like to SELECT * from the first table but with the right column header name.
This is stored in the second table within the filters Schema = 'ABC' and Table = 'A'.
That would be desired output:
How would you do that in SQL?
How can I change the column name?
You would be better off just creating a view with all the columns aliased to your preferred names. Once that's done you can select from the view and get the data back with the headings you want.
Look into Inner Join
Or Left Join.

PostgreSQL: Create/Select a fake/dummy column

How would i write a query so that i can generate myself a column with some specific numbers?
I would prefer not to be forced to create a table and import from excel, nor insert values (as i have many numbers to put into that column, and i'll have to put lots of parantheses around those values)
See picture for reference. Now imagine i have 1000 numbers. I would like to have some query where i could just copy-paste over those numbers.
I tried:
select (231,356,...) as companyid ... obviously this did not provide the desired result.
i succeded with a temp table and insert into .. but i had to go like VALUES(231),(356),... ... which is nasty
For a list of numbers, from some other source, you can use UNNEST():
SELECT UNNEST('{1,2,4,6,54,3,900}'::int[]) AS id;
Just replace "1,2,4,6,54,3,900" with your input, and use the comma to separate the values.

How do you query a table filtering on a substring of one of the columns?

I have a table I wish to query. It has a string variable called comment which contains an ID along with other things. (i.e. "123456;varA;varB")
rowNo
comment
1
"123456;varA;varB"
2
"987654;varA;varB"
I want to filter based on the first substring in the comment variable.
That is, I want to filter the table on rows where the first substring of comment is "123456" (which in the example would return the first row)
How do I do this?
I was thinking something along the lines of the code below, using the "string_split" function, but it doesn't work.
SELECT *,
FROM table
WHERE (SELECT value FROM STRING_SPLIT(comment,';',1)="123456")
Does anyone have any ideas?
Note, I am querying in SQL in SAS, and this is on a large dataset, so I don't want to create a new table with a new column to then query on instead. Ideally I'd want to query on the existing table directly.
You can use the SCAN() function to parse a string.
WHERE '123456'=scan(comment,1,';')

PostgreSQL: return row where any column value like variable

I am trying to have the user search for a value in a SQL table, and the user is returned with any row that contains that value. At the moment, I can make it work such that the code is:
SELECT * FROM table WHERE lower('foo') in (lower('col1'),lower('col2'),etc)
However, I would like it to be able to search every column and return any row LIKE 'foo'. For instance,
SELECT * FROM table WHERE (lower('col1'), lower('col2'), etc) like lower('%foo%')
But that doesn't work.
Any suggestions?
I believe you need to use multiple WHERE clauses instead of grouping them all into one statement. Try this:
SELECT * FROM table
WHERE lower(col1) like lower('%foo%')
OR lower(col2) like lower('%foo%')
OR etc like lower('%foo%')
You can convert the whole row to a string and then use LIKE on the result of that:
select *
from the_table
where lower(the_table::text) like '%foo%';
the_table::text returns all columns of each row as a comma separated list enclosed with parentheses, e.g. (42,Arthur,Dent). So the above is not 100 identical to a LIKE condition applied on each column - but probably does what you want.

Inserting a new column into SQL

I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory