RadGridView for Winforms - foreign key column - winforms

I have the following columns in my bindingsource
BookID, Name, Price, StatusID
StatusID is a foreign key.
Another table has the following columns
StatusID, Status_Name.
I want Radgridview contains the following columns: Name, Price, Status_Name. How can I do that?

Use a query for reading those results, Like this:
SELECT
a.Name, a.Price, b.Status_Name
FROM tableA a
LEFT OUTER JOIN
tableB b ON a.StatusID = b.StatusID

There are two options here:
Use hierarchical grid and you will have the first table on level 1 and the second table on level two. Here is an article how to set this up: enter link description here
If you want the columns at one level, you will have to have them in a single table (see the answer from shA.t). Once you have them in a single table, simply bind the grid to it (set its DataSource property to the table instance)

Related

How do you link a field in a table to the sum of a field in another table?

Please I use Sql Server
I have the Quantity field in the Items table equal to the sum of the Quantity fields in the Stocks table.
Is there a way to do this automatically in?
You could use an indexed view to hold the aggregation, which means that the server will maintain the view whenever there is any modification to the base tables. This removes the possibility of update anomalies.
CREATE VIEW vwTotalStock AS
SELECT i.ItemId, i.ItemName, SUM(s.Quantity) TotalQuantity
FROM dbo.Items i
JOIN dbo.Stocks s ON s.ItemId = i.ItemId
GROUP BY
i.ItemId, i.ItemName;
GO
CREATE CLUSTERED INDEX IX_vwTotalStock ON dbo.vwTotalStock (ItemId);

postgresql change encode to hex for specific columns content in table

Im trying to update all the values in 2 particular columns for two different tables we have to HEX so far without luck in our PostgreSQL 8.4.7 on Centos.
Here are the two columns types and the table name they belong too:
-node_id is a smallint column default 1. this belongs to table1
-value is a bytea column that belongs to table2
I can do a select on table2 for the value:bytea column with the following selec:
SELECT encode(value::bytea, 'hex') FROM table2;
But this syntax does not work for table1 node_id:smallint column.
I've try the following commands without luck in and effort to change the values of the columns to hex on the respective tables.
ALTER TABLE table2 SET value TO 'hex';
ALTER TABLE table2 MODIFY value TO 'hex';
UPDATE table1 (value::bytea, 'hex');
UPDATE tbl table2 SET value::bytea TO 'hex';
SET encode(value::bytea, 'hex') FROM table2;
There is any proper way to do this in a one command like the above?
Thanks in advance for any help.
I was able to solve my problem with the following command:
CREATE TABLE "name" AS select *, encode(value, 'hex') as "column_name" from "table";

SELF JOIN /SUBQUERIES/SSIS performance

Hi I have three tables
Wrk_Order ,Wrk_Info,Wrk_Driver
all the three have DFirstName,DLastName,DUsername columns . I want to anonymise them
What I did is I have got distinct of each three columns in all the three table and loaded into lookup table as
Table Name :LKP_driverinfo
Columns :[DriverInfo,DriverAnonInfo]
DriverInfo column will have data as follows
SELECT Distinct DFirstName FROM Wrk_Order
UNION
SELECt DISTINCT DLastName FROM WRk_Order
UNION
SELECt DISTINCT DUsername FROM WRk_Order
UNION
SELECT Distinct DFirstName FROM Wrk_Info
.
.
.
Similarly for rest two tables
DriverAnonInfo : will have anonymized value of Driverinfo
I need to update DFirstName,DLastName,DUsername of all the three tables to anonymised information [DriverAnonInfo] from Lookup table LKP_driverinfo
whats the best way to achieve this self-join or inner queries or using SSIS?
You seem to create a sort of EAV model with your driver info, but you do not seem to keep any references for the originating tables.
Perhaps you can try adding 3 more columns to your driverinfo table, to hold the id of the table that the value comes from. That way you can directly update or join entries in the tables.

TSQL - Loop over column to query other table

I'm stuck with the following task:
via Excel the user types in an ID then clicks a button (macro) to trigger SP or function
this ID usually "returns" more than one row
from this result I need the values of a single column (LookupNumber) in order to use those values to query or join another view. Looping required?
as a result I want to join the initial ID request table with the result of the other queried table.
In below image, the first table holds the ID the user wants to search.
The second table should be queried by the LookupNumber values that were retrieved from table A.
The third table shows the desired output:
The problem I have is how do I loop (if needed) through the LookupNumber once the search ID has been passed? I don't know what methods I should use for this task.
Because sqlfiddle doesn't seem to work, I put the tables from above screenshot in this link
Hopefully you can give me some ideas how to solve this.
Something like this should work:
select t1.*, t2.*
from table2 t2
inner join (
select ID, Subject, Project, LookupNumber
from table1
where id = <entered id>
) t1 on t1.LookupNumber = t2.LookupNumber

Returning Grouped Data with Extra Row for each group in SQL Server

I have tables Composition, CompositionDetails and Tracks
Composition holds composition Name while compositionDetails is for mapping tracks with composition. One composition can have multiple tracks.
Table Structures are like this:
Composition table - CompositionId, CompositionName
Tracks table - TrackId, TrackName
CompositionDetails - CompositionId (FK), TrackId (FK)
Now with My query I am able to achieve this:
But I want this:
I mean one extra row above each group of composition.
I achieved it creating temp tables and looping to insert extra row. But with millions to data, it is very slow.
Any suggestions on how can can achieve this without creating temp table and going over loop to insert new rows?
Select Componame,TrackTitle from
(
Select Componame,TrackTitle,Componame as h,1 as Sort
from Composition
UNION
Select Componame,Componame,MIN(Componame),0 as Sort
from Composition
group by Componame
) a
Order by h,Sort,Componame,TrackTitle
Try this
SELECT c2.CompositionName, c2.CompositionName, c2.CompositionId
FROM Composition c2
UNION
SELECT c.CompositionName, t.TrackName, cd.CompositionId
FROM CompositionDetails cd join Composition c ON cd.CompositionId = c.CompositionId
JOIN Tracks t on t.Trackid = cd.Trackid
Assuming the CompositionDetails table doesn't have rows with NULLs in the TrackId column:
Using UNION, add one blank (NULL) TrackId per CompositionId into CompositionDetails.
Inner-join the resulting set to Composition.
Left-join the result of the inner join with Tracks. Obviously, this will yield NULL track names for the rows added in #1.
In the ORDER BY clause, sort by CompositionName first, then by TrackName. (In Transact-SQL, NULLs go before any values. Therefore, rows with blank track names will go before the others in their respective groups.)
In the SELECT clause, use either COALESCE or ISNULL to replace blank track names with composition names.
SELECT
c.CompositionName,
COALESCE(t.TrackName, c.CompositionName)
FROM
Composition AS c
INNER JOIN (
SELECT CompositionId, TrackId FROM CompositionDetails
UNION ALL
SELECT DISTINCT CompositionId, NULL FROM CompositionDetails
) AS d ON c.CompositionId = d.CompositionId
LEFT JOIN Tracks AS t ON d.TrackId = t.TrackId
ORDER BY
c.CompositionName,
t.TrackName
;

Resources