I am trying to write this query for a while and have tried a few sites but not explain really how to use the IF statement.
So I have data in columns that are people scores for films
Jim
Jack
Fred
Film 1
6
6
2
Film 2
7
8
5
Film 3
3
5
8
...
for example and i want a query that when i select a cell with a name in it it will return the scores for that person (make top ten and scores by film genera / person etc.)
So I have this query I know the syntax is not correct I have tried many ways and cant get it to work. (E18 is the cell I with a pick list of the names)
=Query(Sheet1!A4:D,"select A & IF(E18=”Jim”, select B order by B desc Limit 10"")")
What I want to do is when I select a name then it returns the top 10 films for that user + the scores
Any help greatly received.
Thankyou
aaron
Let assume you will select actor name in F1 cell (as per my attached screenshot). They try below QUERY() formula.
=QUERY(A2:D4,"select A, " & CHAR(MATCH(F1,A1:D1,0)+64) & " order by " & CHAR(MATCH(F1,A1:D1,0)+64) & " desc limit 10")
Related
Ive got this update query that uses employee activity stored in two tables (Beldata_filter & Urenlog_filter) to create a performance analysis per employee in another table (Bellers_filter).
however, i cannot get my query to update the performance stats per employee using only the activities of that employee in the other two tables, instead, access updates the stats per employee to the total activities of all employees.
here is the update query that i have written, the '.Beller' and '.Naam' signify columns in the tables with the names of the employees that i would like access to distinguish from one another.
UPDATE (Bellers_Filter INNER JOIN Urenlog_Filter ON Bellers_Filter.Naam=[Urenlog_Filter].Naam) INNER JOIN Beldata_Filter ON Bellers_Filter.[Naam]=[Beldata_Filter].Beller SET
Bellers_Filter.[Num b] = Dcount("[Beller]","[Beldata_Filter]"),
Bellers_Filter.[Num o] = Dcount("[Opgenomen]","[Beldata_Filter]","Opgenomen = 1"), Bellers_Filter.[Num nno] = Dcount("[Actie]","[Beldata_Filter]","Actie = 3"),
WHERE ([Beldata_Filter].Beller=[Urenlog_Filter].Naam);
The source table Beldata_filter looks like this:
Beller
ID
Moment
Opgenomen
Actie
Robert
55
8-11-2022
1
1
Susan
56
8-11-2022
1
1
Robert
55
9-11-2022
1
2
Robert
55
9-11-2022
0
3
Susan
56
9-11-2022
1
1
in this table each observation describes an action conducted by an employee. The other source table Urenlog_filter has got the same format, in this case the significance of the data in the tables is not as important as the observations of the data.
The table that will be updated needs to look like this
Id
Naam
Num b
Num o
Num nno
1
Robert
3
2
1
2
Susan
2
2
0
This is the desired result in its most simple form, the update query recognises 3 observations for robert and 2 for Susan under "Num b". Right now, the table looks like this
Id
Naam
Num b
Num o
Num nno
1
Robert
5
4
1
2
Susan
5
4
1
Who can help me with this problem? If you guys need more info please let me know!
Saving aggregate data is usually unnecessary and bad design. If it can be calculated for UPDATE, it can be calculated when needed.
SELECT Beldata_Filter.ID, Beldata_Filter.Beller,
Count(Beldata_Filter.Moment) AS [Num n],
Count(IIf([Opgenomen]=1,[Opgenomen],Null)) AS [Num o],
Count(IIf([Actie]=3,[Actie],Null)) AS [Num nno]
FROM Beldata_Filter
GROUP BY Beldata_Filter.ID, Beldata_Filter.Beller;
However, if you really must save, I don't see need for JOINing tables.
Include filter criteria in DCount() for names.
UPDATE Bellers_Filter SET
[Num b] = DCount("*","Beldata_Filter","Beller='" & [Naam] & "'"),
[Num o] = DCount("*","Beldata_Filter","Beller='" & [Naam] & "' AND Opgenomen=1"),
[Num nno] = Dcount("*","[Beldata_Filter]","Beller='" & [Naam] & "' AND Actie = 3");
I have two tables Medication and Inventory. I'm trying to SELECT all the below details from both tables but there are multiple listings of medication ids with different BRANCH_NO also in the INVENTORY table (the primary key in INVENTORY is actually BRANCH_NO, MEDICATION_ID composite key)
I need to total up the various medication_IDs and also join the tables in one SELECT command and display all the infomation for each med (there are 5) with a total sum of each med at the end of each row. But im getting all muddled trying Group by and Sum and at one point partition. Help please I'm new to this.
Below is the latest non working version - but it doesn't display
Medication Name
Medication Desc
Manufacturer
Pack Size
like i chanced it might.
SELECT I.MEDICATION_ID,
SUM(I.STOCK_LEVEL)
FROM INVENTORY I
INNER JOIN (SELECT MEDICATION_NAME, SUBSTR(MEDICATION_DESC,1,20) "Medication Description",
MANUFACTURER, PACK_SIZE FROM MEDICATION) M ON MEDICATION_ID=I.MEDICATION_ID
GROUP BY I.MEDICATION_ID;
For the data imagine I want this sort of output:
MEDICATION_ID MEDICATION_NAME STOCK_LEVEL OtherColumns.....
1 Alpha 10
2 Bravo 20
3 Charlie 20
1 Alpha 30
4 Delta 10
5 Echo 20
5 Echo 40
2 Bravo 10
grouping and totalling into this:
MEDICATION_ID MEDICATION_NAME STOCK_LEVEL OtherColumns.....
1 Alpha 40
2 Bravo 30
3 Charlie 20
4 Delta 10
5 Echo 60
I can get this when its just one table but when Im trying to join tables and also SELECT things its just not working.
Thanks in advance guys. I appreciate it may be a simple solution, but it will be a big help.
You need to write explicitly all non-aggregated columns into both SELECT and GROUP BY lists ( Btw, no need to use a nested query, and if it's the case MEDICATION_ID column is missing in it ) :
SELECT I.MEDICATION_ID, M.MEDICATION_NAME, SUM(I.STOCK_LEVEL) AS STOCK_LEVEL,
SUBSTR(M.MEDICATION_DESC,1,20) "Medication Description", M.MANUFACTURER, M.PACK_SIZE
FROM INVENTORY I
JOIN MEDICATION M ON M.MEDICATION_ID = I.MEDICATION_ID
GROUP BY I.MEDICATION_ID, M.MEDICATION_NAME, SUBSTR(M.MEDICATION_DESC,1,20),
M.MANUFACTURER, M.PACK_SIZE;
This way, you'll be able to return all the listed columns.
I am having a table such as follows,
select * from tablename;
ID sentence
1 This is a sentence
2 This might be a test
3 America
4 This this
I want to write a query to split the sentence into words and get the count of the words in the descending order. I want to have an output something like,
word count Unique(ids)
This 4 3
a 2 2
might 1 1
.
.
.
where count is the number of times the word has occurred in the column and Unique(ids) is the number of users with that word.
I am thinking in what way we can write a query to do this?
Can anybody help me doing this in hive?
Thanks
laterral View
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LateralView
select id, word
from tablename tn lateral view explode( split( tn.sentense, ' ' ) ) tb as word
the result will be:
1 This
1 is
1 a
1 sentense
2 This
2 might
2 be
2 a
2 test
3 america
aggregate the result
Note that I'm doing this in MS Access, so a solution using basic SQL operators would be appreciated.
Suppose you have a table where each row represents a coin flip in a series of coin flips.
Disclaimer: I'm using coin flips as an analogy so I don't have to explain my actual data set.
SELECT * FROM CoinFlips;
Id Flip Time
-------------------
1 Heads 1
2 Tails 2
3 Heads 3
4 Heads 4
5 Heads 5
6 Tails 6
How would you write a query that returns all of the rows above with an additional column that counts the number of 'head' flips that occurred up to that row's occurrence. In other words, this is what I want the result to look like:
Desired Output
Id Flip Time NumHeads
--------------------------------
1 Heads 1 1
2 Tails 2 1
3 Heads 3 2
4 Heads 4 3
5 Heads 5 4
6 Tails 6 4
To do this in MS Access, you need a correlated subquery or join/aggregation. Other databases have direct support for this functionality, but not MS Access.
select cf.*,
(select count(*)
from CoinFlips as cf2
where cf2.flip = 'Heads' and cf2.id <= cf.id
) as NumHeads
from CoinFlips as cf;
Well here is a solution
SELECT coinFlips.ID
,coinFlips.Flip
,coinFlips.TIME
,DCount("Flip", "coinFlips", "ID <=" & [Time] & "AND Flip ='Heads'") AS CountHeads
,DCount("Flip", "coinFlips", "ID <=" & [Time] & "AND Flip ='" & [Flip] & "'") AS CountEachOccurence
FROM coinFlips;
I've looked into Dynamic SQL and the inc() function, but neither are really what I'm after.
Say I have a database like this:
grade name age
9 Bob 9
10 Sue 11
11 Larry 15
9 Joe 8
10 Carrot 10
I want to create a table that first selects all the rows with the lowest grade (9) then displays the oldest. It then goes through and searches for the next highest grade (10) and displays the oldest. Then goes to the next highest grade (11) and displays the oldest.
I'd like for them all to be in the same table and not have to write out a separate SQL call and different PHP variables for each grade.
This is the SQL call I have right now:
$query = "SELECT * FROM horses WHERE grade='1' ORDER BY points DESC LIMIT 1" or die(mysql_error());
Is there a way I can make the grade column increment until it reaches the highest number in the database?
Thanks for any suggestions.
You don't need a loop for this if I understand your request. Instead, you need a MAX() aggregate grouped by grade. The following method should work independently of your RDBMS. It relies on a JOIN against a subquery which returns the greatest age per group to get the age/group pair and join that back against the main table to retrieve the name (and other columns as needed).
SELECT
horses.grade,
horses.name,
horses.age
FROM
horses
JOIN (
SELECT grade, MAX(age) as maxage
FROM horses
GROUP BY grade
) ma ON horses.grade = ma.grade AND horses.age = ma.maxage
ORDER BY grade ASC
Here is an example on SQLFiddle.com
Returns:
GRADE NAME AGE
9 Bob 9
10 Sue 11
11 Larry 15
It is generally far faster an less resource-intensive to do one query instead of multiple queries in a loop, so this should be the approach whenever possible.