How to Show Two table columns in a aspxgridview? - asp.net

I have tow tables in mssql server, Car & Weight.
Car has IDCar, Name, Username, Description columns.
And Weight has IDWeight, CarID, Name, Date, Weight columns.
I want to show Name of Car table in the first column of aspx gridview and show weight and date from Weight Table.
The Name is static, but everyday user must input date and weight for each car.
I wish it was clear about it!

Write a query with the join with both tables and get the desired result and then bind that result to the gridview. Your query can be like,
Select c.Name,w.Weight,w.Date
FROM
Car c LEFT Join Weight w
ON c.CarID = w.CarID
UPDATE
Applied left join after the comment.Left Join will give you all the car names from the Car table irrespective of if there are data present in the Weight table for that car or not.
To use the Where in join just use it as normal where clause.
WHERE
w.Date = GETDATE() //Change according your need
AND w.Weight = 110 //Change according your need
To read more about join
To bind datatable to the Gridview read here

select car.Name,weigth.weight,weigth.date from car
inner join weight on car.carid=weigth.carid
you can add a filter to specifiy which item you want using where clause:
where carid in ('xx','123','guid') and weight.weight between 100 and 200

Related

Oracle Apex Select List LOV conditional query

I have an Editable Interactive Grid (for product_sale table) with a Select List (to select a product) in Oracle Apex 20.2 app.
Currently I'm using below query to populate this list of values.
SELECT productName,productId FROM product WHERE productAvailability = 'Y'
There are a few products that I need to set the productAvailability as 'N'. When I made this change, Interactive Grid of product_sale shows productId instead of the productName.
What I need to achieve is, only show products with productAvailability = 'Y' for new records (when going to add new record in to the table by clicking Add Row button) and for the old records show the productName of the selected product regardless the productAvailability.
Table Structure
Sample Data
Interactive Grid View
How could I achieve this?
You can achieve this using the "cascading list of values" option in the column settings. Let me illustrate with an example on the EMP sample table - you should be able to translate this to your own code:
Situation: BLAKE can not be selected as manager for any other employees than the one he is already manager for.
This would be the select:
SELECT ename, empno FROM emp WHERE ename != 'BLAKE'
As expected, for records that have BLAKE as manager, the MGR column will display the id because the value is not in the result set of the select list query. This is the behaviour you are seeing.
The solution has 2 steps:
Union the query with a query that has the value of the current row. I have rewritten the query using a CTE.
WITH all_mgr(dv, rv) AS
(SELECT ename, empno FROM emp WHERE ename != 'BLAKE'
UNION
SELECT m.ename, m.empno
FROM emp e
JOIN emp m ON e.mgr = m.empno
WHERE e.ename = :ENAME
)
SELECT dv, rv FROM all_mgr
Make sure column ename from the current row is bind to :ENAME. In "Cascading List Of Values" set both "Parent Column" and "Items to Submit" to ENAME. Now the select list will take the current row value for :ENAME and the list of values will include 'BLAKE' for users that have 'BLAKE' as a manager.

Delete only one column from the Target table even if there are multiple similar columns (Teradata)

I came across a situation where i have to delete a column from a table based on a condition from other table
Let me break it down to you!
There is a master table called MORTALITY (containing info regarding deceased individuals)
And another table called INC_MORTALITY (incremental mortality) table which is refreshed on a weekly basis
Note: Both the tables have similar format
So this week’s new records, containing both additional deceased individuals as well as updates of old data for previously delivered records. This is a single file with a column (OP_DIRECTIVE) specifying if it is an “add” or “delete” record.
Processing Weekly Files
To incorporate the weekly update file, we need to execute the following steps in order.
1. Delete rows in the master table which have a OP_DIRECTIVE = 'D' as the operation in the weekly update. For a given delete row, you should delete a single row in the master table which matches the delete record on all fields aside from the “D” operation column. Warning: please ensure you only delete, or mark as deleted, one record, even if more than one historical record fully matches this new delete record.
2. Add rows in the master table which appear in the “Add” file.
Upon completion of these steps, your master table should be the most up to date master of deaths.

(Note: THESE TABLES DOES NOT HAVE PRIMARY KEYS)
SO WHAT I TRIED:
DEL FROM MORTALITY MI
WHERE MI.DATA_SOURCE = INC_MORTALITY.DATA_SOURCE
AND MI.DD_IMP_FLAG = INC_MORTALITY.DD_IMP_FLAG
AND MI.DOB = INC_MORTALITY.DOB
AND MI.DOD = INC_MORTALITY.DOD
AND MI.DEATH_VERIFICATION = INC_MORTALITY.DEATH_VERIFICATION
AND MI.GENDER_PROBABILITY = INC_MORTALITY.GENDER_PROBABILITY
AND MI.GENDER = INC_MORTALITY.GENDER
AND MI.TOKEN_1 = INC_MORTALITY.TOKEN_1
AND MI.TOKEN_2 = INC_MORTALITY.TOKEN_2
AND MI.TOKEN_4 = INC_MORTALITY.TOKEN_4
AND MI.TOKEN_5 = INC_MORTALITY.TOKEN_5
AND MI.TOKEN_7 = INC_MORTALITY.TOKEN_7
AND MI.TOKEN_16 = INC_MORTALITY.TOKEN_16
AND MI.TOKEN_KEY = INC_MORTALITY.TOKEN_KEY
AND INC_MORTALITY.OP_DIRECTIVE = 'D'
The above Delete statement will delete all the rows satisfying the conditions, my requirement is to delete only one record even if more than one historical record fully matches this new delete record,
and if i include ROW NUMBER() stmt like below my DELETE stmt is not working
QUALIFY ROW_NUMBER() OVER (PARTITION BY MI.DATA_SOURCE,MI.DOB,MI.DOD
ORDER BY MI.DOD DESC ) = 1
Any suggestions on how to approach this scenario, Thanks!!
Approach to solution: Copy unmatched rows to a work table, then truncate the original table and replace with contents of the work table. One way to identify unmatched rows would be to tag each of the input rows in a set of duplicates with a unique number, something like this:
INSERT work_table SELECT MI.col1, MI.col2, ...
FROM
(SELECT M.*,
ROW_NUMBER() OVER (PARTITION BY <join cols> ORDER BY <some col(s)>) AS ROWNUM
FROM MORTALITY M) MI
LEFT JOIN
(SELECT I.*,
ROW_NUMBER() OVER (PARTITION BY <join cols> ORDER BY <some col(s)>) AS ROWNUM
FROM INC_MORTALITY I
WHERE OP_DIRECTIVE='D') INC
ON MI.join_col1 = INC.join_col1
AND MI.join_col2 = INC.join_col2
...
AND MI.ROWNUM = INC.ROWNUM
WHERE INC.ROWNUM IS NULL /* "anti-join" keeps only unmatched rows */
;
DELETE FROM MORTALITY;
INSERT MORTALITY SELECT * FROM work_table;
If INC_MORTALILTY never has duplicates, then you can eliminate numbering that relation and change the last join condition to MI.ROWNUM = 1 and use one of the other JOIN columns for the NULL check.

How to take data from two tables,one table not having common record

Please Help I got a problem with join in my sqlite database,In that I 'm having two tables like userdetails and usertransaction tables,
userdetails having 5 records like userids 1,2,3,4,5 with their details
and 2nd table usertransaction having only 1 and 2 records.
I want to take userdetails with taht user trasactions sum...
if no transactions simply replace zero and remaing details.
query like this...
Select tblfarmers.farmerid,
tblfarmers.farmerName,
tblfarmers.mobileNumber,
tblfarmers.Address,
SUM(IfNULL(tblFarmerAdvanceDetails.presentadvance, 0)) as presentadvance
from tblfarmers
left join tblFarmerAdvanceDetails on tblFarmerAdvanceDetails.farmerid=tblfarmers.farmerid
where tblfarmers.farmerid='2'
and tblFarmerAdvanceDetails.isactive='true'
ORDER BY tblfarmers.farmerid
You are enforcing a condition in your left joined table that only rows that are actually joined can fulfill:
tblFarmerAdvanceDetails.isactive='true'
To avoid this you should remove this condition from the WHERE part and add it to the join condition.
SELECT tblfarmers.farmerid,
tblfarmers.farmerName,
tblfarmers.mobileNumber,
tblfarmers.Address,
SUM(IfNULL(tblFarmerAdvanceDetails.presentadvance, 0)) as presentadvance
FROM tblfarmers
LEFT JOIN tblFarmerAdvanceDetails ON (tblFarmerAdvanceDetails.farmerid = tblfarmers.farmerid AND tblFarmerAdvanceDetails.isactive = 'true')
WHERE tblfarmers.farmerid='2'
ORDER BY tblfarmers.farmerid

SQL update based on column in other table

Using SQLite, I am trying to update three columns based on another table (two columns)
The three columns are (Table1):
'AgentCreatedID'
'AgentOwnedID'
'AgentSentID'
The other table (Table2) consists of 'AgentID' and 'Designation'.
If the ID in one of the three columns matches the 'AgentID' in the second table, I want the 'Designation' value to populate. This table is a list of ALL unique IDs and the corresponding designation. Each row of data has a Creator, Owner, and Sender. I need to see what designation that person is from.
In Access, this would look something like this for the first value. I would also need to add the other two values.
UPDATE Table1
LEFT JOIN Table2 ON Table1.AgentCreatedID = Table2.AgentID
SET raw.AgentCreatedID = [ Table2 ]![ Designation];
I am not sure what that ! command is or how it could be used in SQLite.
SQLite does not suport joins in an UPDATE statement.
You have to look up the new value with correlated subqueries:
UPDATE Table1
SET AgentCreatedID = (SELECT Designation
FROM Table2
WHERE AgentID = AgentCreatedID),
AgentOwnedID = (SELECT Designation
FROM Table2
WHERE AgentID = AgentOwnedID),
AgentSentID = (SELECT Designation
FROM Table2
WHERE AgentID = AgentSentID)
The exclamation mark is used to separate the worksheet name from the reference in that worksheet. Here is Microsoft's explanation of cell references.
Now that you know what [ Table2 ]![Designatio] means, you can simplify it to use only the column name.

How to fetch first occurance of most active row row in job table with specific deptid,

Eg : Emplid 001 most effective dated row (say 01/01/2013 )is active and belongs to deptid 101.Suppose If he has two more rows prior with same deptid say one on 10/12/2012 and 01/12/2012, Then i needs to retrieve 01/12/2012 rows.So it should be the first row of continous occurances, In case if i have row with 05/12/2012 with other deptid (102), In that case my query should return 10/12/2012 rows, Please help on this
Though not with a left join, this uses a sub-select on minimum effective date to get the data you need - the trick to get the min effdt by deptid is putting the deptid in the sub-select. Note that empl_rcd_nbr is usually used as a limiter in the sub-select (and job2.empl_rcd_nbr - job.empl_rcd_nbr) but you didn't have it in your original select. If you get dup rows, check your empl_rcd_nbr value since it is a primary key:
select job.emplid, job.effdt, job.deptid
from ps_job job
where job.effdt = (select Min(job2.effdt)
from ps_job job2
where job2.emplid = job.emplid
and job2.deptid = job.deptid)
order by job.emplid
To achieve what you need, you will have to pick up the min(effdt) row with deptid same as the deptid of the current max(effdt) row and also, there should not exist a row > the min(effdt) with deptid <> deptid of the min(effdt) row.
A query satisfying the above conditions should help you with your result set.

Resources