Query that Gets Transactions a user can take in SQLServer - c#

I have a tblData and a tblUser.
I only want to display the transactions that the user can take.
The transactions are linked by a DisplayNum, if one of the Transactions in the DisplayNum does not match the TransType in the tblUser then the user cannot take any TransType in that DisplayNum.

The final version put together after some chatting:
SELECT DISTINCT q2.Id, q3.SubQ, q1.DisplayNum, q1.TransType, q1.TotalTransTime, q1.UserId
FROM (
SELECT D.DisplayNum, HighestTransTime.TransType, SUM(D.TransTime) AS TotalTransTime, U.UserId
FROM tblData D
INNER JOIN tblUser U ON D.TransType=U.TransType
INNER JOIN
(
SELECT DISTINCT innerQuery.DisplayNum, TransType
FROM tblData
INNER JOIN
(
SELECT DisplayNum, MAX(TransTime) AS TransTime FROM tblData GROUP BY DisplayNum
) innerQuery ON tblData.DisplayNum = innerQuery.DisplayNum AND tblData.TransTime = innerQuery.TransTime
) HighestTransTime ON D.DisplayNum=HighestTransTime.DisplayNum
WHERE U.UserId = 10
AND D.TransType IN (SELECT TransType FROM tblUser WHERE tblUser.UserId = U.UserId)
AND D.DisplayNum NOT IN (SELECT DisplayNum FROM tblData WHERE TransType NOT IN (SELECT TransType FROM tblUser WHERE tblUser.UserId = U.UserId))
GROUP BY D.DisplayNum, HighestTransTime.TransType, U.UserId
) q1
INNER JOIN (SELECT DisplayNum, MAX(ID) AS ID FROM tblData GROUP BY DisplayNum) q2 ON q1.DisplayNum = q2.DisplayNum
INNER JOIN (SELECT SubQ, ID FROM tblData) q3 ON q2.ID=q3.ID
ORDER BY q2.ID

As far I can understand you could use simple join on TransType column.
SELECT *
FROM dbo.tblData INNER JOIN dbo.tblUser ON
dbo.tblData .TransType = dbo.tblUser .TransType

Related

Converting SQL to LINQ using Group By and Max

Is it even possible to write the following query using LINQ? I've been fighting all day, :(
select * from ProductGroup pg
Inner join (
select max(DateShipped) as ShipDate
, ProductId
from Lot lt
group by ProductId) lte
on pg.ProductId = lte.ProductId
Solution1
I tested this on LinqPad using LinqToSQL.
var data = from lt in Lots
group lt by lt.ProductId into grp
join pg in ProductGroups on grp.Key equals pg.ProductId
select new { pg.ProductId,pg.ProductName,ShipDate =grp.Max (g => g.ShipDate)};
data.Dump();
and it produced this SQL (almost like your sql , just order of tables is switched)
SELECT [t2].[ProductId], [t2].[ProductName], [t1].[value] AS [ShipDate]
FROM (
SELECT MAX([t0].[ShipDate]) AS [value], [t0].[ProductId]
FROM [Lot] AS [t0]
GROUP BY [t0].[ProductId]
) AS [t1]
INNER JOIN [ProductGroup] AS [t2] ON [t1].[ProductId] = [t2].[ProductId]
Solution2
This is one way to force a correlated sub-query which produces same result as you want but without a join/group by
var data = from pg in ProductGroups
let sd = Lots.Where (l => l.ProductId == pg.ProductId ).Max(l => l.ShipDate )
select new { pg.ProductId,pg.ProductName,ShipDate =sd};
data.Dump();
and it generated this SQL
SELECT [t0].[ProductId], [t0].[ProductName], (
SELECT MAX([t1].[ShipDate])
FROM [Lot] AS [t1]
WHERE [t1].[ProductId] = [t0].[ProductId]
) AS [sd]
FROM [ProductGroup] AS [t0]
I hope it will help you
Try this:
var result = (from pg in ProductGroup
join lte in
(
from lt in Lot
group lt by lt.ProductId into gr
select new
{
ProductId = gr.Key,
ShipDate = gr.Max(x => x.DateShipped)
}
)
on pg.ProductId equals lte.ProductId
select new
{
ID = pg.ProductId,
MaxShipDate = lte.ShipDate
}).ToList();

Creating Linq from SQL with OrderBy and GroupBy

I have the following table structure.
TableA TableB TableC
- MID - PID - PID
- NAME - INIT_DATE - MID
This is the SQL Query that I need to translate into Linq
SELECT TOP 10 TableA.NAME,
COUNT(TableB.INIT_DATE) AS [TOTALCOUNT]
FROM TableC
INNER JOIN TableA ON TableC.MID = TableA.MID
LEFT OUTER JOIN TableB ON TableC.PID = TableB.PID
GROUP BY TableA.NAME
ORDER BY [TOTALCOUNT] DESC
I tried to reproduce the above query with this Linq query:
iqModel = (from tableC in DB.TableC
join tableA in DB.TableA on tableC.MID equals tableA.MID
select new { tableC, tableA } into TM
join tableB in DB.TableB on TM.tableC.PID equals J.PID into TJ
from D in TJ.DefaultIfEmpty()
select new { TM, D } into MD
group MD by MD.TM.tableA.NAME into results
let TOTALCOUNT = results.Select(item=>item.D.INIT_DATE).Count()
orderby TOTALCOUNT descending
select new SelectListItem
{
Text = results.Key.ToString(),
Value = TOTALCOUNT.ToString()
}).Take(10);
But I think I am doing something wrong.
The Output of the LINQ and SQL is not same. I think up to JOIN or GROUPBY it is Correct.
EDIT :-
I have also tried the following Linq query but still it's not working correctly.
var iqModel = (from c in DB.TableC
join a in DB.TableA on c.MID equals a.MID
join b in DB.b on c.PID equals b.PID into b_join
from b in b_join.DefaultIfEmpty()
select new SelectListItem { Text = a.NAME, Value = b.INIT_DATE != null ? b.INIT_DATE.ToString() : string.Empty });
var igModel = iqModel.GroupBy(item => item.Text);
var result = igModel.OrderByDescending(item => item.Select(r => r.Value).Count());
I want to understand what am I doing wrong and how can it be fixed.
I am newbie to LINQ to SQL I think in above LINQ I really made it complicated by adding more select.
I think the difference is caused by the fact that the SQL COUNT(field) function does not include NULL values. There is no direct equivalent construct in LINQ, but it could be simulated with Count(e => e.Field != null) or like this (which seems to produce better SQL):
var query =
(from a in db.TableA
join c in db.TableC on a.MID equals c.MID
join b in db.TableB on c.PID equals b.PID into joinB
from b in joinB.DefaultIfEmpty()
group b by a.Name into g
let TOTALCOUNT = g.Sum(e => e.INIT_DATE != null ? 1 : 0)
orderby TOTALCOUNT descending
select new SelectListItem { Text = g.Key, Value = TOTALCOUNT }
).Take(10);
which generates the following SQL
SELECT TOP (10)
[Project1].[C2] AS [C1],
[Project1].[Name] AS [Name],
[Project1].[C1] AS [C2]
FROM ( SELECT
[GroupBy1].[A1] AS [C1],
[GroupBy1].[K1] AS [Name],
1 AS [C2]
FROM ( SELECT
[Join2].[K1] AS [K1],
SUM([Join2].[A1]) AS [A1]
FROM ( SELECT
[Extent1].[Name] AS [K1],
CASE WHEN ([Extent3].[INIT_DATE] IS NOT NULL) THEN 1 ELSE 0 END AS [A1]
FROM [dbo].[TableAs] AS [Extent1]
INNER JOIN [dbo].[TableCs] AS [Extent2] ON [Extent1].[MID] = [Extent2].[MID]
LEFT OUTER JOIN [dbo].[TableBs] AS [Extent3] ON [Extent2].[PID] = [Extent3].[PID]
) AS [Join2]
GROUP BY [K1]
) AS [GroupBy1]
) AS [Project1]
ORDER BY [Project1].[C1] DESC
I assume, that you not see "group by" command at resulting query, instead of it "distinct" command is used. Am I right?
First query makes distinct by TableA.NAME and then calculates COUNT(TableB.INIT_DATE) with the help of subquery like this:
select distinct1.Name, (select count() from *join query* where Name = distinct1.Name)
from (select distinct Name from *join query*) as distinct1
If so, not worry about it. Because conversion from linq to real t-sql script sometimes very unpredictable (you can not force them to be equal, only when query is very simple), but both queries are equivalent one to another and return same results (compare them to make sure).

joining to tables in a query for ASP.NET C# ~ multi-part-identifier-could-not-be-bound

Among other answers I have looked at the following:
The multi-part identifier could not be bound
However tying to use that answer I was still not able to make my grouping work.
The following query works:
string query = #"SELECT
ud.FirstName,
ud.LastName,
p.Name AS Product,
p.ItemNumber,
sc.AmountPurchased * sc.Price AS Total,
sc.DatePurchased,
sc.IsInCart AS [NotShipped]
FROM
ShoppingCarts sc INNER JOIN
UserDetails ud ON sc.ClientID = ud.Guid Left
OUTER JOIN
Products p ON sc.ProductID = p.ProductsId
WHERE sc.DatePurchased >= #date1
AND sc.DatePurchased <= #date2
AND sc.IsInCart = #shipped ";
but when I try to group it like this:
string query = #"SELECT ud.LastName, sc.DatePurchased, SUM(Total)
FROM(
SELECT
ud.FirstName,
ud.LastName,
p.Name AS Product,
p.ItemNumber,
sc.AmountPurchased * sc.Price AS Total,
sc.DatePurchased,
sc.IsInCart AS [NotShipped]
FROM
ShoppingCarts sc INNER JOIN
UserDetails ud ON sc.ClientID = ud.Guid Left
OUTER JOIN
Products p ON sc.ProductID = p.ProductsId
WHERE sc.DatePurchased >= #date1
AND sc.DatePurchased <= #date2
AND sc.IsInCart = #shipped
) AS result
GROUP BY ud.LastName, sc.DatePurchased";
I get the "multi-part identifier ud.LastName, etc cannot be bound"
When doing nested selects the aliases do not propagate out of their scope. This is more evident when looking at the equivalent CTE query (assuming you're using MS SQL Server / T-SQL):
;WITH
(
SELECT
ud.FirstName,
ud.LastName,
p.Name AS Product,
p.ItemNumber,
sc.AmountPurchased * sc.Price AS Total,
sc.DatePurchased,
sc.IsInCart AS [NotShipped]
FROM
ShoppingCarts sc INNER JOIN
UserDetails ud ON sc.ClientID = ud.Guid Left
OUTER JOIN
Products p ON sc.ProductID = p.ProductsId
WHERE sc.DatePurchased >= #date1
AND sc.DatePurchased <= #date2
AND sc.IsInCart = #shipped
) AS Result
SELECT
LastName,
DatePurchased,
SUM(Total)
FROM Result
GROUP BY
LastName,
DatePurchased
So your problem is solved by removing the ud and sc identifiers from the outer select and group by.

Get records on the base of one field which should distinct

i have query like
SELECT distinct(a.CreatedOn),
a.id AS ID ,
u1.FirstName CreatedByFirstName,
u1.LastName
FROM AuditDetail a
LEFT JOIN UserProfile u1 ON a.CreatedBy = u1.UserProfileID
LEFT JOIN UserProfile u2 ON a.UpdatedBy = u2.UserProfileID
this shows me all the records. and distinct createdon also repeated in each records because all the records comming. i want that if the createdon is same in all the records then its first record or single record should come not all. where i'm wrong?
You can use Ranking Function such as ROW_NUMBER() which give rank on each record for every group.
WITH recordList
AS
(
SELECT a.CreatedOn,
a.id as ID ,
u1.FirstName CreatedByFirstName,
u1.LastName,
ROW_NUMBER() OVER(PARTITION BY a.CreatedOn ORDER BY a.CreatedOn) rn
FROM AuditDetail a
left join UserProfile u1
on a.CreatedBy = u1.UserProfileID
left join UserProfile u2
on a.UpdatedBy = u2.UserProfileID
)
SELECT CreatedOn,
ID,
CreatedByFirstName,
LastName
FROM recordList
WHERE rn = 1
Try this one,
Might will help you, used subquery
SELECT a.CreatedOn,
a.id AS ID ,
u1.FirstName CreatedByFirstName,
u1.LastName
FROM AuditDetail a
LEFT JOIN UserProfile u1 ON a.CreatedBy = u1.UserProfileID LEFT joinUserProfileu2 ON a.UpdatedBy = u2.UserProfileID
WHERE a.CreatedOn IN
(SELECT DISTINCT CreatedOn
FROM AuditDetail)

Linq Combine Left Join Data

Say I have the following database:
Users
-------
UserId (PK)
UserName
Roles
-----
RoleId (PK)
RoleName
UserRoles
---------
UserId (PK)
RoleId (PK)
Users 1-M UserRoles M-1 Roles
Using LinqToSQL, I want to return the following set:
[User1], [Role1, Role2, Role3]
[User2], [Role2, Role3]
[User3], []
Etc...
What is the most efficient way to create this LinqToSql Query?
In addition, if I want to create a filter to return only users that have Role1, what would that entail?
Thx.
Define "efficient". But otherwise...
from u in dataContext.Users
select new { User = u, Roles = u.UserRoles.Select(ur => ur.Role) }
And filtering users by RoleID:
from u in dataContext.Users
where u.UserRoles.Any(ur => ur.RoleID == 1)
select u
Or by some other Role attribute, say, Name:
from u in dataContext.Users
where u.UserRoles.Any(ur => ur.Role.Name == "Role 1")
select u
Combining it all together:
from u in dataContext.Users
select new
{
User = u,
Roles = from ur in u.UserRoles
where ur.RoleID == 1 || ur.Role.Name == "Role 1"
select ur.Role
}
This is the single query that I would construct to get your desired result set all at once
from u in Users
join ur in UserRoles on u.UserId equals ur.UserId
join r in Roles on ur.RoleId equals r.RoleId
group r by u into grouping
select grouping
It produces the following SQL:
SELECT [t0].[UserId], [t0].[Username]
FROM [Users] AS [t0]
INNER JOIN [UserRoles] AS [t1] ON [t0].[UserId] = [t1].[UserId]
INNER JOIN [Roles] AS [t2] ON [t1].[RoleId] = [t2].[RoleId]
GROUP BY [t0].[UserID], [t0].[Username]
GO
-- Region Parameters
DECLARE #x1 Int = 2
-- EndRegion
SELECT [t2].[RoleId], [t2].[RoleName]
FROM [Users] AS [t0]
INNER JOIN [UserRoles] AS [t1] ON [t0].[UserId] = [t1].[UserId]
INNER JOIN [Roles] AS [t2] ON [t1].[RoleId] = [t2].[RoleId]
WHERE #x1 = [t0].[UserId]
#Pavel's looks like it produces a better SQL statement:
SELECT [t0].[UserId], [t0].[Username], [t2].[RoleId], [t2].[RoleName] (
SELECT COUNT(*)
FROM [UserRoles] AS [t3]
INNER JOIN [Roles] AS [t4] ON [t4].[RoleId] = [t3].[RoleId]
WHERE [t3].[UserId] = [t0].[UserId]
) AS [value]
FROM [Users] AS [t0]
LEFT OUTER JOIN ([UserRoles] AS [t1]
INNER JOIN [Roles] AS [t2] ON [t2].[RoleId] = [t1].[RoleId]) ON [t1].[UserId] = [t0].[UserId]
ORDER BY [t0].[UserId], [t1].[UserRoleId], [t2].[RoleId]
In terms of efficient, testing is going to be the best way to figure out what most performant approach is for your situation.

Categories