Modify Ms-Access Table Structure - c#

I have an access table with certain structure and I want to copy it to a table with a different structure. The structure of the original table is:
col1-ddate(datetime)
col2-type(text)
col3-vvalue(number).
Each 3 rows have the same date. The type has repeated 3 values(sys, dia, pul).
Now I want to insert this table data to a new(temporary) table which has the following structure
col1-ddate(datetime)
col2-sys(number)
col3-dia(number)
col4-Pul(number)
How can I do that?

If I've understood correctly, a SQL query using conditional aggregation should achieve what you are looking for, grouping by the datetime field, e.g.:
select
col1-ddate,
max(iif(col2-type = "sys", col3-vvalue, null)) as col2-sys,
max(iif(col2-type = "dia", col3-vvalue, null)) as col3-dia,
max(iif(col2-type = "pul", col3-vvalue, null)) as col4-pul
into
NewTemporaryTable
from
YourTable
group by
col1-ddate
Change YourTable to the name of your existing table.

Related

In Data table first column displaying with tablename.columnname in c#

When i am running query in data base and fill data table with load method first column name coming with table name.column name(Employee.Name,sal,location) rest of the column name coming only column names present in the database.Why table name appending first column name while displaying in data table structure please help me in this regards.
DataTable table = new DataTable();
try
{
using (IDbCommand DbCommand = dbConnection.CreateCommand())
{
DbCommand.CommandText = query;
IDataReader reader = DbCommand.ExecuteReader();
table.Load(reader);
}
}
Sample query Select Employee.Numnber, Employee.salary, Employee.city,dept.deptId from Employee inner join dept where deptiId=EmployeeDeptId
The actual query is :
Select
AP_LINE.LINE_NO, AP_LINE.PRODUCT, AP_LINE.QTY, AP_LINE.REQUESTED_DATE,
AP_LINE.LIST_PRICE, AP_LINE.CONS_NET_MULT,AP_LINE.NETADDERS,
AP_LINE.CONS_NET_PRICE, AP_LINE.Details,TDP_JSP.ID, TDP_JSP.LINE_NO,
TDP_JSP_AppInfo.Id
From AP_LINE, CONFIG, TDP_JSP
inner join TDP_JSP_AppInfo on TDP_JSP.ID= TDP_JSP_AppInfo.ID
where AP_LINE.LINE_NO = TDP_JSP.LINE_NO ;
Out put in data table as coming below
Ap_Line.Line_No,Product,QTY,Requested Date...
There are two LINE_NO columns in the query, AP_LINE.LINE_NO and TDP_JSP.LINE_NO. Columns must have unique names, so DataTable.Load used the two-part name wherever needed. It that wasn't possible it would start appending indexes or even generate names like Column0, Column1 etc.
In this case though, one of those columns isn't needed. The two tables are join those columns. WHERE AP_LINE.LINE_NO = TDP_JSP.LINE_NO is the old-style, frowned-upon join syntax. Those two columns will always have the same data so one of them can be removed.

get and post table type using pqxx

Good day, friends. I'm using pqxx and I have some questions.
1. I have two tables:
table1 (table1_id integer) and table2 (table1_id integer, another_id integer) with relation one-to-many.
How I can easy get information in view like: table1_id, vector another_ids?
Now I use serializtion in script (string concat into "%d %d %d...") and deserialization in my c++ code.
Also I need insert value into table1. And how I can do this in one transaction?
2. I call stored procedure like
t.exec("SELECT * FROM my_proc(some_argument)");
May be exists any way to do this like in c#?
Thank you very much!
So, may be it can help someone.
In first case I find and use two ways:
1. Group concat in sql function and deserialization in c++. It fast if table2 has only table1_id and another integer.
2. I call two function: get_table1() and get_table2() with order by table1_id. And then with two pointers I create array of table1:
std::vector<Table1> table1Array;
auto ap = wrk.prepared(GetTable1FuncName).exec();
auto aps = wrk.prepared(GetTable2FuncName).exec();
auto pos = aps.begin();
for (auto row = ap.begin(); row != ap.end(); ++row) {
std::vector<Table2> table2Array;
while (pos != aps.end()
&& row["table1_id"].as(int()) == pos["table1_id"].as(int())) {
table2Array.push_back(Table2(pos["first_id"].as(int()),
pos["second_string"].as(std::string())));
++pos;
}
Table1 tb1(row["table1_id"].as(int()), row["column2"].as(int()),
row["column3"].as(int()), row["column4"].as(int()),
table2Array);
table1Array.push_back(tb1);
}
May be it is not pretty, but it's working.
Insert into database I write for one element. Firstly insert into Table1, and after several lines into Table2. After call pqxx::work.commit().
In second case Not, doesn't exists. Also remember, function always return 1 line! Be careful!

How to update whole column in sql server table

I want to update a column values in sql server table after copying those value from some other table. i.e.
where I have tested this query but not succeeded
update subset_aminer.dbo.sub_aminer_paper
set sub_aminer_paper.p_abstract =
(select aminer_paper.p_abstract
from aminer.dbo.aminer_paper
where pid IN (86257, 116497, 119248, 135555, 147554,
149720, 173254, 191333, 196650, 196656,
.....long list of other values ......
1727408, 1737809, 2034956)
)
I have to copy data from other database table to my destination database table. So that's y using subset_aminer.dbo.subset_aminer_paper.p_abstract and aminer.dbo.aminer_paper.p_abstract
Please help with acknowledgements. Thanks
update subset_aminer.dbo.sub_aminer_paper SI
set SI.p_abstract = AP.p_abstract
from aminer.dbo.aminer_paper AP
where AP.pid IN (86257, 116497, 119248, 135555, 147554,
149720, 173254, 191333, 196650, 196656,
.....long list of other values ......
1727408, 1737809, 2034956) AND AP.ID = SI.ID
Just a suggestion,
UPDATE subset_aminer.dbo.sub_aminer_paper
SET p_abstract=aminer.dbo.aminer_paper.p_abstract
FROM aminer.dbo.aminer_paper
WHERE subset_aminer.dbo.sub_aminer_paper.pid IN (86257, 116497, 119248, 135555, 147554,
149720, 173254, 191333, 196650, 196656,
.....long list of other values ......
1727408, 1737809, 2034956)

How to Update one table by another table datas using storedprocedure?

I have a Table named Dummywith marks and student id (3 mark fields are there). I have another one table Applicantdetails this table also contains mark fields. what I want to do is I want to updates Dummy tables marks to Applicantdetails table's marks as per student id. I want to do this by mssql Storedprocedure. Any way to achieve it. If we write in code wise it should be like this
qry="select Applicantid,mark1,mark2,mark3 from Dummy"
//saved result to Datatable dt
foreaach(DataRow. rows in dt.rows)
{
string id=Convert.ToString(row["ApplicantID"].tostring();
string mark1=Convert.ToString(row["ApplicantID"].tostring();
string mark2=Convert.ToString(row["ApplicantID"].tostring();
string mark3=Convert.ToString(row["ApplicantID"].tostring();
qry="update Applicantdetails set Mark1=mark1,Mark2=Mark2,Mark3=Mark3
where ApplicantID=id";
}
This format I want to bring in storedprocedure..Please help me
Your Stored procedure would receive #applicationid ? Otherwise it would update all, Create a stored procedure with the following SQL
UPDATE A
SET Mark1= d.mark1, Mark2 = d.mark2, Mark3= d.mark3
FROM ApplicationDetail A
JOIN Dummy d on d.Applicationid = A.Applicationid

DataTable sum duplicate rows

i have DataTable which can have semi-duplicate rows. In the picture example two highlighted rows have all the same values but the amounts in 'Amount' columns. I would need to identify those rows and sum the amount $. Data comes from text file and there is no key to uniquely identify rows.
I looked at some answers like this one
Best way to remove duplicate entries from a data table but in my case would need to match on not just one column but 10.
Also I tried different LINQ queries but was not successful in getting far.
This is the SQL query which does the job:
SELECT [Date1],[Date2],[Date3]
,SUM([Amount1]) as Summary1
,SUM([Amount2]) as Summary2
,SUM([Amount3]) as Summary3
,[col1],[col2],[Rate1],[Rate2],[Rate3],[product],[comment]
FROM [Table]
group by [Date1],[Date2],[Date3],[col1],[col2],[Rate1],[Rate2],[Rate3],[product],[comment]
EDIT: Just to clarify, SQL query was an example of how I could get a successful results if I was querying SQL table.
This is how you would do it with EF:
var result=db.Table
.GroupBy(r=>new {
r.Date1,
r.Date2,
r.Date3,
r.col1,
r.col2,
r.Rate1,
r.Rate2,
r.Rate3,
r.product,
r.comment},
p=>new {
p.Amount1,
p.Amount2,
p.Amount3},
(key,vals)=>new {
key.Date1,
key.Date2,
key.Date3,
Amount1=vals.Sum(v=>v.Amount1),
Amount2=vals.Sum(v=>v.Amount2),
Amount3=vals.Sum(v=>v.Amount3),
key.col1,
key.col2,
key.Rate1,
key.Rate2,
key.Rate3,
key.product,
key.comments}
);
Slight modification if you are actually doing it from a dataTable:
var result=dt.AsEnumerable()
.GroupBy(d=>new {
Date1=d.Field<datetime>("Date1"),
Date2=d.Field<datetime>("Date2"),
Date3=d.Field<datetime>("Date3"),
col1=d.Field<string>("col1"),
col2=d.Field<string>("col2"),
Rate1=d.Field<decimal>("Rate1"),
Rate2=d.Field<decimal>("Rate2"),
Rate3=d.Field<decimal>("Rate3"),
product=d.Field<string>("product"),
comments=d.Field<string>("comments")},
p=>new {
Amount1=p.Field<decimal>("Amount1"),
Amount2=p.Field<decimal>("Amount2"),
Amount3=p.Field<decimal>("Amount3")},
(key,vals)=>new {
key.Date1,
key.Date2,
key.Date3,
Amount1=vals.Sum(v=>v.Amount1),
Amount2=vals.Sum(v=>v.Amount2),
Amount3=vals.Sum(v=>v.Amount3),
key.col1,
key.col2,
key.Rate1,
key.Rate2,
key.Rate3,
key.product,
key.comments}
);
If you need the result as a datatable, you can use one of the many List To Datatable extension methods out there. Just add ".ToList().AsDataTable()" at the end of the above query to get it back into a datatable.

Categories