Avatar of minglelinch
minglelinch asked on

Query Issue

I need a help for a query which need to list each type of order for every person, and I want quatity value as 0 for the type that is not ordered.  

I have used full outer join two sets/tables, but the type which is not ordered won't show.
I also used cross join, the type which is not ordered will show but with too many confusioning rows.


OrderTable
orderid  ordername   tid  quantity   location  ordertime
001          Joe             1        5               NY         ...
002          Joe             2        4              ...         ...
003          Joe             4        3               ...         ...
004          Joe             5        2               MO        ...
004          Mike           3        30                ...         ...
005          Mike           4        40               ...         ...
006          Mike           5        20               ...         ...


TypeTable
tid    type
1       type1
2       type2
3       type3
4       type4
5       type5

I need to make a query to generate the following, listing every person name, some info, typeid and quantity for that type for the person (0 for not ordered item). No SUM or COUNT needed. I appreciate any help.

ordername     location     tid     Quantity
Jeo                 ...               1        5
Jeo                 ...               2        4
Jeo                 ...               3        0                -- 0 here
Jeo                 ...               4        3
Jeo                 ...               5        2
Mike               ...               1        0                -- 0 here
Mike               ...               2        0                -- 0 here
Mike               ...               3        30
Mike               ...               4        40
Mike               ...               5        20
 ...               ...      ...
SSRSMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
minglelinch

8/22/2022 - Mon
Ess Kay

use a Left join
SOLUTION
Ess Kay

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mankowitz

Try this:

select ifnull(ot.ordername, names.ordername), ifnull(ot.tid,tids.tid), ifnull(ot.quantity,0), ot.location from
(select distinct ordername from ordertable) names
join (select distinct tid from ordertable) tids
left join ordertable ot
on (ot.ordername = names.ordername AND ot.tid = tids.tid)
order by ifnull(ot.ordername, names.ordername),
ifnull(ot.tid,tids.tid)

I'll put it in a fiddle in a second
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
minglelinch

mankowitz: The above query has a nice structure, but  it is complained with "Multi part identifier "NAMES.ordername" cannot be bound at the line:
ON (ot.ordername = NAMES.ordername AND ot.tid = tids.tid)

esskayb2d: The query works well, and all typeid showed for each person, but SUM function should be removed from the query. There should be no SUM/Count required in query, Quantity is the final value for the typeid. How can I make the query so that all typeid display for each person without a sum/count/max function?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
PortletPaul

No points please.

Nice solution mankowitz :)

I would suggest just 2 small changes
SELECT
       ifnull(ot.ordername, NAMES.ordername) AS ordernames
     , ifnull(ot.tid, tids.tid)              AS tids
     , ifnull(ot.quantity, 0)                AS quantity
     , ot.location
FROM (
     SELECT DISTINCT ordername
     FROM ordertable
     ) NAMES
CROSS JOIN (               /*<< use "CROSS JOIN" it clearly identifies */
     SELECT DISTINCT tid   /*<< that the Cartesian product is deliberate */
     FROM ordertable
     ) tids
LEFT JOIN ordertable ot ON (
          ot.ordername = NAMES.ordername
          AND ot.tid = tids.tid
          )
ORDER BY
       ordernames /* << optional, can use the selection list aliases here */
     , tids       /* << optional, can use the selection list aliases here */
;

Open in new window

While an inner join (without ON conditions) appears to work, it also appears "very odd" and might be missing ON conditions, for maintenance purposes using CROSS JOIN makes it clear that no ON conditions are required. http://sqlfiddle.com/#!2/64da5/1
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
minglelinch

Great! With the above query, I get exact data that I want.

Thanks for all comments.
PortletPaul

With your permission I would like to propose a redistribution of accepted answer, assists and points.

ID: 39454068 Accepted Answer 0 points
ID: 39453604 Assisted Answer 167 points (no change in points)
ID: 39453660 Assisted Answer 333 points

Mankowitz saw the need for the Cartesian product and his sqlfiddle produced the expected results, all I added was a small syntactical change (and asked for no points)

Additionally, and perhaps more importantly, the accepted answer should be the solution.
ID: 39453604 does not fully satisfy the requirements.

I should add that MySQL syntax was offered above, but believe (now as I re-read this) that you were needing MS SQL Server. Ooops.

NB: I'm a "Topic Advisor" (with training wheels) and believe I can perform the suggested changes - if agreeable.

Or, you can propose your own redistribution - but I would like to see the accepted answer moved so as a PAQ folks will find the best fit.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
minglelinch

Good morning, and thank you!