Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I'm trying to do a query to calculate the total entries per hour in a table and show only the top value for the date.
For example, Today model A has results at 6 for 100 pcs, at 7 for 200 pcs and at 8 for 50 pcs. In this case what I need to show is only 200 pcs in a column and the Time (7) in another column.
I have been able to show the top values but I cannot show the time, if I show the time then the top does not work...
Samples:
Including Hours:
Without Top[^]
Not Including Hours:
WithTop[^]
I really appreciate your help!

What I have tried:

This is the query that I've tried so far (Without Hour)
SELECT *
FROM
       (
       select count(barcodenum)as count1, date1, Model_sap, model_customer, linea
       from   (  
                           SELECT FinishGoods.linea, FinishGoods.Model_sap, model.model_customer,FinishGoods.BarCodeNum,CONVERT(VARCHAR(10), scanprod.fecha, 102) as date1, scanprod.fecha
                           FROM FinishGoods  INNER JOIN model ON FinishGoods.Model_sap = model.model_ext INNER JOIN scanprod ON FinishGoods.BarCodeNum = scanprod.barcodenum  
                           WHERE (FinishGoods.linea <> '0') AND (FinishGoods.linea <> '') AND FinishGoods.scandt is not null  AND (scanprod.fecha >= '2016-03-18 06:00:00' AND scanprod.fecha <= '2016-03-22 23:59:59'  ) 
                     ) as tbl1 
       group by date1,  DATEPART(Hour, fecha), Model_sap, model_customer,linea--,hora1
       --order by model_sap, linea
) AS HourlySalesData
PIVOT( max(count1) FOR [date1] IN ([2016.03.18],[2016.03.19],[2016.03.20],[2016.03.21],[2016.03.22])) AS DatePivot
order by model_customer


And This one with hour included:
SELECT *
FROM
       (
       select count(barcodenum)as count1, date1, Model_sap, model_customer, linea, (convert(int, DATEPART(Hour, fecha))) as hour_
       from   (  
                           SELECT FinishGoods.linea, FinishGoods.Model_sap, model.model_customer,FinishGoods.BarCodeNum,CONVERT(VARCHAR(10), scanprod.fecha, 102) as date1, scanprod.fecha
                           FROM FinishGoods  INNER JOIN model ON FinishGoods.Model_sap = model.model_ext INNER JOIN scanprod ON FinishGoods.BarCodeNum = scanprod.barcodenum  
                           WHERE (FinishGoods.linea <> '0') AND (FinishGoods.linea <> '') AND FinishGoods.scandt is not null  AND (scanprod.fecha >= '2016-03-18 06:00:00' AND scanprod.fecha <= '2016-03-22 23:59:59'  ) 
                     ) as tbl1 
       group by date1,  DATEPART(Hour, fecha), Model_sap, model_customer,linea--,hora1
       --order by model_sap, linea
) AS HourlySalesData
PIVOT( max(count1) FOR [date1] IN ([2016.03.18],[2016.03.19],[2016.03.20],[2016.03.21],[2016.03.22])) AS DatePivot
order by model_customer
Posted
Updated 18-Apr-17 1:06am
v2
Comments
CHill60 18-Apr-17 7:24am    
If Solution 1 does not work for you then please use the "Improve question" link to post some sample data from each of the tables - most people will not even attempt to help if they can't easily reproduce your problem. "Easily" means being able to cut & paste, not look at a picture on another site.

One word of advice - get rid of all of those CONVERT's - use CAST( xxx AS DATE) to get just the date and DATEPART(hh, xxx) to get the hour. Not only is it faster but you are using the correct data types (only use varchar for strings)

1 solution

Do you know the
ROW_NUMBER() OVER(PARTITION BY date1 ORDER BY count1 ASC) 
it returns an integer begining from 1 to x groupped as the collumns in partition by and orderd as in order by something like this

SELECT *
FROM
       (
       select count(FinishGoods.BarCodeNum) as count1, CONVERT(VARCHAR(10), scanprod.fecha, 102) as date1, (convert(int, DATEPART(Hour, fecha))) as hour_,
	   ROW_NUMBER() OVER(PARTITION BY CONVERT(VARCHAR(10), scanprod.fecha, 102) ORDER BY count(FinishGoods.BarCodeNum) ASC)  as rnum
       FROM FinishGoods  INNER JOIN model ON FinishGoods.Model_sap = model.model_ext INNER JOIN scanprod ON FinishGoods.BarCodeNum = scanprod.barcodenum  
       WHERE (FinishGoods.linea <> '0') AND (FinishGoods.linea <> '') AND FinishGoods.scandt is not null  AND (scanprod.fecha >= '2016-03-18 06:00:00' AND scanprod.fecha <= '2016-03-22 23:59:59'  ) 
       group by CONVERT(VARCHAR(10), scanprod.fecha, 102), DATEPART(Hour, fecha)
       --order by model_sap, linea
) AS HourlySalesData where rnum=1

it should work
ROW_NUMBER (Transact-SQL) | Microsoft Docs[^]
 
Share this answer
 
Comments
EddyGuzman 21-Apr-17 18:36pm    
Works good, thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900