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 |
|
Tava
Starting Member
3 Posts |
Posted - 2010-09-24 : 01:27:52
|
| Hi All,I'm just new to this forum, so not sure if i'm in the right place to ask or sorry if this has been asked before.Currently i'm writing a simple query (what i thought was anyway to retrieve transaction history. Our database is based about transactions and items per transaction and im only trying to pull out certain information based on say promotions, below is an example of the Columns in a table for a database called "testdb".[testdb].[dbo].[transactions]ReceiptNo | Product | Qty | Price | Logged1 | KingPackage | 1 | 500 | 2010-09-211 | King Bed | 1 | N/A | 2010-09-211 | Bedsides | 2 | N/A | 2010-09-21-----------------------------------------2 | KingPackage | 1 | 500 | 2010-09-212 | King Bed | 1 | N/A | 2010-09-212 | Bedsides | 2 | N/A | 2010-09-212 | GiftCard | 1 | 100 | 2010-09-21I'm trying to build a query to see how many KingPackage's are sold but rule is it must contain GiftCard as part of the filter otherwise it is not the right promotion and i have no idea what the receipt numbers are. So in this case the output of information would be only for Receipt #2 and needs to be displayed as below:OutputReceiptNo | Product | Qty | Price2 | KingPackage | 1 | 5002 | King Bed | 1 | N/A2 | Bedsides | 2 | N/A2 | GiftCard | 1 | 100I've already written a Query to try to get this result & completed the necessary scriptSELECT t.ReceiptNo, t.Product, t.Qty, t.Price t.LoggedFROM [testdb].[dbo].[transactions] as tWHERE (t.Logged > '2010-09-21 00:01' AND t.Logged < '2010-09-21 23:59')But i dont know how to pull through all the information as below, if i filter it to product = KingPackage can just get 1 line but for all receipts that have sold this.ReceiptNo | Product | Qty | Price2 | KingPackage | 1 | 5002 | King Bed | 1 | N/A2 | Bedsides | 2 | N/A2 | GiftCard | 1 | 100Any help would be greatly appreciated.Tava |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-24 : 04:21:40
|
| SELECT * FROM transactionsWHERE t.ReceiptNo IN (SELECT t.ReceiptNo,FROM [testdb].[dbo].[transactions] as tWHERE (t.Logged > '2010-09-21 00:01' AND t.Logged < '2010-09-21 23:59') AND t.Product = 'KingPackage' AND EXISTS(SELECT 1 FROM transactions WHERE Product ='GiftCard' AND ReceiptNo = t.ReceiptNo)) |
 |
|
|
Tava
Starting Member
3 Posts |
Posted - 2010-09-26 : 19:06:46
|
quote: Originally posted by rohitvishwakarma SELECT * FROM transactionsWHERE t.ReceiptNo IN (SELECT t.ReceiptNo,FROM [testdb].[dbo].[transactions] as tWHERE (t.Logged > '2010-09-21 00:01' AND t.Logged < '2010-09-21 23:59') AND t.Product = 'KingPackage' AND EXISTS(SELECT 1 FROM transactions WHERE Product ='GiftCard' AND ReceiptNo = t.ReceiptNo))
Thanks for your help rohitvishwakarma,I'm still struggling to get this working i tried using your statement just in a new query but that failed but modified it slightly to get it to work but still not sure how to incorporate it in my code. I think when i started this thread i thought it would be a simple statement but missed out key areas of information to help.I've modified the code but not sure where to put in your select statement you provided, Here is sample of code which is more accurate: SELECT *FROM [testdb].[dbo].[transsummary] AS tsINNER JOIN [testdb].[dbo].[transdetail] AS td ONts.storeid = td.storeid ANDts.transnumber = td.transnumberWHERE (ts.Logged > '2010-09-21 00:01' AND ts.Logged < '2010-09-21 23:59')ORDER BY ts.storeidthe "Transsummary" table holds the original receipt numbers, logged dates and cost but "Transdetails" holds all items, receipt numbers as well, which is why i've done a join. |
 |
|
|
|
|
|
|
|