Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 combine two select queries into one?

Author  Topic 

sabbawow
Starting Member

1 Post

Posted - 2011-03-21 : 06:41:35
Hello,

I'm new to these forums and new to sql queries, but I have a question about it and can't seem to find the answer. So I thought to try it here maybe someone could help me with it.

At the moment I have two different queries running to fill my datagridview in my vb.net project but to load the datagridview it takes some times to execute the two select queries so I thought maybe I could combine them into one to speed up the wait time.

These are the queries that I have at the moment:

SELECT * FROM Contacts LEFT OUTER JOIN Company ON Contacts.company = Company.id ORDER BY Company.companyname ASC



SELECT name FROM Contactsoff WHERE id='" & SQLdr("id") & "' ORDER BY name ASC


What it does in the first query it searches for all the contacts in the table and in the second query it searches for the Company.id name and collects all the names that exists in the Contactsoff table.

Is it possible to combine those two different select queries?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-03-21 : 08:58:49
How many columns do you want from the first query?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-03-21 : 20:31:27
>> I'm new to these forums and new to SQL queries, but I have a question about it and can't seem to find the answer. So I thought to try it here maybe someone could help me with it. <<

Please post real DDL and not narratives. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. People cannot read your mind, so post your code and clear specs if you really want help.

You have made several classic noob errors. We don't care about the front end; we are the backend and that it all we care about.


SELECT << real list of columns >>
FROM Contacts
LEFT OUTER JOIN
Companies
ON Contacts.duns = Companies.duns;

1) we never use SELECT * in production code. It will screw you eventually.
2) Tables are named with plural or collective names; they model sets.
3) Columns are scalars (First Normal Form, 1NF) so they are singular.
4) RDBMS programmers use industry standards; the one for companies is the DUNS
5) Data elements do not change names from table to table.
6) There is no such thing as a magical, universal “id”
7) Be exact about data elements – name of what? Learn those ISO-11179 rules?

Your second query was not SQL. I will guess that it was supposed to be:

SELECT company_name
FROM Contacts_Off -– unh??
WHERE duns = @in_duns;

Which could be turned into this:

SELECT company_name
FROM Contacts_Off -– unh??
WHERE duns
IN (SELECT duns
FROM Contacts
LEFT OUTER JOIN
Companies
ON Contacts.duns = Companies.duns);

But I have to know what "Contacts_Off" is.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -