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...
use the following query -
SELECT Cust_Name,[Apple],[Apricot],[Banana]
-
FROM (
-
SELECT ct.Name as Cust_Name,ft.name as ft_Name,SUM(ot.mass) AS Mass
-
FROM CT INNER JOIN
-
OT ON CT.ID = OT.Ct_Id INNER JOIN
-
FT ON FT.ID = OT.ft_Id
-
GROUP BY CT.Name,ft.Name
-
) src
-
PIVOT (SUM(Mass) FOR FT_Name
-
IN([Apple],[Apricot],[Banana])) AS pvt
-
Note: make the query as dynamic string to the values dynamicallu
2 2054
use the following query -
SELECT Cust_Name,[Apple],[Apricot],[Banana]
-
FROM (
-
SELECT ct.Name as Cust_Name,ft.name as ft_Name,SUM(ot.mass) AS Mass
-
FROM CT INNER JOIN
-
OT ON CT.ID = OT.Ct_Id INNER JOIN
-
FT ON FT.ID = OT.ft_Id
-
GROUP BY CT.Name,ft.Name
-
) src
-
PIVOT (SUM(Mass) FOR FT_Name
-
IN([Apple],[Apricot],[Banana])) AS pvt
-
Note: make the query as dynamic string to the values dynamicallu
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: thelightkeeper |
last post by:
Hi,
I have 1 table contains about 4 millions entries structure like below:
(
AlarmID int,
SetTime datetime
)
|
by: dbrewerton |
last post by:
The answer I was looking for was staring at me. Disregard this post.
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |