Snowflake offers the (unusual) ability to have bind variables in table names:
https://docs.snowflake.com/en/sql-reference/literals-table.html
I'm wondering if the same can be done for columns, something like this:
select count(1) from mytable where ? is null
If I understand the question correctly, the identifier literal is what you are looking for:
select count(1) from mytable where identifier(?) is null
Docs: https://docs.snowflake.com/en/sql-reference/identifier-literal.html
Related
I am having a table like this.
I want to write a query in sqlite in flutter to get all the records where id are different. For above table output should be:-
How should I work this out?
Try this query
SELECT * FROM table_name GROUP BY id
Just use the word "distinct on columnName" after the select statement like this:
select distinct on id *
from table
For reference and more clarity check this out.
I'd like to simultaneously do two things in an MSSQL query:
select a field's value into a variable
select #myvar = colName from tableName
alias my column
select colName as [myCol] from tableName
I've tried these things:
Attempted Syntax select #myvar = colName as [myCol] from tableName
Attempted Syntax select #myvar = (colName as [myCol]) from tableName
Attempted Syntax select (#myvar = colName) as [myCol] from tableName
checked select statement syntax: https://msdn.microsoft.com/en-us/library/ms176104.aspx
If this is possible, how can it be accomplished?
A select can either assign values to variables or return column values, but not both.
In some cases, e.g. when using select to provide data to an insert, an output clause may be useful. A tool well worth having in your pocket as it provides access to identity values from insert and both before and after values when used with update.
Building on this post: MS Version of This MySQL View with GROUP BY?
How can subqueries be added?
All I want is to add to that view is an alias from a SELECT as an alias like:
... AS table2column2, (SELECT column1 FROM dbo.table3 WHERE constant = 1) AS table3column1, ...
Yes, I've looked everywhere. Interwebs have nada.
Thanks in advance!
Basically correct, just add a TOP 1 , as you cannot return more than one value:
(SELECT top 1 column1 FROM dbo.table3 WHERE constant = 1)
Following is example of SQL database query
SELECT *
FROM suppliers
WHERE supplier_name not like 'T%';
This query will returns the records where supplier_name does not starts with T
But how can I achieve the same functionality in salesforce?
There's no select *, so you have to specifiy the exact fields to query, you'd then use NOT and LIKE, e.g.
SELECT id,name, otherFields from suppliers where NOT supplier_name like 'T%'
You can also use something like this: SELECT 'fieldNamesNeeded' FROM suppliers WHERE supplier_name !=:'T%'
In some SQL dialects, you can state (something as):
SELECT * FROM SomeTable WHERE (val1,val2) IN
(SELECT val1,val2 FROM SomeOtherTable)
But I don't know how to do that in the TSQL (sql server 2k) I am using.
I am aware of (and using for now) workarounds like using joins or concatenated values,
but is there some syntax in TSQL I am overlooking to do just that?
UPDATE : This is valid SQL-99 syntax, that's why I consider a join a workaround, even if it would be more performant. My question is maybe put better as :
Is there an implementation of this syntax in TSQL?
UPDATE2 : I just tested this syntax om Mysql and it works fine there.
SELECT *
FROM SomeTable st
WHERE EXISTS
(
SELECT 1
FROM SomeOtherTable sot
WHERE sot.val1 = st.val1
AND sot.val2 = st.val2
)
This is actually what an IN construct is optimized to with any SEMI JOIN method.
As for your question,
Is there an implementation of this syntax in T-SQL?
the answer is no
As documentation says:
… subquery that has a result set of one column. This column must have the same data type as test_expression.
There is not a sql server implementation of that syntax. You'll have to do something like this:
SELECT st.*
FROM SomeTable st
INNER JOIN
(
SELECT val1, val2
FROM SomeOtherTable
GROUP BY val1, val2
) sot ON sot.val1= st.val1 AND sot.val2 = st.val2
Joining would be the way to go here.
IN only works on single column arrays. Since you've explicitly stated that you want to do an IN clause:
SELECT * FROM SomeTable
WHERE val1 IN (SELECT val1 FROM SomeOtherTable WHERE val2 = SomeTable.val2)
AND val2 IN (SELECT val2 FROM SomeOtherTable WHERE val1 = SomeTable.val1)
Check out the documentation on it. Since there's a where clause which depended on each row (e.g.-WHERE SomeOtherTable.ID = SomeTable.MyOtherID), it will hose performance, and a join is absolutely the way to go.
Select SomeTable.*
FROM SomeTable, SomeOtherTable
WHERE SomeTable.Val1 = SomeOtherTable.Val1
And SomeTable.Val2 = SoemeOtherTable.Val2
You should not consider this a "workaround." This is the most basic and standard way of accomplishing what you want to accomplish.
Edit: It may not be your standard "JOIN" syntax, but it's a habit.