First of all i've to tell you that i'm a beginner in SQL.
I want to select all the informations related to my PLAN thanks to it's id to put them in a DataGridView in my C# program.
We can affect multiples voltage levels, catenary types etc... to this plan and it would be stored in a table called plans_into_voltage and plans_into_type there.
So when I try to selecet all the informations related to a plan, if it has multiple voltage and/or many catenary types, it would duplicate rows like this :
Then I tried to STRING_AGG but now I have duplicates into the row and I don't know how to avoid that/remove them..
This is my query :
SELECT TOP 20 pinfos.id
, STRING_AGG(pinfos.plan_name, '|')
, STRING_AGG (voltage.name, '|') AS vname
, STRING_AGG(catType.name, '|') AS catname
FROM [allocation_schematic.information].primary_infos pinfos
LEFT JOIN [allocation_schematic.types].plans_into_type pit
ON pinfos.id = pit.id_plan
LEFT JOIN [allocation_schematic.types].catenary_type catType
ON pit.id_type = catType.id
LEFT JOIN [allocation_schematic.types].plans_into_voltage pot
ON pinfos.id = pot.id_plan
LEFT JOIN [allocation_schematic.types].voltage voltage
ON pot.id_voltage = voltage.id
GROUP BY pinfos.id
But i want it like:
I've already check this first solution and this second one but I don't understand how to implement it with my query.
Could someone help me with this please ? I've been on this trouble since 2 days..
Thanks !
How about try this query:
SELECT TOP 20 pinfos.id
, STRING_AGG(pinfos.plan_name, '|')
, STRING_AGG (voltage.name, '|') AS vname
, STRING_AGG(catType.name, '|') AS catname
FROM (
SELECT DISTINCT pinfos.id
pinfos.plan_name,
voltage.name,
catType.name
FROM [allocation_schematic.information].primary_infos pinfos
LEFT JOIN [allocation_schematic.types].plans_into_type pit
ON pinfos.id = pit.id_plan
LEFT JOIN [allocation_schematic.types].catenary_type catType
ON pit.id_type = catType.id
LEFT JOIN [allocation_schematic.types].plans_into_voltage pot
ON pinfos.id = pot.id_plan
LEFT JOIN [allocation_schematic.types].voltage voltage
ON pot.id_voltage = voltage.id
)
GROUP BY pinfos.id
This query is modified accordding this statement:
SELECT STRING_AGG(data)
FROM (
SELECT DISTINCT FROM ...
)
I tested with my table and it works.
All data in my table employee:
Run my demo query:
SELECT emp.deptno, STRING_AGG(emp.ename,',') AS ename from (
SELECT DISTINCT ename, deptno FROM employee) as emp
GROUP BY emp.deptno
Update:
This query does't work as expected.
But Congratulations that you have solved it:
Used multiple queries and joined them thanks to a Dictionary in my c# Program and it works fine now
Hope this helps.
Related
I have put down below a query to retrieve from four tables which are
Sales_Invoice, New_Customer, Company_Information, Expense
Query:
select
isnull(sum(Expense.Expense_Amount), 0.00), as ExpenseAmount,
Company_Information.Company_Name,
Sales_Invoice.Invoice_No, Sales_Invoice.Invoice_Date, Sales_Invoice.Item_Name,
New_Customer.Customer_Name, New_Customer.Customer_ID
from
Sales_invoice, Company_Information, New_Customer, Expense
where
Sales_Invoice.Customer_Id = New_Customer.Customer_ID
and Sales_Invoice.Invoice_No = Expense.Invoice_No
group by
Company_Information.Company_Name,
Sales_Invoice.Invoice_No, Sales_Invoice.Invoice_Date, Sales_Invoice.Customer_ID,
Sales_Invoice.Item_Name, New_Customer.Customer_Name, New_Customer.Customer_ID
The query is working well but if the Expense table has no values Expense.Invoice_No does not match with Sales_Invoice.Invoice_No, then the query above will return empty rows.
But what I wish to do is that, if Expense.Invoice_No does not exists then I still want to have my rows provided that expense amount return 0.00
Use standard joins! Then, you can easily handle "missing" relations with a left join.
Your question suggests:
select
coalesce(sum(e.expense_amount), 0.00) as expenseamount,
ci.company_name,
si.invoice_no, si.invoice_date, si.item_name,
nc.customer_name, nc.customer_id
from new_customer nc
inner join company_information ci on ???
inner join sales_invoice si on si.customer_id = nc.customer_id
left join expense e on e.invoice_no = si.invoice_no
group by
ci.company_name,
si.invoice_no, si.invoice_date, si.customer_id,
si.item_name, nc.customer_name, nc.customer_id
Note that you original code seems to me missing a join condition between the customers and companies. I represented it as ??? in the query.
You could also express the same logic with a correlated subquery, which would avoid outer aggregation:
select
(
select coalesce(sum(e.expense_amount), 0.00)
from expense e
where e.invoice_no = si.invoice_no
) as expenseamount,
ci.company_name,
si.invoice_no, si.invoice_date, si.item_name,
nc.customer_name, nc.customer_id
from new_customer nc
inner join company_information ci on ???
inner join sales_invoice si on si.customer_id = nc.customer_id
SQL Query taking too much time to execute. Working fine at UAT. I need to compare data of two tables and want to get difference. Below mention is my query.
Select *
from tblBrandDetailUsers tbdu
inner join tblBrands tbs on tbs.BrandId = tbdu.BrandId
left join tblBrandDetails tbd on tbd.CategoryId = tbdu.CategoryId
and tbd.BrandId = tbdu.BrandId
and tbd.CityId = tbdu.CityId
inner join tblCategory tc on tbdu.CategoryId = tc.CategoryId
inner join tblCity tcc on tcc.CityId = tbdu.CityId
where isnull(tbdu.SaleAmount,-1) <> isnull(tbd.SaleAmount,-1)
and isnull(tbdu.CityId,0) = 3
and isnull(tbdu.TopLevelCategoryId,0) = 2;
Need to optimize query.
a number of things you need to check:
number of rows for each table. the more rows you have the slower it gets. Do you have the same size of data with UAT?
SELECT * : avoid the * and only retrieve columns you need.
ISNULL function on left side of the WHERE predicate will scan the index because it is non-sargable. you can check the answer here and rewrite your predicate without any function on the left side of WHERE clause.
You need to provide a detailed information like actual execution plan. I can only give you a generic answer because not much detail was provided.
Remember the UAT is very different in PROD. the hardware you used, the number of rows, etc..
Every advises in comment looks right. The difference between UAT and Prod should be the volume of data.
Your issue should come of lack or inefficient indices.
You should add compound index on
tblBrandDetails.CategoryId,tblBrandDetails.BrandId, tblBrandDetails.CityId
and on
tblBrandDetailUsers.CategoryId,tblBrandDetailUsers.BrandId, tblBrandDetailUsers.CityId
ensure that all unique ids have a btree index (or similar type of index depending on your DB)
You can also add conditional indices to filter quicker the null values:
https://www.brentozar.com/archive/2015/09/filtered-indexes-and-is-not-null/
Rewrite your query like this :
Select *--> AVOID "*" put all the necessary columns
from tblBrandDetailUsers AS tbdu
inner join tblBrands AS tbs on tbs.BrandId = tbdu.BrandId
left join tblBrandDetails AS tbd on tbd.CategoryId = tbdu.CategoryId
and tbd.BrandId = tbdu.BrandId
and tbd.CityId = tbdu.CityId
inner join tblCategory AS tc on tbdu.CategoryId = tc.CategoryId
inner join tblCity AS tcc on tcc.CityId = tbdu.CityId
where tbdu.SaleAmount <> tbd.SaleAmount
and tbdu.CityId = 3
and tbdu.TopLevelCategoryId = 2
UNION ALL
SELECT * --> AVOID "*" put all the necessary columns
from tblBrandDetailUsers AS tbdu
inner join tblBrands AS tbs on tbs.BrandId = tbdu.BrandId
left join tblBrandDetails AS tbd on tbd.CategoryId = tbdu.CategoryId
and tbd.BrandId = tbdu.BrandId
and tbd.CityId = tbdu.CityId
inner join tblCategory AS tc on tbdu.CategoryId = tc.CategoryId
inner join tblCity AS tcc on tcc.CityId = tbdu.CityId
where tbdu.SaleAmount IS NULL
AND tbd.SaleAmount IS NULL
and tbdu.CityId = 3
and tbdu.TopLevelCategoryId = 2;
Modify the SELECT clause to have only the necessary columns and not *
Be sure that you have index that are close to :
For tblBrandDetailUsers TABLE :
index KEY (CityId, TopLevelCategoryId, BrandId, CategoryId) INCLUDE (SaleAmount)
index KEY (CityId, TopLevelCategoryId, CategoryId) INCLUDE (SaleAmount)
For tblBrandDetails TABLE :
index (CityId, BrandId, CategoryId)
And also :
tblCategory (CategoryId)
tblCity (CityId)
tblBrands (BrandId)
When you will rectify the query especially the SELECT clause, we can give you more accurate indexes, because selected columns have a big weight on indexes performances !
as other suggested, try to add index on columns used for joins
How to write below given Sql Query in Linq?
SELECT
Invoice.ExtInvoiceId AS Id,
Invoice.ExtClientBranchId,
Invoice.CustomerReference,
Invoice.CountryDataSetId,
Invoice.JmsJobNumber,
Invoice.InvoiceDate,
Invoice.TotalAmount,
Invoice.RequestedBy,
Invoice.DateCreated,
PaidTable.Paid,
Invoice.So_Terms_Disc,
'' AS RequestedByValue,
Client.ParentId,
Invoice.CurrencyCode
FROM Invoice
INNER JOIN Client
ON Invoice.ExtClientBranchId = Client.ExtClientBranchId
LEFT OUTER JOIN (
SELECT
ExtInvoiceId,
(CASE WHEN SUM(TotalAmount) = 0 THEN 1 ELSE 0 END) AS Paid
FROM [Transaction] GROUP BY ExtInvoiceId
) AS PaidTable
ON Invoice.ExtInvoiceId = PaidTable.ExtInvoiceId
Hey yes the query looks it has few operations/join to be dealt with , so approach it step by step :
Fetch all the records , using inner join
Try using left outer join
Further simplify the query to the best of what you need.
Also note if you are visual-studio , it gives you this best interface wherein you can get to know at which point your linq is correct and what all options you can use with it.
Just to set the context a little, I'm trying to use queries with mysql that use Late row lookup as shown in this article
https://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
but that's a story for another day but the idea is that you do a key search on the table and then join it onto the whole table to force a late row lookup and the problem is coming from my LINQ queries when joined together.
-- Key search query --
Calling Code
IQueryable<int> keySearch = _defaultQueryFactory.Load(ContextEnums.ClientContext, MapEntityToDTO(), whereStatement, clientID).OrderBy(orderBy).Skip(startRow).Take(pageSize).Select(x => x.ID);
Resulting Query
SELECT
`Extent1`.`Sys_InvoiceID`
FROM `tblinvoice` AS `Extent1`
WHERE 3 = `Extent1`.`FK_StatusID`
ORDER BY
`Extent1`.`InvoiceDate` ASC LIMIT 0,430
-- Full Table Search --
Calling Code
IQueryable<InvoiceDTOModel> tableSearch = _defaultQueryFactory.Load(ContextEnums.ClientContext, MapEntityToDTO(), null, clientID, true).OrderBy(orderBy);
Resulting Query
SELECT
`Extent1`.`ID`,
`Extent1`.`C1`,
`Extent1`.`C2`,
`Extent1`.`C3`,
`Extent1`.`C4`,
`Extent1`.`C5`,
`Extent1`.`C6`,
`Extent2`.`SID`,
`Extent2`.`S1,
`Extent2`.`S2`,
`Extent2`.`S3`,
`Extent3`.`EID`,
`Extent3`.`E1`,
`Extent4`.`DID`,
`Extent4`.`D1`,
`Extent4`.`D2`,
`Extent4`.`D3`,
`Extent4`.`D4`,
`Extent4`.`D5`
FROM `tbl1` AS `Extent1` INNER JOIN `tbl2` AS `Extent2` ON `Extent1`.`SID` = `Extent2`.`SID` INNER JOIN `tbl3` AS `Extent3` ON `Extent1`.`EID` = `Extent3`.`EID` LEFT OUTER JOIN `tbl4` AS `Extent4` ON `Extent1`.`ID` = `Extent4`.`DID`
ORDER BY
`Extent1`.`C4` ASC
-- Joining the Two Together --
Calling Code
keySearch.Join(tableSearch, key => key, table => table.ID, (key, table) => table).OrderBy(orderBy).ToListAsync();
Resulting Query
SELECT
`Join3`.`ID`,
`Join3`.`C1`,
`Join3`.`C1`,
`Join3`.`C1`,
`Join3`.`C1`,
`Join3`.`C1`,
`Join3`.`C1`,
`Join3`.`SID`,
`Join3`.`S1,
`Join3`.`S2`,
`Join3`.`S3`,
`Join3`.`EID`,
`Join3`.`E1`,
`Join3`.`DID`,
`Join3`.`D1`,
`Join3`.`D2`,
`Join3`.`D3`,
`Join3`.`D4`,
`Join3`.`D5`
FROM (
`Extent1`.`ID`,
`Extent1`.`C1`,
`Extent1`.`C2`,
`Extent1`.`C3`,
`Extent1`.`C4`,
`Extent1`.`C5`,
`Extent1`.`C6`
FROM `tblinvoice` AS `Extent1`
WHERE 3 = `Extent1`.`EID`
ORDER BY
`Extent1`.`C4` ASC LIMIT 0,430) AS `Limit1` INNER JOIN (SELECT
`Extent1`.`ID`,
`Extent1`.`C1`,
`Extent1`.`C2`,
`Extent1`.`C3`,
`Extent1`.`C4`,
`Extent1`.`C5`,
`Extent1`.`C6`,
`Extent2`.`SID`,
`Extent2`.`S1,
`Extent2`.`S2`,
`Extent2`.`S3`,
`Extent3`.`EID`,
`Extent3`.`E1`,
`Extent4`.`DID`,
`Extent4`.`D1`,
`Extent4`.`D2`,
`Extent4`.`D3`,
`Extent4`.`D4`,
`Extent4`.`D5`
FROM `tbl1` AS `Extent2` INNER JOIN `tbl2` AS `Extent3` ON `Extent2`.`SID` = `Extent3`.`SID` INNER JOIN `tblstatus` AS `Extent4` ON `Extent2`.`EID` = `Extent4`.`EID` LEFT OUTER JOIN `tbl3` AS `Extent5` ON `Extent2`.`ID` = `Extent5`.`DID`) AS `Join3` ON `Limit1`.`ID` = `Join3`.`ID`
ORDER BY
`Join3`.`C4` ASC
Basically the inner select brings back
FROM (
`Extent1`.`ID`,
`Extent1`.`C1`,
`Extent1`.`C2`,
`Extent1`.`C3`,
`Extent1`.`C4`,
`Extent1`.`C5`,
`Extent1`.`C6`
FROM `tblinvoice` AS `Extent1`
WHERE 3 = `Extent1`.`EID`
ORDER BY
`Extent1`.`C4` ASC LIMIT 0,430) AS `Limit1`
Instead of
FROM (
`Extent1`.`ID`,
FROM `tblinvoice` AS `Extent1`
WHERE 3 = `Extent1`.`EID`
ORDER BY
`Extent1`.`C4` ASC LIMIT 0,430) AS `Limit1`
--Note--
The actual query selects around 15 columns, I've just shortened it to this example, it has an effect on the search as the dataset grows in size and it shouldn't be selecting all of the fields but i suspect there's an error in my join.
Any help is much appreciated.
I have the following query:
SELECT
std. [Name]
,cl.Name as clsName
,stu.Name as student
,sub.Name as subject
,r.ObtainedMarks
FROM [School.Model.SchoolContext].[dbo].[Standards] std
join [School.Model.SchoolContext].[dbo].ClassSections cl on std.Id = cl.StandardId
join [School.Model.SchoolContext].[dbo].Students stu on cl.id = stu.ClassSectionId
join [School.Model.SchoolContext].[dbo].Subjects sub on std.id = sub.StandardId
join [School.Model.SchoolContext].[dbo].Results r on stu.Id = r.StudentId)
Which show following results:
I would like to add two columns for subjects e.g. English and Maths and remove column Objects marks. The obtained marks should be shown under subject.
Please let me know how can I achieve through SQL query or LINQ in C#.
I have gone through Pivot queries in SQL, but I didn't get much.
Try like this, it maybe won't work as i didn't test it out, but continue from here, PIVOT is the thing you wish for
SELECT * FROM (
SELECT
std. [Name]
,cl.Name as clsName
,stu.Name as student
,sub.Name as subject
,r.ObtainedMarks
FROM [School.Model.SchoolContext].[dbo].[Standards] std
join [School.Model.SchoolContext].[dbo].ClassSections cl on std.Id = cl.StandardId
join [School.Model.SchoolContext].[dbo].Students stu on cl.id = stu.ClassSectionId
join [School.Model.SchoolContext].[dbo].Subjects sub on std.id = sub.StandardId
join [School.Model.SchoolContext].[dbo].Results r on stu.Id = r.StudentId)
) SRC
PIVOT
(
MAX(ObtainedMarks) FOR [Subject] IN ([English], [Maths])
) piv