This question already has answers here:
sql ORDER BY multiple values in specific order?
(12 answers)
Closed 9 years ago.
I have a sql statement which I would want to ORDER BY a specific sequence.
SELECT * FROM UserDB ORDER BY [Role]
How can I make it such that the data brought to my GridView table is listed from Admin on the top, follow by User and Guests?
So you want to order by Admin/User/Guest?
Try something like :
SELECT *
FROM UserDB
ORDER BY
CASE Role WHEN 'Admin' THEN 0
WHEN 'User' THEN 1
WHEN 'Guest' THEN 2
END
Does that work for you?
Another option would be to have (or add) a column Sequence to your Role table so you could define the sequence in the table itself - and then just to an ORDER BY Role.Sequence.
This is substantively identical to the question " sql ORDER BY multiple values in specific order? " and I strongly recommend you look at the solutions presented there.
Related
This question already has answers here:
Django equivalent of SQL not in
(4 answers)
Closed 3 years ago.
Im new to Django. Anyone help me to write "Not In" sql query in django.
here i used raw() query set without raw() query set how to write this query in django.
query = 'SELECT basic_uom FROM uom_master WHERE id="'+ id +'" and basic_uom not in (SELECT next_uom from uom_master WHERE id="'+ id +'") and basic_uom not in(SELECT std_uom FROM std_uom_master WHERE id"'+ id +'")ORDER BY next_uom ASC'
data = uom_master.objects.raw(query)
If [1,2,3] is your list then you can do something like this, you modify according to your table names
uom_master.objects.filter(id=some_id).exclude(id__in=[1,2,3]).order_by("next_uom")
To get list of ids you can do like
list_of_ids = std_uom_master.objects.filter().values_list("id",flat=True)
filter is your filtering criteria and exclude is records to be omitted.
See filters like id__in and guide about making queries
This question already has answers here:
Does the order of where clauses matter in SQL?
(6 answers)
Closed 7 years ago.
If there is a very large table T1(Id INT, Name VARCHAR(MAX), Category VARCHAR(MAX)) and Category is INDEX UNIQUE NONCLUSTERED, does it matter the order of operands if I do the select like
SELECT * FROM T1 WHERE Name = 'name' and Category = 'cat'
vs
SELECT * FROM T1 WHERE Category = 'cat' and Name = 'name'
?
Theres isnt any difference, you db planner will parse the code and choose the better option.
What is interesting is the order of execution can change if table sizes change. But how you write it wont.
There might be a difference based on which indexes you have on the table. Check actual execution plan to see what sql decides to use
This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a little problem, but I'm sure it s not really complicated.
It's just hard to find the key word to describe the problem and find a solution
I want to update a column in a table using parameters from this table for a query on an other table.
Example : I have Header + 2 lines
IDSOURCE, IDCIBLE, IDENTIFIANT, TABLE_CIBLE, NOM_ATTRIBUT, NOM_CHAMP_IDENTTIFIANT, NOM_CIBLE
--------------------------------------------------------------------------------------------
DMT_1000, DMT_1000, 1000, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
DMT_1001, DMT_1001, 1001, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
And I want to update the last column of each line with something like :
UPDATE
Table
SET
Table.NOM_CIBLE = SELECT table.NOM_ATTRIBUT FROM table.TABLE_CIBLE WHERE table.NOM_CHAMP_IDENTTIFIANT = table.IDCIBLE
FROM
Table
Don't know if it s clear.
Thanks for your help.
This sounds like situation where you could use a cursor. Check out this StackOverflow question How to update a column fetched by a cursor
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
I've been tasked with reviewing some SQL stored procedures and have seen many that look like the following:
SELECT
X, Y, Z
FROM
Table
WHERE
1 = 1
ORDER BY
X
Why would someone use '1 = 1' for the where clause?
Thanks!
It's common in dynamic SQL, in order to append additional criteria to a WHERE clause. Otherwise, it's useless and it is ignored by the optimizer.
possibly to dynamically add conditions to the where clause.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?
Is it possible to exclude a column from a select * from table statement with SQL Server?
I have a need for this and this is my only option other than parsing a raw SQL string to get out the required field names (I really don't want to do that).
Just to be bold. When the query is made I do not have access to the list of fields needed from the table but I do know which field I do not need. This is part of a complex multi-part query.
Surely there must be some way even if it's "hackish" such as using table variables or views
My other option is to reorder the columns. My problem is with ExecuteScalar SQL functions which get the first row and first column.
EDIT
I can't add an answer since this is now closed but the way I ended up doing it was like so:
;with results_cte as (
select (calculation) as calculated_column, * from table
)
select * into #temptable from results_cte
where calculated_column<10 /*or whatever*/
alter table #temptable
drop column calculated_column
select * from #temptable
drop table #temptable
Nope. You'll have to build your statement manually or just select *.
No.
Instead, you could check syscolumns to get all of the field names, or (perhaps) SELECT * and ignore that column.
If you use dynamic SQL, you can generate the query from metadata about the table or view (INFORMATION_SCHEMA.COLUMNS) and exclude columns that way. I do this a lot to generate triggers or views.
But there is nothing in the SQL language which supports this.
The best way to handle this would be to select * and then just not present the excluded column to your users in your frontend. As others have noted, SQL has no direct capability of doing an all-columns-except construct.