I have a table like
+--------+-------+
| id | name |
+--------+-------+
| 302345 | Name1 |
| 522345 | Name2 |
| 1X2345 | Name3 |
| 2X2345 | Name4 |
| 1X8765 | Name5 |
| 2X2123 | Name6 |
| 502345 | Name7 |
| M62345 | Name8 |
+--------+-------+
I want to take id that doesn't have prefix 30,1X,2X. Like this I have more than 20 prefix to be excluded. I was using NOT LIKE 20 times and looking to shorten it. From some of stackoverflow question, I have found we can create a table and store all these values in a column and use that. But in my case I don't have permission to create table. Hence tried the below code but it is giving strange results.
SELECT *
FROM mytable
INNER JOIN (
SELECT '30%' Prefix
UNION ALL
SELECT '50%'
UNION ALL
SELECT '1X%'
) list ON id NOT LIKE prefix
FIDDLE HERE . Please suggest some alternative.
Expected output
+--------+-------+
| id | name |
+--------+-------+
| 522345 | Name2 |
+--------+-------+
| 502345 | Name7 |
+--------+-------+
| M62345 | Name8 |
+--------+-------+
You could use a NOT EXISTS with a VALUES construct for all your prefixes.
Something like this:
SELECT *
FROM mytable mt
WHERE NOT EXISTS (SELECT 1
FROM (VALUES('30%'),('50%'),('1X%'),('2X%')/*,...*/)V(expr)
WHERE mt.id LIKE V.expr);
Related
TABLE 1: Data sent to vendor
| MemberID | FirstName | LastName | Etc |
| :------: | :-------: | :------: | :-: |
| 1 | John | Smith | Etc |
| 2 | Jane | Doe | Etc |
| 3 | Dan | Laren | Etc |
TABLE 2: Data returned from vendor
| MemberID | FirstName | LastName | Etc |
| :------: | :-------: | :------: | :-: |
| 1 | John | Smith | Etc |
| 2 | Jane | Doe | Etc |
| 3 | Dan | Laren | Etc |
We send data to a vendor which is used for their matching algorithm and they return the data with new information. The members are matched with a MemberID data element. How would I write a query which shows me which MemberIDs we sent to the vendor but the vendor didn't return?
NOT EXITS would be my first choice here.
Example
SELECT *
FROM Table1 A
WHERE NOT EXISTS (SELECT 1
FROM Table2 B
WHERE A.MemberID = B.MemberID )
SELECT MemberID
FROM Table1
WHERE MemberID NOT IN (SELECT MemberID FROM Table2)
Using EXCEPT is one option.
SELECT sent.[MemberID] FROM Tbl1_SentToVendor sent
EXCEPT
SELECT recv.[MemberID] FROM Tbl2_ReturnedFromVendor recv
This is just on MemberID, but the "EXCEPT" syntax can also support additional columns (e.g., in case you want to filter out data that may be the same as what you already have.)
I'm having a table called table such that:
| id | name | city |
|----|-------|---------|
| 0 | Rose | Madrid |
| 1 | Alex | Lima |
| 2 | Rose | Sidney |
| 3 | Mario | Glasgow |
And I need to UPDATE the table so that two rows sharing the same name combined into a new one and deleted.
| id | name | city |
|----|-------|----------------|
| 1 | Alex | Lima |
| 3 | Mario | Glasgow |
| 4 | Rose | Madrid, Sidney |
I don't care if it has to be done in several SQL statements.
So far all I've done is to list the rows that are affected by this.
SELECT *
FROM table
WHERE name IN (
SELECT name
FROM table
GROUP BY name
HAVING COUNT(*) > 1
);
Assuming that id is auto increment primary key, you need an INSERT and a DELETE statement:
insert into tablename(name, city)
select name, group_concat(city, ',')
from tablename
group by name
having count(*) > 1;
delete from tablename
where instr(name, ',') = 0
and exists (
select 1 from tablename t
where t.id <> tablename.id and t.name = tablename.name
and ',' || t.city || ',' like '%,' || tablename.city || ',%'
);
See the demo.
Results:
| id | name | city |
| --- | ----- | ------------- |
| 1 | Alex | Lima |
| 3 | Mario | Glasgow |
| 4 | Rose | Madrid,Sidney |
I would like to have something like a procedure that takes a query definition as input and output a set of tables containing the individual elements of the query.
Searching the internet for this yields me numerous results in various programming languages, but not in tsql itself. Is there such a resource around?
An example in order to illustrate what I mean by parser:
Input example (any query, really:)
'select t1.col1,t2.col2
from table1 t1
inner join table2.col2
on t1.t2ref=t2.key'
The output, of course, will be a multitude of data. I mentioned tables, but it could be in any form eg an xml. Here is a VERY SIMPLISTIC and arbitrary example of decomposition for the query above:
tables_used:
+----+-----------+--------+------------+
| id | object_id | name | alias used |
+----+-----------+--------+------------+
| 1 | 43252345 | table1 | t1 |
| 2 | 6542625 | table2 | t2 |
+----+-----------+--------+------------+
columns_used:
+----------+-------------+
| table_id | column name |
+----------+-------------+
| 1 | col1 |
| 1 | t2ref |
| 2 | key |
| 2 | col2 |
+----------+-------------+
joins_used:
+-----+-----+-------+-----------------+
| tb1 | tb2 | type | on |
+-----+-----+-------+-----------------+
| 1 | 2 | inner | t1.t2ref=t2.key |
+-----+-----+-------+-----------------+
I've imported data from an XML file by using SSIS to SQL Server.
The result what I got in the database is similar to this:
+-------+---------+---------+-------+
| ID | Name | Brand | Price |
+-------+---------+---------+-------+
| 2 | NULL | NULL | 100 |
| NULL | SLX | NULL | NULL |
| NULL | NULL | Blah | NULL |
| NULL | NULL | NULL | 100 |
+-------+---------+---------+-------+
My desired result would be:
+-------+---------+---------+-------+
| ID | Name | Brand | Price |
+-------+---------+---------+-------+
| 2 | SLX | Blah | 100 |
+-------+---------+---------+-------+
Is there a pretty solution to solve this in T-SQL?
I've already tried it with a SELECT MAX(ID) and then a GROUP BY ID, but I'm still stuck with the NULL values. Also I've tried it with MERGE, but also a failure.
Could someone give me a direction where to search further?
You can select MAX on all columns....
SELECT MAX(ID), MAX(NAME), MAX(BRAND), MAX(PRICE)
FROM [TABLE]
Click here for a fiddley fidd fiddle...
I have list a data ORDER BY DCREPORTTIME.
My problem is I need to select the row which is alternating (ALARMSET/ALARMCLEARED).
Pls refer pic below... TQ~
**RED - no need to select.
shift | LOCATIONNAME | CALARMSTATE | DCREPORTTIME
--------- ----------------------------------------------------------
5/15/2013 | MYQ01_CTES_A_LTS | **AlarmSet** | 11:12:26
5/15/2013 | MYQ01_CTES_A_LTS | **AlarmCleared** | 11:12:26
5/15/2013 | MYQ01_CTES_A_LTS 5 | AlarmCleared | 11:12:53
5/15/2013 | MYQ01_CTES_A_LTS 5 | AlarmSet | 14:57:38
5/15/2013 | MYQ01_CTES_A_LTS 5 | AlarmSet | 14:57:43
5/15/2013 | MYQ01_CTES_A_LTS 5 | AlarmCleared | 14:57:43
Example data : HERE!
Try this :-
Select a.shift,a.LOCATIONNAME,a.DCREPORTTIME,a.CALARMSTATE
from yourTable a
inner join yourTable b
on a.CALARMSTATE <> b.CALARMSTATE and a.LOCATIONNAME=b.LOCATIONNAME
and a.DCREPORTTIME= b.DCREPORTTIME
Demo in SQL FIDDLE