Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2007-01-10 : 01:00:16
|
| Please help me with the query to get the following result. I am so much worried with this and if some one can give me a query to form this, I will be so thank fullI have 3 tables1. Quote2. QuoteProdList3. QuoteVersionIn Quote Table, I am storing the QuoteNumber,QuoteReferenceNumber and CustomerNameIn QuoteprodList I am storing QuoteNumber and FasValueIn QuoteVersion, I am storing different versions of these QuoteRefNoMy problem is I have to get the sum of Fas value group by Customer name, but this should be for the latest version of quote.ie, In QuoteVersion Table, if some value is there in version2 field, it should take the fas value from QuoteProdList, given with the quotenumber specified in Version2 field of QuoteVersion Table.Quote-----QuoteNumber.......CustomerName......QuoteRefNo----------------------------------------------1.................a.................xyz2.................b.................abc3.................a.................xyz4.................c.................efg5.................d.................hij6.................a.................klm7.................d.................hijQuoteVersion------------QuoteRefNo..............Version1........Version2------------------------------------------------xyz.....................1..................3abc.....................2..................Nullefg.....................4..................Nullhij.....................5..................7klm.....................6..................NullQuoteProdList-------------QuoteNumber...........Fasvalue------------------------------1.......................202.......................323.......................404.......................125.......................66.......................107.......................30For me, the out put should beCustomerName......FasValue--------------------------a..................50b..................32c..................12d..................30Customer ' a is having 2 quoterefno in Quote Table,xyz & klm. From QuoteVersion Table ,take QuoteNumber 3 for xyz specified by Version2 field, take it's fas value from QuoteprodList(40)+QuoteNUmber 6 specified by Version2 field, take is fas value(10)=50)Customer d is having two quotenumbers in version1 and 2, so take the quote number in version 2 and take fas of 2 from quoteprodlist,ie, 30Please help me to solve this.RegardsCeema |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 01:11:41
|
Try this suggestionSELECT d.CustomerName, SUM(qpl.FasValue) AS FasValueFROM ( SELECT DISTINCT q.CustomerName, q.QuoteRefNo ISNULL(qv.Version2, qv.Version1) AS Version FROM Quote AS q INNER JOIN QuoteVersion AS qv ON qv.QuoteRefNo = q.QuoteRefNo ) AS dINNER JOIN QuoteProdList AS qpl ON qpl.QuoteNumber = d.VersionGROUP BY d.CustomerNameORDER BY d.CustomerName Peter LarssonHelsingborg, Sweden |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2007-01-10 : 02:39:26
|
Hello Peso, Thank you for the reply, but I have one more problem, in my Quoteversion table I have fields like Quoterefno,version1,version2,version3,version4 (I haven't never imagine, you will use it in this manner, that's why I haven't menioned about that in the query,sorry) and instead of null I am storing 0 in the field. So, could you please tell me how to change the query accordingly. Thank you so muchCeemaquote: Originally posted by Peso Try this suggestionSELECT d.CustomerName, SUM(qpl.FasValue) AS FasValueFROM ( SELECT DISTINCT q.CustomerName, q.QuoteRefNo ISNULL(qv.Version2, qv.Version1) AS Version FROM Quote AS q INNER JOIN QuoteVersion AS qv ON qv.QuoteRefNo = q.QuoteRefNo ) AS dINNER JOIN QuoteProdList AS qpl ON qpl.QuoteNumber = d.VersionGROUP BY d.CustomerNameORDER BY d.CustomerName Peter LarssonHelsingborg, Sweden
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 03:25:16
|
| [code]SELECT d.CustomerName, SUM(qpl.FasValue) AS FasValueFROM ( SELECT DISTINCT q.CustomerName, q.QuoteRefNo COALESCE(NULLIF(qv.Version4, 0), NULLIF(qv.Version3, 0), NULLIF(qv.Version2, 0), qv.Version1) AS Version FROM Quote AS q INNER JOIN QuoteVersion AS qv ON qv.QuoteRefNo = q.QuoteRefNo ) AS dINNER JOIN QuoteProdList AS qpl ON qpl.QuoteNumber = d.VersionGROUP BY d.CustomerNameORDER BY d.CustomerName[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-01-10 : 03:28:11
|
| Your QuoteVersion table doesn't seem to be normalized. Handling this kinds of query will become more complicated if you have large no. of versions, say 10. Why don't you keep simple table with just QuoteRefNo and Version columns. It will be much easier to manage and query.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2007-01-10 : 03:32:07
|
Hello Peso, Thank you, it done the trick. Thank you so much.RegardsCeemaquote: Originally posted by Peso
SELECT d.CustomerName, SUM(qpl.FasValue) AS FasValueFROM ( SELECT DISTINCT q.CustomerName, q.QuoteRefNo COALESCE(NULLIF(qv.Version4, 0), NULLIF(qv.Version3, 0), NULLIF(qv.Version2, 0), qv.Version1) AS Version FROM Quote AS q INNER JOIN QuoteVersion AS qv ON qv.QuoteRefNo = q.QuoteRefNo ) AS dINNER JOIN QuoteProdList AS qpl ON qpl.QuoteNumber = d.VersionGROUP BY d.CustomerNameORDER BY d.CustomerName Peter LarssonHelsingborg, Sweden
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 03:32:42
|
| I agree with Harsh. Try to normalize your table.But that might not help, if the application using this table is third party.How many version do you have at maximum? If the version is maximum 10, use thisCOALESCE(NULLIF(qv.Version10, 0), NULLIF(qv.Version9, 0), NULLIF(qv.Version8, 0), NULLIF(qv.Version7, 0), NULLIF(qv.Version6, 0), NULLIF(qv.Version5, 0), NULLIF(qv.Version4, 0), NULLIF(qv.Version3, 0), NULLIF(qv.Version2, 0), qv.Version1) AS Versioninstead ofCOALESCE(NULLIF(qv.Version4, 0), NULLIF(qv.Version3, 0), NULLIF(qv.Version2, 0), qv.Version1) AS VersionPeter LarssonHelsingborg, Sweden |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2007-01-10 : 03:33:31
|
Hello harsh_athalye, Some times, we are not controlling the database design, so i have to run with what I am getting, that's why I am struggling now.Thank youCeemaquote: Originally posted by harsh_athalye Your QuoteVersion table doesn't seem to be normalized. Handling this kinds of query will become more complicated if you have large no. of versions, say 10. Why don't you keep simple table with just QuoteRefNo and Version columns. It will be much easier to manage and query.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 03:36:47
|
quote: Originally posted by harsh_athalye Handling this kinds of query will become more complicated if you have large no. of versions, say 10.
No that much more complicated, if you use thisCOALESCE(NULLIF(qv.Version4, 0), NULLIF(qv.Version3, 0), NULLIF(qv.Version2, 0), qv.Version1) compared toISNULL(qv.Version21, qv.Version1) Peter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-01-10 : 04:01:30
|
| Okay, that's not much complicated although bit lengthy. But still you need to change the query every time you add new version.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 04:11:06
|
Yes, the drawback of not normalized tables.I think a new version columns is added for every new major version of the third party application.Pain in the butt, but managable, if the upgrade process description is accurate to the DBA responsible for altering the query to accomodate the new column.I think I will have to remember this oneCOALESCE(NULLIF(qv.Version4, 0), NULLIF(qv.Version3, 0), NULLIF(qv.Version2, 0), qv.Version1) I have a feeling this can come in handy in the future.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|