473,569 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with a query

Suppose I have two tables:

CREATE TABLE Tab1 (
[A1] [int] NOT NULL,
[A2] [int] NOT NULL,
[B1] [int] NOT NULL,
[B2] [int] NOT NULL)

CREATE TABLE Tab2 (
[A1] [int] NOT NULL,
[A2] [int] NOT NULL,
[B1] [int] NOT NULL,
[B2] [int] NOT NULL)

I need to sum B1 and B2 values grouped by A1 and A2 values from Tab1
united with the portion of Tab2 for which the A1 and A2 values do not
exist in Tab1. Is there a nice T-SQL way to put it into one and
possibly small query?
Jul 20 '05 #1
5 1339
On 20 Oct 2004 02:22:58 -0700, Alexander Korovyev wrote:
Suppose I have two tables:

CREATE TABLE Tab1 (
[A1] [int] NOT NULL,
[A2] [int] NOT NULL,
[B1] [int] NOT NULL,
[B2] [int] NOT NULL)

CREATE TABLE Tab2 (
[A1] [int] NOT NULL,
[A2] [int] NOT NULL,
[B1] [int] NOT NULL,
[B2] [int] NOT NULL)

I need to sum B1 and B2 values grouped by A1 and A2 values from Tab1
united with the portion of Tab2 for which the A1 and A2 values do not
exist in Tab1. Is there a nice T-SQL way to put it into one and
possibly small query?


Hi Alexander,

From the narrative, it's hard to understand what you want. Please provide
some illustrative sample data (in the form of INSERT statements, so that I
can copy and paste for testing purposes) and the output you expect for
that sample data.

Also, provide some explanation about the real world problem you're trying
to solve. This problem looks like a homework assignment; if it is I'll
gladly give you some pointers, but not a complete solution. If it's a real
world problem you're facing in your job, I'll be more tempted to give a
complete solution.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
Hugo Kornelis <hugo@pe_NO_rFa ct.in_SPAM_fo> wrote in message news:<r2******* *************** **********@4ax. com>...
On 20 Oct 2004 02:22:58 -0700, Alexander Korovyev wrote:
Suppose I have two tables:

CREATE TABLE Tab1 (
[A1] [int] NOT NULL,
[A2] [int] NOT NULL,
[B1] [int] NOT NULL,
[B2] [int] NOT NULL)

CREATE TABLE Tab2 (
[A1] [int] NOT NULL,
[A2] [int] NOT NULL,
[B1] [int] NOT NULL,
[B2] [int] NOT NULL)

I need to sum B1 and B2 values grouped by A1 and A2 values from Tab1
united with the portion of Tab2 for which the A1 and A2 values do not
exist in Tab1. Is there a nice T-SQL way to put it into one and
possibly small query?


Hi Alexander,

From the narrative, it's hard to understand what you want. Please provide
some illustrative sample data (in the form of INSERT statements, so that I
can copy and paste for testing purposes) and the output you expect for
that sample data.

Also, provide some explanation about the real world problem you're trying
to solve. This problem looks like a homework assignment; if it is I'll
gladly give you some pointers, but not a complete solution. If it's a real
world problem you're facing in your job, I'll be more tempted to give a
complete solution.


Hello Hugo,

Here is my illustrative sample data:

INSERT INTO Tab1 VALUES(0, 0, 10, 20)
INSERT INTO Tab1 VALUES(0, 1, 15, -5)
INSERT INTO Tab1 VALUES(0, 1, 25, 15)
INSERT INTO Tab1 VALUES(1, 0, 35, 10)

INSERT INTO Tab2 VALUES(0, 1, 40, 0)
INSERT INTO Tab2 VALUES(1, 0, 15, 15)
INSERT INTO Tab2 VALUES(1, 1, 25, 30)

The following is what the result should look like:

0, 0, 10, 20
0, 1, 40, 10
1, 0, 35, 10
1, 1, 25, 30

I have come up with this perverted code so far:

SELECT A1, A2, SUM(B1), SUM(B2) FROM (
SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2, 1 as p FROM Tab1
GROUP BY A1, A2
UNION
SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2, 0 as p FROM Tab2
GROUP BY A1, A2
) AS t GROUP BY A1, A2 HAVING SUM(p)=0
UNION SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2 FROM Tab1 GROUP BY
A1, A2 ORDER BY A1, A2

It shows bad performance and looks ugly. There certainly must be a
better (=more concise) way of expressing it. Thank you.

P.S. Since you asked, this is not a homework problem.. but for the
sake of sparing you the full detail (which is long and not very
interesting) it can be considered so :) (i.e. I don't mind pointers
only).
Jul 20 '05 #3
On 20 Oct 2004 12:24:33 -0700, Alexander Korovyev wrote:

(snip)
I have come up with this perverted code so far:

SELECT A1, A2, SUM(B1), SUM(B2) FROM (
SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2, 1 as p FROM Tab1
GROUP BY A1, A2
UNION
SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2, 0 as p FROM Tab2
GROUP BY A1, A2
) AS t GROUP BY A1, A2 HAVING SUM(p)=0
UNION SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2 FROM Tab1 GROUP BY
A1, A2 ORDER BY A1, A2

It shows bad performance and looks ugly. There certainly must be a
better (=more concise) way of expressing it. Thank you.


Hi Alexander,

I've got two alternatives that look better and have a "cleaner" execution
plan, but show worse performance on the limited set of data you posted
here. Try them against your database to see if they start behaving better
when there's more data to work on:

SELECT A1, A2, SUM(B1), SUM(B2)
FROM Tab1
GROUP BY A1, A2
UNION ALL
SELECT A1, A2, SUM(B1), SUM(B2)
FROM Tab2
WHERE NOT EXISTS
(SELECT *
FROM Tab1
WHERE Tab1.A1 = Tab2.A1
AND Tab1.A2 = Tab2.A2)
GROUP BY A1, A2

or

SELECT A1, A2, SUM(B1), SUM(B2)
FROM Tab1
GROUP BY A1, A2
UNION ALL
SELECT Tab2.A1, Tab2.A2, SUM(Tab2.B1), SUM(Tab2.B2)
FROM Tab2
LEFT JOIN Tab1
ON Tab1.A1 = Tab2.A1
AND Tab1.A2 = Tab2.A2
WHERE Tab1.A1 IS NULL
GROUP BY Tab2.A1, Tab2.A2
Another approach gave ma a query that is (in my opinion) not so ugly as
yours, but uglier than my first two, but that gives better performance (on
your 7-row testset) than all others is this one:

SELECT COALESCE(t1.A1, t2.A1), COALESCE(t1.A2, t2.A2),
COALESCE(t1.B1, t2.B1), COALESCE(t1.B2, t2.B2)
FROM (SELECT A1, A2, SUM(B1) AS B1, SUM(B2) AS B2
FROM Tab2
GROUP BY A1, A2) AS t2
FULL JOIN (SELECT A1, A2, SUM(B1) AS B1, SUM(B2) AS B2
FROM Tab1
GROUP BY A1, A2) AS t1
ON t1.A1 = t2.A1
AND t1.A2 = t2.A2

A final hint: if you do keep your version of the query, change UNION to
UNIOAN ALL. It should not change the results, but UNION ALL is generally
faster then UNION (no removal of duplicates needed - might save you a sort
and will definitely save you some processing logic)
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #4
On 20 Oct 2004 12:24:33 -0700, Alexander Korovyev wrote:
It shows bad performance and (...)


Hi Alexander,

Forgot to add this on my previous reply:

If all alternatives perform bad as well, you probably need to have a look
at the indexes available for the tables used in your query.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #5
Hugo Kornelis <hugo@pe_NO_rFa ct.in_SPAM_fo> wrote in message news:<t4******* *************** **********@4ax. com>...
On 20 Oct 2004 12:24:33 -0700, Alexander Korovyev wrote:

(snip)
I have come up with this perverted code so far:

SELECT A1, A2, SUM(B1), SUM(B2) FROM (
SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2, 1 as p FROM Tab1
GROUP BY A1, A2
UNION
SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2, 0 as p FROM Tab2
GROUP BY A1, A2
) AS t GROUP BY A1, A2 HAVING SUM(p)=0
UNION SELECT A1, A2, SUM(B1) as B1, SUM(B2) as B2 FROM Tab1 GROUP BY
A1, A2 ORDER BY A1, A2

It shows bad performance and looks ugly. There certainly must be a
better (=more concise) way of expressing it. Thank you.


Hi Alexander,

I've got two alternatives that look better and have a "cleaner" execution
plan, but show worse performance on the limited set of data you posted
here. Try them against your database to see if they start behaving better
when there's more data to work on:

SELECT A1, A2, SUM(B1), SUM(B2)
FROM Tab1
GROUP BY A1, A2
UNION ALL
SELECT A1, A2, SUM(B1), SUM(B2)
FROM Tab2
WHERE NOT EXISTS
(SELECT *
FROM Tab1
WHERE Tab1.A1 = Tab2.A1
AND Tab1.A2 = Tab2.A2)
GROUP BY A1, A2

or

SELECT A1, A2, SUM(B1), SUM(B2)
FROM Tab1
GROUP BY A1, A2
UNION ALL
SELECT Tab2.A1, Tab2.A2, SUM(Tab2.B1), SUM(Tab2.B2)
FROM Tab2
LEFT JOIN Tab1
ON Tab1.A1 = Tab2.A1
AND Tab1.A2 = Tab2.A2
WHERE Tab1.A1 IS NULL
GROUP BY Tab2.A1, Tab2.A2
Another approach gave ma a query that is (in my opinion) not so ugly as
yours, but uglier than my first two, but that gives better performance (on
your 7-row testset) than all others is this one:

SELECT COALESCE(t1.A1, t2.A1), COALESCE(t1.A2, t2.A2),
COALESCE(t1.B1, t2.B1), COALESCE(t1.B2, t2.B2)
FROM (SELECT A1, A2, SUM(B1) AS B1, SUM(B2) AS B2
FROM Tab2
GROUP BY A1, A2) AS t2
FULL JOIN (SELECT A1, A2, SUM(B1) AS B1, SUM(B2) AS B2
FROM Tab1
GROUP BY A1, A2) AS t1
ON t1.A1 = t2.A1
AND t1.A2 = t2.A2

A final hint: if you do keep your version of the query, change UNION to
UNIOAN ALL. It should not change the results, but UNION ALL is generally
faster then UNION (no removal of duplicates needed - might save you a sort
and will definitely save you some processing logic)
Best, Hugo


Thanks a lot!
Jul 20 '05 #6

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

Similar topics

2
3040
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought...
9
3115
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be...
6
2418
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18. I need a query that will return the c date for TypeID 18 if it exist else the date for TypeID 1, for all jobs. the table structure is the...
3
1853
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype, answer) They are related by quesnum and questype. There are records in
7
2360
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller...
3
10621
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays...
0
2247
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script...
10
2562
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only tool you know how to use is a hammer, every problem tends to look like a nail. That said, I could solve my problem in C, but it's not the right...
7
2014
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First has about 30 fields. Every entry in this table will be unique. Second table has about 7 fields and is for reference - strictly a look up type table. I...
3
2547
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks like the following: <a href="?searchtype=2">Community</a>
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.