Author |
Topic |
dhani
Posting Yak Master
132 Posts |
Posted - 2010-09-23 : 18:51:48
|
Hello All,Please help me here, to solve this query, i included in detail, what i trying to achieve for-- Create Table DDLSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[TestTable2]( [code] [varchar](5) NOT NULL, [midvendor] [varchar](5) NOT NULL, [odate] [varchar](6) NOT NULL, [orderid] [varchar](10) NULL, [marketid] [varchar](5) NOT NULL, [Amount] [float] NULL, [MagicAmount] [float] NULL, [Units] [int] NULL, [MagicUnits] [int] NULL, [Final] [varchar](10) NULL, [Reason] [varchar](30) NULL)GOSET ANSI_PADDING OFF---- Data Insert DDLinsert into testtable2 values ('BAY1','MAD','DEC21',NULL,'1070',0,48526,NULL,8,NULL,NULL)insert into testtable2 values ('BAY1','MAD','DEC21','161711','1070',70965,0,9,NULL,NULL,NULL)insert into testtable2 values ('BAY1','MKA','DEC21',NULL,'1070',0,372419,NULL,43,NULL,NULL)insert into testtable2 values ('BAY1','MKA','DEC21','161891','1070',389392,0,49,NULL,NULL,NULL)insert into testtable2 values ('BAY1','MLD','DEC21','162102','1070',0,0,0,NULL,NULL,NULL)insert into testtable2 values ('BAY1','MLD','DEC21',NULL,'1070',0,35408,NULL,446,NULL,NULL)insert into testtable2 values ('BAY1','MLD','DEC21','M1620','1070',35408,0,446,NULL,NULL,NULL)insert into testtable2 values ('BAY1','NOD','DEC21',NULL,'1070',0,38053,NULL,359,NULL,NULL)insert into testtable2 values ('BAY1','NOD','DEC21','MO20','1070',38053,0,286,NULL,NULL,NULL)insert into testtable2 values ('BAY1','NOD','DEC21','MO21','1070',0,0,73,NULL,NULL,NULL)insert into testtable2 values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,5,NULL,NULL)insert into testtable2 values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,10,NULL,NULL)insert into testtable2 values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,20,NULL,NULL)insert into testtable2 values ('BAY1','MTT','DEC21','405423','1070',170,0,7,NULL,NULL,NULL)insert into testtable2 values ('BAY1','MTT','DEC21','405534','1070',130,0,28,NULL,NULL,NULL)insert into testtable2 values ('BAY1','MVT','DEC21',NULL,'1070',0,117216,NULL,223,NULL,NULL)insert into testtable2 values ('BAY1','MVT','DEC21','4055-8','1070',117216,0,223,NULL,NULL,NULL)insert into testtable2 values ('BAY1','TOM','DEC21',NULL,'1070',0,121328,NULL,118,NULL,NULL)insert into testtable2 values ('BAY1','TOM','DEC21','F4054','1070',1213,0,118,NULL,NULL,NULL)insert into testtable2 values ('BAY1','SAM','DEC21',NULL, '1070',0,425702,NULL,277,NULL,NULL)insert into testtable2 values ('BAY1','SAM','DEC21','F0556','1070',425702,0,278,NULL,NULL,NULL)--truncate table testtable2select * from testtable2this is assumption data, and there is no keys in the table--Check 1for each set of code,midvendor,oDate,Marketid need to calculate sum(Amount), Sum(MagicAmount) if sum(amount)= sum (MagicAmount) then --Check 2for each set of code,midvendor,oDate,Marketid need to calculate sum(Amount), Sum(MagicAmount) if sum(units)= sum (Magicunits) then if both checks true / passes then all of the rows in that set (code,midvendor,odate,marketid) final column value will be Good, if not then all rows will be Badexpecting final result will be CREATE TABLE [dbo].[ResultTable]( [code] [varchar](5) NOT NULL, [midvendor] [varchar](5) NOT NULL, [odate] [varchar](6) NOT NULL, [orderid] [varchar](10) NULL, [marketid] [varchar](5) NOT NULL, [Amount] [float] NULL, [MagicAmount] [float] NULL, [Units] [int] NULL, [MagicUnits] [int] NULL, [Final] [varchar](10) NULL, [Reason] [varchar](30) NULL)insert into ResultTable values ('BAY1','MAD','DEC21',NULL,'1070',0,48526,NULL,8,'BAD','Amounts Units Match')insert into ResultTable values ('BAY1','MAD','DEC21','161711','1070',70965,0,9,'BAD','Amounts Units Match')insert into ResultTable values ('BAY1','MKA','DEC21',NULL,'1070',0,372419,NULL,43,'BAD','Amounts Units Match')insert into ResultTable values ('BAY1','MKA','DEC21','161891','1070',389392,0,49,NULL,'BAD','Amounts Units Match')insert into ResultTable values ('BAY1','MLD','DEC21','162102','1070',0,0,0,NULL,'Good',NULL)insert into ResultTable values ('BAY1','MLD','DEC21',NULL,'1070',0,35408,NULL,446,'Good',NULL)insert into ResultTable values ('BAY1','MLD','DEC21','M1620','1070',35408,0,446,NULL,'GooD',NULL)insert into ResultTable values ('BAY1','NOD','DEC21',NULL,'1070',0,38053,NULL,359,'GooD',NULL)insert into ResultTable values ('BAY1','NOD','DEC21','MO20','1070',38053,0,286,NULL,'GooD',NULL)insert into ResultTable values ('BAY1','NOD','DEC21','MO21','1070',0,0,73,NULL,'GooD',NULL)insert into ResultTable values ('BAY1','MTT','DEC21',NULL, '1070',0,100,NULL,5,'GooD',NULL)insert into ResultTable values ('BAY1','MTT','DEC21',NULL, '1070',0,100,NULL,10,'GooD',NULL)insert into ResultTable values ('BAY1','MTT','DEC21',NULL, '1070',0,100,NULL,20,'GooD',NULL)insert into ResultTable values ('BAY1','MTT','DEC21','405423','1070',170,0,7,NULL,'GooD',NULL)insert into ResultTable values ('BAY1','MTT','DEC21','405534','1070',130,0,28,NULL,'GooD',NULL)insert into ResultTable values ('BAY1','MVT','DEC21',NULL,'1070',0,117216,NULL,223,'GooD',NULL)insert into ResultTable values ('BAY1','MVT','DEC21','4055-8','1070',117216,0,223,NULL,'GooD',NULL)insert into ResultTable values ('BAY1','TOM','DEC21',NULL,'1070',0,121328,NULL,118,'BAD','Amounts Mismatch')insert into ResultTable values ('BAY1','TOM','DEC21','F4054','1070',1213,0,118,NULL,'BAD','Amounts Mismatch')insert into ResultTable values ('BAY1','SAM','DEC21',NULL, '1070',0,425702,NULL,277,'BAD','Units Mismatch')insert into ResultTable values ('BAY1','SAM','DEC21','F0556','1070',425702,0,278,NULL,'BAD','Units Mismatch')Thanks in advanceDhani |
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-24 : 05:24:26
|
Here is your query:UPDATE TSET T.Final = CASE WHEN F.SumAmt = F.SumMagicAmt THEN CASE WHEN F.SumUnit = F.SumMagicUnits THEN 'GOOD' ELSE 'BAD' END ELSE 'BAD' END, T.Reason = CASE WHEN (F.SumAmt <> F.SumMagicAmt) AND (F.SumUnit <> F.SumMagicUnits) THEN 'Amounts Units Mismatch' WHEN (F.SumAmt = F.SumMagicAmt) AND (F.SumUnit <> F.SumMagicUnits) THEN 'Units Mismatch' WHEN (F.SumAmt <> F.SumMagicAmt) AND (F.SumUnit = F.SumMagicUnits) THEN 'Amounts Mismatch' ELSE NULL ENDFROM TestTable2 TINNER JOIN ( SELECT code,midvendor,oDate,Marketid ,SUM(Amount) AS [SumAmt],SUM(MagicAmount)AS [SumMagicAmt],SUM(Units) AS [SumUnit],SUM (MagicUnits) AS [SumMagicUnits] FROM TestTable2 GROUP BY code,midvendor,oDate,Marketid)F ON T.code = F.code AND T.midvendor = F.midvendor AND T.oDate = F.oDate AND T.Marketid = F.Marketid Thanks Rohit |
 |
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2010-09-24 : 09:02:52
|
declare @TEST table ([code] [varchar](5) NOT NULL,[midvendor] [varchar](5) NOT NULL,[odate] [varchar](6) NOT NULL,[orderid] [varchar](10) NULL,[marketid] [varchar](5) NOT NULL,[Amount] [float] NULL,[MagicAmount] [float] NULL,[Units] [int] NULL,[MagicUnits] [int] NULL,[Final] [varchar](10) NULL,[Reason] [varchar](30) NULL)insert into @TEST values ('BAY1','MAD','DEC21',NULL,'1070',0,48526,NULL,8,NULL,NULL)insert into @TEST values ('BAY1','MAD','DEC21','161711','1070',70965,0,9,NULL,NULL,NULL)insert into @TEST values ('BAY1','MKA','DEC21',NULL,'1070',0,372419,NULL,43,NULL,NULL)insert into @TEST values ('BAY1','MKA','DEC21','161891','1070',389392,0,49,NULL,NULL,NULL)insert into @TEST values ('BAY1','MLD','DEC21','162102','1070',0,0,0,NULL,NULL,NULL)insert into @TEST values ('BAY1','MLD','DEC21',NULL,'1070',0,35408,NULL,446,NULL,NULL)insert into @TEST values ('BAY1','MLD','DEC21','M1620','1070',35408,0,446,NULL,NULL,NULL)insert into @TEST values ('BAY1','NOD','DEC21',NULL,'1070',0,38053,NULL,359,NULL,NULL)insert into @TEST values ('BAY1','NOD','DEC21','MO20','1070',38053,0,286,NULL,NULL,NULL)insert into @TEST values ('BAY1','NOD','DEC21','MO21','1070',0,0,73,NULL,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,5,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,10,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,20,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21','405423','1070',170,0,7,NULL,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21','405534','1070',130,0,28,NULL,NULL,NULL)insert into @TEST values ('BAY1','MVT','DEC21',NULL,'1070',0,117216,NULL,223,NULL,NULL)insert into @TEST values ('BAY1','MVT','DEC21','4055-8','1070',117216,0,223,NULL,NULL,NULL)insert into @TEST values ('BAY1','TOM','DEC21',NULL,'1070',0,121328,NULL,118,NULL,NULL)insert into @TEST values ('BAY1','TOM','DEC21','F4054','1070',1213,0,118,NULL,NULL,NULL)insert into @TEST values ('BAY1','SAM','DEC21',NULL, '1070',0,425702,NULL,277,NULL,NULL)insert into @TEST values ('BAY1','SAM','DEC21','F0556','1070',425702,0,278,NULL,NULL,NULL);with myCTE as(select code,midvendor,oDate,Marketid ,case when ( SUM(amount) = sum(MagicAmount)) then 1 else 0 end 'AmountMatch',case when ( SUM(units) = sum(Magicunits)) then 1 else 0 end 'UnitMatch'from @TESTgroup by code,midvendor,oDate,Marketid)select t.code,t.midvendor,t.oDate,t.Marketid,t.orderid,t.Amount,t.MagicAmount,t.Units,t.MagicUnits ,case when m.AmountMatch + m.UnitMatch=2 then 'Good' else 'Bad' end 'Final',case when m.AmountMatch + m.UnitMatch=2 then null when m.AmountMatch + m.UnitMatch=0 then 'Amounts Units Mis Match' else case when m.AmountMatch = 1 then 'Amounts Mis Match' else 'Units Mis Match' end end 'Reason'from @TEST tleft outer join myCTE mon t.code = m.code and t.midvendor = m.midvendor and t.oDate=m.odate and t.Marketid=m.marketid |
 |
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-24 : 10:26:45
|
@kunal.mehtaWhere is the UPDATE part??? dhani needs to update the columns Final & Reason.quote: Originally posted by kunal.mehta declare @TEST table ([code] [varchar](5) NOT NULL,[midvendor] [varchar](5) NOT NULL,[odate] [varchar](6) NOT NULL,[orderid] [varchar](10) NULL,[marketid] [varchar](5) NOT NULL,[Amount] [float] NULL,[MagicAmount] [float] NULL,[Units] [int] NULL,[MagicUnits] [int] NULL,[Final] [varchar](10) NULL,[Reason] [varchar](30) NULL)insert into @TEST values ('BAY1','MAD','DEC21',NULL,'1070',0,48526,NULL,8,NULL,NULL)insert into @TEST values ('BAY1','MAD','DEC21','161711','1070',70965,0,9,NULL,NULL,NULL)insert into @TEST values ('BAY1','MKA','DEC21',NULL,'1070',0,372419,NULL,43,NULL,NULL)insert into @TEST values ('BAY1','MKA','DEC21','161891','1070',389392,0,49,NULL,NULL,NULL)insert into @TEST values ('BAY1','MLD','DEC21','162102','1070',0,0,0,NULL,NULL,NULL)insert into @TEST values ('BAY1','MLD','DEC21',NULL,'1070',0,35408,NULL,446,NULL,NULL)insert into @TEST values ('BAY1','MLD','DEC21','M1620','1070',35408,0,446,NULL,NULL,NULL)insert into @TEST values ('BAY1','NOD','DEC21',NULL,'1070',0,38053,NULL,359,NULL,NULL)insert into @TEST values ('BAY1','NOD','DEC21','MO20','1070',38053,0,286,NULL,NULL,NULL)insert into @TEST values ('BAY1','NOD','DEC21','MO21','1070',0,0,73,NULL,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,5,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,10,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21',NULL,'1070',0,100,NULL,20,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21','405423','1070',170,0,7,NULL,NULL,NULL)insert into @TEST values ('BAY1','MTT','DEC21','405534','1070',130,0,28,NULL,NULL,NULL)insert into @TEST values ('BAY1','MVT','DEC21',NULL,'1070',0,117216,NULL,223,NULL,NULL)insert into @TEST values ('BAY1','MVT','DEC21','4055-8','1070',117216,0,223,NULL,NULL,NULL)insert into @TEST values ('BAY1','TOM','DEC21',NULL,'1070',0,121328,NULL,118,NULL,NULL)insert into @TEST values ('BAY1','TOM','DEC21','F4054','1070',1213,0,118,NULL,NULL,NULL)insert into @TEST values ('BAY1','SAM','DEC21',NULL, '1070',0,425702,NULL,277,NULL,NULL)insert into @TEST values ('BAY1','SAM','DEC21','F0556','1070',425702,0,278,NULL,NULL,NULL);with myCTE as(select code,midvendor,oDate,Marketid ,case when ( SUM(amount) = sum(MagicAmount)) then 1 else 0 end 'AmountMatch',case when ( SUM(units) = sum(Magicunits)) then 1 else 0 end 'UnitMatch'from @TESTgroup by code,midvendor,oDate,Marketid)select t.code,t.midvendor,t.oDate,t.Marketid,t.orderid,t.Amount,t.MagicAmount,t.Units,t.MagicUnits ,case when m.AmountMatch + m.UnitMatch=2 then 'Good' else 'Bad' end 'Final',case when m.AmountMatch + m.UnitMatch=2 then null when m.AmountMatch + m.UnitMatch=0 then 'Amounts Units Mis Match' else case when m.AmountMatch = 1 then 'Amounts Mis Match' else 'Units Mis Match' end end 'Reason'from @TEST tleft outer join myCTE mon t.code = m.code and t.midvendor = m.midvendor and t.oDate=m.odate and t.Marketid=m.marketid
|
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-25 : 03:52:38
|
Much simpler...Update T set T.Final=case when a1=a2 and a3=a4 then 'Good' else 'Bad' end from (select *,sum(amount)over(partition by code,midvendor,odate,marketid)a1,sum(MagicAmount)over(partition by code,midvendor,odate,marketid)a2,sum(units)over(partition by code,midvendor,odate,marketid)a3,sum(Magicunits)over(partition by code,midvendor,odate,marketid)a4 from testtable2)T PBUH |
 |
|
|
|
|