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
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

use a Left join
SOLUTION
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mankowitz
mankowitz
Flag of United States of America image

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
Avatar of mankowitz
mankowitz
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of minglelinch
minglelinch

ASKER

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?
Avatar of PortletPaul
PortletPaul
Flag of Australia image

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
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of minglelinch
minglelinch

ASKER

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

Thanks for all comments.
Avatar of PortletPaul
PortletPaul
Flag of Australia image

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.
Avatar of minglelinch
minglelinch

ASKER

Good morning, and thank you!
Microsoft SQL Server 2008
Microsoft SQL Server 2008

Microsoft SQL Server 2008 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. Major improvements include the Always On technologies and support for unstructured data types.

50K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo