is it possible to do this? (I'm a SAS programmer usually so am used to building code in SAS macros like this). I have a table (lets call it TableCode) that holds lines of code (dynamically built by previous queries from metadata etc), e.g.
code pos
---- ---
a.id as id_a, 1
b.id as id_b, 2
a.var1 as var1_a, 3
b.var1 as var_b 4
from tablea a, 991
join tableb b 992
on a.id=b.id; 993
it would be bigger than that but you get the idea.
So, I'd like to be able to do something like:
execute 'select '||code||' from TableCode order by pos';
meaning that the code stored in TableCode would run. Is such a thing possible with Redshift?
Run your query to generate the code as the result of the query. Copy the text generated and paste it back into your SQL Workbench editor window. Submit it.
Related
I have two access tables, A and B:
Table A
Identifier BenefitBase PlanNav
1 131368.46 131368.46
2 201768.8 201768.79
3 54057.46 54057.46
4 7397.51 7397.51
5 9931.4 9931.4
6 178200 178200
Table B
p ValidityDate LockInAmount
1 2016-4 3.82
2 2016-4 19.97
3 2016-4 26.85
4 2016-6 34.95
I just want to create a query which extracts records from B where the "p" ID is not found in table A.
My current code is:
SELECT B.p, B.ValidityDate, B.LockInAmount
FROM B
WHERE (((B.p) Not In (select Identifier from A)));
Now to me, this code should work fine. However, because the tables are so large (B consists of 486,000 rows (the "p"'s repeats in this table for different dates) whereas A consists of circa 19,000), whenever I run the query, access fills the query progress bar but freezes when near full.
Is there another way to do this?
Thanks
You could also use a left join to do the same thing Gustav does. It's easier for me to read, and I believe that it will operate with the same execution plan.
select B.p, B.ValididtyDate, B.LockInAmount
from B
left join A on B.P = A.Identifier
where A.Identifier is null
And add to that the indexes recommended by Erik up above. (That said, if P and Identifier are primary keys on your tables then they are already indexed and you don't need to add the indexes)
Since you don't know if the fields are indexed:
Create indexes for both fields (see this page by Microsoft for information on indexes):
Execute these queries to create the indexes (or use the GUI)
CREATE INDEX TblAIdentifier ON A(Identifier)
CREATE INDEX TblBP ON B(p)
As long as you at least create the first index, Access won't even need to open up table A. It can just look in the index which fields are taken.
You can use this answer together with the one provided by #Gustav
You could "reverse" the seek - first find those that have a match, then exclude these from Table B:
Select B.*
From B
Where B.ID Not In
(Select A.Id
From A, B
Where A.ID = B.ID)
SELECT B.p, B.ValidityDate, B.LockInAmount
FROM
B
Left join
A
B.p=A.Identifier
WHERE A.Identifier Is Null);
In a query on a DB2 v10 for z/os, I need to pull data from live production lines, so I can't have any lock put on them. Accuracy in this situation is second to locking up data, as locking data would be very bad for production. My goal is to do something like this:
INSERT INTO newTable
SELECT a, b, c
FROM (SELECT d, e, f
FROM tableB b
FOR FETCH ONLY WITH UR)
I've tried all that I can think of doing, but it seems like whenever i put code inside of the FROM statement, it gives me an error on the "FOR FETCH ONLY WITH UR" even though it hasn't before when the same code is in other places. The error I get when I put "FOR FETCH ONLY WITH UR' or even just "WITH UR" is usually SQLCODE -199 "ILLEGAL USE OF FOR/WITH....."
I've also tried doing something like this:
INSERT INTO newTable
WITH tableA AS (
SELECT d, e, f
FROM tableB b
FOR FETCH ONLY WITH UR
)
SELECT a, b, c
FROM tableA
but I so far haven't found a way to specify WITH UR or to not lock data in tableB, while at the same time being able to use it for processing and putting it into newTable. What am I doing wrong and is it even possible to do what I want to do?
Thanks in Advance
I have a problem with full outer join in SAS
I want to join two database.
A is the "mama" containing patient ID,SEX,RACE,blablabla...but dont have the status variable.
B is the one only containing ID and status.
So A is actually a way bigger database than B and what I'm going to do is to put B including status into A. Here's my code:
proc sql;
CREATE TABLE C AS
select *
from A full outer join B
on A.id=B.id ;
RUN;
The result I got is actually not merging two database. Instead, I got the database C, which all the data from A on the top(status variable is null), and then the data B following by A (status variable is there but all other variables are showing Null). Thus, what i did is just adding rows....
Here is some conditions on my codes;
1. I use the University Edition
2. the format of ID is actually Char. Since B's ID (example:BD123), I convert numeric variable ID from A into char variable .
Anybody could help me with this? Thank you very much :-D
If you got an entire concatenation (100 rows in A, 15 rows in B, 115 in C) then you likely didn't correctly match the ID variable format when you converted. You may have an issue with additional spaces or something to that effect (the length of B.id may not match A.id). If possible I would convert the ID to numeric, or do a more careful conversion to character.
Second, if you're intending to just get the number of rows of A back (just adding B information to A), then you want a left join not a full outer join.
I Think you might be looking at left join instead.
proc sql;
create table C as
select A.*, B.*
from A left join B
on A.ID=B.ID;
quit;
This may not be a Birt question and may be solved with SQL itself. Please find my query below.
I have a complex query, 6 small queries joined with unions and intersects. All the small queries have a common inner query. Now writing it as a part of query will run this inner query 6 times, which I want to avoid. I want to pass the values of this inner query directly to the Main query.
The Main Query template is given below.
Select A_ID id from A where A.TYPE in (select Type from My_Type_List where Type_id=?)
UNION
Select B_ID id from B where B.TYPE in (select Type from MY_TYPE_LIST where Type_id=?)
UNION
Select C_ID id from C where C.TYPE in (select Type from MY_TYPE_LIST where Type_id=?)
Is there a way to optimize this query or to pass the values of the inner query as a parameter to Main Dataset.
The general approach is to run the first query and collect the results in a global JavaScript object.
You can then reuse the elements elsewhere using JavaScript.
The only alternative is to use an inner join in the second query.
I have two tables: A, B. I need to select all data from B.
I could do this like
SELECT id, b1, b2, ... b20 FROM A,B WHERE A.id = B.id;
it's not cool solution. I will need to update this statement if I modify B's database. Is exist something like ?
SELECT *(B)
It selected all data from B and didn't selected any data from A.
My databases
A
id
a1
a2
...
a20
B
id
b1
b2
...
b20
So if you want create database seriously you shouldn't see on complexity of way but on efficiency and speed. So my advice to you is use JOIN that is the best solution for selecting data from two and more tables, because this way is fast as possible, at least for my more cleaner like for example inserted select.
You wrote: I need to select all data from B this means SELECT * FROM B not that you wrote.
My advice to your is using this:
SELECT * FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;
or to select specific columns
SELECT A.column1, A.column2, ... FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;
Note:
NATURAL JOIN will work in above example since the primary key
and the foreign key in the two tables have the same name. So you must be very
careful in using NATURAL JOIN queries in the absence of properly
matched columns.
So you really should think about how you will create database and how you will working with database, how you will pulling data for View from database, how you will insert new potential data to database etc.
Regards man!
Use following query:
SELECT * FROM B;
or
SELECT * FROM B INNER JOIN A ON A.id = B.id;
if you want to join tables A and B.
I suspect that the others have sufficiently answered your question about selecting all fields from table B. That's great, as you really should understand the SQL basics. If they haven't, I'd also advise that you check out SQLite.org site for a clarification on SQL syntax understood by SQLite.
But, assuming you've answered your question, I just want to voice two words of caution about using the asterisk syntax.
First, what if, at some later date, you add a column to B that is a big hairy blob (e.g. a multimegabyte image). If you use the * (or B.*) syntax to retrieve all of the columns from B, you may be retrieving a ton of information you might not need for your particular function. Don't retrieve data from SQLite if you don't need it.
Second, is your Objective C retrieving the data from your select statement on the basis of the column names of the result, or based upon the index number of the column in question. If you're doing the latter, then using the * syntax can be dangerous, because you can break your code if the physical order of columns in your table ever changes.
Using named columns can solve the problem of memory/performance issues in terms of retrieving too much data, as well as isolating the Objective C from the physical implementation of the table in SQLite. Generally, I would not advise developers to use the * syntax when retrieving data from a SQL database. Perhaps this isn't an issue for a trivial project, but as projects become more complicated, you may want to think carefully about the implications of the * syntax.
I don't know if the following query would work in sqlite, I know it works in Oracle, but you can give it a try...
SELECT B.* FROM A,B WHERE A.id = B.id;