Link to home
Start Free TrialLog in
Avatar of Erneeraq Fleischer
Erneeraq Fleischer

asked on

Joining four queries in SQL Server Report Builder

I want to use these three queries:

First query:

SELECT 
    AMICOS.PNRREG.PN, Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastissue 
FROM 
    AMICOS.HISTORYSTOCKFLOAT 
INNER JOIN 
    AMICOS.PNRREG ON AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID 
WHERE 
    (((AMICOS.HISTORYSTOCKFLOAT.ACTION)='ISSUE') AND    
    ((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1)) 
GROUP BY 
    AMICOS.PNRREG.PN;

Open in new window


Second query:

SELECT 
    AMICOS.PNRREG.PN, Max(AMICOS.HISTORY.HISTORYDATE) AS lastissuehistory
FROM
    AMICOS.HISTORY 
INNER JOIN 
    AMICOS.PNRREG ON AMICOS.HISTORY.PARTID = AMICOS.PNRREG.PARTID
WHERE 
   (((AMICOS.HISTORY.HISTORYACTION)='ISSUE') AND 
    ((AMICOS.HISTORY.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
GROUP BY 
    AMICOS.PNRREG.PN;

Open in new window

Third query:

SELECT 
    AMICOS.PNRREG.PN, Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastpurchase
FROM
    AMICOS.HISTORYSTOCKFLOAT 
INNER JOIN 
    AMICOS.PNRREG ON AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID
WHERE 
    (((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
GROUP BY 
    AMICOS.PNRREG.PN, AMICOS.HISTORYSTOCKFLOAT.ACTION
HAVING 
    (((AMICOS.HISTORYSTOCKFLOAT.ACTION)='PURCHASE'));

Open in new window


And this last query which put those together:

SELECT [6301_2018_03].PN, [6301_2018_03].ABC_CATEGORY, [6301_2018_03].PRIMUTILISATION, [6301_2018_03].DESCRIPTION, [6301_2018_03].ACCOUNTNO, [6301_2018_03].HISTORYDATE, [6301_2018_03].STOCK_FLOAT, [6301_2018_03].STOCK_PRICE, [6301_2018_03].STOCKVALUE, lastpurchase.lastpurchase, lastissue.lastissue, lastissuehistory.lastissuehistory
FROM ((6301_2018_03 LEFT JOIN lastpurchase ON [6301_2018_03].PN = lastpurchase.PN) LEFT JOIN lastissue ON [6301_2018_03].PN = lastissue.PN) LEFT JOIN lastissuehistory ON [6301_2018_03].PN = lastissuehistory.PN;

Open in new window


The querie from  from 6301_2018_03 (Which is generated to MS Excel file before joining the other three queries are:

select p.pn, c.abc_category, i.primutilisation, p.description, hsf.accountno,  hsf.historydate, hsf.stock_float, hsf.stock_price, hsf.stock_float*hsf.stock_price stockvalue from amicos.pnrreg p, amicos.irc i, amicos.ircabccat c, amicos.historystockfloat hsf, (select max(stockfloatid) stockfloatid from amicos.historystockfloat hsf  where historydate = (select max(hsf1.historydate) from amicos.historystockfloat hsf1  where hsf1.historydate<to_date('31032019','DDMMYYYY')+1 and hsf1.partid=hsf.partid) group by partid) x  where x.stockfloatid = hsf.stockfloatid and p.partid = hsf.partid and p.irc = i.irc and p.irc = c.irc and hsf.accountno like '6301%' and hsf.stock_float <> 0

Open in new window


These codes are used on MS Access Database which works and now i will make the same thing on Microsoft SQL Server Report Builder but i don't have much experience in SQL Server Report Builder.

I have tried UNION, JOIN to put those Queries together to get one result from this last queries which is dependending of those three other queries.

How can I achieve this?

Any help will be much appreciated

Regards

Erneeraq
ASKER CERTIFIED SOLUTION
Avatar of Erneeraq Fleischer
Erneeraq Fleischer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial