• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Select within select?

Hi.

Is it possible in SQL to create query that will use Select within select statement.

For example:

I have two tables in database. I need data SUM,IDN,DATE

In one table i have SUM and IDN, in another I have DATE and IDN.

Now i want to create query that will gather data SUM,IDN, and DATE and order it by DATE value.

IDN is of same value in both tables (always).


0
Grogel
Asked:
Grogel
  • 2
1 Solution
 
Patrick MatthewsCommented:
You can, but in this case there is no need:

SELECT t2.[DATE], t2.IDN, SUM(t2.[SUM]) AS Qty
FROM table1 t1 INNER JOIN
    table2 t2 ON t1.[DATE] = t2.[DATE] AND t1.IDN = t2.IDN
GROUP BY t2.[DATE], t2.IDN
ORDER BY t2.[DATE], t2.IDN

Open in new window

0
 
GrogelAuthor Commented:
Thank you, however i do have one problem with this solution.

That data must not be summed. When i change SUM(t2.[SUM]) to t2.[SUM] i am getting following error

Column ' t2.[SUM]' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SUM field is number which is added in database by user, Date is automatic datetime, IDN is somekind of uniqueidentifier.

I need all SUM inputs (but not SUMMED) to be displayed along with Date.


Sorry for my English, it's not my native language.
0
 
GrogelAuthor Commented:
Never mind, sorry. I don't need Group by so everything works just fine. Sometimes my fingers are faster then my brain.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now