Solved

TSQL help

Posted on 2015-02-17
6
67 Views
Last Modified: 2015-02-27
How can I write a sql statement from tOrders table where I only want to return the result for min products seq.    for example

tProductSeq
---------------------
ProductTYpe      seq
Dairy                        1
Fruits                        2
Vagitables      3
Paper                        4


tOrders
-------------------------------------------------------
OrderID      producttype      qty
2      Fruits      22
3      Vagitables      5
4      Paper      2
1      Fruits      36
3      Paper      24
1      Dairy      23
2                Paper       25
8                Paper       24

Tsql result should be as below
OrderID      producttype      qty
2      Fruits                     22
3      Vagitables      5
4      Paper                       2
1      Dairy                     23
8                Paper       24


select * from tOrders O
where O.tProductType in (select top 1 PT.producttype PT from tProductSeq  PT where O.tProductType = PT.producttype  order by seq)
0
Comment
Question by:Bharat Guru
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40615398
SELECT pc.producttype ,    COUNT(o.OrderID) As seq 
FROM tOrders o Inner Join productCategory pc
On pc.producttype= o.producttype
GROUP BY pc.producttype
ORDER BY COUNT(o.OrderID)

Open in new window


This post has been revised
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40615442
Please try the following:
SELECT
      O.OrderID
    , O.producttype
    , O.qty
FROM productCategory PC
INNER JOIN (
      SELECT
            OrderID
          , producttype
          , qty
          , ROW_NUMBER() OVER (PARTITION BY producttype
                               ORDER BY OrderId DESC) AS RN
) AS O ON PC.producttype = O.producttype
            AND O.RN = 1
;

Open in new window

It appears that you want "the latest" order rows but the only information I can see to achieve "latest" is the orderid.

Note, the sequence of tables could be reversed and/or there may be a need to an outer join, but this isn't indicated by the sample data.
0
 

Author Comment

by:Bharat Guru
ID: 40615740
Above solution seems little tricky, how about if I import the data into the  ttemporder table and some how if i can delete the  records except min seq to get the final result?

I was to get the result inside the stored procedure so if it's necessary its it's ok get the result in two steps.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 40615778
This result:
| ORDERID | PRODUCTTYPE | QTY |
|---------|-------------|-----|
|       1 |       Dairy |  23 |
|       2 |      Fruits |  22 |
|       8 |       Paper |  24 |
|       3 |  Vagitables |   5 |

Open in new window


Produced by this query:
SELECT
      O.OrderID
    , O.producttype
    , O.qty
FROM tProductSeq PC
INNER JOIN (
      SELECT
            OrderID
          , producttype
          , qty
          , ROW_NUMBER() OVER (PARTITION BY producttype
                               ORDER BY OrderId DESC) AS RN
      FROM tOrders
) AS O ON PC.producttype = O.producttype
            AND O.RN = 1
;

Open in new window


From the sample data of the question.

ROW_NUMBER() is an efficient system function, and is frequently used to locate conditions such as "the latest" or "most recent" rows.

If the tables are exactly as you indicate in the question then you can get the same result from:
SELECT
      O.OrderID
    , O.producttype
    , O.qty
FROM (
      SELECT
            OrderID
          , producttype
          , qty
          , ROW_NUMBER() OVER (PARTITION BY producttype
            ORDER BY OrderId DESC) AS RN
      FROM tOrders
    ) AS O
WHERE RN = 1

Open in new window


I would regard using temp tables and a multi-staged approach as more tricky than using ROW_NUMBER()

see it working at: http://sqlfiddle.com/#!3/f97f9/6

{+ edit}
The expected result in your question isn't met by these queries. Appears I don't understand the logic of arriving at the expected result, can you explain what that logic is?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40615803
In the question your sql contains: order by seq

So, if the expected is ordered that way it would look like this:
OrderID producttype   qty
1       Dairy         23
2       Fruits        22
3       Vagitables     5
4       Paper          2
8       Paper         24

Open in new window


Why are there two rows in this output for Paper?

If I include order by seq in my query that joins the 2 tables - and output that column the result is:
| ORDERID | PRODUCTTYPE | QTY | SEQ |
|---------|-------------|-----|-----|
|       1 |       Dairy |  23 |   1 |
|       2 |      Fruits |  22 |   2 |
|       3 |  Vagitables |   5 |   3 |
|       8 |       Paper |  24 |   4 |

Open in new window


SELECT
      O.OrderID
    , O.producttype
    , O.qty
    , PC.seq
FROM tProductSeq PC
INNER JOIN (
      SELECT
            OrderID
          , producttype
          , qty
          , ROW_NUMBER() OVER (PARTITION BY producttype
                               ORDER BY OrderId DESC) AS RN
      FROM tOrders
) AS O ON PC.producttype = O.producttype
            AND O.RN = 1
ORDER BY PC.seq

Open in new window

0
 

Author Closing Comment

by:Bharat Guru
ID: 40635723
Thanks
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now