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
 ...               ...      ...
LVL 1
minglelinchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ess KayEntrapenuerCommented:
use a Left join
0
Ess KayEntrapenuerCommented:
SELECT ordername,    location,   TypeTable .tid,   SUM(ISNULL(Quantity,0))Quantity
 from TypeTable LEFT JOIN OrderTable on OrderTable.tid on TypeTable.tid
Group BY ordername,     location,     TypeTable.tid

Open in new window



hope that helps
0
mankowitzCommented:
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
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

mankowitzCommented:
Fiddle: http://sqlfiddle.com/#!2/d684e/8

code:

CREATE TABLE OrderTable
	(`orderid` int, `ordername` varchar(4), `tid` int, `quantity` int, `location` varchar(3), `ordertime` varchar(3))
;
	
INSERT INTO OrderTable
	(`orderid`, `ordername`, `tid`, `quantity`, `location`, `ordertime`)
VALUES
	(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, '...', '...')
;



CREATE TABLE TypeTable
	(`tid` int, `type` varchar(5))
;
	
INSERT INTO TypeTable
	(`tid`, `type`)
VALUES
	(1, 'type1'),
	(2, 'type2'),
	(3, 'type3'),
	(4, 'type4'),
	(5, 'type5')
;

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)

Open in new window

0
minglelinchAuthor Commented:
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?
0
PortletPaulfreelancerCommented:
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
0
PortletPaulfreelancerCommented:
No points pl.

try changing the alias "names" to something else (maybe just "N"?)

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
minglelinchAuthor Commented:
Great! With the above query, I get exact data that I want.

Thanks for all comments.
0
PortletPaulfreelancerCommented:
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.
0
minglelinchAuthor Commented:
Good morning, and thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SSRS

From novice to tech pro — start learning today.