SQL order by clause customized - sql

I want to run a query that fetches me all the names that have for say "ana" in it but i want the result like this
Anna
Brianna
not like
Brianna
Anna
which means the name starting with "an" should come first and then anything containing "an" in them
My SQL query is like this but this dpes not give me the desired results. I checked some other stuff but not quite sure how to use and does not give the desired results.
SELECT *
FROM TABLE_NAME
WHERE Name LIKE 'ana%'
AND Name LIKE '%ana%'

You need to select the records only once and then order them and according to your requirements i understand that you need to order something like this by using ORDER BY CASE
SELECT *
FROM TABLE_NAME
WHERE Name LIKE '%ana%'
ORDER BY CASE
WHEN Name LIKE 'an%' THEN 1
WHEN Name LIKE '%an%' THEN 2
ELSE 3
END

Related

sql query order by parts of

lets say you have a table with 10 000 records of different email adresses, but within this tables there are a few hundred (this can vary and should not matter) addresses that contains a specific domain name ie #horses.com.
I would like in one single query retrieve all 10 000 record, but the ones that contains #horses.com will always be on top of the list.
Something like this " SELECT TOP 10000 * FROM dbo.Emails ORDER BY -- the records that contains #horses.com comes first"
OR
Give me 10000 records from the table dbo.Emails but make shure everyone that contains "#horses.com" comes first, no matter how many there is.
BTW This is on an sql 2012 server.
Anyone??
Try this:
SELECT TOP 10000 *
FROM dbo.Emails
ORDER BY IIF(Email LIKE '%#horses.com', 0, 1)
This assumes the email ends in '#horses.com', which isn't unreasonable. If you really want a contains-like function, add another % after the .com.
Edit: The IIF function is only available in sql server 2012 and later, for a more portable solution use CASE WHEN Email LIKE '%#horses.com' THEN 0 ELSE 1 END.
SELECT TOP 10000 *
FROM dbo.Emails
ORDER BY case when charindex('#horses.com', email) > 0
then 1
else 2
end,
email
SELECT 1,* FROM dbo.Emails where namn like '%#horses.com%'
union
SELECT 2,* FROM dbo.Emails where namn not like '%#horses.com%'
order by 1

SQL: How to select and sum up columns in this query?

I have been looking but not finding my precise case so I try asking here.
There is a query (which I unfortunately may not disclose) that has this structure:
WITH MAINRESULT AS
(
SELECT ... FROM ... WHERE...GROUP BY...
)
SELECT Name, SUM(MAINRESULT.Amount1 * AnotherAmount) AS MySum FROM MAINRESULT
WHERE .....
GROUP BY Name, AnotherAmount
ORDER BY Name
Now, I get something like this:
**Name** **MySum**
A 5
A 5
B 1
C 2
But I want to have this result SUM-med up so that I get:
**Name** **MySum**
A 10
B 1
C 2
How do I do this by modifying the query struture that I have?
Tried adding a "SELECT FROM" around both the WITH-query parn and the Second SELECT below it but it says I have syntax errors then.
UPDATE:
I had been staring for too long at that Query I missed that the AnotherAmount should not be included in the GROUP BY part. Thanks everyone for pointing it out so quickly!
It looks like you need to remove GROUP BY Name, AnotherAmount and left only GROUP BY Name in your query. Otherwise your results being groupped not only by Name but also by some AnotherAmount field - this may cause unexpected result you're getting.

sqlite query unsorted result

I have list of Ids 31165,31160,31321,31322,31199,31136 which is dynamic.
When I run query
select id,name from master_movievod where id in(31165,31160,31321,31322,31199,31136);
I get following result
31136|Independence Day
31160|Planet of the Apes
31165|Mrs. Doubtfire
31199|Moulin Rouge
31321|Adult Movie 2
31322|Adult Movie 3
This is sorted list in ascending order.
I want the list in the same order which I give as input like
31165|Mrs. Doubtfire
31160|Planet of the Apes
31321|Adult Movie 2
31322|Adult Movie 3
31199|Moulin Rouge
31136|Independece Day
Without an order by clause, there's no guarantee on the order a database returns the results to you. SQLite, unfortunately, doesn't have something like MySQL's field for custom sorting, but you can jimmy-rig something with a case expression:
SELECT id, name
FROM master_movievod
WHERE id IN (31165, 31160, 31321, 31322, 31199, 31136)
ORDER BY CASE ID WHEN 31165 THEN 0
WHEN 31160 THEN 1
WHEN 31321 THEN 2
WHEN 31322 THEN 3
WHEN 31199 THEN 4
WHEN 31136 THEN 5
END ASC
Unfortunately, SQLite does not have an option like MySQL's FIELD for doing a custom ordering. You are left with two options. The first is that you could create a custom table containing the ordering you want and use that to sort. This option isn't very attractive. The second (and easier) option is to use ORDER BY CASE to achieve the order you want:
SELECT id, name FROM master_movievod
WHERE id IN (31165,31160,31321,31322,31199,31136)
ORDER BY
CASE id
WHEN 31165 THEN 0
WHEN 31160 THEN 1
WHEN 31321 THEN 2
WHEN 31322 THEN 3
WHEN 31199 THEN 4
WHEN 31136 THEN 5
END ASC

How can I select the Nth row of a group of fields?

I have a very very small database that I am needing to return a field from a specific row.
My table looks like this (simplified)
Material_Reading Table
pointID Material_Name
123 WoodFloor
456 Carpet
789 Drywall
111 Drywall
222 Carpet
I need to be able to group these together and see the different kinds (WoodFloor, Carpet, and Drywall) and need to be able to select which one I want and have that returned. So my select statement would put the various different types in a list and then I could have a variable which would select one of the rows - 1, 2, 3 for example.
I hope that makes sense, this is somewhat a non-standard implementation because its a filemaker database unfortunately, so itstead of one big SQL statement doing all I need I will have several that will each select an individual row that I indicate.
What I have tried so far:
SELECT DISTINCT Material_Name FROM MATERIAL_READING WHERE Room_KF = $roomVariable
This works and returns a list of all my material names which are in the room indicated by the room variable. But I cant get a specific one by supplying a row number.
I have tried using LIMIT 1 OFFSET 1. Possibly not supported by Filemaker or I am doing it wrong, I tried it like this - it gives an error:
SELECT DISTINCT Material_Name FROM MATERIAL_READING WHERE _Room_KF = $roomVariable ORDER BY Material_Name LIMIT 1 OFFSET 1
I am able to use ORDER BY like this:
SELECT DISTINCT Material_Name FROM MATERIAL_READING WHERE Room_KF = $roomVariable ORDER BY Material_Name
In MSSQL
SELECT DISTINCT Material_Name
FROM MATERIAL_READING
WHERE _Room_KF = 'roomVariable'
ORDER BY Material_Name
OFFSET N ROWS
FETCH NEXT 5 ROWS ONLY
where N->from which row does to start
X->no.of rows to retrieve which were started from (N+1 row)

How do I preserve the order of a SQL query using the IN command

SELECT * FROM tblItems
WHERE itemId IN (9,1,4)
Returns in the order that SQL finds them in (which happens to be 1, 4, 9) however, I want them returned in the order that I specified in the array.
I know I could reorder them after in my native language (obj c), but is there a neat way to do this in SQL?
Somthing like this would be great:
ORDER BY itemId (9,1,4) -- <-- this dosn't work :)
Probably the best way to do this is create a table of item IDs, which also includes a rank order. Then you can join and sort by the rank order.
Create a table like this:
itemID rank
9 1
1 2
4 3
Then your query would look like this:
select tblItems.* from tblItems
inner join items_to_get on
items_to_get.itemID = tblItems.itemID
order by rank
Use a CASE expression to map the ID values to an increasing sequence:
... ORDER BY CASE itemId
WHEN 9 THEN 1
WHEN 1 THEN 2
ELSE 3
END
I had the same task once in a mysql environment.
I ended up using
ORDER BY FIND_IN_SET(itemID, '9,1,4')
this is working for me since then. I hope it also works for sqlite
You can add a case construct to your select clause.
select case when itemid = 9 then 1
when itemid = 1 then 2 else 3 end sortfield
etc
order by sortfield
You could create a procedure to order the data in SQL, but that would be much more complicated than its native language counterpart.
There's no "neat way" to resort the data like that in SQL -- the WHERE clause of a SELECT simply says "if these criteria are matched, include the row"; it's not (and it cannot be) an ordering criterion.