473,395 Members | 2,010 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,395 software developers and data experts.

Inserting a record for each separate aggregate (solved)

Hi,

As I wrote my message the solution came to me, so I thought I would
post anyway for others to see in case it was useful:

Here is some sample DDL for this question:

CREATE TABLE Source (
my_value INT NOT NULL )
GO
INSERT INTO Source VALUES (1)
INSERT INTO Source VALUES (2)
INSERT INTO Source VALUES (3)
GO
CREATE TABLE Destination (
value_type VARCHAR(10) NOT NULL,
value INT )
GO

I would like to fill the destination with a row for the COUNT, SUM,
MIN, and MAX. My own problem is of course much more complex than this,
but this is the basic stumbling block for me now. So, the rows that I
would expect to see in Destination are:

value_type value
---------- -----
COUNT 3
SUM 6
MIN 1
MAX 3

The solution that I came up with was to add a Value_Types table:

CREATE TABLE Value_Types (
value_type VARCHAR(10) NOT NULL )
GO
INSERT INTO Value_Types VALUES ('COUNT')
INSERT INTO Value_Types VALUES ('SUM')
INSERT INTO Value_Types VALUES ('MAX')
INSERT INTO Value_Types VALUES ('MIN')
GO

Now the SQL is pretty simple:

SELECT V.value_type,
CASE V.value_type
WHEN 'COUNT' THEN COUNT(*)
WHEN 'SUM' THEN SUM(S.my_value)
WHEN 'MAX' THEN MAX(S.my_value)
WHEN 'MIN' THEN MIN(S.my_value)
END
FROM Source S
INNER JOIN Value_Types V ON 1=1

-Tom.

P.S. - I know that I did not include primary or foreign keys in my DDL.
I'll leave it as an excercise to the reader to figure those out. I
think the code adequately explains the concept.

Jul 23 '05 #1
3 1910
The final SQL statement should have read:

SELECT V.value_type,
CASE V.value_type
WHEN 'COUNT' THEN COUNT(*)
WHEN 'SUM' THEN SUM(S.my_value)
WHEN 'MAX' THEN MAX(S.my_value)
WHEN 'MIN' THEN MIN(S.my_value)
END
FROM Source S
INNER JOIN Value_Types V ON 1=1
GROUP BY V.value_type

I left out the GROUP BY clause in my original post.

-Tom.

Jul 23 '05 #2
On 19 Apr 2005 09:03:39 -0700, Thomas R. Hummel wrote:

(snip)
FROM Source S
INNER JOIN Value_Types V ON 1=1
GROUP BY V.value_type


Hi Tom,

Since you're effectively doing a cross join, why not eliminate all doubt
and write it explicitly as such:

FROM Source S
CROSS JOIN Value_Types V
GROUP BY V.value_type

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #3
Excellent point.

Thanks!
-Tom.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
I have a table; CREATE TABLE theLiterals ( theKey varchar (255) NOT NULL , theValue varchar (255) NULL ) INSERT INTO theLiterals VALUES('defaultServer','\\MyServer\') INSERT INTO...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
5
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for...
3
by: sms1 | last post by:
My Participants table in Access 2002 contains about 1500 records. The Notes field is OLE. I want to use Insert Object to insert MS Word into the Notes field. It is easily done using: Insert...
5
by: Brian | last post by:
I need help PLEASEEEEEEEEEEEEEEE..... I am trying to create a list of questions that have yes/no answers. There are 8 groups of questions. Based on who the user is that logs in will determine...
1
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO...
0
by: Andy_Khosravi | last post by:
I'm having a problem trying to optimize the performance of one of my A97 databases. I have very slow record navigation after a change I made to the table structure, and I'm not sure how best to...
4
by: QntmPg | last post by:
Hi all, I have read through what I could find on previous questions regarding using the OpenArgs property, but I'm still not able to get my form to open correctly. I'm not sure exactly where the...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.