简体   繁体   中英

Query database for multiple columns from a key value pair table

I am working on defining a sql query for use in sql server compact edition 3.5 on a windows mobile handset. I am going to need to get back a result set from three tables.

I don't exactly remember all the column names, as I'm asking this question at home, but here is a good example of the tables I'm dealing with.

Table 1: Customers
Table 2: PresoldOrders
Table 3: CustomerDetails

*

 ________________________________________
|                                        |
|--------------- Customers --------------|
|________________________________________|
|                                        |
| PK    int            CustomerNumber    |
|       varchar(125)   FirstName         |
|       varchar(125)   LastName          |
|       varchar(125)   Email             |
|       varchar(200)   Address1          |
|       varchar(200)   Address2          |
|       varchar(200)   City              |
|       varchar(2)     State             |
|       varchar(5)     Zip               |
|________________________________________|

*

 ________________________________________
|                                        |
|------------ CustomerDetails -----------|
|________________________________________|
|                                        |
| PK    int            CustomerDetailsId |
| FK    int            CustomerNumber    |
|       varchar(255)   FieldName         |
|       varchar(255)   FieldValue        |
|________________________________________|

*

 ________________________________________
|                                        |
|------------ PresoldOrders -------------|
|________________________________________|
|                                        |
| PK    int            PresoldOrderId    |
| FK    int            CustomerNumber    |
|       int            OrderNumber       |
|       int            RouteStopNumber   |
|       datetime       DeliveryDate      |
|       varchar(100)   Other1            |
|       varchar(100)   Other2            |
|________________________________________| 

Now, the query should return all records that exist in customers even if they don't exist in 'PresoldOrderHeaders' table. This part of it is pretty easy, I plan to just use a left outer join. The second part of the query is a bit more complex.

Here is the query I've constructed so far.

SELECT c.CustomerNumber
       c.FirstName
       c.LastName
       c.Email
       c.Address1
       c.Address2
       c.City
       c.State
       c.Zip
       po.OrderNumber
       po.DeliveryDate
       po.Other1
       po.Other2
FROM Customer c
LEFT OUTER JOIN PresoldOrders po on c.CustomerNumber = po.CustomerNumber
ORDER BY po.RouteStopNumber; 

Tricky part is the CustomerDetails table. Here is an example of some data

 _________________________________________________________
|       |                 |              |                |
| PK    | CustomerNumber  | FieldName    | FieldValue     |
|-------|-----------------|--------------|----------------|
| 1     | 1               | A            | 125            |
|-------|-----------------|--------------|----------------|
| 2     | 1               | B            | 126            |
|-------|-----------------|--------------|----------------|
| 3     | 1               | C            | 127            |
|-------|-----------------|--------------|----------------|
| 4     | 2               | A            | 138            |
|-------|-----------------|--------------|----------------|
| 5     | 2               | B            | 140            |
|-------|-----------------|--------------|----------------|
| 6     | 2               | C            | 143            |
|-------|-----------------|--------------|----------------|
|_________________________________________________________|

For the information that I will be displaying in the Component One Flex Grid, the FieldName's listed in the CustomerDetails table will be fixed.

Here is want I want to archive:

 _____________________________________________________________________________________________________________________
|                 |           |          |     |                     |                     |                          |
| CustomerNumber  | FirstName | LastName | ... | FieldName A's value | FieldName B's Value | FieldName C's Value      |
|-----------------|-----------|----------|-----|---------------------|---------------------|--------------------------|
| 1               | John      | Someone  | ... | 125                 | 126                 | 127                      |
|-----------------|-----------|----------|-----|---------------------|---------------------|--------------------------|
| 2               | Dan       | Other    | ... | 138                 | 140                 | 143                      |
|-----------------|-----------|----------|-----|---------------------|---------------------|--------------------------|
|_____________________________________________________________________________________________________________________|

Normally, I'd have column names for A, B, and C defined in the 'CustomerDetails' table; however, this table can't be changed, so I must work with what I've been given. The requirements in the spec for my task is to have 15 plus columns to be displayed in a grid on a mobile device; not something I'd go for but those are the requirements.

Ok finally, the question:

Can one use sql to query a key value pairing table and have those key's value's displayed in columns like the above? This is the requirement I have and I'm thinking I'll need to create one query with my join on presoldorders table and then get a list of all details for each customer in a list and iterate through and combine into data table in code on handheld.

If you know in advance all key values, you can pivot resultset. I'm not sure if sql server compact supports PIVOT , but you can do: select CustomerNumber,
Max(Case when FieldName='A' then FieldValue end) as a_value,
// the same for B, C, all keys.
From CustomerDetails
Group by CustomerNumber
select CustomerNumber,
Max(Case when FieldName='A' then FieldValue end) as a_value,
// the same for B, C, all keys.
From CustomerDetails
Group by CustomerNumber

For simplicity I don't join other tables, but I hope it gives you an idea how to turn rows into columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM