Cross worksheet Sql query taking time? - sql

I have around 10 thousand records(rows) in one sheet A and around a thousand in sheet B. Both have 5 columns. I need to filter sheet A using column filters and then find everything which is not on sheet B using the ID in both the sheets. Eg: Sheet A has 10 thousand , apply filter and it comes to 1200. Now select the records which are not in Sheet B. I have written a query for it.
rs.Open "SELECT * FROM [A$] WHERE ([Class]<>'Other' OR...) AND ([Sub Class]='Good' OR...) AND [ID] NOT IN (SELECT [B$].ID FROM [B$])"
Query runs extremely slow and I don't know if I will even get any answer waiting hours, may be it is wrong too. I checked and found that if I exclude the last AND , the query executes in a second. Any help is appreciated. Thanks.

Why would you use a SQL query within an Excel workbook if you're not querying a database? Wouldn't VLOOKUP be much simpler?

Related

How to run a sql query multiple times and combine the results into single output?

I have a list of 2500 obj numbers stored in Excel for which I need to run the below SQL:
SELECT
a.objno,
a.table_comment,
b.queue_comment
FROM
aq$_queue_tables a
JOIN
AQ$_QUEUES b ON a.objno = b.table_objno
WHERE
a.objno = 19551;
Is there any way I can write a loop on above SQL with objno feeding from a list or from a different table? I also want to store/produce all the results from each loop run as a single output.
I considered the option to upload the numbers into a new table and add a where condition:
a.objno=(SELECT newtab.objectno FROM newtab);
However, the logic I'll be writing in the query would exclude certain objectno results. Let's say that the associated objectno has certain queue_comment as of certain date associated with that objectno. I do not want to pull that record. This condition would match with some objectno and wouldn't match with others. Having that condition and running the query against all the objectno is returning 0 results. I couldn't share the original logic as it would reveal certain business rules and it'll be a violation of some policy.
So, I need to run the query on each objectno separately and combine the results.
I'm totally new to SQL and got this task assigned. I'm aware of the regular loop, for in SQL, but I don't think I can apply them in this situation.
Any guidance or reference links to helpful topics is much appreciated as well.
Thanks in advance for the help.
One option is to upload the object numbers from Excel sheet to a table in the database and run the query as following. Assuming newtab is the table where the objectno are uploaded.
SELECT
a.objno,
a.table_comment,
b.queue_comment
FROM
aq$_queue_tables a JOIN AQ$_QUEUES b on a.objno = b.table_objno
WHERE
a.objno IN (SELECT newtab.objectno FROM newtab);
I have used a subquery here, join to the aq$ can work as well.
Reading the comments and all I think you need to enhance your Excel with 2 additional columns and load to a new table.
IN can be used in the following way too:
SELECT
a.objno,
a.table_comment,
b.queue_comment
FROM
aq$_queue_tables a
JOIN
AQ$_QUEUES b ON a.objno = b.table_objno
WHERE
(a.objno,a.table_comment,b.queue_comment) IN (19551,'something','something');
so with the new table will be:
WHERE
(a.objno,a.table_comment,b.queue_comment) IN
(select n.objno, n.table_comment, n.queue_comment from new_table n)

Using Different Parameters in Same excel Workbook for SQL

I have an excel Workbook with 2 sheets.
In each sheet I have a Query that extracts data from an external Server through SQLserver.
in Sheet1, Cell A1, I have the value 1000
and starting from Cell B2 I need the Query to get all the Students that have score more than 1000
I write the code in Excel in Data > Connection Properties > Definition > Command text
My code goes as follows:
SELECT Results.ID, Results.Score
FROM Results
Where Results.Score > ?
When I press OK, I am prompted to select a cell to be used as a parameter instead of the '?' Character
and then I get all Students ID & Score of Students with score more than 1000
and when I change the Cell A1 in Sheet1
Now in Sheet2, Cell A1, I have the Value 5
and starting from Cell B2 I need the Query to get all students in Grade 5
My code goes as follows:
SELECT Students.ID, Students.Grade
FROM Students
WHERE Students.Grade = ?
and When I press ok, the query runs without asking me to assign cell to hold the '?' Parameter , and it uses the parameter from the first query
How can I tell excel that I need a different parameter for each Query
Thanks in advance
Well, I searched a lot for anyone who have the same problem as me and I could't find So I thought it must be something I am doing to cause the problem
To facilitate creating the second query, I used to make a replicate of the 1st sheet and change the SQL command of the 2nd Query in the 2nd Sheet, which I found causes the replication of the parameter used
So the solution is simply create new query for each new Sheet

Excel find row of given string in range and copy to another tab

I have a column of data that looks like this:
The above is in the 'NEW PLANNER WORKBOOK' tab. Tabs are shown below:
I'm trying to find a way to get the values for the rows where "Switched" occur (in this case 13 and 16 but won't always be the same and could be more or less than 2 instances) and take the information from columns B, F, and U associated to those rows and put them into the "Switched Info" tab.
For example in the above, row 13 has the following in columns B, F, and U:
So I would like to move values 'BBMK01150', '201T1ZFPMBHFLFEDC0PMBHFLFETMD', and 'WCA' to the 'Switched Info' tab which looks like this:
The end result looking something like this:
Is there a way to do this only using formulas?
Thanks in advance for the help!
A pivot table would be best. But if a formula solution is wanted then:
These are all array formulas and must be confirmed with Ctrl-Shift-Enter.
In A4 put:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$B:$B,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
In B4:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$F:$F,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
In C4:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$U:$U,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
Again Each needs to be confirmed with Ctrl-Shift-Enter. Then they can be copied down for as many rows as desired.
One Note. This will be slow. To speed up, Change all the full column references to the specific area, ie $A:$A to $A$6:$A$1000, if 1000 was the last row of data.
You can definitely do this with code, but I think you could just use pivot tables if you didn't want to use a macro. Use the table in the New Planner Workbook worksheet as your data source and then make a pivot table on a new sheet and filter the data.

Concatenation and proportion in access

A little background:
I have two tables imported from excel. One is 300k + rows so when I do updates to it in excel it just runs too slow, and often doesn't process on my comp. Anyways, I used a 'outer' left join to bring the two together.
Now when I run the query, I get the result which works fine but I need to add some fields to these results.
I am hoping to mimic what Ive done in excel, so I can create my summary pivots in the same manner.
First, I need a field that just concatenates two others after the join.
Then I need to add a field the equivalent of:
1/Countif($T$2:$T$3330,T2) from excel to access. However, the range does not need to be fixed. I will get it so that all the text entries are at the top of the field, so in theory, i need the equivalent of Sheets("").Range("T2").End(xldown). This proportion is used to eliminate double counting when i do pivot tables.
I am probably making this much more complicated than it has to be but I am new to Access as well, so please try to explain some things in explanations.
Thanks
Edit: I currently have:
Select [Table1].*, [Table2].PlaySk, [Table2].Service
From [Table1] Left Join [Table2] On [Table1].Play + [Table1].Skill
= [Table2].PlaySk
And in a general case, what I am trying to solve is something to get ColAB and ColProportion.
ColA ColB ColAB ColProportion
a 1 a1 .5
b 1 b1 1
a 1 a1 .5
b 2 b2 .3333333
b 2 b2 .3333333
b 2 b2 .3333333
Sounds to me like you'll need to make a couple queries in sequence to do everything you need.
The first part (concatenate) is relatively easy though -- just take the two field names you wish to concatenate together, say [Play] and [Skill], and, in design view, make a new field like "PlaySk: [Play] & [Skill]".
If you want to put a character between them (I often do when I concatenate, just to keep things straight), like a semicolon for example, you can do "PlaySk: [Play] & ';' & [Skill]".
As for the second part, I think you'll want to build a "Group By" query on top of the other one. In your original query, make another field in design view like this: "T2_Counter: Iif([The field you're checking, i.e. whatever column T is] = 'whatever value you're checking for, i.e. whatever T2 is',1,0)". This will result in a column that's a 1 when the check is true, and a zero otherwise.
Then bring this query into a new one, click "Totals" at the top in the Design tab, then bring the fields you want to group by down. Then create a field in design view like this: "MagicField: 1/Sum(T2_Counter)".
Hopefully this helps get you started at least.

Excel: Selecting Specific Columns and Rows in Pivot Table and Formatting

I'm using a Pivot Table in Excel 2010, and while searching posts I find that a lot of users are frustrated like me because it doesn't keep all formats.
So what I'm trying to do is run a macro that formats columns in a Pivot table, but limited to the last row and column in the table. I have the formatting info, but I just need to know how to apply it to specific columns and rows.
What I was thinking might work is finding the last column of the Values row, in this case "Stops per Rte" which is the last Values column; however, I have months listed at the top, so it repeats across months. If the user filters only certain months then the # of columns will decrease.
Same goes for the # of rows: of course, the user should be able to expand/collapse rows as needed, so I only want the column format to go to the last row or just above "Grand Total", if possible.
Hopefully, this makes sense. Thanks in advance! = )