How to check the same values between 2 table (MSAccess) - vba

enter image description here
I think I have to use loop function, but I don't know how to write that (Thank you)

SELECT * From TableName
Where columnName ==(
SELECT TableName from AnotherTableName
)

DELETE FROM BB
WHERE CODE NOT IN
(SELECT AA.code FROM AA)

Related

Convert horizontal table to vertical in oracle

I have the following table format:
ID | Key | Value
-- --- -----
1 A aa
2 B bb
3 A ay
4 C cc
5 B bx
6 C ct
I need to convert this table to following format:
Output:
A B C
--- --- ---
aa bb cc
ay bx ct
I looked for PIVOT function in oracle 11g, but the "Key" values in input table is not a fixed set of values, they can be anything.
I also looked for other such questions but I am not sure in my case, how the query should be written.
Any help will be appreciated, thanks!
Edited:
For the solution, I want to execute the following query but it gives me error at subquery of IN clause. I don't understand why is that.
Select * from (Select Key, Value, Id from tableName
pivot (max(Value) for Key IN (SELECT distinct Key from tableName)));
Thanks!
You can apply the pivot function as below.
select * from
(select Key,Value from yourtable)
pivot(max(Value) for Key in ('A', 'B', 'C'));
A subquery in pivot is used only in conjunction with the XML keyword
Make your query as below:
Select * from (Select Key, Value, Id from tableName)
pivot xml (max(Value) for Key IN (SELECT distinct Key from tableName));
You can use dynamic sql as you have said that the key is not fixed
create a string for keys this will help you. pass this string to you pivot function as the keys are in string so this will help you.

Insert from select with sequence and group by

I want to insert some data into table tableA, but I have to use group by and my_sequence.nextval. It is impossible to use both in the same statment, is exists any workaround ?
For example:
insert into tableA (
taba_id,
taba_sum,
taba_date
) select
tabb_sequence.nextval,
sum(tabb_value),
tabb_date
from
tableB group by (tabb_date);
After execute this statment, I got:
ORA-02287: sequence number not allowed here
According oracle documentation,I should get this error.
How to deal with sequence and group by caluses in one statment?
The problem here is because your sequence is not aggregated, therefore you have this error. Try this way:
insert into tableA (
taba_id,
taba_date,
taba_sum
)
select tabb_sequence.nextval,
tabb_date,
stv
from (select tabb_date,
sum(tabb_value) stv,
from tableB
group by tabb_date) a;

SQl select specific column but with use of *

I need help, my SQL Server select statement is:
select * from schematemplate.scanner
the columns of this table are:
id
Asset_Category
Asset_Classification
Brand
Model
Supplier
Color
I can select all the columns except the Asset_Category and Asset_Classification by using this:
Select id, brand, model, supplier, color
from schematemplate.scanner
But I don't want to specify the columns that I will select like the code above.
Is it possible to use SELECT * from schematemplate.scanner and add a code like EXCEPT asset_category and asaset_classification?
Those are only five columns. Why not select it?
Anyway, here's a suggestion that you may take,
create a view and
run select on it,
example
CREATE VIEW viewScanner
AS
SELECT id, brand, model, supplier, color
FROM schematemplate.scanner
and when you want to select records,
SELECT * FROM viewScanner
You could do it dynamically, e.g.:
declare #s varchar(max) = 'select '
select #s=#s+name+',' from sys.columns
where object_id=object_id('schematemplate.scanner')
and name not in ('asset_category','asset_classification')
order by column_id
set #s=substring(#s,1,len(#s)-1)+' from schematemplate.scanner'
exec(#s)
sqlfiddle

How to show the vertical data horizentally through sql

my table has two column columnname and data
i issue a simple sql like select * from mytable then data is showing like
colname data
------------------- -----------
JID 41185
WID 0
AccountReference LH169
OEReference Ari002
InvoiceNumber 0
but i want to display data in different way like
JID WID AccountReference OEReference InvoiceNumber
41185 0 LH169 Ari002 0
if i need to show data horizentally then what sql i need to issue..........please help.
SQL isn't really about display. The problem you have is that you'd really need 2 queries (1 for colname and 1 for data) with no guarantee the data would be returned in the same order for each query. You really need to wrap some external code around this - save the query results in a 2-d array of string (or a collection etc) then iterate through each
SELECT JID,WID,AccountReference,OEReference,InvoiceNumber
FROM
(
SELECT colname, data FROM YourTableName
)
p
PIVOT
(
Max(data) FOR colname
IN ([JID],[WID],[AccountReference],[OEReference],[InvoiceNumber])
) AS pvt
you can try below links. contains tutorials for the usage of Pivot.
Link1
Link2
If the values of colname are known in advance & unique;
SELECT * FROM tbl
PIVOT (
MAX(data)
FOR colname in ([JID],[WID],[AccountReference],[OEReference],[InvoiceNumber])
) pv
You can find this in my blog:
http://sql-tricks.blogspot.com/2011/04/sql-server-rows-transpose.html
You should change #xml variable like this:
SET #xml = ( SELECT colname,data,
Row_Number() OVER ( ORDER BY ( SELECT 1
) ) Rn
FROM mytable
FOR
XML PATH('Row') ,
ROOT('Root') ,
ELEMENTS XSINIL
) ;

Is it possible to use WHERE IN along with LIKE?

If I have to search for some data I can use wildcards and use a simple query -
SELECT * FROM TABLE WHERE COL1 LIKE '%test_string%'
And, if I have to look through many values I can use -
SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable)
But, is it possible to use both together. That is, the query doesn't just perform a WHERE IN but also perform something similar to WHERE LIKE? A query that just doesn't look through a set of values but search using wildcards through a set of values.
If this isn't clear I can give an example. Let me know. Thanks.
Example -
lets consider -
AnotherTable -
id | Col
------|------
1 | one
2 | two
3 | three
Table -
Col | Col1
------|------
aa | one
bb | two
cc | three
dd | four
ee | one_two
bb | three_two
Now, if I can use
SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable)
This gives me -
Col | Col1
------|------
aa | one
bb | two
cc | three
But what if I need -
Col | Col1
------|------
aa | one
bb | two
cc | three
ee | one_two
bb | three_two
I guess this should help you understand what I mean by using WHERE IN and LIKE together
SELECT *
FROM TABLE A
INNER JOIN AnotherTable B on
A.COL1 = B.col
WHERE COL1 LIKE '%test_string%'
Based on the example code provided, give this a try. The final select statement presents the data as you have requested.
create table #AnotherTable
(
ID int IDENTITY(1,1) not null primary key,
Col varchar(100)
);
INSERT INTO #AnotherTable(col) values('one')
INSERT INTO #AnotherTable(col) values('two')
INSERT INTO #AnotherTable(col) values('three')
create table #Table
(
Col varchar(100),
Col1 varchar(100)
);
INSERT INTO #Table(Col,Col1) values('aa','one')
INSERT INTO #Table(Col,Col1) values('bb','two')
INSERT INTO #Table(Col,Col1) values('cc','three')
INSERT INTO #Table(Col,Col1) values('dd','four')
INSERT INTO #Table(Col,Col1) values('ee','one_two')
INSERT INTO #Table(Col,Col1) values('ff','three_two')
SELECT * FROM #AnotherTable
SELECT * FROM #Table
SELECT * FROM #Table WHERE COL1 IN(Select col from #AnotherTable)
SELECT distinct A.*
FROM #Table A
INNER JOIN #AnotherTable B on
A.col1 LIKE '%'+B.Col+'%'
DROP TABLE #Table
DROP TABLE #AnotherTable
Yes. Use the keyword AND:
SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable) AND COL1 LIKE '%test_string%'
But in this case, you are probably better off using JOIN syntax:
SELECT TABLE.* FROM TABLE JOIN AnotherTable on TABLE.COL1 = AnotherTable.col WHERE TABLE.COL1 LIKE '%test_string'
no because each element in the LIKE clause needs the wildcard and there's not a way to do that with the IN clause
The pattern matching operators are:
IN, against a list of values,
LIKE, against a pattern,
REGEXP/RLIKE against a regular expression (which includes both wildcards and alternatives, and is thus closest to "using wildcards through a set of valuws", e.g. (ab)+a|(ba)+b will match all strings aba...ba or bab...ab),
FIND_IN_SET to get the index of a string in a set (which is represented as a comma separated string),
SOUNDS LIKE to compare strings based on how they're pronounced and
MATCH ... AGAINST for full-text matching.
That's about it for string matching, though there are other string functions.
For the example, you could try joining on Table.Col1 LIKE CONCAT(AnotherTable.Col, '%'), though performance will probably be dreadful (assuming it works).
Try a cross join, so that you can compare every row in AnotherTable to every row in Table:
SELECT DISTINCT t.Col, t.Col1
FROM AnotherTable at
CROSS JOIN Table t
WHERE t.col1 LIKE ('%' + at.col + '%')
To make it safe, you'll need to escape wildcards in at.col. Try this answer for that.
If I understand the question correctly you want the rows from "Table" when "Table.Col1" is IN "AnotherTable.Col" and you also want the rows when Col1 IS LIKE '%some_string%'.
If so you want something like:
SELECT
t.*
FROM
[Table] t
LEFT JOIN
[AnotherTable] at ON t.Col1 = at.Col
WHERE (at.Col IS NOT NULL
OR t.Col1 LIKE '%some_string%')
Something like this?
SELECT * FROM TABLE
WHERE
COL1 IN (Select col from AnotherTable)
AND COL1 LIKE '%test_string%'
Are you thinking about something like EXISTS?
SELECT * FROM TABLE t WHERE EXISTS (Select col from AnotherTable t2 where t2.col = t.col like '%test_string%' )