simple sql query. how to solve it..? - sql

ID MOBILE
1 9869600733
2 9869600793
3 9869600799
all id whose mobile number containing 9 three times... ?

Your RDBMS is needed to know the EXACT solution, but this works for most. I used MOBILE_ID_TABLE because you didn't provide table name. Depending on your RDBMS LIKE will want % or *.
SELECT ID FROM MOBILE_ID_TABLE WHERE MOBILE LIKE '%9%9%9%';
SELECT ID FROM MOBILE_ID_TABLE WHERE MOBILE LIKE '*9*9*9*';

my solution would be:
the statement will return all records, where the MOBILE column includes three times the number 9.
SELECT *
FROM youtable
WHERE (LEN(MOBILE) - LEN(REPLACE(MOBILE,'9',''))) = 3

Related

Removing SQL Rows from Query if two rows have an identical ID but differences in the columns

I´m currently working stuck on a SQL issue (well, mainly because I can´t find a way to google it and my SQL skills do not suffice to solve it myself)
I´m working on a system where documents are edited. If the editing process is finished, users mark the document as solved. In the MSSQL database, the corresponding row is not updated but instead, a new row is inserted. Thus, every document that has been processed has [e.g.: should have] multiple rows in the DB.
See the following situation:
ID
ID2
AnotherCondition
Steps
Process
Solved
1
1
yes
Three
ATAT
AF
2
2
yes
One
ATAT
FR
2
3
yes
One
ATAT
EG
2
4
yes
One
ATAT
AF
3
5
no
One
ABAT
AF
4
6
yes
One
ATAT
FR
5
7
no
One
AVAT
EG
6
8
yes
Two
SATT
FR
6
9
yes
Two
SATT
EG
6
10
yes
Two
SATT
AF
I need to select the rows which have not been processed yet. A "processed" document has a "FR" in the "Solved" column. Sadly other versions of the document exist in the DB, with other codes in the "Solved" columns.
Now: If there is a row which has "FR" in the "Solved" column I need to remove every row with the same ID from my SELECT statement as well. Is this doable?
In order to achieve this, I have to remove the rows with the IDs 2 | 4 (because the system sadly isn´t too reliable I guess) | and 6 in my select statement. Is this possible in general?
What I could do is to filter out the duplicates afterwards, in python/js/whatever. But I am curious whether I can "remove" these rows directly in the SQL statement as well.
To rephrase it another time: How can I make a select statement which returns only (in this example) the rows containing the ID´s 1, 3 and 5?
If you need to delete all rows where every id doesn't have any "Solved = 'no'", you can use a DELETE statement that will exclude all "id" values that have at least one "Solved = 'no'" in the corresponding rows.
DELETE FROM tab
WHERE id NOT IN (SELECT id FROM tab WHERE Solved1 = 'no');
Check the demo here.
Edit. If you need to use a SELECT statement, you can simply reverse the condition in the subquery:
SELECT *
FROM tab
WHERE id NOT IN (SELECT id FROM tab WHERE Solved1 = 'yes');
Check the demo here.
I'm not sure I understand your question correct:
...every document that has been processed has [...] multiple rows in the DB
I need to find out which documents have not been processed yet
So it seems you need to find unique documents with no versions, this could be done using a GROUP BY with a HAVING clause:
SELECT
Id
FROM dbo.TableName
GROUP BY Id
HAVING COUNT(*) = 1

How to check changes in column values?

I need to try to check some device IDs for work. These are values (15 characters, random string of numbers+letters) that mostly remain constant for users. However, every now and then these deviceIDs will change. And I'm trying to detect when they do change. Is there a way to write this kind of a dynamic query with SQL? Say, perhaps with a CASE statement?
user
device
date
1
23127dssds1272d
10-11
1
23127dssds1272d
10-11
1
23127dssds1272d
10-12
1
23127dssds1272d
10-12
1
04623jqdnq3000x
10-12
Count distinct device by id having count > 1?
Consider below approach
select *
from your_table
where true
qualify device != lag(device, 1, '') over(partition by user order by date)
if applied to sample data in your question - output is
As you can see here - at 10-11 first 'change, assignment' happened for user=1 ; and then on 10-12 he device changed

SQL list only unique / distinct values

I have a table which contains geometry lines (ways).There are lines that have a unique geometry (not repeating) and lines which have the same geometry (2,3,4 and more). I want to list only unique ones. If there are, for example, 2 lines with the same geometry I want to drop them. I tried DISTINCT but it also shows the first result from duplicated lines. I only want to see the unique ones.
I tried window function but I get similar result (I get a counter on first line from the duplicating ones). Sorry for a newbie question but I'm learning :) Thanks!
Example:
way|
1 |
1 |
2 |
3 |
3 |
4 |
Result should be:
way|
2 |
4 |
That actually worked. Thanks a lot. I also have other tags in this table for every way (name, ref and few other tags) When I add them to the query I loose the segregation.
select count(way), way, name
from planet_osm_line
group by way, name
having count(way) = 1;
Without "name" in the query I get all unique values listed but I want to keep "name" for every line. With this example I stilll get all the lines in the table listed.
To expound on #Nithila answer:
select count(way), way
from your_table
group by way
having count(way) = 1;
You first calculate the rows you want, and then search for the rest of the fields. So the aggregation doesnt cause you problems.
WITH singleRow as (
select count(way), way
from planet_osm_line
group by way
having count(way) = 1
)
SELECT P.*
FROM planet_osm_line P
JOIN singleRow S
ON P.way = S.way
you can group by way and while taking the data out check the count=1.It will give non duplicating data.
#voyteck
As I understood your question you need to get only non duplicating records of way column and for each row you need to show the name is it
If so, you have to put all the column in select statement, but no need to group by all the columns.
select count(way), way, name
from planet_osm_line
group by way
having count(way) = 1;

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

How to select ten values, five larger and five smaller than a given value in one Postgre query?

I have an interesting question to ask you :)
Assumptions:
I am using Spring and Hibernate over Postgres database.
I have User table and every user has it's name and it's score.
I want to generate a sort of a rank where: Given the user's name I extrapolate 5 users that have more scores than the given user and 5 users that have less scores like this:
user1 p.105
user2 p.104
user3 p.103
user4 p.102
given user p.100
user6 p.99
user7 p.98
...
My question is: is it possible to make it in one query only? Will it be more optimal and faster in one query? Do you have any proposal of such a query?
Doing it in two queries is very simple but I want to know if there is a way to do it in one time?
PostgreSQL can do that with a single query. You might need to use native (raw) SQL in Hibernate. Here's one way.
with lowers as (
select *
from test
where score < (select score from test where user_id = 13)
limit 5
), highers as (
select *
from test
where score > (select score from test where user_id = 13)
limit 5
)
select * from lowers
union all
select * from highers
union all
select * from test where user_id = 13
order by score;
You should think about what should happen if there aren't five scores greater or five scores less than the target (user_id = 13). Also, this query won't bring in any rows that have scores identical to the target, which might or might not be what you're looking for. It also makes no attempt to get the 10 rows that are closest to the target.
On my dev box, which is no speedster, this query runs in 0.1 ms on a table of a million rows.