I believe it's quite simple and for multi column pivot it's the best solution.
I know SQL Server MVP who prefers this case based syntax and never uses PIVOT syntax.
PluralSight Learning Library
Use case based pivot instead of the regular Pivot. Check this blog post that explains it
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/understanding-sql-server-2000-pivot
PluralSight Learning Library
I think your code is OK, but you can also do:
UPDATE LOAD
SET ERR_F = 'W'
OUTPUT INSERTED.*,
'G1',
GETDATE(),
COALESCE((SELECT top (1) EXC_X
FROM ExceptionCode WHERE EXC_C = 'G1')
+ ' : ','') + ACCT,
1
INTO EXCEPTION
WHERE CRIS_Code in (
SELECT...
;with cte as (select *, row_number() over (partition by item order by [Date] DESC) as rn from PricesInfo)
select * from cte where Rn = 1
PluralSight Learning Library
You don't need to join back to CTE, you can UPDATE cte directly, e.g.
UPDATE MaxCounter SET NewILandId = ILandId WHERE RowID = 1
PluralSight Learning Library
You will need to use a cursor to 'loop' through each row in a table. Take a look at this link with the similar concept
http://www.sqlusa.com/bestpractices2005/importimage/
See also this link as how to use cursors the best way (the above sample does not use the proper keywords):
The Truth about...
Are you sure your where clause is correct? Do you keep (ItemNumber) Description as ItemNumber in the Balance table? Is the ItemNumber unique in the Inventory?
Anyway, try:
;with cte as (Select '(' + RTrim(i.ItemNumber) + ') ' + RTrim(s.ItemDescription) as FullItemName...
You will have to execute it dynamically by building the whole string and then executing it execute(@SQL). You need to double every single quote.
PluralSight Learning Library
Your WHERE clause makes no sense. Do you only want to update one row with the Max date? If so, try:
;with cte as (select top (1) FileLoadDate, FileLoadStatus,
CASE WHEN exists
(
SELECT 1
FROM TestSubmission t
WHERE t.Region <> @region OR
t.SUB_TYPE <> @subType OR
t.VENDOR_ID <> @vendorID...
May be:
update SubmissionManagement SET FileLoadStatus = case when exists (SELECT 1
FROM TestSubmission t
WHERE t.Region <> @region OR
t.SUB_TYPE <> @subType OR
t.VENDOR_ID <> @vendorID OR
t.DELIVERY_TYPE <> @deliveryType OR
t.DELIVERY_TYPE <> @deliveryMethod
) then 4 else 1 end...
Dmitry Korotkevich has a blog post on this exact topic
http://aboutsqlserver.com/2010/09/12/sunday-t-sql-tip-uniqueness-of-nullable-field/
PluralSight Learning Library
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.