473,386 Members | 1,630 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,386 software developers and data experts.

Merging 2 columns from the same table

Hi All,

This is what I'm trying to accomplish:

I have a table T1 with the following fields
- [UID]
- [OrigUID]
- [Type] (where type can be either B or C)

| UID | OrigUID | Type |
+-----+---------+------+
U1 | | B |
U1 | U1 | C |
U3 | U1 | C |
U3 | U2 | C |
U3 | U2 | C |

What I want to do is to get:
- the count of B in [Type] as [B count]for each distinct [UID]
- the count of C in [Type] as [C count]for each distinct [OrigUID]
- [All UID] where it combines the distinct [UID] and distinct [OrigUID]

| All UID | B count | C Count |
+---------+---------+---------+
U1 | 1 | 1 |
U2 | 0 | 2 |
U3 | 0 | 0 |

What I have coded right now is:
SELECT T1.[UID],
(Select Count(*) FROM T1 AS test WHERE (test.[Type]='B') and
(test.[UID] = T1.[UID])) AS [B Count],
(SELECT Count(*) FROM T1 AS test1 WHERE (test1.[Type]='C') and
(test1.[origUID] = T1.[origUID])) AS [C Count]
FROM Table
GROUP BY T1.[UID], T1.[origUID];

The results I got:
| All UID | B count | C Count |
+---------+---------+---------+
U1 | 1 | 0 |
U1 | 1 | 2 |
U3 | 0 | 2 |
U3 | 0 | 2 |

Please advise! TIA!

regards,

Nov 13 '05 #1
1 1587
be********@gmail.com wrote:
Hi All,

This is what I'm trying to accomplish:

I have a table T1 with the following fields
- [UID]
- [OrigUID]
- [Type] (where type can be either B or C)

| UID | OrigUID | Type |
+-----+---------+------+
U1 | | B |
U1 | U1 | C |
U3 | U1 | C |
U3 | U2 | C |
U3 | U2 | C |

What I want to do is to get:
- the count of B in [Type] as [B count]for each distinct [UID]
- the count of C in [Type] as [C count]for each distinct [OrigUID]
- [All UID] where it combines the distinct [UID] and distinct [OrigUID]

| All UID | B count | C Count |
+---------+---------+---------+
U1 | 1 | 1 |
U2 | 0 | 2 |
U3 | 0 | 0 |

What I have coded right now is:
SELECT T1.[UID],
(Select Count(*) FROM T1 AS test WHERE (test.[Type]='B') and
(test.[UID] = T1.[UID])) AS [B Count],
(SELECT Count(*) FROM T1 AS test1 WHERE (test1.[Type]='C') and
(test1.[origUID] = T1.[origUID])) AS [C Count]
FROM Table
GROUP BY T1.[UID], T1.[origUID];

The results I got:
| All UID | B count | C Count |
+---------+---------+---------+
U1 | 1 | 0 |
U1 | 1 | 2 |
U3 | 0 | 2 |
U3 | 0 | 2 |

Please advise! TIA!

regards,

create a query like this:

select uid, type, sum(countoftype)
from (
select uid, type, count(type) as countoftype
from mytable
union all
select origuid, type, count(type) as countoftype
from mytable)
....and then base a cross-tab query on it, with uid as a Row Value, type
as the Column value, and the 3rd column as the Value...
Nov 13 '05 #2

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

Similar topics

0
by: Marc Floessel | last post by:
Considering the following example table: Column1 | Column2 | Column3 =========================== NULL |"Value1" | NULL --------------------------- "Value2"| NULL | NULL...
1
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word...
2
by: Dan Cooper | last post by:
I've got two datasets, each containing a single data table. dstDataSetA.Tables("TableA") dstDataSetB.Tables("TableB") I want to merge them together and delete any non-matching rows. ...
1
by: John | last post by:
Hi I have two tables with two columns each. Each table has an id column and a value column. I would like the two tables to be merged so there are three columns id, valuefromtable1 and...
6
by: John | last post by:
Hi I have three tables with a common id with which they can be linked. I need to merge them in a way that the resultant table has all records from three tables. Below is what sort of result I am...
4
by: hunter1978 | last post by:
Hi, I hope somebody can help me? I've got a macro that pulls data from an Excel sheet into a table. Everything works okay but I need some way of merging the data in two columns together so that...
1
by: keveen | last post by:
Can someone tell me how I can import tables from another non-Joomla mysql file into Joomla? Basically it is just from one mySQL database into another. I use phpMyAdmin to import and export the entire...
0
by: gimme_this_gimme_that | last post by:
I have a table A with the following columns: A.id A.parent_id A.z_id A.z_parent_id A.schema The values of A.parent_id are currently null and require assigning.
6
by: dannylam4 | last post by:
Hello, I've got a question about merging/concatenating rows. There's a similar topic here: Combining Multiple Rows of one Field into One Result but I didn't know if I should hijack it. Basically, I...
2
by: Neil Chambers | last post by:
I am trying to get my head around dataset merging but despite a little research I could still use a pointer (or ten). Basically I want to perform an outer join operation on a dataset (created from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...

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.