I have a SQL assignment I am working on for school (just a series of query and sub-query questions) and I have them all complete but one. Here is the prompt for reference:
10. Find the AM hours with total traffic load with 200 or more.
I have the code for the first part, the AM hours. It is as such:
select hour(traffic.ttime), sum(traffic.packetsize)
from traffic
where hour(traffic.ttime) <= 12
group by hour(traffic.ttime);
The only thing I cant figure out is how to get only the ones with 200 or more. It should return only 3 rows but regardless of what I try it still returns all or non. I'm pretty sure a sub-query needs to be used here but I cant seem to figure it out. This is what I have tried so far:
select hour(traffic.ttime), sum(traffic.packetsize)
from traffic
where hour(traffic.ttime) <= 12 and (select sum(traffic.packetsize) from
traffic)>=200
group by hour(traffic.ttime) ;
Logically I know that is incorrect due to the fact that it will just return the sum of all the packet sizes in that given table. I cant seem to come up with anyway to make it work without it throwing an error. Any help would be appreciated!
select hour(traffic.ttime), sum(traffic.packetsize)from traffic where hour(traffic.ttime) <= 12 group by hour(traffic.ttime) having sum(traffic.packetsize)>=200
Related
I need to get the total from a column with SQL but it's not working, can anybody see what I do wrong.
SELECT a.Artikelnummer
,a.Artikelnamn
,a.Antalperpall
,COUNT(*) AS AntalArtiklar
,SUM(e.Antalpallar) AS TotalPall
,SUM(e.Antalperpall) AS TotalStyck
FROM Artikel AS a
INNER JOIN Evig AS e ON a.ArtikelnummerID = e.ArtikelnummerID
WHERE (e.Datum <= '{0}')
AND (a.Kundkund = '{1}')
AND (a.Artikelnamn = '{2}')
GROUP BY a.Artikelnummer
,a.Artikelnamn
,a.Antalperpall
SUM(e.Antalperpall) AS TotalStyck: it is this one who returns a strange value. What I wanna do is take the integer value in each row and get a total from that.
OK I went down to the basement and visited the server, and I found the problem. I needed to multiply with Antalpallar like this SUM(e.Antalperpall * ABS(e.Antalpallar)) . But it is still not working and I think it is becouse of the negative values.
se data here
so where it is negativ value in Antalpallar like this -1200 *-2 should be -2400 but i don't think it's doing that, or? It is stuff going in and out of a warehouse.
Anyhow, the final value of adding those togheter should be 14320, but i get one on 20 000 something and without ABS()(or with) a sum on 5000 something.
Anyone knows how to write this SUM(e.Antalperpall * ABS(e.Antalpallar)) to get the value i want?
You might wanna try it by eliminating if there are any strange values (characters) in Antalperpall. Use sum(cast (e.Antalperpall as Money)) and in filterclause
where ISNUMERIC( e.Antalperpall) = 1. If there are any stange values in the field, you will obviously get conversion error.
So I am honestly a little puzzled by this!
I have a query that returns a set of transactions that contain both repair costs and an odometer reading at the time of repair on the master level. To get an accurate Cost per mile reading I need to do a subquery to get both the first meter reading between a starting date and an end date, and an ending meter.
(select top 1 wf2.ro_num
from wotrans wotr2
left join wofile wf2
on wotr2.rop_ro_num = wf2.ro_num
and wotr2.rop_fac = wf2.ro_fac
where wotr.rop_veh_num = wotr2.rop_veh_num
and wotr.rop_veh_facility = wotr2.rop_veh_facility
AND ((#sdate = '01/01/1900 00:00:00' and wotr2.rop_tran_date = 0)
OR ([dbo].[udf_RTA_ConvertDateInt](#sdate) <= wotr2.rop_tran_date
AND [dbo].[udf_RTA_ConvertDateInt](#edate) >= wotr2.rop_tran_date))
order by wotr2.rop_tran_date asc) as highMeter
The reason I have the tables aliased as xx2 is because those tables are also used in the main query, and I don't want these to interact with each other except to pull the correct vehicle number and facility.
Basically when I run the main query it returns a value that is not correct; it returns the one that is second(keep in mind that the first and second have the same date.) But when I take the subquery and just copy and paste it into it's own query and run it, it returns the correct value.
I do have a work around for this, but I am just curious as to why this happening. I have searched quite a bit and found not much(other than the fact that people don't like order bys in subqueries). Talking to one of my friends that also does quite a bit of SQL scripting, it looks to us as if the subquery is ordering differently than the subquery by itsself when you have multiple values that are the same for the order by(i.e. 10 dates of 08/05/2016).
Any ideas would be helpful!
Like I said I have a work around that works in this one case, but don't know yet if it will work on a larger dataset.
Let me know if you want more code.
I have a SQL query
SELECT
Group_Id, MIN(Rec_Number) as RecStart, MAX(Rec_Number) AS RecEnd
FROM
Rec
WHERE
Group_Id != ''
GROUP BY
Group_Id
ORDER BY
Group_Id
This produces the following kind of results.
92-2274 9222740001 9222740004
92-2275 9222750001 9222750026
etc...
However if record 3 is missing (in the first row for instance) the query obviously doesn't account for it. What I am trying to do is the following
92-2274 9222740001 9222740002
92-2274 9222740004 9222740018
92-2275 9222750001 9222750016
92-2275 9222750018 9222750026
etc...
So essentially each time the script sees a record missing inside the group it starts a new line whilst staying inside the group before iterating on the next group. The group_Id is of course the first 6 digits of the rec_Number
I would also like to do this as well
92-2274 0001 0002
92-2274 0003 0004
Or even trim it to and remove the leading 0's as well if possible I know about using Right (Rec_Number, 4) however as this is a float the automatic convert to string seems to be messing something up as I get +009 is many columns so I assume I need to cast first or something. This particular function I could do it Excel after the fact I guess but I'm sure SQL could do it if the guy writing the query was a DBA and not a bumbling server admin (that's me!)
So is there a way of doing that in SQL also I must warn you that the standard CTE or using functions such as row number don't work as this is SQL Server 2000 - yes it is that old!
Hence me struggling to find posts on Stack Overflow that apply. Many of them start with the WITH keyword which means I can't use any of those to start with!
I think I am needing an IF ELse kind of block but I am not sure what kind of method I can use to get the query to create a new row each time it hits a missing concurrent number in the group range.
The final output will show me the ranges of records in each group whilst highlighting the missing ones via a new line each time.
For the second part, this should work :
RIGHT ( CAST ( MIN (Rec_Number) as Decimal(10)), 4)
It will only keep the last 4 characters of your number.
I'm not sure if this is a symptom of my relative inexperience with SQL, or with h2. I have a view, called VIEW_TRANSACTION_LEGS_DATA, which I need to search in various ways. So, for example, I have:
SELECT HEAD_ID FROM VIEW_TRANSACTION_LEGS_DATA WHERE AMOUNT > 1000
and I also have:
SELECT * FROM
(SELECT HEAD_ID FROM VIEW_TRANSACTION_LEGS_DATA WHERE AMOUNT > 1000)
INTERSECT
(SELECT HEAD_ID FROM VIEW_TRANSACTION_LEGS_DATA WHERE AMOUNT < 2000)
Unfortunately, this isn't working as I expect! There should only be 3 rows returned, whereas I am getting 57 returned.
(Note that the above is a simplified version of what my code actually says; please don't suggest to me to combine the INTERSECTed lines using a BETWEEN since this will not work with the rest of the code.)
I'm sure my problem is a typical SQL newbie type problem, but I simply can't see it! Can some kind person please point me in the right direction?
I guess this has been asked in the site before but I can't find it.
I've seen in some sites that there is a vague count over the results of a search. For example, here in stackoverflow, when you search a question, it says +5000 results (sometimes), in gmail, when you search by keywords, it says "hundreds" and in google it says aprox X results. Is this just a way to show the user an easy-to-understand-a-huge-number? or this is actually a fast way to count results that can be used in a database [I'm learning Oracle at the moment 10g version]? something like "hey, if you get more than 1k results, just stop and tell me there are more than 1k".
Thanks
PS. I'm new to databases.
Usually this is just a nice way to display a number.
I don't believe there is a way to do what you are asking for in SQL - count does not have an option for counting up until some number.
I also would not assume this is coming from SQL in either gmail, or stackoverflow.
Most search engines will return a total number of matches to a search, and then let you page through results.
As for making an exact number more human readable, here is an example from Rails:
http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_human
With Oracle, you can always resort to analytical functions in order to calculate the exact number of rows about to be returned. This is an example of such a query:
SELECT inner.*, MAX(ROWNUM) OVER(PARTITION BY 1) as TOTAL_ROWS
FROM (
[... your own, sorted search query ...]
) inner
This will give you the total number of rows for your specific subquery. When you want to apply paging as well, you can further wrap these SQL parts as such:
SELECT outer.* FROM (
SELECT * FROM (
SELECT inner.*,ROWNUM as RNUM, MAX(ROWNUM) OVER(PARTITION BY 1) as TOTAL_ROWS
FROM (
[... your own, sorted search query ...]
) inner
)
WHERE ROWNUM < :max_row
) outer
WHERE outer.RNUM > :min_row
Replace min_row and max_row by meaningful values. But beware that calculating the exact number of rows can be expensive when you're not filtering using UNIQUE SCAN or relatively narrow RANGE SCAN operations on indexes. Read more about this here: Speed of paged queries in Oracle
As others have said, you can always have an absolute upper limit, such as 5000 to your query using a ROWNUM <= 5000 filter and then just indicate that there are more than 5000+ results. Note that Oracle can be very good at optimising queries when you apply ROWNUM filtering. Find some info on that subject here:
http://www.dba-oracle.com/t_sql_tuning_rownum_equals_one.htm
Vague count is a buffer which will be displayed promptly. If user wants to see more results then he can request more.
It's a performance facility, after displaying the results the sites like google keep searching for more results.
I don't know how fast this will run, but you can try:
SELECT NULL FROM your_tables WHERE your_condition AND ROWNUM <= 1001
If count of rows in result will equals to 1001 then total count of records will > 1000.
this question gives some pretty good information
When you do an SQL query you can set a
LIMIT 0, 100
for example and you will only get the first hundred answers. so you can then print to your viewer that there are 100+ answers to their request.
For google I couldn't say if they really know there is more than 27'000'000'000 answer to a request but I believe they really do know. There are some standard request that have results stored and where the update is done in the background.