I have a table ContactsCategoriesTable with a column name. Names in such column can be anything and 'UNSORTED' in particular. I want to make such an order by clause that my query is first sorted by ContactsCategoriesTable.Name, then by ContactsTable.NAME and UNSORTED rows are always at the bottom of table.
I tried this code, but without success. How to write such a clause?
sortOrder = ContactsCategoriesTable.NAME + " ASC, " + ContactsTable.NAME + " ASC, "
+ ContactsCategoriesTable.NAME + " = 'UNSORTED' ASC ";
A category name being "UNSORTED" is 'stronger' than any other sorting criteria, so you need to sort by that first:
ORDER BY Category.Name = 'UNSORTED', Category.Name, Contact.Name
Related
I want conditionally orderBy my query. All completed tasks (completed 1) should be below uncompleted and ordered by completion time and uncompleted task should be ordered by priority value. I am getting this with following statement:
orderBy = TaskTable.COLUMN_TASK_COMPLETED + " ASC, CASE " +TaskTable.COLUMN_TASK_COMPLETED+" WHEN 0 THEN " +TaskTable.COLUMN_TASK_PRIORITY +" WHEN 1 THEN "+TaskTable.COLUMN_TASK_COMPLETED_TIME + " END DESC";
Now, I want to change it, such way that priority is ordered with ASC attribute and completed_time is ordered with DESC attribute.
I tried:
orderBy = TaskTable.COLUMN_TASK_COMPLETED + " ASC, CASE " +TaskTable.COLUMN_TASK_COMPLETED+" WHEN 0 THEN " +TaskTable.COLUMN_TASK_PRIORITY +" ASK WHEN 1 THEN "+TaskTable.COLUMN_TASK_COMPLETED_TIME + " DESC END";
But it returns error:
Caused by: android.database.sqlite.SQLiteException: near "ASC": syntax error (code 1): , while compiling: SELECT * FROM task_table WHERE (task_name LIKE ? ) ORDER BY task_completed ASC, CASE task_completed WHEN 0 THEN task_priority ASC WHEN 1 THEN task_completed_time DESC END
How to fix it?
The ASC/DESC specifications can be applied only to the top-level expressions used in the ORDER BY clause.
When you have a numeric column, you can simply negate its values, i.e., sort not by X but by -X.
example: SELECT title,ROW_NUM FROM article ORDER BY count_read.
What should ROW_NUM be replace by ?
I don't like to after getting the results generate the index by program, because I want to insert into a table Rank with the result data by querying the example DQL above.
What I want to achieve maybe like :
"INSERT INTO RANK r (title, index, lastIndex)
SELECT title,ROW_NUM,(SELECT index FROM RANK WHERE id = :id - 1) FROM article ORDER BY count_read"
Thanks in advance..
I think you might use variables, like this:
"
SET #row_num := 1;
INSERT INTO RANK r (title, index, lastIndex)
SELECT title,
(#row_num := #row_num + 1),
(SELECT index FROM RANK WHERE id = :id - 1)
FROM article ORDER BY count_read
"
I am not familiar with SQL that much. I'm trying to insert multiple rows of data into a table that if there exist a row with with duplicate value in BusinessFilterPhrase column then just don't insert. I wrote a pseudocode of what I think it should be.
if (filterCategoryList != null)
{
foreach (KeyValuePair<string, int> filter in filterCategoryList)
{
cmd.CommandText = "insert into tblBusinessName (BusinessFilterPhrase,BusinessCategoryID)" +
"select #BusinessFilterPhrase,#BusinessCategoryID" +
"from tblBusinessName as t1" +
"where NOT EXISTS" +
"( select * from tblBusinessName as d1 where d1.BusinessFilterPhrase = #BusinessFilterPhrase) ";
cmd.Parameters.AddWithValue("#BusinessFilterPhrase", filter.Key);
cmd.Parameters.AddWithValue("#BusinessCategoryID", filter.Value.ToString());
cmd.ExecuteNonQuery();
}
}
You code looks correct. I would write it as:
insert into tblBusinessName(BusinessFilterPhrase, BusinessCategoryID)
select #BusinessFilterPhrase, #BusinessCategoryID
from tblBusinessName t1
where NOT EXISTS (select 1
from tblBusinessName d1
where d1.BusinessFilterPhrase = #BusinessFilterPhrase
)
(The changes are only cosmetic.)
EDIT:
If performance is an issue, create an index on BusinessFilterPhrase:
create index idx_tblBusinessName_BusinessFilterPhrase on tblBusinessName(BusinessFilterPhrase);
You can make this a unique index, if you want the database to enforce the uniqueness of the column (it will generate an error when duplicate values would be inserted).
I have two tables, vessel_details and vessel_history. Both tables have the same fields. I have to compare the values of all the fields and select the column with the different value and not null. For example the field of ship_name, i have to check if the value of the field ship_name from the vessel_details table is different with the ship_name in vessel_history table. If the value is different and not null, i have to select and display the value from the vessel_history.
Any ideas for the right query?
thanks.
SELECT vessel_details.<your_id>, 'Difference in Column ship_name for ship_name = ' + vessel_history.ship_name
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_name <> vessel_history.ship_name
UNION
SELECT vessel_details.<your_id>, 'Difference in Column ship_model for ship_model = ' + vessel_history.ship_model
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_model <> vessel_history.ship_model
.
.
.
And so on for all the columns you want to check.
Ist that what you are looking for?
EDIT for one row per item:
SELECT vessel_details.<your_id>,
CASE WHEN vessel_details.ship_name <> vessel_history.ship_name THEN Convert(bit, 1) ELSE Convert(bit, 0) END AS ship_name_different,
CASE WHEN vessel_details.ship_model <> vessel_history.ship_model THEN Convert(bit, 1) ELSE Convert(bit, 0) END AS ship_model_different
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_name <> vessel_history.ship_name
OR vessel_details.ship_model <> vessel_history.ship_model
I have this select statement:
SELECT *
FROM Room
LEFT JOIN booking ON (Room.RoomId = booking.RoomId)
WHERE booking.Roomid is null
AND GETDATE() BETWEEN bookin.checkindate '" + TxtCheckIn.Text + "'
AND booking.checkoutdate '" + TxtCheckOut.Text + "'" + "
ORDER BY Room.RoomType
I want to check in the booking table if the date matches the checkin and checkout dates selected by users. If it doesn't match, the query should show all rooms in the room table (even if it is in the booking table), provided that they have different dates.
You need to determine whether any rows match the condition on the date. To do this, the following query moves the date condition into the on clause. Then it uses the window function count() to count the number of matches. If there are none, then all rows are returned. Otherwise, only matches are returned.
select t.*
from (SELECT *, count(booking.RoomId) over () as numMatches
FROM Room LEFT JOIN
booking
ON Room.RoomId = booking.RoomId and
GETDATE() BETWEEN booking.checkindate '" + TxtCheckIn.Text + "' and
booking.checkoutdate '" + TxtCheckOut.Text + "'" + "
) t
where numMatches > 0 and RoomId is not null or numMatches = 0
ORDER BY Room.RoomType