Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TSQL help

Posted on 2015-02-17
6
Medium Priority
?
82 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 34

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 49

Accepted Solution

by:
PortletPaul earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 49

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 49

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

926 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