Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TSQL help

Posted on 2015-02-17
6
70 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 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