|
So I have this SQL Linq statement, pretty easy until I added Free Shipping.
In the original statement, I get the data from ORDER_COMPLETED and do a join to SHIPPING_REALTIME in which I grab the Realtime rate name.
But Now, I added Free Shipping, by using another table that holds my criteria. So now I have 2 tables to get the rate name from, the other called SHIPPING_FREEMATRIX
I'm trying to think of a better more proper way to do this.
So what I have so far is
pResults = _
(
From oc In context.Order_Completed
Join srt In context.Shipping_Realtime On srt.RateAPICode Equals oc.RateAPICode
Where oc.OrderStatus = "QUEUED_FOR_FULFILLMENT" _
Or oc.OrderStatus = "OUT_FOR_FULFILLMENT" _
Or oc.OrderStatus = "ORDER_CANCELED" _
And oc.OrderDate >= DbFunctions.AddDays(Today, -7)
Order By oc.OrderNumber
Select New coOrderIndex With
{
.OrderID = oc.CompletedOrderID,
.OrderDate = oc.OrderDate,
.OrderNumber = oc.OrderNumber,
.OrderStatus = oc.OrderStatus,
.LoginID = oc.LoginID,
.GrandTotal = oc.GrandTotal,
.RateAPICode = oc.RateAPICode,
.RateCharge = oc.TotalNetCharge,
.RateAPIName = If(srt.RateName Is Nothing, "", srt.RateName)
}
).AsEnumerable()
pValue = pResults.Count()
I tried using a tenary operator on RateName, but got an error saying First or Single can only be used at the end of the statement.
.RateAPIName = If(
I tried a function but got the error can't execute an external function within a store statement
.RateAPIName = get_rateAPIName(oc.RateAPICode)
I tried a double join but got nothing
Join srt In context.Shipping_Realtime On srt.RateAPICode Equals oc.RateAPICode
Join fsm In context.Shipping_FreeMatrix On fsm.RateAPICode Equals oc.RateAPICode
I thought about taking the pResults and just looping back through using for each and populating the RateAPI Name, but I tried the above methods instead and aborted this thought.
for each pResult as coOrderIndex in pResults
pResult.RateAPIName =
next
If it was a single record that was easy for me, but doing it with multiple records is, well I'm not sure how to proceed.
|
|
|
|
|
I assume the SHIPPING_FREEMATRIX is not having corresponding rows for all rows in ORDER_COMPLETED.
If that's the case you'll need to do an outer join.
Here's[^] how to do that in query syntax.
|
|
|
|
|
That was hard to do. I got confused on whether to do a Join and Outer Join, so I had to experiment with it. But I got it to work in LinqPad4
Dim orders = _
(
From oc In context.Order_Completed
Group Join srs In context.Shipping_Realtime On oc.RateAPICode Equals srs.RateAPICode Into rRates = Group From srs In rRates.DefaultIfEmpty()
Group Join fsm In Context.Shipping_Free_Matrix On oc.RateAPICode Equals fsm.RateAPICode Into fRates = Group From fsm In fRates.DefaultIfEmpty()
Where oc.OrderStatus = "QUEUED_FOR_FULFILLMENT" _
Or oc.OrderStatus = "OUT_FOR_FULFILLMENT" _
Or oc.OrderStatus = "ORDER_CANCELED" _
And oc.OrderDate >= DbFunctions.AddDays(Today, -7)
Order By oc.OrderNumber
Select New With
{
.OrderID = oc.CompletedOrderID,
.OrderDate = oc.OrderDate,
.OrderNumber = oc.OrderNumber,
.OrderStatus = oc.OrderStatus,
.LoginID = oc.LoginID,
.GrandTotal = oc.GrandTotal,
.RateAPICode = oc.RateAPICode,
.RateCharge = oc.TotalNetCharge,
.RateAPIName = If(Not srs.RateName is Nothing, srs.RateName, fsm.RateName)
}
).AsEnumerable()
Thanks for the point in the right direction!
I didn't know what to search for.
|
|
|
|
|
Personally I prefer Method syntax[^] to Query syntax as the similarity to SQL confuses me rather than helps me.
|
|
|
|
|
I've been writing more method syntax in my new MVC projects in c#.
I guess I was trying to just get this to work, and didn't want to totally rewrite it.
This isn't a new project, just an expansion of my web forms based eCommerce program that I converted to Entity Framework over the summer, and flattened out the tables like you suggested last year, a total database overhaul. But for whats it worth, your advice has finally paid off for me, I haven't a complaint in over 30 days now. I'm stilling getting the hang of SQL Linq, and my Data Access Layer.
All I have left now is to convert my web forms based app to a responsive design using the twitter bootstrap, and go back to the DAL for automatic database creation.
If I haven't thanked you yet for the help and advice you gave me, then thank you very much. And yes I did implement it all.
|
|
|
|
|
|
Hi all,
There is a bad game going on here that there was a column in the table that's dropped. Can anybody please help me in finding who has dropped that column. Anything like that like table, column or a view I want to check who dropped it.
I am also googling, if anybody can help it would be a great help, any link, suggestion or code snippet would be greatly helpful.
Thanks in advance buddy.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Profiler might help.
If you are not tracking it this is a problem.
In the future you might look at setting up SQL Sever Profiler or using a third party (we use Idera Compliance Manager).
Mongo: Mongo only pawn... in game of life.
|
|
|
|
|
Try
SELECT SUSER_SNAME([Transaction SID])
FROM fn_dblog (NULL, NULL)
WHERE [Transaction Name] = 'DROPOBJ'
e.g.
USE [TestDB]
GO
CREATE TABLE [dbo].[tblTestEmployee]
(
[EmployeeID] [int] IDENTITY(1,1) NOT NULL,
[EmpName] [varchar](100) NOT NULL,
[Address] [varchar](100) NOT NULL
)
INSERT INTO [dbo].[tblTestEmployee]
VALUES('Emp1','Address1'),('Emp2','Address2'),('Emp3','Address3'),('Emp4','Address4')
ALTER TABLE [dbo].[tblTestEmployee] DROP COLUMN [Address]
GO
SELECT SUSER_SNAME([Transaction SID])
FROM fn_dblog (NULL, NULL)
WHERE [Transaction Name] = 'DROPOBJ'
GO
Result
sa
You can also have a look at Audit SQL Server database and see who deleted a column value
Hope this helps
|
|
|
|
|
Have you used the Apex auditor app and if so was there a noticeable performance hit.
Useful bits of info thanks...
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
No, these can only be found if you are logging anything in your system. Like, which account initiated which request, which request made what change and what time it was etc. These type of logged data can help in understanding what went wrong (if the actions were wrong) and who made a change.
If there is no log, then stop searching any further because there ain't any. But this one loss would teach you to create a logging system in your environment. The system would log the changes being made, it would store the user's ID, time of change and what change was made.
In my opinion, you should also keep a backup of your databases so that if (under any case) a table is lost or data is not integrated anymore, you can revert it back to a previous version.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Hi everyone!
My question is the subject of this Post.
Who know that?
|
|
|
|
|
|
And if not Google, then try out MSDN, Understanding Isolation Levels[^]. (It has the answer)
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Hi I have a table Student like below
SName Subject Marks Date
A AAA 10 2011-01-01
B AAB 20 2012-04-01
B AAB 20 2012-03-01
I need sql query to find the 3rd highest marks for each subject. please do the needful
Cloumname DATA
Sname -- A, B, B
Subject - AAA, AAB, AAB
Marks -- 10,20,20
Date --2011-01-01,2012-04-01,2012-03-01
|
|
|
|
|
We are not going to do your homework for you, but will give you hints
There are several ways you could do this - the classic way would be by using the TOP[^] predicate and subqueries, or using the ROW_NUMBER()[^] function, partitioned by subject and ordering by mark.
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Try this
DECLARE @T TABLE(SName VARCHAR(20), Subject VARCHAR(20), Marks INT, ExamDate DATE)
INSERT INTO @T
SELECT 'A', 'Subject1', 77, '2011-01-01' UNION ALL
SELECT 'A', 'Subject2', 97, '2011-01-01' UNION ALL
SELECT 'B', 'Subject1', 80 ,'2012-04-01' UNION ALL
SELECT 'B', 'Subject2', 70, '2012-03-01' UNION ALL
SELECT 'C', 'Subject1', 44, '2011-01-01' UNION ALL
SELECT 'C', 'Subject2', 90, '2011-01-01' UNION ALL
SELECT 'D', 'Subject1', 79 ,'2012-04-01' UNION ALL
SELECT 'D', 'Subject2', 66, '2012-03-01'
SELECT X.*
FROM ( SELECT
t.*
,Rn = DENSE_RANK() OVER(PARTITION BY t.Subject ORDER BY t.Marks DESC)
FROM @T t) X WHERE X.Rn = 3
/*
SName Subject Marks ExamDate Rn
A Subject1 77 2011-01-01 3
B Subject2 70 2012-03-01 3
*/
modified 13-Nov-15 6:52am.
|
|
|
|
|
And I have a question in biology that needs a response for, can you do the needful for that one as well.
This will be pay back when the idjit ends up working with you!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Respected Sir,
Hope you are doing well.
The question being asked can be solved in 2 ways, as per my understanding.
#1: If the data storage format is like StudentRoll,Name,English,Physics,Biology,Maths etc. (which he didn't asked )
In this case, in-order to flatten the data, first we will use UNPIVOT . Once done, the next step will be to use Row_Number() and get the N-th highest marks. Finally , to get the original structure back, we should apply PIVOT.
#2: If the storage format is like SName,Subject,Marks(which he asked )
In this case, since the data is already flattened by Subjects, we must apply the RANKING function (as I applied Row_Number()) here and get the N-th highest one.
Kindly let me know, if my understanding of your question is correct in which case please provide the feedback of the response.
|
|
|
|
|
My apologies, I was being sarcastic. The point I was trying to make is that Chris had given him the pointers where he could research and learn how to do the job. Whereas you gave him the solution, which does not help him learn.
If it was a difficult question then the example may have been the best way to answer. As it is an extremely basic question the OP needs to do some learning/research/school work rather than cut and paste your answer.
Asking someone to "do the needful" is just plain rude.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi All,
I am using the following script to transpose a particular column row as columns, but the problem is I am able to get only one row, for example I have StudentNames, Ids, AggregateMarks, and TotalMarks.
If I use the below approach and generate a column for every student then I am able to get only TotalMarks for each student, but I want to get Ids, AggregateMark also under each studentName
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(StudentNames)
from yourtable
group by StudentNames, id
order by id
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = N'SELECT ' + @cols + N' from
(
select value, ColumnName
from yourtable
) x
pivot
(
max(value)
for ColumnName in (' + @cols + N')
) p '
exec sp_executesql @query;
Can anybody please help me in this, any code snippet or suggestion or a link would be very very helpful, please thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
You could have a look at this[^]
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
I have Printed the dynamic query, it is as below. But same thing how to get that ServiceLevel from the below query, it is printing only Service id for each column in a row, but I want Service Level also in another row, can't we do it? Any sort of help would be very very helpful, please help me I am also trying my best. Thanks in advance.
SELECT [Cartilage Regeneration],[Foot and Ankle Care],[Hand, Wrist and Elbow Care],[Hip Care],[Joint Replacement],[Knee Care],
[Orthopedic Trauma Surgery],[Pediatric Orthopedics],[Shoulder Care],[Sports Medicine],[Tendon and Ligament Repair] from
(
select ServiceLevel, ServiceId, Name
from [Service]
) x
pivot
(
max(ServiceId)
for Name in ([Cartilage Regeneration],[Foot and Ankle Care],[Hand, Wrist and Elbow Care],[Hip Care],[Joint Replacement],
[Knee Care],[Orthopedic Trauma Surgery],[Pediatric Orthopedics],[Shoulder Care],[Sports Medicine],[Tendon and Ligament Repair])
)
p
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
@indian1433, here is a way (The first version is static)
DECLARE @T TABLE(StudentNames VARCHAR(20), Id INT, AggregateMarks INT, TotalMarks INT)
INSERT INTO @T VALUES
('NAME1',1,300,503),
('NAME2',2,250,500),
('NAME3',3,378,504),
('NAME4',4,490,500),
('NAME5',5,399,500)
SELECT X.ID,X.TotalMarks,t1.[AggregateMarks],X.[NAME1],X.[NAME2],X.[NAME3],X.[NAME4],X.[NAME5]
FROM @T
PIVOT
(
MAX(AggregateMarks)
FOR StudentNames
IN ([NAME1],[NAME2],[NAME3],[NAME4],[NAME5])
) AS X
JOIN @T t1 on t1.Id = X.Id
ORDER BY 1
I have done this one to show you how it must go like. The dynamic version follows
CREATE TABLE #T (StudentNames VARCHAR(20), Id INT, AggregateMarks INT, TotalMarks INT)
INSERT INTO #T VALUES
('NAME1',1,300,503),
('NAME2',2,250,500),
('NAME3',3,378,504),
('NAME4',4,490,500),
('NAME5',5,399,500)
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(StudentNames)
from #T
group by StudentNames, id
order by id
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = N'SELECT X.ID,X.TotalMarks,t1.AggregateMarks, ' + @cols + N' from #T
PIVOT
(
MAX(AggregateMarks)
FOR StudentNames IN (' + @cols + N')
) AS X
JOIN #T t1 on t1.Id = X.Id
ORDER BY 1'
print @query
exec sp_executesql @query
DROP TABLE #T
Hope this helps.
modified 31-Oct-15 2:14am.
|
|
|
|
|
|