How do I create a view on data - sql

I have a table with 13 fields including:
Computer, Application
I need to have a similar table/view that lists COUNT(Application) along with Application listed only once for each Computer. All fields must exist with the addition of the new field.
I need something similar to:
Computer| Application | AppCount | ...
USD9090 MS Outlook 3
UOD0909 MS Outlook 5
UDL4563 Skype 4
Ive tried grouping by Computer with Application COUNT
SELECT TOP 5 Computer, ComputerID, Application FROM AppReliability WHERE EXISTS
(SELECT TOP 5 Count(Application) AS App, Computer
FROM AppReliability
WHERE Date >= DATEADD(day,-30,GETDATE())
GROUP BY Computer
ORDER BY App DESC)
I cant get the correct output

Do you just want an aggregation query?
select Computer, Application, count(*) as AppCount
from AppReliability
group by Computer, Application;
Your question doesn't mention anything about dates or why you would be using top (5).

Related

Apache Hive command

I have this question: Show the top 5 game Disciplines for the countries who got more than 10 gold medals.
my code is: select distinct t.discipline, m.team from teams t join medals m on (t.noc=m.team and m.numbergold>10) order by m.team;
cloud you please help me with this code to show the top 5 game for each country?
change the code to display 5 games for each country
Please use the group by and windows function for your required result.
In 'numbergold' - Use windows functions like dense_rank so that you can filter on the top 5.

Select specific grouped element

Hi I would like to ask you because I cannot find solution.
For example I have data like that:
number | date | user
10 | 2022-07-01 | A
15 | 2022-07-08 | A
9 | 2022-07-10 | A
Right now I need get the number for user where date is the newer one.
In this case I need get value 9
Ofcourse I have many diffrent users it is only for illustrate the issue.
Now I would like to select all unique users with his number that date is the newer one.
Is it possible to do it in one query?
I like to teach these problems by breaking it down into smaller problems.
First, write a query that tells you which is the "newer one" for each user.
SELECT user, max(date) as newer_one
FROM tbl
GROUP BY user
Next, you can join that result back to the original data. My favorite style is called a CTE which makes things readable and easier to debug. Like so,
WITH newest AS (
SELECT user, max(date) as newer_one
FROM tbl
GROUP BY user
)
SELECT original.*
FROM tbl AS original
INNER JOIN newest
ON original.user = newest.user
AND original.date = newest.newer_one
Some RDBMS don't support this CTE style, but you can put the query in the body which makes it harder to read sometimes but will work basically anywhere.
SELECT original.*
FROM tbl AS original
INNER JOIN
(
SELECT user, max(date) as newer_one
FROM tbl
GROUP BY user
) as newest
ON original.user = newest.user
AND original.date = newest.newer_one

Access SQL database - ORDER BY

I'm using an MRP system for stocking inventory where I work. The interface it self isn't the best, so I have decided to open up the database file and do everything manually. I'm having some issues though. I'm trying to sort my database by using ORDER BY. I'm not getting the results I thought I would. It is showing them in this format:
1
10
100
101
101
11
110
111
etc
Instead of
1
2
3
4
5
etc
This is my query
SELECT *
FROM tblStockItems
Order By (`MasterPNo`)
I'm currently working in access, and then database is in the JET format. If you're wondering why I am using access instead of the MRP Interface, it is because later down the line I will be needing to re-organise the whole stock system, so a lot of fields will have their product numbers changed.
Thanks for reading
if possible, change the column type to number
if not, a cast should do it:
ORDER BY Val(MasterPNo)

SQL query to only retrieve records which match a list or its subset

I am working on analyzing Add/Remove Program data from our SCCM server and need some assistance with a query.
The data has been retrieved from SCCM and scrubbed so that I'm only left with a list of actual applications I'm interested in. Common apps/security updates/runtimes, etc have been filtered out.
I need to define an SQL query which will give me a list of computers and their applications only if they have one or more applications defined by a list.
e.g.
Computer | Application
--------------------------------------
WKS001 | Microsoft Office
WKS001 | WebEx
WKS001 | Java Runtime 1.6_33
--------------------------------------
WKS002 | Microsoft Office
WKS002 | WebEx
WKS002 | Java Runtime 1.6_33
WKS002 | Photoshop
--------------------------------------
WKS003 | WebEx
WKS003 | Java Runtime 1.6_33
--------------------------------------
WKS004 | WebEx
WKS004 | Photoshop
I need to retrieve all computers which have any or all of the following applications:
Microsoft Office
WebEx
Java Runtime 1.6_33
That should give me WKS001 and WKS003
If you need to exclude computers that have an application not in that list:
SELECT DISTINCT t1.Computer
FROM Table AS t1
LEFT OUTER JOIN Table AS t2 ON t1.Computer = t2.Computer
AND t2.Application NOT IN ('Microsoft Office','WebEx','Java Runtime 1.6_33')
WHERE t1.Application IN ('Microsoft Office','WebEx','Java Runtime 1.6_33')
AND t2.Computer IS NULL;
SELECT Computer, Application
FROM TABLE_NAME_HERE
WHERE Application='Microsoft Office'
OR Application='WebEx'
OR Application='Java Runtime 1.6_33'
The other answers don't bring back the computer's applications. Try this:
SELECT
computer
,application
FROM
TABLENAME
WHERE
computer IN
(
SELECT
computer
FROM
TABLENAME
WHERE
application IN
(
'Microsoft Office'
,'WebEx'
,'Java Runtime 1.6_33'
)
)
AND comptuer NOT IN
(
SELECT
computer
FROM
TABLENAME
WHERE
application NOT IN
(
'Microsoft Office'
,'WebEx'
,'Java Runtime 1.6_33'
)
)

Cumulative average number of records created for specific day of week or date range

Yeah, so I'm filling out a requirements document for a new client project and they're asking for growth trends and performance expectations calculated from existing data within our database.
The best source of data for something like this would be our logs table as we pretty much log every single transaction that occurs within our application.
Now, here's the issue, I don't have a whole lot of experience with MySql when it comes to collating cumulative sum and running averages. I've thrown together the following query which kind of makes sense to me, but it just keeps locking up the command console. The thing takes forever to execute and there are only 80k records within the test sample.
So, given the following basic table structure:
id | action | date_created
1 | 'merp' | 2007-06-20 17:17:00
2 | 'foo' | 2007-06-21 09:54:48
3 | 'bar' | 2007-06-21 12:47:30
... thousands of records ...
3545 | 'stab' | 2007-07-05 11:28:36
How would I go about calculating the average number of records created for each given day of the week?
day_of_week | average_records_created
1 | 234
2 | 23
3 | 5
4 | 67
5 | 234
6 | 12
7 | 36
I have the following query which makes me want to murderdeathkill myself by casting my body down an elevator shaft... and onto some bullets:
SELECT
DISTINCT(DAYOFWEEK(DATE(t1.datetime_entry))) AS t1.day_of_week,
AVG((SELECT COUNT(*) FROM VMS_LOGS t2 WHERE DAYOFWEEK(DATE(t2.date_time_entry)) = t1.day_of_week)) AS average_records_created
FROM VMS_LOGS t1
GROUP BY t1.day_of_week;
Halps? Please, don't make me cut myself again. :'(
How far back do you need to go when sampling this information? This solution works as long as it's less than a year.
Because day of week and week number are constant for a record, create a companion table that has the ID, WeekNumber, and DayOfWeek. Whenever you want to run this statistic, just generate the "missing" records from your master table.
Then, your report can be something along the lines of:
select
DayOfWeek
, count(*)/count(distinct(WeekNumber)) as Average
from
MyCompanionTable
group by
DayOfWeek
Of course if the table is too large, then you can instead pre-summarize the data on a daily basis and just use that, and add in "today's" data from your master table when running the report.
I rewrote your query as:
SELECT x.day_of_week,
AVG(x.count) 'average_records_created'
FROM (SELECT DAYOFWEEK(t.datetime_entry) 'day_of_week',
COUNT(*) 'count'
FROM VMS_LOGS t
GROUP BY DAYOFWEEK(t.datetime_entry)) x
GROUP BY x.day_of_week
The reason why your query takes so long is because of your inner select, you are essentialy running 6,400,000,000 queries. With a query like this your best solution may be to develop a timed reporting system, where the user receives an email when the query is done and the report is constructed or the user logs in and checks the report after.
Even with the optimization written by OMG Ponies (bellow) you are still looking at around the same number of queries.
SELECT x.day_of_week,
AVG(x.count) 'average_records_created'
FROM (SELECT DAYOFWEEK(t.datetime_entry) 'day_of_week',
COUNT(*) 'count'
FROM VMS_LOGS t
GROUP BY DAYOFWEEK(t.datetime_entry)) x
GROUP BY x.day_of_week