Query database for multiple columns from a key value pair table - c#

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
For simplicity I don't join other tables, but I hope it gives you an idea how to turn rows into columns.

Related

Most efficient way to query for multiple rows and their specific sub-items (e.g. A `Sale` table and its `Items`)

I have the following tables:
Sales Table:
+----+------------+
| ID | Date |
+----+------------+
| 1 | 11/20/2018 |
| 2 | 11/21/2018 |
+----+------------+
Items Table:
+----+------------+----------+-------+----------+
| ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----------+-------+----------+
| 1 | 1 | 5 | 100 | 500 |
| 2 | 1 | 3 | 50 | 150 |
| 3 | 2 | 5 | 60 | 300 |
+----+------------+----------+-------+----------+
Currently, I query for the Sale rows and then run another query for each to retrieve all of its Items after which, I initialize them as objects in my C# Program. My problem is that I frequently run into scenarios where I'm dealing with hundreds of sales at the same time. Because of this, I have to run hundreds of queries as well.
My question is, is there a more efficient way to query for all Sales and their Items or is this it? I thought of trying a query like
SELECT *
FROM items_table
WHERE FK_Sale_ID = '1' OR FK_Sale_ID = '2'
And then manually sorting which Item belonged to which sale for initialization but this query quickly got messy after dealing with more than a few sales. Any ideas?
You can use join clause to combine multiple tables data in your query:
SELECT *
FROM items_table
JOIN sales_table ON items_table.FK_Sale_ID = sales_table.ID
WHERE sales_table.Date = #somedate
With this query when #somedate = 11/20/2018 you basically will get this data in one go:
+----+------------+----+------------+----------+-------+----------+
| ID | Date | ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----+------------+----------+-------+----------+
| 1 | 11/20/2018 | 1 | 1 | 5 | 100 | 500 |
| 1 | 11/20/2018 | 2 | 1 | 3 | 50 | 150 |
+----+------------+----+------------+----------+-------+----------+
Use SELECT items_table.* to get all fields of items table only

SQL Get matching values from other table and show in main table

I'm trying to do some SQL in C# along with an Access Database. I have two tables. Main Table and a Second Table:
MAIN TABLE
DoubleValue | | | |
----------------------------------------
1,40 | | | |
1,80 | | | |
2,00 | | | |
1,80 | | | |
1,60 | | | |
1,60 | | | |
----------------------------------------
SECOND TABLE
DoubleValue | Points | Weight |
-------------------------------
1,00 | 100 | 2 |
1,20 | 98 | 2 |
1,40 | 96 | 2 |
1,60 | 94 | 2 |
1,80 | 92 | 2 |
2,00 | 90 | 2 |
-------------------------------
I need to find all matching rows in "SECOND TABLE" based on the column "Double Value". Then for the rows that matches I want to get the value in columns "Points" and "Weight" as well as multplie those two columns and create a columns with the name "Sum" and add all three columns to the "MAIN TABLE":
MAIN TABLE - RESULT/OUTPUT
DoubleValue | Points | Weight | Sum |
-------------------------------------
1,40 | 96 | 2 | 192 |
1,80 | 92 | 2 | 184 |
2,00 | 90 | 2 | 180 |
1,80 | 92 | 2 | 184 |
1,60 | 94 | 2 | 188 |
1,60 | 94 | 2 | 188 |
-------------------------------------
The "MAIN TABLE" doesn't need to actually have the new columns "physically" inserted. I would very much prefer if they could just be displayed in the output very much like "SELECT Points * Weight AS Sum" would produce where "Sum" would be displayed but not actually inserted in the table. BUT OK, if it needs to actually be inserted then I will go with that.
How can this be done?
You are looking for a simpler INNER JOIN statement. Please notice that Sum is wrapped as it is a Reserved word in most SQL variants. Please try to avoid naming items with these words.
SELECT m.DoubleValue
, s.Points
, s.Weight
, [Sum] = s.Points * s.Weight
FROM MainTable AS m
INNER JOIN SecondTable AS s ON m.DoubleValue = s.DoubleValue

How to solve null value in MySQL?

I want to show data separately from income and expense table in MySQL using same query for both things but it show me when show income title date then same show expense title date. But I need to do if it find income table's 4th row and if find expense table's 2nd row, but it show me 4th row. Why?
SELECT income.date,
IF(income.income_sourch IS NULL, 'N/A', income.income_sourch )as 'Income Title',
income.amount as 'income',
IF(expense.expense_sourche IS NULL, 'N/A', expense.expense_sourche) as 'ExpenseTitle',
expense.amount as 'Expense'
from expense RIGHT JOIN income
ON expense.date=income.date
where expense.date='30-Aug-2016'
Why - because that's how joins work when you have only a date to join the 2 tables. To solve this you need to create a relationship in addition to the date bearing in mind that at any one time the number of records in income could be greater than, less than or equal to the number of records in expenses. If we generate a row number for each row in income and independently generate a row number for each row in expenses then we can join income and expenses on row number
for example
/*
CREATE TABLE INCOME (ID INT,DESCRIPTION VARCHAR(10),DT DATE,AMOUNT INT);
INSERT INTO INCOME VALUES
(1,'SALE','2016-08-30',100),
(2,'ROKY','2016-08-30',200),
(3,'KORIM','2016-08-30',300),
(4,'SALEVOUCHR','2016-08-30',400);
CREATE TABLE EXPENSES (ID INT,DESCRIPTION VARCHAR(10),DT DATE,AMOUNT INT);
TRUNCATE TABLE EXPENSES;
INSERT INTO EXPENSES VALUES
(1,'RENT','2016-08-30',100),
(2,'UTILITIES','2016-08-30',100);
/*
This code
SELECT S.DT,S.DESCRIPTION,S.AMOUNT, s.rn,T.DESCRIPTION,T.AMOUNT, t.rn1
FROM
(
SELECT I.DT,I.DESCRIPTION,I.AMOUNT,
#RN:=#RN + 1 RN
FROM (SELECT #RN:=0) RN,INCOME I
) S
LEFT OUTER JOIN
(SELECT E.DT,E.DESCRIPTION,E.AMOUNT,
#RN1:=#RN1 + 1 RN1
FROM (SELECT #RN1:=0) RN1, EXPENSES E
) T ON T.RN1 = S.RN AND T.DT = S.DT
WHERE S.DT IS NOT NULL
results in
+------------+-------------+--------+------+-------------+--------+------+
| DT | DESCRIPTION | AMOUNT | rn | DESCRIPTION | AMOUNT | rn1 |
+------------+-------------+--------+------+-------------+--------+------+
| 2016-08-30 | SALE | 100 | 1 | RENT | 100 | 1 |
| 2016-08-30 | ROKY | 200 | 2 | UTILITIES | 100 | 2 |
| 2016-08-30 | KORIM | 300 | 3 | NULL | NULL | NULL |
| 2016-08-30 | SALEVOUCHR | 400 | 4 | NULL | NULL | NULL |
+------------+-------------+--------+------+-------------+--------+------+
BUT this is UNSAFE because it assumes that there are always more income than expense items
for example if we add 3 more expense rows
TRUNCATE TABLE EXPENSES;
INSERT INTO EXPENSES VALUES
(1,'RENT','2016-08-30',100),
(2,'UTILITIES','2016-08-30',100),
(3,'RATES','2016-08-30',100),(4,'SALARY','2016-08-30',100),(5,'INSURANCE','2016-08-30',100);
then the last expense item is lost resulting in
+------------+-------------+--------+------+-------------+--------+------+
| DT | DESCRIPTION | AMOUNT | rn | DESCRIPTION | AMOUNT | rn1 |
+------------+-------------+--------+------+-------------+--------+------+
| 2016-08-30 | SALE | 100 | 1 | RENT | 100 | 1 |
| 2016-08-30 | ROKY | 200 | 2 | UTILITIES | 100 | 2 |
| 2016-08-30 | KORIM | 300 | 3 | RENT | 100 | 3 |
| 2016-08-30 | SALEVOUCHR | 400 | 4 | UTILITIES | 100 | 4 |
+------------+-------------+--------+------+-------------+--------+------+
To cope with this we can simulate a full join like so
SELECT S.*,T.*
FROM
(
SELECT I.DT,I.DESCRIPTION,I.AMOUNT,
#RN:=#RN + 1 RN
FROM (SELECT #RN:=0) RN,INCOME I
) S
LEFT JOIN
(SELECT E.DT,E.DESCRIPTION,E.AMOUNT,
#RN1:=#RN1 + 1 RN1
FROM (SELECT #RN1:=0) RN1, EXPENSES E
) T ON T.RN1 = S.RN AND T.DT = S.DT
UNION
SELECT S.*,T.*
FROM
(
SELECT I.DT,I.DESCRIPTION,I.AMOUNT,
#RN3:=#RN3 + 1 RN3
FROM (SELECT #RN3:=0) RN3,INCOME I
) S
RIGHT JOIN
(SELECT E.DT,E.DESCRIPTION,E.AMOUNT,
#RN4:=#RN4 + 1 RN4
FROM (SELECT #RN4:=0) RN4, EXPENSES E
) T ON T.RN4 = S.RN3 AND T.DT = S.DT
result
+------------+-------------+--------+------+------------+-------------+--------+------+
| DT | DESCRIPTION | AMOUNT | RN | DT | DESCRIPTION | AMOUNT | RN1 |
+------------+-------------+--------+------+------------+-------------+--------+------+
| 2016-08-30 | SALE | 100 | 1 | 2016-08-30 | RENT | 100 | 1 |
| 2016-08-30 | ROKY | 200 | 2 | 2016-08-30 | UTILITIES | 100 | 2 |
| 2016-08-30 | KORIM | 300 | 3 | 2016-08-30 | RATES | 100 | 3 |
| 2016-08-30 | SALEVOUCHR | 400 | 4 | 2016-08-30 | SALARY | 100 | 4 |
| NULL | NULL | NULL | NULL | 2016-08-30 | INSURANCE | 100 | 5 |
+------------+-------------+--------+------+------------+-------------+--------+------+
you just need to add limits inside the query
if expense table limit 2
if other limit 4
See how to add limit to the query with if condition

MySQL Subquery Display in UPPERCASE

[DONE] for those who need this.. I will edit this and put the answer below..
How to make LName and FName all CAPS using subquery?
I already make all LName and FName CAPS but not in subquery using this code
Select UPPER(LName) AS "Lastname", UPPER(FName) AS "Firstname" FROM Trainers;
OUTPUT:
+-----------+-----------+
| Lastname | Firstname |
+-----------+-----------+
| MONTANO | GERALD |
| ALEJANDRO | LEONARD |
| MONTEZ | GARY |
| GUILLERMO | JACKIE |
| SANCHEZ | VERNA |
| GABORNI | MICHELLE |
+-----------+-----------+
Table Trainers
+-----------+-----------+----------+-------+
| TrainerNo | LName | FName | Rate |
+-----------+-----------+----------+-------+
| 00804871 | Montano | Gerald | 200 |
| 00807461 | Alejandro | Leonard | 175 |
| 00807462 | Montez | Gary | 150 |
| 00823253 | Guillermo | Jackie | 200 |
| 00827320 | Sanchez | Verna | 125 |
| 00845907 | Gaborni | Michelle | 150 |
+-----------+-----------+----------+-------+
ANSWER:
Select UPPER(Lname) AS "Lastname", UPPER(FName) AS "Firstname" from Trainers Where Rate <= (Select Max(Rate) from Trainers);
IF you want to return the first name concatenated with the second name in upper case in a sub-query. Then you can do this.
Select * from Table Name, (Select UPPER(LName) + UPPER(FName) FROM Trainers) as Name

Creating editable table from multiple sql database tables in ASP.Net

I have a problem with displaying and editing some data from database to ASP.Net webpage.
Data from database consist from few different tables:
Table Files, witch contains file_id and a file name
| file_id | file_name |
--------------------------
| GUID | VARCHAR |
file is saved in database as a collection of lines for one file_id,
Table Items,witch contains basically all the lines from all the files
| item_id | file_id | step | number |
---------------------------------------------------
| GUID | VARCHAR | BIGINT | BIGINT |
Table Wording, witch contains all distinctive wording
| wording_id | wording_eng | description |
-----------------------------------------
| GUID | VARCHAR | VARCHAR |
And M_N_table witch contains connection between items and wording
| item_id | wording_id |
----------------------------
| GUID | GUID |
my final display table should look like this and should be editable
| | file1 | file2 | file3 |.........| fileN | | |
| sortorder --------------------------------------------------------------------------| wording_eng | desription |
| | step | number | step | number | step | number |.........| step | number | | |
------------------------------------------------------------------------------------------------------------------
| 1 | 10 | 15 | | | | |.........| | | something1 | something2 |
------------------------------------------------------------------------------------------------------------------
| 2 | | | | | 25 | 15 |.........| 5 | 17 | something3 | something4 |
------------------------------------------------------------------------------------------------------------------
| 3 | 2 | 4 | 5 | 16 | 17 | 3 |.........| 1 | 15 | something5 | something6 |
So for every file from some selected list,witch means variable number of files, I need to read all the wordings and if a wording is connected to item in M_N_table and that item exist in selected files I have to write value of step and number to that files column as shown, if they don't exist it will be written blank value.
Problem I'm having is creating Table or DataList or GridView where I can have connection to item_id and wording_id when I'm editing data. I managed to create "pivot" datatable in C# in backcode, using all of these tables and created gridview with dynamic number of columns but problem is I don't have information about what item_id I'm actually editing it just goes back to changing value in datatable.
What is the best way to display this data? I tried making dynamic pivot table in SQL to display it directly with gridview but without aggregation it's impossible,so I had to do it with datatable's in C# back-code. And how can I make it so that when I edit data in displayed table I get the same changes in database?

Categories