I am new to SAS and I need to recreate a query I had running using R.
The syntax rules may be different in SAS but I dont see where I am going wrong here
Table "Old" columns: A, B, C, D, E
Table "New" columns: A, B, C, D, E
PROC SQL;
create table delta as
SELECT *
FROM New
WHERE
(A, B, C)
IN(
SELECT (A, B, C)
FROM New
EXCEPT
SELECT A, B, C
FROM Old);
QUIT;
My code should find delta rows based on A, B, C variables.
Error Message on comma
WHERE(A, B, C): ERROR 79-322: Expecting a (.
I'm not in sas but could be that this db don't allow the use of tuple in WHERE IN clause.
in this case you could try refactoring your quesry as an inner join
SELECT *
FROM New N
INNER JOIN (
SELECT A, B, C
FROM New
EXCEPT
SELECT A, B, C
FROM Old
) T ON T.A = N.A
AND T.B = N.B
AND T.C = N.C
Related
My data is structured with 5 columns (let's call them a, b, c, d, e) and 1,000,000+ rows. Each value in b has the potential for ~50 possibilities in e - so there could be up to 50 lines for each unique b value. Every b should have a '-27' among their e values. I would like to query all UNIQUE b where it doesn't have the -27 e value, ignoring all other possibilities for e.
Code so far:
select a, b, c, d, e
from TestDB
where not exists (select count(distinct b) from TestDB where e = '-27')
Would this code be sufficient? In initial tests I've done it appears to be either a) working or b) returning nothing. I'm new to SQL so I appreciate any help or being pointed in the right direction!
**edited to make it clearer I was looking for unique 'b' values.
I would like to query all b where it doesn't have the -27 e value, ignoring all other possibilities for e.
If you just want the b values, then aggregation should work:
select b
from testdb t
group by b
having sum(case when e = '-27' then 1 else 0 end) = 0;
You need a correlated subquery with NOT EXISTS, i.e. a reference to the main query table.
select a, b, c, d, e
from TestDB t1
where not exists (select 1 from TestDB t2 where t2.e = '-27' and t1.b = t2.b)
This query:
select b from TestDB where e = '-27'
returns all the bs that you want filtered out.
Use it with the operator NOT IN:
select a, b, c, d, e
from TestDB
where b not in (select b from TestDB where e = '-27')
I'm reviewing some SQL queries in SAS and I encountered the following query structure:
SELECT distinct A, B, Sum(C) FROM Table1 GROUP BY A, B;
I would like to know if it's strictly equivalent to:
SELECT A, B, Sum(C) FROM Table1 GROUP BY A, B;
Or if I'm missing a nuance, in the output or the way the computation is handled
The two queries are equivalent.
Generally,
SELECT DISTINCT a, b, c
FROM <something>
is equivalent to
SELECT a, b, c
FROM <something>
GROUP BY a, b, c
In your case, <something> happens to be a result of GROUP BY query, which has distinct columns A and B. This is enough to ensure that triples A, B, SUM(C) are going to be unique as well.
I have these two queries:
SELECT DISTINCT a, b, c, d, FROM x WHERE b IN (1, 2)
SELECT DISTINCT c, d, FROM y
I would now like to merge these queries such that the statement initiated in the first query only includes rows where the c, d combination is in the output resulting from the second query. Any thoughts on how to do this? My table is large, so efficiency is important.
Use exists?
SELECT DISTINCT a, b, c, d
FROM x
WHERE b IN (1, 2) AND
EXISTS (SELECT 1 FROM y WHERE x.c = y.c and x.d = y.d);
When using exists, the select distinct is only necessary if x has duplicate values. Otherwise it is not necessary.
And, for performance, you want an index on y(c, d). Also, an index on x(b, a, c, d) would also be helpful in most databases.
Note: The distinct is not necessary in the subquery. In some databases, you can use in with composite values as well.
SELECT DISTINCT x.a,x.b,x.c,x.d
FROM x
INNER JOIN y ON x.c = y.c
AND x.d = y.d
WHERE b in (1,2)
Regarding efficiency, your indexing will determine how well that performs.
I have multiple SELECT queries using same subset of data. I would like to reuse it so no repeated subqueries or WITH clause. However, I can't CREATE TABLE or VIEW because of insufficient privileges. So is there a workaround?
I'm using TOAD Oracle.
For example,
WITH LOCAL_RESULTS
AS (SELECT a, b, c, d...
FROM SURVEY )
SELECT A, B
FROM LOCAL_RESULTS
where condition=1
WITH LOCAL_RESULTS
AS (SELECT a, b, c, d...
FROM SURVEY )
SELECT A, C
FROM LOCAL_RESULTS
where condition=2
WITH LOCAL_RESULTS
AS (SELECT a, b, c, d...
FROM SURVEY )
SELECT B, D, A...
FROM LOCAL_RESULTS
where condition=3
Thanks for any help.
A union query might work.
with local_results as
(subquery goes here)
select a, b, c, 1 condition
from local_results
where whatever
union
select a, b, null c, 2 condition
from local_results
etc
I m new to SQL Server, please help me with the solution to following problem.
I have two tables which have the following columns:
Table-A only has one column with data like (A, B, C, D)
Table-B is a temporary table which can contain (B, B, C, C, E, E, F, G)
I need to insert contents of table B into table A.
Conditions are
if table A already contains any alphabet in B then that alphabet should be ignored.
Only a unique alphabet needs to be inserted into table A. i.e. Only one E can be inserted from two set of E and
if F and G also needs to be inserted.
How can I possibly solve this problem?
Thanking you in advance....
Try this....
INSERT INTO Table (Column)
SELECT DISTINCT tt.Column
FROM TempTab tt
WHERE tt.Column NOT IN (SELECT DISTINCT Column FROM Table)
Here is the SQL FIDDLE with this query.
INSERT INTO A(col)
(SELECT distinct col from B
minus
(SELECT distinct col FROM A INTERSECT SELECT distinct col FROM B))