473,434 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

SQL tables' cartesian product

2
I need a help to solve following interesting problem for me.

I've 2 id-name tables:
***************************************
ct (customers_table)
------------------------------
id name
1 Alice
2 Bob
3 Carl
4 John
***************************************
***************************************
ft (fruits_table)
-----------------------------
id name
1 Apple
2 Banana
3 Apricot
***************************************

And 3rd table that consists of data that tells us which customer bought which fruit and how much. For example:
***************************************
ot (orders_table)
------------------------------
id ct_id ft_id mass
1 1 1 12
2 1 2 10
3 2 1 5
4 3 3 6
5 1 3 13
6 1 1 5
7 2 3 15
8 3 2 4
9 3 2 11
***************************************

I need the result of total bought fruits, like a following table:
************************************************** ********
Cust_name Apple Banana Apricot
Alice 17 10 13
Bob 5 0 15
Carl 0 15 6
John 0 0 0
************************************************** ********

Beforehand thank you very much for your help creating the latter dynamic table...
May 11 '10 #1

✓ answered by deepuv04

use the following query

Expand|Select|Wrap|Line Numbers
  1. SELECT Cust_Name,[Apple],[Apricot],[Banana]
  2. FROM    (
  3.             SELECT  ct.Name as Cust_Name,ft.name as ft_Name,SUM(ot.mass) AS Mass
  4.             FROM     CT INNER JOIN
  5.                      OT ON CT.ID = OT.Ct_Id INNER JOIN
  6.                      FT ON FT.ID = OT.ft_Id
  7.             GROUP BY CT.Name,ft.Name 
  8.         ) src
  9. PIVOT    (SUM(Mass) FOR FT_Name
  10.         IN([Apple],[Apricot],[Banana])) AS pvt
  11.  
Note: make the query as dynamic string to the values dynamicallu

2 2054
deepuv04
227 Expert 100+
use the following query

Expand|Select|Wrap|Line Numbers
  1. SELECT Cust_Name,[Apple],[Apricot],[Banana]
  2. FROM    (
  3.             SELECT  ct.Name as Cust_Name,ft.name as ft_Name,SUM(ot.mass) AS Mass
  4.             FROM     CT INNER JOIN
  5.                      OT ON CT.ID = OT.Ct_Id INNER JOIN
  6.                      FT ON FT.ID = OT.ft_Id
  7.             GROUP BY CT.Name,ft.Name 
  8.         ) src
  9. PIVOT    (SUM(Mass) FOR FT_Name
  10.         IN([Apple],[Apricot],[Banana])) AS pvt
  11.  
Note: make the query as dynamic string to the values dynamicallu
May 12 '10 #2
uzeyir
2
Thank you very much deepuv04 for your excellent answer !!!

To be honestly, I've never heard that SQL has a PIVOT capability !! You solved me a problem, and I'll share this PIVOT and UNPIVOT property to my friends...

Again thanks a lot,
best regards Uzeyir Suleymanov,
Baku, Azerbaijan.
May 12 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: deancoo | last post by:
I need to do a Cartesian product, which is inherently expensive. Turns out, it's too expensive. I've dropped in that portion of my C++ code in hopes that someone with greater expertise with STL...
2
by: Wenin | last post by:
I have four total tables. Table One (Documents)- List of Documents. Each record has two fields related to this issue. First field (Document_ID) is the ID of the document, second field is the...
5
by: Ã…smund Kveim Lie | last post by:
Hi, We have found a possible bug in 7.3.1. It seems that using CROSS JOIN and doing plain Cartesian product, listing to tables in the from clause, gives different results. According to the...
7
by: Eric Slan | last post by:
Hello All: I'm having a problem that's been baffling me for a few days and I seek counsel here. I have an Access 2000 DB from which I want to run several reports. These reports are...
4
by: John Smith | last post by:
Isn't life a bitch! You know what you want but you don't know how to get it. I have produced 12 queries that calculate a payment profile over 12 months. For a number of the records (ie with...
2
by: manning_news | last post by:
Using A2K. I've got a database with client info and each client has a subform which contains types of income and the amount they each receive. The record source of the subform is a cartesian...
44
by: Christoph Zwerschke | last post by:
In Python, it is possible to multiply a string with a number: >>> "hello"*3 'hellohellohello' However, you can't multiply a string with another string: >>> 'hello'*'world' Traceback (most...
2
by: zfareed | last post by:
I have a program that creates two sets, one thru user interaction and the other with the use of an array. Can anyone help with coding for finding the cartesian product of the two sets; i.e a...
5
by: thelightkeeper | last post by:
Hi, I have 1 table contains about 4 millions entries structure like below: ( AlarmID int, SetTime datetime )
0
dbrewerton
by: dbrewerton | last post by:
The answer I was looking for was staring at me. Disregard this post.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.