Reply to Message Icon Go to Main Page Icon

SQL 2005 UNION question

Original Message
Name: byrnesit
Date: January 1, 2008 at 06:56:47 Pacific
Subject: SQL 2005 UNION question
OS: XP
CPU/Ram: PM1.7/2gig
Model/Manufacturer: D505
Comment:

Hi, I have a quick question regarding unions in SQL 2005 (using express edition for development at the moment).

I am setting up somewhat of a stock management system and I want to be able to edit descriptions on an invoice (and only for that invoice), so what i have is:

*the stock table which holds the stock info (really cryptic) including the description
*a 'sales lines' table which holds the stock id, sale id, qty, etc etc...
*a 'sales lines descriptions table which will hold the description for a particular sales line ONLY if it has been edited
(amongst the other tables)
the only problem i'm facing at the moment is an easy way to determine if i use the standard or custom description, and what i was hoping i could do is something along the lines of 'unioning' the result of

"SELECT description FROM tblSalesLinesDescriptions WHERE lineID = tblSalesLines.LineID"

and

"SELECT description FROM tblStock WHERE stockID = tblSalesLines.StockID"

and just selecting the 'TOP 1' results, or grouping or something...

this works logically in my head as it should select the custom description if there is one, or the standard if there isnt...

I googled and found an example which was along the lines of

SELECT id, sum(amount)
FROM
(SELECT id, amount FROM table1
UNION ALL
SELECT id, amount FROM table2)
GROUP BY id


I tried adopting this to:

SELECT TOP 1 description FROM
(
SELECT description from tblStock where stockid = '<INSERT RELEVANT GUID HERE>'
UNION
SELECT description from tblsaleslinesdescriptions where lineid = '<INSERT RELEVANT GUID HERE>'
)

but this didnt work... came up with the error:
Incorrect syntax near ')'

referring to the last ')'

the subquery (unions) does return the correct result (ie: 2 descriptions), its just not doing the "SELECT TOP 1".

anyone got any suggestions??
Thanks


Report Offensive Message For Removal


Response Number 1
Name: byrnesit
Date: January 1, 2008 at 07:27:44 Pacific
Subject: SQL 2005 UNION question
Reply: (edit)

Never mind... I just had a 2am brain snap and realised i could do it in a user defined function... but if anyone has an answer to the above i'd still be interested in hearing it :)


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: SQL 2005 UNION question

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software