简体   繁体   中英

How to combine two sql select statement

I have two SQL select statement that says

SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood FROM FoodTable WHERE FoodID IS NOT NULL AND FoodDate >= '2015-10-01' GROUP BY FoodID ORDER BY NumberOfFood DESC

I also have another SQL statement

SELECT FoodSellerID, Market1, SellerLastName, SellerFirstName, PrimaryAddress1, PrimaryAddress2, PrimaryCity, PrimaryState, PrimaryZip FROM SellerTable, MarketTable 
WHERE Market1= MarketTable.MarketID

Few notes to point out, FoodID lookup to SellerTable, Market1 Lookup to MarketTable. My question is how can I combine both and return the tables like

FoodID | NumberofFood | SellerLastName | SellerFirstName | PrimaryAddress1 | PrimaryAddress2 | PrimaryCity | PrimaryState| PrimaryZip

Manny, you won't be able to join these two queries without some common field in both. If you have a common field, then you could build each as a temp table and join them with a third query. It looks like you have 'Food ID' in the FoodTable (from the first query). If that exists on the SellerTable, then you could join the two (and also join the MarketTable to get rows from that table).

That would look something like this:

    create temp table food_nums_temp as 
    SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood 
    FROM FoodTable 
    WHERE FoodID IS NOT NULL 
    AND FoodDate >= '2015-10-01'
    GROUP BY FoodID 
    ORDER BY NumberOfFood DESC
    ; 

    Create temp table seller_temp as
    SELECT FoodSellerID, FoodID,
    Market1,
    SellerLastName, 
    SellerFirstName, 
    PrimaryAddress1, 
    PrimaryAddress2, 
    PrimaryCity, 
    PrimaryState,
    PrimaryZip 
    FROM SellerTable, MarketTable 
    WHERE Market1= MarketTable.MarketID
    ;

    select a.*, b.SellerLastName  
    , b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2
    , b.PrimaryCity, b.PrimaryState, b.PrimaryZip
    from food_nums_temp a
    inner join seller_temp b on a.FoodID = b.FoodID

EDIT: knowing that full db access (create temp table) is not allowed, lets use 'WITH' (aka common table expressions or CTE) to stage our data prior to our desired query:

    with 
    food_nums_temp as (
    SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood 
    FROM FoodTable 
    WHERE FoodID IS NOT NULL 
    AND FoodDate >= '2015-10-01'
    GROUP BY FoodID 
    ORDER BY NumberOfFood DESC
    ),       

    seller_temp as
    SELECT FoodSellerID, FoodID,
    Market1,
    SellerLastName, 
    SellerFirstName, 
    PrimaryAddress1, 
    PrimaryAddress2, 
    PrimaryCity, 
    PrimaryState,
    PrimaryZip 
    FROM SellerTable, MarketTable 
    WHERE Market1= MarketTable.MarketID
    )

    select a.*, b.SellerLastName  
    , b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2
    , b.PrimaryCity, b.PrimaryState, b.PrimaryZip
    from food_nums_temp a
    inner join seller_temp b on a.FoodID = b.FoodID

Based on your vague description of FoodID lookup to SellerTable , try this

SELECT FoodSellerID, 
       Market1,
       SellerLastName, 
       SellerFirstName, 
       PrimaryAddress1, 
       PrimaryAddress2, 
       PrimaryCity, 
       PrimaryState,
       PrimaryZip 
FROM SellerTable ST
INNER JOIN MarketTable MT
  ON ST.Market1 = MT.MarketID
inner join 
    (
    SELECT FoodID, COUNT(*) AS NumberOfFood 
    FROM FoodTable 
    WHERE FoodID IS NOT NULL 
    AND FoodDate >= '2015-10-01'
    GROUP BY FoodID 
    ) a1
  on a1.FoodID = ST.FoodID

Try to this:

SELECT TOP (150) a.FoodID, COUNT(*) AS a.NumberOfFood,b.SellerLastName, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2, b.PrimaryCity, b.PrimaryState, b.PrimaryZipFROM FoodTable a inner join SellerTable b on a.FoodID = b.FoodSellerID WHERE a.Market1= b.MarketTable.MarketID IS NOT NULL AND FoodDate >= '2015-10-01' GROUP BY a.FoodID, b.SellerLastName, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2, b.PrimaryCity, 
b.PrimaryState, b.PrimaryZip ORDER BY a.NumberOfFood DESC

Note: Could be another solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM