January 1, 2009 at 11:41 am
I have a table of following schema, lets say its name is TABLE1
Item(PK) | Name(PK) | Date(PK)| Amount|
-----------------------------------------
1 |A |02/01/08 |2.44 |
1 |B |02/01/08 |2.56 |
1 |A |02/02/08 |2.57
1 |A |02/03/08 |2.58 |
2 |C |02/01/08 |1.99 |
2 |A |02/02/08 |2.00 |
Now I want to extract data from it in such a way that it can be fit in the table (TABLE2) as below
Item(PK) |Name(PK)|Num |FirstP |LastP |MaxAmt|MinAmt|
-------------------------------------------------------
1 |A |3 |02/01/08 |02/03/08 |2.44 | 2.58|
1 |B |1 |02/01/08 |02/01/08 |2.56 | 2.56|
2 |A |1 |02/02/08 |02/02/08 |2.00 | 2.00|
2 |C |1 |02/01/08 |02/01/08 |1.99 | 1.99|
I would like to explain the problem little bit here.
Table 1 contains the item (1,2 and so on) and Name of the one who purchases that item. The date of the purchase and amount for that item he or she spent(The price of item always increases after one purchase is made).
In table 2, the detail information from table1 should be listed.
So, for item number 1, A made 3, purchase, his first purchase was 0n 02/01/08 whereas his last purchase was on 02/03/08. The maximum amount he paid was 2.44 and minimum amount was 2.58.
Similarly, B made one purchase, so his first and last date purchase will be the same one i.e. 02/01/08 and his minimum and maximum purchase amount will be same too i.e. 2.56
and so on.
I was able to extract three columns from the Table1 using the following query, but I need complete query to extract all data. I think we can use MIN and MAX to get the desired result, but I am not sure.
INSERT INTO Table2(item, name, num) SELECT item, name, Count(amount) from table1 GROUP BY name, item
Thanks in advance
January 1, 2009 at 11:55 am
Look to use MAX() and MIN() for the dates and amounts to get what you need.
I'm hinting here since this looks like homework potentially, so I'd like to see you do some more querying. There shouldn't be an INSERT as you figure this out, just SELECT the data from Table 1 and see what you get back.
January 1, 2009 at 12:14 pm
Just make sure that you have the correct data types in your source table, ie dates are stored as datetimes and the numbers are stored as numeric, decimal or money, or you may get unexpected results from the min and max.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 1, 2009 at 1:00 pm
Thank you very much I have figured it out. It was quite easy. Thanks for advice regarding datatype. now I have to work on datatype of Table1. Due to datatype mismatch my results were not as desired. Thanks again
January 1, 2009 at 1:27 pm
santosh.shrestha (1/1/2009)
Thank you very much I have figured it out. It was quite easy. Thanks for advice regarding datatype. now I have to work on datatype of Table1. Due to datatype mismatch my results were not as desired. Thanks again
Ok... a little lesson in forum ettiquette...
First, when you come up with a solution, you should post it.
Also, now that you've actually tried something, if you post the query, now people will actually want to help because they will see that you've actually tried. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
January 1, 2009 at 9:04 pm
Sorry about my ignorance.
The solution query is as follow:
SELECT Item, Name, COUNT(Amt), MIN (Amt), MAX(Amt), MIN (Date), MAX (Date) FROM Table1 GROUP BY Name, Item
Thanks again.
January 2, 2009 at 5:38 pm
santosh.shrestha (1/1/2009)
Sorry about my ignorance.The solution query is as follow:
SELECT Item, Name, COUNT(Amt), MIN (Amt), MAX(Amt), MIN (Date), MAX (Date) FROM Table1 GROUP BY Name, Item
Thanks again.
Cool... Thanks, Santosh. Others will learn from it.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply