August 3, 2007 at 2:39 pm
my current sql query :
SELECT GdsSellPriceDet.GdsCode, Gds.GdsName, Gds.CcyCode, Gds.UnitCode, GdsSellPriceDet.CustGroupCode, CustomerGroup.CustGroupName,
GdsSellPriceDet.DefaultSellPrice
FROM GdsSellPriceDet LEFT OUTER JOIN
CustomerGroup ON GdsSellPriceDet.CustGroupCode = CustomerGroup.CustGroupCode LEFT OUTER JOIN
Gds ON GdsSellPriceDet.GdsCode = Gds.GdsCode
and the result is :
GdsCode GdsName CcyCode UnitCode CustGroupCode CustGroupName SellPrice
AAA GOODS AAA USD UNIT RTL RETAIL 1.7
AAA GOODS AAA USD UNIT WHO WHOLESALE 1.5
BBB GOODS BBB USD UNIT RTL RETAIL 1.8
BBB GOODS BBB USD UNIT WHO WHOLESALE 1.8
CCC GOODS CCC USD UNIT RTL RETAIL 2.1
CCC GOODS CCC USD UNIT MOD MODERN MARKET 2.1
CCC GOODS CCC USD UNIT WHO WHOLESALE 2.0
the result i need is below :
GdsCode GdsName CcyCode UnitCode CustGroupCode CustGroupName SellPrice
AAA GOODS AAA USD UNIT RTL RETAIL 1.7
AAA GOODS AAA USD UNIT WHO WHOLESALE 1.5
BBB GOODS BBB USD UNIT RTL RETAIL 1.8
CCC GOODS CCC USD UNIT RTL RETAIL 2.1
CCC GOODS CCC USD UNIT WHO WHOLESALE 2.0
the query i need : if a goods code has 3 records with 2 of them have the same price, the second record with th same price will be skipped ( like the example above ).
i need some advice, would you help me to solve this case ?
thanks alot for your help.....
rom beng
August 3, 2007 at 3:16 pm
August 3, 2007 at 4:44 pm
So why did one of the BBB rows go away? BBB only had two rows...
--Jeff Moden
Change is inevitable... Change for the better is not.
August 3, 2007 at 9:29 pm
to mr jeff moden,
yes, 1 of BBB records will not be included in the new result because has the same price with the other BBB row on purpose to save paper if it is printed.
the current query i mentioned is the normal query, and i want only rows with difference price viewed in the new result.
sorry, i hope you'll understand my english, my english not so good and understand what i mean.
please, any suggestion ? thanks in advance
rom beng
August 3, 2007 at 9:40 pm
to mr bledu,
the priority is the first record found using different price will be viewed in the new result.
in the purposed result, the second BBB record will not be included because has same price with previous BBB shown.
anu suggestion ? please help and thanks alot....
below the table definition :
/****** Object: Table [dbo].[GdsSellPriceDet] Script Date: 8/3/07 3:06:35 PM ******/
CREATE TABLE [dbo].[GdsSellPriceDet] (
[GdsCode] [nvarchar] (20) NOT NULL ,
[CustGroupCode] [nvarchar] (3) NOT NULL ,
[DefaultSellPrice] [decimal](18, 2) NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Gds] Script Date: 8/3/07 3:06:35 PM ******/
CREATE TABLE [dbo].[Gds] (
[GdsCode] [nvarchar] (20) NOT NULL ,
[GdsName] [nvarchar] (100) NULL ,
[BrandCode] [nvarchar] (3) NULL ,
[GdsGroupCode] [nvarchar] (3) NULL ,
[CcyCode] [nvarchar] (3) NULL ,
[UnitCode] [nvarchar] (4) NULL ,
[LastUpdate] [datetime] NULL ,
[UserID] [nvarchar] (10) NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[CustomerGroup] Script Date: 8/3/07 3:06:33 PM ******/
CREATE TABLE [dbo].[CustomerGroup] (
[CustGroupCode] [nvarchar] (3) NOT NULL ,
[CustGroupName] [nvarchar] (50) NULL ,
[LastUpdate] [datetime] NULL ,
[UserID] [nvarchar] (10) NULL
) ON [PRIMARY]
GO
CREATE UNIQUE CLUSTERED INDEX [i_Gds] ON [dbo].[Gds]([GdsCode]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE UNIQUE CLUSTERED INDEX [i_GdsSellPriceDet] ON [dbo].[GdsSellPriceDet]([GdsCode], [CustGroupCode]) ON [PRIMARY]
GO
CREATE UNIQUE CLUSTERED INDEX [i_CustomerGroup] ON [dbo].[CustomerGroup]([CustGroupCode]) ON [PRIMARY]
GO
August 4, 2007 at 5:17 am
Can you define which row to be considered "first"?
There is no natural physical order of rows in tables.
_____________
Code for TallyGenerator
August 4, 2007 at 9:42 am
Rom Beng,
Something like this should do it for you... read the embedded comments for an explanation, please... I did not use your given table structures... this is all based on the data from your first post... if you were to use your first query to populate a temp table similar to that below, you'll be done.
--=======================================================================================
-- Setup and populate a test table.
-- NOTE THAT NOTHING IN THIS SECTION IS PART OF THE SOLUTION. IT'S FOR DEMO ONLY!.
--=======================================================================================
--DROP TABLE #GdsSellPriceDet
--===== Create a test table to run with
CREATE TABLE #GdsSellPriceDet
(
GdsCode VARCHAR( 5),
GdsName VARCHAR(10),
CcyCode VARCHAR( 5),
UnitCode VARCHAR( 5),
CustGroupCode VARCHAR( 5),
CustGroupName VARCHAR(20),
SellPrice DECIMAL(9,1) --Precision of 1 to 9 always takes 5 bytes
)
--===== Populate the test table with the given data
INSERT INTO #GdsSellPriceDet
(GdsCode, GdsName, CcyCode, UnitCode, CustGroupCode, CustGroupName, SellPrice)
SELECT 'AAA','GOODS AAA','USD','UNIT','RTL','RETAIL' ,'1.7' UNION ALL
SELECT 'AAA','GOODS AAA','USD','UNIT','WHO','WHOLESALE' ,'1.5' UNION ALL
SELECT 'BBB','GOODS BBB','USD','UNIT','RTL','RETAIL' ,'1.8' UNION ALL
SELECT 'BBB','GOODS BBB','USD','UNIT','WHO','WHOLESALE' ,'1.8' UNION ALL
SELECT 'CCC','GOODS CCC','USD','UNIT','RTL','RETAIL' ,'2.1' UNION ALL
SELECT 'CCC','GOODS CCC','USD','UNIT','MOD','MODERN MARKET','2.1' UNION ALL
SELECT 'CCC','GOODS CCC','USD','UNIT','WHO','WHOLESALE' ,'2.0'
--=======================================================================================
-- Demo the solution for the given data.
-- In the absence of all other information (like what the PK is), it is assumed
-- that the following columns are sufficient for identifying unique rows...
-- GdsCode, CcyCode, UnitCode, CustGroupCode
--
-- Note, too, that the "dupes" are selected in alpha order according to the
-- CustGroupCode. This means that the precise row you wanted suppressed for CCC is
-- has been replaced by a different row. Again, in the absense of all other info,
-- this is about the best that can be done.
--=======================================================================================
SELECT t3.*
FROM #GdsSellPriceDet t3
LEFT OUTER JOIN
(--==== Derived table "d" finds "dupes" to NOT be shown (alpha order on CustGroupCode)
SELECT t2.GdsCode,t2.CcyCode,t2.UnitCode,t2.CustGroupCode
FROM #GdsSellPriceDet t1,
#GdsSellPriceDet t2
WHERE t2.GdsCode = t1.GdsCode
AND t2.CcyCode = t1.CcyCode
AND t2.UnitCode = t1.UnitCode
AND t2.SellPrice = t1.SellPrice
AND t2.CustGroupCode > t1.CustGroupCode
)d
ON t3.GdsCode = d.GdsCode
AND t3.CcyCode = d.CcyCode
AND t3.UnitCode = d.UnitCode
AND t3.CustGroupCode = d.CustGroupCode
WHERE d.GdsCode IS NULL
ORDER BY t3.GdsCode, t3.CcyCode, t3.UnitCode, t3.CustGroupCode
If you have SQL Server 2005, you could use your original query as a CTE for the above solution.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 4, 2007 at 10:06 am
P.S. If you do decide to go the temp table route as I suggested, then adding the appropriate indexes and a primary key on the temp table may help speed things up a bit, but it's going to be pretty fast as is and I don't think you'll be able to get any Index SEEKs out of it.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 4, 2007 at 4:05 pm
Jeff,
It seems you overcomplicated the last query.
This returns the same result:
SELECT t1.*
FROM #GdsSellPriceDet t1
LEFT JOIN #GdsSellPriceDet t2
ON t2.GdsCode = t1.GdsCode
AND t2.CcyCode = t1.CcyCode
AND t2.UnitCode = t1.UnitCode
AND t2.SellPrice = t1.SellPrice
AND t2.CustGroupCode < t1.CustGroupCode
WHERE t2.GdsCode IS NULL
And again,
you (and me after you ) assume that "first" means "having minimal CustGroupCode".
No evidence it's true.
_____________
Code for TallyGenerator
August 4, 2007 at 5:44 pm
Boy, I'll say... dunno why I made the dupe check separate... must've been that old donut I ate this morning
--Jeff Moden
Change is inevitable... Change for the better is not.
August 5, 2007 at 6:18 am
> must've been that old donut I ate this morning
Mmmmm... Dooonut...
_____________
Code for TallyGenerator
August 5, 2007 at 1:55 pm
to mr jeff moden and mr serqiy,
i already combined your suggestions and the result was very good, met my requirement about saving papers.
i considered my case is solved, thank you very much for all your help and attention.
thanks again....
rom beng
August 5, 2007 at 4:14 pm
Thanks for the feedback, Rom.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 6, 2007 at 8:42 am
If the purpose was to save paper, why not do a sort of pivot query and put all the prices for each line item on a seperate line?
August 6, 2007 at 11:22 am
Heh... or concatenate the groups on a single line?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply