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

Combine multiple rows into 1

aas4mis
97
I have a table with a deviceid, attribute type, and attribute value. There are many attributes for a single device id. I want my query to retrieve a single device id with multiple attribute values. Here's what I have so far.
Expand|Select|Wrap|Line Numbers
  1. SELECT devId,
  2. (CASE WHEN daName = 'userAttribute.assetname' THEN daValue END) AS assetname,
  3. (CASE WHEN daName = 'userAttribute.adapter.pccardx5cphoton1.ipaddr' THEN daValue END) AS ipaddr
  4. FROM tDeviceAttributes
  5. WHERE daName='userAttribute.assetname' or daName='userAttribute.adapter.pccardx5cphoton1.ipaddr'
  6. GROUP BY devId, daName, daValue
  7. ORDER BY devID
  8.  
This gives me a row for each attribute value.. not good.

current output:
devid assetname ipaddr
0006a7019547b6f138000050bf7a60e2 NULL 172.23.152.201
0006a7019547b6f138000050bf7a60e2 104178 NULL
0046ae019447db01a8000050bf7a60e2 104115 NULL
0146ae01934726f178000050bf7a60e2 NULL 172.23.152.207
0146ae01934726f178000050bf7a60e2 104108 NULL
0146ae01934788e178000050bf7a60e2 NULL 172.23.152.191

needs to be:
devId assetname ipaddr
0006a7019547b6f138000050bf7a60e2 104178 172.23.152.201
0046ae019447db01a8000050bf7a60e2 104115 172.23.152.207
0146ae01934726f178000050bf7a60e2 104108 172.23.152.191

Help would be appreciated.
Jul 1 '10 #1
10 2374
gpl
152 100+
@aas4mis
This is very nearly impossible, it would be ugly and slow

Could you normalise your database, it will be a lot easier on you
Jul 1 '10 #2
ck9663
2,878 Expert 2GB
Here, read this. As you can see, you're going to need a key that can be used for sorting. But you should have some idea on how it's done.

Happy Coding!!!

~~ CK
Jul 1 '10 #3
aas4mis
97
@gpl
No, unable to normalize. This is not my DB, actually designed by motorola (and works for their purposes). I'm making a custom web page to interact with the database and need this query for a stored procedure. I don't see where this would be slow as there are less than 7,000 total records in this table. Surely there must be a clean way to write this. ... is there are Shirley in the building???
Jul 1 '10 #4
aas4mis
97
@ck9663
Thanks ck9663, but I don't think this is the correct route. The suggested query relies on the previous row, regardless of identifier (in my case devId). Should a device be missing an attribute the next device will receive the wrong value. I'm thinking this would best be solved with a self join, but not sure how to implement it.
Jul 1 '10 #5
ck9663
2,878 Expert 2GB
Why did you join this two rows:

0046ae019447db01a8000050bf7a60e2 104115 NULL
0146ae01934726f178000050bf7a60e2 NULL 172.23.152.207

Into one row:
0046ae019447db01a8000050bf7a60e2 104115 172.23.152.207

What's your logic in consolidating these rows?

~~ CK
Jul 1 '10 #6
aas4mis
97
@ck9663
Oops, my mistake. Typo, I just copy/pasted the "needs to be" table, only the first row is correct. But this illustrates a good point, the suggested query would've done the same thing, since it's ordered by devId but apparently a couple attributes are MIA.
Jul 1 '10 #7
ck9663
2,878 Expert 2GB
Yes. As I mentioned, you're going to need something to sort your table with. If you just want it to run through the table picking the value from the next row, use ROW_NUMBER() to have a sequential record/row number that you can use.

Good Luck!!!

~~ CK
Jul 1 '10 #8
aas4mis
97
@ck9663
There's still the issue of missing attributes. Say devid 1 has ipaddr and assetname, devid 2 has assetname, devid3 has assetname.. won't devid 2 and devid 3 now have devid 1's ipaddr? sorted by devid that is.
Jul 1 '10 #9
ck9663
2,878 Expert 2GB
Use the PARTIOTION BY clause of the ROW_NUMBER() to restart numbering. It means you'll have a sequence of number per DEVID. If the DEVID change, it'll restart the numbering to 1...and so on...

Good Luck!!!

~~ CK
Jul 1 '10 #10
aas4mis
97
Thanks for the suggestion CK, but I believe I have this one whipped.

Expand|Select|Wrap|Line Numbers
  1. SELECT t1.devId, t1.daValue as assetname, t2.daValue as ipaddr
  2. FROM tDeviceAttributes t1
  3. INNER JOIN tDeviceAttributes t2 on t1.devId = t2.devId
  4. WHERE t1.daName='userAttribute.assetname' and t2.daName='userAttribute.adapter.pccardx5cphoton1.ipaddr'
  5. ORDER BY assetname
  6.  
results:
devid assetname ipaddr
0992a301b1474be178000050bf7a60e2 103189 172.23.223.87
1b26a701944709d118000050bf7a60e2 104102 172.23.152.212
19039601854796e128000050bf7a60e2 104103 172.23.152.206
1d46ae019447fc91b8000050bf7a60e2 104104 172.23.152.228
0246b301824755c1e8000050bf7a60e2 104105 172.23.152.223
2346ae019347d6f178000050bf7a60e2 104106 172.23.152.252
0a86a00198474ee1c8000050bf7a60e2 104107 172.23.152.210
0146ae01934726f178000050bf7a60e2 104108 172.23.152.207
0546ae019347b7a178000050bf7a60e2 104109 172.23.152.242
1b46ae01944777e128000050bf7a60e2 104110 172.23.152.219
1846ae019447741198000050bf7a60e2 104111 172.23.152.221
1106a70195477fb138000050bf7a60e2 104112 172.23.152.209
0846ae019447d321a8000050bf7a60e2 104113 172.23.152.142
1f46ae019447ae4198000050bf7a60e2 104114 172.23.152.216
0746ae01944735e198000050bf7a60e2 104116 172.23.152.240

Verified results, Good to go.
Jul 1 '10 #11

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

Similar topics

1
by: Valerie | last post by:
Hi everyone, I really appreciate if anyone could help me with this tricky problem that I'm having. I'm looking for a sample script to combine data in multiple rows into one row. I'm using...
5
by: Antanas | last post by:
Is it possible to combine multiple rows returned from select statement into one row? SELECT NAME FROM TABLE1; I want all names to be combined into one row seperated by commas.
4
by: musicloverlch | last post by:
I have a table with multiple records in it. I am being forced to combine multiple records into one record in order to be uploaded to a website, and I only get one record per client. How can I...
4
by: Iprogramforlawyers | last post by:
I need to take any amount of rows and turn them into one single row. I'm exporting some data from sql. The program its going into only has one note field. I've used UNION and those types of...
7
by: Mintyman | last post by:
Hi, I'm working on a system migration and I need to combine data from multiple rows (with the same ID) into one comma separated string. This is how the data is at the moment: Company_ID ...
18
by: Apple001 | last post by:
Hi all! I am having trouble with joining multiple rows into one row. I will appreciate any help. For columns in the query, I have: invID(autot number), entryDate, invDate, vendor, invoiceAmount...
8
by: Mike Reid | last post by:
I have designed a Database that will import data and assign values for ordering material. However I am having problems as I currently have to drop this data into an Excel sheet and apply a Pivot...
2
by: kagweh | last post by:
Am new to Access so go easy Each serial number has several comments made for it and what I need is to combine all comments for each serial into one row separated by a comma or l. Now here are...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
3
by: klbachrodt | last post by:
Hi all - I've been browsing this forum for a while now, looking for help, and finally decided to join and ask my question, since I'm not terribly good at writing SQL queries in Access. Here is my...
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
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,...
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
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...
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.