473,761 Members | 5,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cant get cool query

4 New Member
Hi,

I've worked with sql for some time now but i can't figure out this one. I'm pretty sure i've come across a very similar query before and i know there is a neat, good solution to this one, hope you can give me a hand.

The schema (simplified) is as follows:

TABLE staff
============
id | name
============
1 | john
2 | mark
3 | brown
============

TABLE staff_worked
=============== =============== =============== ========
staffid (FK staff.id) | days | week_ending | worked_on
=============== =============== =============== ========
1 | 3 | 2002-01-11 | A
1 | 2 | 2002-01-11 | B
2 | 5 | 2002-01-11 | A
3 | 2 | 2002-01-11 | C
1 | 1.5 | 2002-01-18 | D
=============== =============== =============== ========

staff_worked has a column 'id' as PK but i don't think is of any use in here. This table records the number of days per week a staff has worked on something. This something is irrelevant for this query but i've included it to make more sense of the table.

That's it, only two tables. Now, what I want is to know what members of staff have entered less than 5 days in a weekly basis or have not entered anything at all (if no one has entered anything for a certain week then we don't care). We assume that the maximum number of days a staff can enter is 5. We also assume ALL staff must enter something every week.

So, the kind of output i'm looking for according to the schema above is:

QUERY result
=============== ===============
staffid | days | week_ending
=============== ===============
3 | 2 | 2002-01-11
1 | 1.5 | 2002-01-18
2 | 0 | 2002-01-18
3 | 0 | 2002-01-18
=============== ===============

I hope it makes sense. Note that staff ids 1 & 2 don't come up for the week ending 2002-01-11 because they've entered 5 days and staff ids 2 and 3 are shown in week_ending 2002-01-18 because they haven't entered anything and there's one entry for that week (staff id 1)


Any help would be much appreciated.

Happy coding.
Aug 1 '07 #1
4 1380
ilearneditonline
130 Recognized Expert New Member
Not sure if this is what you are looking for or not.

Expand|Select|Wrap|Line Numbers
  1. SELECT a.id, a.name, b.days, b.week_ending
  2. FROM staff a LEFT OUTER JOIN staff_worked b
  3. ON a.id=b.staffid
  4. WHERE b.days < 5
Aug 1 '07 #2
aherraiz
4 New Member
Not sure if this is what you are looking for or not.

Expand|Select|Wrap|Line Numbers
  1. SELECT a.id, a.name, b.days, b.week_ending
  2. FROM staff a LEFT OUTER JOIN staff_worked b
  3. ON a.id=b.staffid
  4. WHERE b.days < 5
Thanks for your reply. However that won't give me what I need. I concluded that I need to dome some coding with the query result to accomplish the final result so I'm afraid MySQL is not enough in this case.
Aug 10 '07 #3
henryrhenryr
103 New Member
Hi

I was having a go but I wasn't quite getting it. Like you say, it will probably be easier to play with the results rather than try to get the outcome purely in SQL. Perhaps you could do it if you changed your table structure. Like moving the 'worked_on' column to another table and having total_days_work ed column in the staff_worked table. Then you would be able to use an IF condition in the query.

Sorry - not very helpful!

Henry
Aug 10 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya, Aherraiz.

This is as close as I can get you:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `id`,
  3.         SUM(`days`)  -- Some staff have multiple logs per week.
  4.             AS `days`,
  5.         `week_ending`
  6.     FROM
  7.     (
  8.             `staff`
  9.         LEFT JOIN
  10.             `staff_worked`
  11.                 ON
  12.                     `staff`.`id` = `staff_worked`.`staffid`
  13.     )
  14.     WHERE
  15.     (
  16.         `week_ending`  -- Only show weeks where somebody worked.
  17.             IN
  18.             (
  19.                 SELECT
  20.                     DISTINCT
  21.                         `week_ending`
  22.                     FROM
  23.                         `staff_worked`
  24.                     WHERE
  25.                         `days` > 0
  26.                     GROUP BY
  27.                         `week_ending`
  28.                     ORDER BY  -- IN subclause s/b sorted.
  29.                         `week_ending` ASC
  30.             )
  31.     )
  32.     GROUP BY
  33.         `week_ending`,
  34.         `staffid`
  35.     HAVING
  36.         `days` < 5  -- 51aX0rZ!
  37.     ORDER BY
  38.         `week_ending` ASC,
  39.         `staffid` ASC
  40.  
The problem with showing staff who logged zero days has to do with a fundamental limitation of MySQL: It can't fetch what isn't there.
Aug 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2246
by: Niranjan | last post by:
I have a relatively straight forward report. The query that populates data for this report is complicated. The report opens without any problem and I can print it as well. However, If I have another form open (has about 12 combo boxes) and then I run the report, it appears on the screen, no problem. When I print the report, I get 'Cant open any more databases' error. I am using OpenQuery method to run some queries before opening the...
0
1394
by: dd_bdlm | last post by:
I have recently updated my database to include a postcode recognition function. And to perform calculations based on that recognised value. The query that makes all these calculations is very large and takes a good while to run (up to a minute?). (But I need all this data in one query so it can be reported in one report.). My problem now is I enter my client details screen and then click to go through to their records (which involves...
4
2513
by: zack | last post by:
Any help with this would be greatly appreciated, as cannot work out how to resolve. I have a report called "3_Strikes". In its 'On open' event is command to also open a criteria form popup form called, "3_Strikes_Search". The purpose of the form is to allow input of data to filter the results of the report, (In this case mail-merged letters). The issue I have is that when I open the report via a command button, the "3_Strikes_Search"...
1
2141
by: erotikaboi | last post by:
Guys, Firstly let me say I am a photographer so if you can offer me a solution I will really appreciate it in simple english not techiespeak as I wont understand it. Thanks. Ok, I am using Access from the Office XP suite on a pentium 4 running WinXP Home. We have a database which has been running successfully for approx 12 months now. Suddenly when I tried to generate a report or query using a wizard I get the mssg "Active X cant create...
17
2034
by: so many sites so little time | last post by:
all right so the script is pretty simple it goes it retrives what the id of the post is and it lets you edit it well no it doesnt. now if you go to www.kirewire.com/pp2/index/php you will see a number 1 that is the value of collumn home_id which is set to auto increment ect ect but this script which gets the id of home_id of a row and lets you edit it does not work for somereason...
6
1963
flexsingh
by: flexsingh | last post by:
Hello there, I have constructed a option of choosing a court only available when member no = "0". This works fine and is in the first code: - <html> <head> </head> <body background="main background1.jpg" link="blue" vlink="blue"> <table width="350" border="1" align="center" cellpadding="0" cellspacing="1"> <tr>
1
1531
by: Ragavendran | last post by:
Hi, I am using this method for search: Query =org.apache.lucene.queryParser.QueryParser.parse(String arg0) throws ParseException Hits = org.apache.lucene.search.Searcher.search(Query query, Sort sort) throws IOException Problem : I cant able to search the word. If the word contain special character like , %BF It just taken that special character a empty space and search the remaining character
2
2526
by: Mucahit ikiz | last post by:
I cant make a full dynamic query in LINQ I have 2 situation methods (only_exp_query, only_tbl_query) those are working. .... using System.Linq.Dynamic; using System.Data.Linq; .... string connString = @"Data Source=.;Initial
66
8192
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it didnt display the html after the perl script executed. Using perl 5.1 and apache 2.2.9 version(apache installed and run without any errors and no warning, perl tested fine) b) Also, when i clicked the html code to submit the upload of the...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.