I have a table with data like
I want all the records from the table but which the record having EnquiryStatus=1 and order by LastAttendendedDate should come on the top and remaining records should come after those records. I tried to select twice with where condition and tried to union all them, But with that union all not allowing me to order by on different ways. I can do it in c# by retriving the data as two table and merge them as single. But I want it in sql..
EDITS:
I want something like
Select * From EnquiryMaster A Where A.BranchID=16 and EnquiryStatus=1 ORDER BY A.CreatedDate Desc
UNION ALL
Select * From EnquiryMaster A Where A.BranchID=16 and EnquiryStatus in(0,2,3) ORDER BY EnquiryStatus,A.CreatedDate Desc
SELECT * FROM TABLE_NAME WHERE ORDER BY CASE WHEN EnquiryStatus='1' THEN LastAttendendedDate END DESC
You can use this in sql.
Try this
select * from your_table
order by case when EnquiryStatus=1 then LastAttendendedDate end DESC
Related
Following example here -> https://www.tutorialspoint.com/how-to-get-the-first-and-last-record-of-the-table-in-mysql
To get first and last element of database we can use UNION.
I have a table called location with three queries.
And I would like to get rows with id 1 and in this case 3 (db will be much larger)
So I tried
(select * from location order by id ASC LIMIT 1)
UNION
(select * from location order by id DESC LIMIT 1);
But It doesn't work in my example. Does sqlite allow to use UNION operation?
SQLite does support UNION but does not like the queries inside the parentheses.
Use each of your queries as a subquery:
select * from (select * from location order by id ASC LIMIT 1)
UNION
select * from (select * from location order by id DESC LIMIT 1);
I am trying to display the records,order as in the where clause..
example:
select name from table where name in ('Yaksha','Arun','Naveen');
It displays Arun,Naveen,Yaksha (alphabetical order)
I want display it as same order i.e 'Yaksha''Arun','Naveen'
how to display this...
I am using oracle db.
Add this ORDER BY at the query's end:
order by case name when 'Yaksha' then 1
when 'Arun' then 2
when 'Naveen' then 3
end
(There's no other way to get that order. You need an ORDER BY to get a specific result set order.)
It may be a bit clunky, but you can create a custom ordering with a case expression:
SELECT *
FROM my_table
WHERE name IN ('Yaksha', 'Arun','Naveen')
ORDER BY CASE name WHEN 'Yaksha' THEN 1
WHEN 'Arun' THEN 2
WHEN 'Naveen' THEN 3
END ASC
A slightly longer option, but one that prevents duplication of the string literals is to use a subquery:
SELECT m.*
FROM my_table m
JOIN (SELECT 'Yaksha' AS name, 1 AS name_order FROM dual
UNION ALL
SELECT 'Arun' AS name, 2 AS name_order FROM dual
UNION ALL
SELECT 'Naveen' AS name, 3 AS name_order FROM dual) o
ON o.name = m.name
ORDER BY o.name_order ASC
You can try with something like the following:
SELECT *
FROM test
WHERE name IN ( 'Yaksha', 'Arun', 'Naveen' )
ORDER BY instr ( q'['Yaksha', 'Arun', 'Naveen']', name ) ASC
This way could be useful if your IN list is somehow dynamic.
If the list of values is dynamic or you just don't want to repeat the values you could use (or abuse, depending on your point of view) a table collection, and join your real table to a table collection expression instead of using IN:
select your_table.name
from table(sys.odcivarchar2list('Yaksha','Arun','Naveen')) t
join your_table on your_table.name = t.column_value;
Which will generally work, but of course without an order-by clause is not guaranteed to work, so you can use an inline view to assign the order:
select your_table.name from (
select row_number() over (order by null) as rn, column_value as name
from table(sys.odcivarchar2list('Yaksha','Arun','Naveen'))
) t
join your_table on your_table.name = t.name
order by t.rn;
This still relies on row_number() over (order by null) using the order of the elements in the collection; which relies on collection unnesting preserving the element order. I don't think that's guaranteed either, so there is still some risk involved.
I am working on python sqlite3.
This statement gets records 5 - 14;
SELECT * FROM something LIMIT 5, 10;
But how do I get, lets say the first five and last five records through a single statement?
You can combine output of two select statement like this:
(SELECT * FROM `something` order by some_column_name
limit 0,5)
union
(SELECT * FROM `something` order by some_column_name desc
limit 0,5
)
Specify some ordering of rows, so that it will select rows accordingly.
Maybe the better way is using rowid in your order by clause to get the first and last rows based on inserting the rows:
select test from
(select test,rowid from table1 order by rowid asc limit 0,5)t1
union all
select test from
(select test,rowid from table1 order by rowid desc limit 0,5)t2;
Here is a sample in SQL Fiddle
select *
from webOrders
where lastModifiedDate in (select max (lastModifiedDate) from webOrders)
Is there a simpler way without nesting the selects?
Doing something like this also results in an error:
select id, amount, quantity, max(lastModifiedDate) from webOrders.
There are a couple of approaches, depending on your needs.
If you only need one row returned, you can sort on the column of interest and return the top row. For example:
select top 1 *
from `order`
order by last_modified_date desc
if you use SQL Server or
select *
from `order`
order by last_modified_date desc
limit 1
if you use MySQL.
If you need to get one row per group, then you do typically have to use a subquery or a join.
select TOP 1 * from [order] order by lastModifiedDate desc
You can do select
SELECT TOP 1 * FROM XXX
http://www.w3schools.com/sql/sql_top.asp
If i perform a standard query in SQLite:
SELECT * FROM my_table
I get all records in my table as expected. If i perform following query:
SELECT *, 1 FROM my_table
I get all records as expected with rightmost column holding '1' in all records. But if i perform the query:
SELECT *, COUNT(*) FROM my_table
I get only ONE row (with rightmost column is a correct count).
Why is such results? I'm not very good in SQL, maybe such behavior is expected? It seems very strange and unlogical to me :(.
SELECT *, COUNT(*) FROM my_table is not what you want, and it's not really valid SQL, you have to group by all the columns that's not an aggregate.
You'd want something like
SELECT somecolumn,someothercolumn, COUNT(*)
FROM my_table
GROUP BY somecolumn,someothercolumn
If you want to count the number of records in your table, simply run:
SELECT COUNT(*) FROM your_table;
count(*) is an aggregate function. Aggregate functions need to be grouped for a meaningful results. You can read: count columns group by
If what you want is the total number of records in the table appended to each row you can do something like
SELECT *
FROM my_table
CROSS JOIN (SELECT COUNT(*) AS COUNT_OF_RECS_IN_MY_TABLE
FROM MY_TABLE)