INSERT, subquery, VALUES [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This query insert into table values from othes table(it returned more than 1 rows)
INSERT INTO metric_values(mv_db_id, mv_cat_id)
(SELECT DISTINCT dbs_id, cat_id FROM
(SELECT DISTINCT cat_id, dbs_id FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo)
But I have some more variables(mv_metric_id, mv_period_id, mv_period_startdate), which values I receive from program (c#). Anybody knows how to insert this variables with this query?
Something like this:
INSERT INTO metric_values(mv_metric_id, mv_period_id, mv_period_startdate, mv_db_id, mv_cat_id)
VALUES (1, 1, '2009-09-28', (SELECT DISTINCT dbs_id, cat_id
FROM (SELECT DISTINCT cat_id, dbs_id
FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo))

You can put your own values in the outermost SELECT clause, for example:
INSERT INTO metric_values(mv_metric_id, mv_period_id, mv_period_startdate, mv_db_id, mv_cat_id)
(SELECT DISTINCT MYDATA1, MYDATA2, MYDATA3, dbs_id, cat_id FROM
(SELECT DISTINCT cat_id, dbs_id FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo)
Of course this will make the whole row DISTINCT so might have regroup your SELECT clauses.

Related

Convert Sql Statement to Entity Framework Core [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can i convert this sql query to linq in entity framework core ?
Select a.Id,a.Url,c.Title,a.ParentId,b.totalSubCats
From [Tbl_Menu] As a
LEFT OUTER JOIN
( Select ParentId, Count(*) As totalSubCats From [Tbl_Menu] Group By ParentId ) As b
On a.Id=b.ParentId
LEFT OUTER JOIN [Tbl_Menu] As c On a.Id = c.Id
ORDER BY a.Id
And we have this columns in Menu_Tbl
Id , Url , Title , Description , ParentId
For Left Join I wrote this query but can`t run
var query = (from m1 in _context.Menu_Tbl
join m2 in _context.Menu_Tbl on m1.Id equals m2.Id
into m3 from m in m3.DefaultIfEmpty() select m1);
I think it would be easier to just make that query a stored procedure and then call the stored procedure using yourReturnedDbSet objectReceived = context.yourReturnedDbSet.FromSqlRaw("EXECUTE StoredProcedureName #exampleParam"). But as for converting it you can pretty much use the same syntax if you have the correct objects. You will also need a list that holds the type you are being returned.
This one should work. Just divide big query by small pieces (subqueries)
var grouped =
from m in ctx.Tbl_Menu
group m by new { m.ParentId } into g
select new
{
g.Key.ParentId,
totalSubCats = g.Count()
};
var query =
from a in ctx.Tbl_Menu
join b in grouped on a.Id equals b.ParentId into gj
from b in gj.DefaultIfEmpty()
join c in ctx.Tbl_Menu on a.Id equals c.Id into ggj
from c in ggj.DefaultIfEmpty()
select new
{
a.Id,
a.Url,
c.Title,
a.ParentId,
b.totalSubCats
};

How to convert this SQL to LINQ of ASP.NET MVC? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have this SQL statement:
select
table1.DIRECTORATE_ID,
table2.PLA_NAME + '//' + table1.ENGLISH_DIRECTORATE_CODE as EngCode
from table1
inner join table2 on table1.PLA_ID = table2.PLA_ID order by EngCode
I need to do the same type of query in my EF code that gives me a List<>. Something like this:
var query = (from t1 table1.DIRECTORATE_ID, table2.PLA_NAME + '//' + table1.ENGLISH_DIRECTORATE_CODE as EngCode
from table1
inner join table2 on table1.PLA_ID = table2.PLA_ID order by EngCode)
.ToList();
You've got column references in your from clause. In SQL, the order is (in this particular example)
select
from
join
order by
In LINQ, the order is
from
join
orderby
select
You can write this in Query Syntax
var query = from t1 in table1
join t2 in table2 on t1.PLA_ID equals t2.PLA_ID
let EngCode = t2.PLA_NAME + "//" + t1.ENGLISH_DIRECTORATE_CODE
orderby EngCode
select new
{
t1.DIRECTORATE_ID,
EngCode
}
var data = query.ToList();
Or with extension methods
var data = table1
.Join(
table2,
t1 => t1.PLA_ID,
t2 => t2.PLA_ID,
(t1, t2) => new
{
t1.DIRECTORATE_ID,
EngCode = t2.PLA_NAME + "//" + t1.ENGLISH_DIRECTORATE_CODE
})
.OrderBy(x => x.EngCode)
.ToList();

retrieving values from one table based condition on the retrieving table and reefrencing table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have two tables tblorders and tblprogramme as shown in below images.
I will select PartyName (from tblorders) from a dropdownlist in asp.net, based on that only those JobNo should come whose all the Status (from tblprogramme) is dispatched.
I want query for that.
e.g
I want "Parry's"(in table tblorders) That "JobNo" whose all the "Status"(in tblprogramme ) is "Dispatched")
In my case from below table images
Query should return "JobNo"( 2).as Parry's this JobNo's (in tblProgramme") has status (Dispatched ib tblprogramme
Here is the image).
SELECT t1.JobNo
FROM tblOrders t1
WHERE t1.PartyName = 'Parry'
AND NOT EXISTS ( SELECT *
FROM tblProgramme t2
WHERE t1.JobNo = t2.JobNo
AND t2.ProgrammeStatus <> 'Dispatched' );
Try below,
SELECT *
FROM (
SELECT
jobno,
name,
TotalCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno
),
DispatchedCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno AND status = 'dispatched'
)
FROM tblOrder o
) t
WHERE name = 'Parry'
AND TotalCount = DispatchedCount

Ambigous error c# / sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
can someone please tell me why i am getting an error with this code ?
SqlCommand scGetClaimedDetails = new SqlCommand(
"SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed,SUM(total) as total from" +
" ( SELECT CASE WHEN claimed = 'Y' THEN inv_amt *.45 END as isclaimed, (inv_amt *.45) as inclaimable, inv_amt as total from invasset" +
" INNER JOIN Invoice ON invoice.invoice = invasset.invoice WHERE invasset.asset_no = #AssetNumber ) as D2", DataAccess.AssetConnection);
In subquery specifiy where is inv_amt column coming from (is it invoice.inv_amt or invasset.inv_amt)
ambigous column name inv_amt
It seams like the two tables invasset and Invoice both contains the column inv_amt, You have to reference it to an alias in the inner SELECT statement, something like: invoice.inv_amt or invasset.inv_amt:
SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed,
SUM(total) as total
FROM
(
SELECT CASE WHEN claimed = 'Y' THEN invoice.inv_amt *.45 END as isclaimed,
(inv_amt *.45) as inclaimable, inv_amt as total
from invasset INNER JOIN Invoice ON invoice.invoice = invasset.invoice
WHERE invasset.asset_no = #AssetNumber
) as D2
When I format your code to be human-readable...
SqlCommand scGetClaimedDetails = new SqlCommand(
"SELECT
SUM(isclaimable) AS claimable,
SUM(isclaimed) AS claimed,
SUM(total) AS total
FROM
(SELECT
CASE WHEN claimed = 'Y' THEN inv_amt *.45 END AS isclaimed,
(inv_amt *.45) AS inclaimable,
inv_amt AS total
FROM
invasset
INNER JOIN Invoice ON invoice.invoice = invasset.invoice
WHERE
invasset.asset_no = #AssetNumber) AS D2",
DataAccess.AssetConnection);
I can't help but notice that your outer SELECT is looking for a column called isclaimable while your inner SELECT is returning a column called inclaimable. There's a typo in your code.
Edit: In response to your comment on the question, which tables contain the field inv_amt? Clearly you're referencing two tables which have it. Since the only tables you're referencing are invasset and Invoice then clearly both of those tables have a column named inv_amt. You'll have to specify which one you want in your query.
So instead of:
`inv_amt AS total`
You'd want something like:
`invasset.inv_amt AS total`
(Assuming that's the table you want, otherwise use the other table.)

Convert SQL to LINQ Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have the following SQL query and I need to have it in LINQ, I tried several things but I can not get it working.
Here is the SQL query
SELECT ST.Description, ST.STId, COUNT(SI.SIId) AS Expr1
FROM BP INNER JOIN
MbrBP ON BP.BPId = MbrBP.BPId INNER JOIN
SI ON BP.BPId = SI.BPId RIGHT OUTER JOIN
ST ON SI.STId = ST.STId
WHERE (BP.CountryId = 1) AND (BP.RegionId = 1) AND (MbrBP.MemberId = 1)
AND (SI.IsActive = 1)
GROUP BY ST.Description, ST.STId
UNION
SELECT ST.Description, ST.STId, COUNT(SI.STId) AS Expr1
FROM SI RIGHT OUTER JOIN
ST ON SI.STId = ST.STId
GROUP BY ST.Description, ST.STId

Categories