Im trying to use either CONTAINS or FREETEXT in SQL programing to be able to search couple words all at the same time. The issue is when you search couple words FREETEXT or CONTAINS will search by column not by row for example:
Imagine this is my database
id | c1 | c2
===============================
a | 1 | 2
b | 1 | 3
c | 1 | 2
d | 2 | 2
e | 3 | 3
f | 2 | 1
When you search CONTAINS(*, '1,2') OR off curse FREETEXT(*, '1 2') it will return
id | c1 | c2
===============================
a | 1 | 2
b | 1 | 3
c | 1 | 1
d | 2 | 2
e | 2 | 3
f | 2 | 1
Which is basically entire database. But what I wanted was this
id | c1 | c2
===============================
a | 1 | 2
f | 2 | 1
Which is the rows that contains 1 and 2 combine.
By the way I'm using SQL 2008 and ASP classic.
I would really appreciate for your suggestion.
I have had this problem often. The solution I have used has never really satisfied me performance wise, but it does work. It is CONCATENATING the values I am searching for.
SELECT *
FROM (
SELECT (T.C1 + '-' + T.C2) AS C3, *
FROM TABLE T
WHERE T.C1 IN (ALL VALUES FOR C1)
) T2
WHERE T2.C3 IN (ALL CONCATENATED VALUES)
IF TYPES ARE NUMERIC, YOU SHOULD USE CONVERT OR CAST TO VARCHAR.
Hope this helps.
EDIT: I got an error using contains for lack of indexing. Change to IN.
Related
Sorry for the awkwardly-phrased title; I couldn't think of another way to describe this problem!
I have a table (lets call it table1) which looks roughly like this:
OFFER | DEAL
------------
A | 1
B | 1
C | 1
D | 2
E | 2
F | 3
I want to write a query which lists all offers and their deal number, along with an additional field showing any offers which share that deal number.
In other words, the results should look like this:
OFFER | DEAL | SHARED
---------------------
A | 1 | B
A | 1 | C
B | 1 | A
B | 1 | C
C | 1 | A
C | 1 | B
D | 2 | E
E | 2 | D
Does anyone know how to do this please?
You can use a self-join:
select t1.*, t2.offer
from t t1 join
t t2
on t1.deal = t2.deal and t1.offer <> t2.offer;
I know my subject is a little sparse, but for the life of me I cannot figure out how to do this. I could accomplish this in C# but I am getting confused by the SQL syntax. I searched and searched and I can't seem to find what I am looking for probably because I don't understand some of the SQL that I am looking at.
TABLE 1
-----------
| CustNo | Catalog1 | Catalog2 | Catalog3 | Catalog4 |
| 1 | A | B | C | NULL |
| 2 | B | C | NULL | D |
| 3 | A | C | E | F |
TABLE 2 (empty)
COLUMNS: CustNo|Catalog
So Basically for each record in Table 1, I want to insert the catalogs into table 2.
So the desired output would look like the following.
TABLE 2
CustNo|Catalog
| 1 | A
| 1 | B
| 1 | C
| 2 | B
| 2 | C
| 2 | D
| 3 | A
| 3 | C
| 3 | E
| 3 | F
Thank you all for any help!
Just unpivot. I like to do this using apply;
insert into table2 (CustNo, Catalog)
select t1.CustNo, v.Catalog
from table1 t1 cross apply
(values (t1.Catalog1), (t1.Catalog2), (t1.Catalog3), (t1.Catalog4)
) v(catalog)
where v.Catalog is not null;
So I am wondering. I fell into an interesting suggestion from another developer. So i basically have two tables I join in a query and I want the resulting table from the query to have an extra column that comes from the table on from the joint.
Example:
#table A: contains rating of players, changes randomly at any date depending
#on drop of form from the players
PID| Rating | DateChange |
1 | 2 | 10-May-2014 |
1 | 4 | 20-May-2015 |
1 | 20 | 1-June-2015 |
2 | 4 | 1-April-2014|
3 | 4 | 5-April-2014|
2 | 3 | 3-May-2015 |
#Table B: contains match sheets. Every player has a different match sheet
#and plays different dates.
MsID | PID | MatchDate | Win |
1 | 2 | 10-May-2014 | No |
2 | 1 | 15-May-2015 | Yes |
3 | 3 | 10-Apr-2014 | No |
4 | 1 | 21-Apr-2015 | Yes |
5 | 1 | 3-June-2015 | Yes |
6 | 2 | 5-May-2015 | No |
#I am trying to achieve this by running the ms-access query: i want to get
#every players rating at the time the match was played not his current
#rating.
MsID | PID | MatchDate | Rating |
1 | 2 | 10-May-2014 | 4 |
2 | 1 | 15-May-2015 | 2 |
3 | 3 | 10-Apr-2014 | 4 |
4 | 1 | 21-Apr-2015 | 4 |
5 | 1 | 3-June-2015 | 20 |
6 | 2 | 5-May-2015 | 3 |
This is what I have tried below:
Select MsID, PID, MatchDate, A-table.rating as Rating from B-table
left Join A-table
on B-table.PID = A-table.PID
where B-table.MatchDate > A-table.Datechange;
any help is appreciated. The solution can be in Vba as long as it returns something like a view/table I can manipulate using other queries or report.
Think of this in terms of sets of data... you need a set that lists the MAX dateChange for each player's and match date.
Soo...
SELECT MAX(A.DateChange) MDC, A.PID, B.Matchdate
FROM B-table B
INNER Join A-table A
on B.PID = A.PID
and A.DateChange <= B.MatchDate
GROUP BY A.PID, B.Matchdate
Now we take this and join it back to what you've done to limit the results in table A and B to ONLY those with that date player and matchDate (my inline table C)
SELECT B.MsID, B.PID, B.MatchDate, A.rating as Rating
FROM [B-table] B
INNER JOIN [A-table] A
on B.PID = A.PID
INNER JOIN (
SELECT MAX(Y.DateChange) MDC, Y.PID, Z.Matchdate
FROM [B-table] Z
INNER Join [A-table] Y
on Z.PID = Y.PID
and Y.DateChange <= Z.MatchDate
GROUP BY Y.PID, Z.Matchdate) C
on C.mdc = A.DateChange
and A.PID = C.PId
and B.MatchDate = C.Matchdate
I didn't create a sample for this using your data so it's untested but I believe the logic is sound...
Now Tested! SQL Fiddle using SQL server though...
My results don't match yours exactly. I think you're expected results are wrong though for MSID 4 given rules defined.
I have two tables :
the first one called "card" with one column "id".
| id |
| 1 |
| 2 |
| 3 |
| .. |
The second table is named "waste" with two columns "card_id" and "waste_type".
| card_id | waste_type |
| 1 | 1 |
| 1 | 3 |
| 2 | 2 |
| 2 | 1 |
And i want to select only the card where there is no waste_type = 2
The query should look like this :
SELECT c.id FROM card c
JOIN waste w
ON c.id = w.card_id
WHERE waste_type <> 2
I want this result :
id
1
But i get :
id
1
2
How can i do that ? Thank you so much in advance !
You should use a not exists clause for that.
select c.id
from card c
where not exists (select null from waste w
where w.card_id = c.id
and w.waste_type = 2)
With your query, I would guess you rather retrieve
1
1
2
i want to start by saying that i know there are a couple of questions regarding similar problems but they either dont answer my question fully or seem to be incompatible with SQLite.
I want to query all rows with value -1 and the first rows with values other than -1.
And by "first rows" i mean the group of rows that are first with a certain value. the first row is the row that is first stumbled upon depending on the SORT BY clause
An example of the data and outcome:
Data:
a b -1
c d 1
e f 2
g h 2
i j 2
k l -1
Result:
a b -1
c d 1
e f 2
k l -1
And as said above, i am using a SQLite database
Do this as two separate queries, one of them containing an inline view; UNION the two
select blah, blah from T where ...
UNION
select * from
(
select blah, blah from T where something else order by somecolumn limit 1
)
With simple example:
> select * from ex1;
+------+----------+
| id | name |
+------+----------+
| 1 | Pirate |
| 2 | Monkey |
| 3 | Ninja |
| 4 | Spagheti |
| 5 | kumar |
| 6 | siva |
+------+----------+
> select * from ex1 union select "1", "sing" order by case name when 'sing' then 1 else 2 end, name;
+------+----------+
| id | name |
+------+----------+
| 1 | sing |
| 5 | kumar |
| 2 | Monkey |
| 3 | Ninja |
| 1 | Pirate |
| 6 | siva |
| 4 | Spagheti |
+------+----------+