how to joining two group-by queries - ms-access

alright this is my mistakes, I asked question that nobody would understand it, now I'm trying to make my question clearer and more simple.
Here I've 3 tables, first is employeeid contains (nameid, salary), second is overtimeid contains (nameid 'from employeeid', period, tot_ot), and the third is absenceid contains (nameid 'from employeeid', period, tot_absence).
how can I populate those three tables into one query containing (nameid, salary, period (fr overtimeid should be the same with absenceid), tot_ot, tot_absence).
please help me master of ms-access, I can't do wonder thing without your help,..... thanks before.

You can use query like this:
SELECT nameid, period, Sum(tot_absence) AS SumOftot_absence, Sum(tot_ot) AS SumOftot_ot
FROM
(SELECT nameid, period, tot_absence, Null AS tot_ot
FROM absenceid
UNION ALL
SELECT nameid, period, Null AS tot_absence, tot_ot
FROM overtimeid)
GROUP BY nameid, period;
Then outer join it with employeeid and you will receive the list of employees for each period
Edit
Query for tables from comments:
SELECT NMKARY, PERIODGJ, sum(JMLMBUR) as sumofJMLMBUR, sum(TOTABS) as sumofTOTABS, sum(KASBON) as sumofKASBON
FROM (
SELECT NMKARY, PERIODGJ, JMLMBUR, Null as TOTABS, Null as KASBON
FROM overtimeid
Union ALL
SELECT NMKARY, PERIODGJ, Null as JMLMBUR, TOTABS, KASBON
FROM potonganid
)
GROUP BY NMKARY, PERIODGJ;

Related

SQL: Replacing NULL with 0 in monthly count table

I have a table named GAINLP
The table contains fields
'Record#" (INT),
'SightingDate' (DATE),
'SpeciesName' (VARCHAR)
Need SQL to output an array that contains an integer that corresponds to the sum of SightingDate for each month.
Example: 0,0,0,0,1,5,10,12,5,3,0,0
Instead, the following code causes null value sums to be skipped and I'm left with 1,5,10,12,5,3
`select count(SightingDate) from GAINLP where SpeciesName LIKE '%Actias luna' GROUP BY MONTH(SightingDate)`
I understand that this can be done by joining with a calendar table, but I've not found examples of such code that also employs the WHERE operator.
You could use conditional aggregation here:
SELECT
MONTH(SightingDate) AS month,
COUNT(CASE WHEN SpeciesName LIKE '%Actias luna' THEN 1 END) cnt
FROM GAINLP
GROUP BY
MONTH(SightingDate);
But, this might not be as efficient as a calendar table based join approach. Here is how you might do that:
SELECT
t1.month,
COUNT(t2.SightingDate) cnt
FROM (SELECT DISTINCT MONTH(SightingDate) AS month FROM GAINLP) t1
LEFT JOIN GAINLP t2
ON t1.month = MONTH(t2.SightingDate)
WHERE
t2.SpeciesName LIKE '%Actias luna'
GROUP BY
t1.month;
Note: It might be possible that your data could span more than a given year, in which case you would probably want to report both the year and month.
You seem to be looking for conditional aggregation. The principle is to move the filtering logic to a CASE construct within the aggregate function.
SELECT
SUM(
CASE
WHEN SpeciesName LIKE '%Actias luna'
THEN 1
ELSE 0
END
)
GROM gainlp
GROUP BY MONTH(SightingDate)

SQL: Prevent data from displaying with condition

is their any way or a query to prevent data from displaying if it has same incident_id and the pnt_id is NULL using
in my attached image. the highlighted data must not be displayed
is it possible? if so. can anyone help me.? and Thanks in Advance
Try this query:
SELECT respond_id, incident_id, pnt_id FROM TBL
WHERE pnt_id IS NOT NULL
UNION
SELECT respond_id, incident_id, pnt_id FROM TBL
WHERE incident_id in (SELECT distinct(incident_id)
FROM TBL
GROUP BY incident_id
HAVING count(id) = 1);
The first part of the union will get all the not null pnt_ids
and the second part will get all the records that may be null but go in the result.
Union merges the two results eliminating duplicates

Mysql query is not working but i could not guess why

Hello Friends I have two tables one is customer and the other one is new_party_estimate. But My following query is not working. Please help me to solve this problem.
SELECT
acc_name,
customer_id
FROM
customers
WHERE STATUS = 'e'
AND acc_name NOT IN (
SELECT DISTINCT
customer
FROM
new_party_estimate
WHERE closed = '0'
AND (
customer_alt = ''
OR customer_alt IS NULL
)
)
ORDER BY acc_name
I am running the sub query separately and it is giving the output. But When i run the full query at once the mysql shows empty result. Please tell what would be the problem!
Table Customers is Following with few records.
acc_name customer_id
CAMPUS FASHION_khyati CAM-11
PAPPU SUIT HOUSE PAPAAR5
R K FASHION R KAAR6
SELECTION MENS WEAR SELAAR7
Table new_party_estimate is Following with few records.
customer
LOVELY DRESSES
ASHIRWAD GARMENTS
AKASH DEEP
ABDUL LATIF READYMADE SALE
Nothing obvious in you query, although IN can be inefficient (using EXISTS would be more efficient).
Another alternative would be to do a LEFT JOIN but only bring back records where there is no match:-
SELECT acc_name,
customer_id
FROM customers
LEFT OUTER JOIN new_party_estimate
ON customers.acc_name = new_party_estimate.customer
AND closed = '0'
AND (customer_alt = ''
OR customer_alt IS NULL)
WHERE new_party_estimate.customer IS NULL
AND STATUS = 'e'
ORDER BY acc_name
I got the answer of this question but i am not sure why it is happening.
While running this query if there is any row in new_party_estimate table having all values equal to null then this query does not work.

MySQL query was working before, now it isn't?

I'm having an odd problem, and I don't have the slightest idea of why it isn't working.
I have the following query that I constructed:
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, ROUND( AVG( reviews.average ) , 0 ) AS review
FROM servers
INNER JOIN reviews ON servers.id = reviews.server
ORDER BY servers.score DESC
This query was working fine a few weeks ago. It is meant to get many fields from the "servers" table, and the average field from the "reviews" table where the server in the "reviews" table is the same as the id in the "servers" table.
Like I said, this query was working fine before. Yesterday I noticed that a vital part of my site wasn't working, and I figured out that this query is failing.
I've confirmed that is returning exactly 1 row (when, at the moment, it should be returning 4, because there are 4 entries in the "servers" table.)
This is what phpMyAdmin gives me when I execute that query:
id name address port up down genre score version country review
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Could anybody enlighten me? I've come here as a last resort, because I am stuck.
As mentioned in the comments, try changing the INNER JOIN to a LEFT OUTER JOIN which will return servers, regardless if there is a matched row in the review table or not. Also, you didn't post your schema, but double check the reviews.server column in the reviews table, it may be server_id instead. Another issue, you are doing an AVG which is a grouped calculation, but you have no GROUP BY clause, so I would suggest adding it, so your full query should look like:
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, ROUND( AVG( reviews.average ) , 0 ) AS review
FROM servers
LEFT OUTER JOIN reviews ON servers.id = reviews.server # might be reviews.server_id
GROUP BY reviews.server
ORDER BY servers.score DESC
More info about GROUP BY functions.
-- Update --
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, IFNULL(ROUND(AVG(reviews.average)), 0) AS review
FROM servers
LEFT OUTER JOIN reviews ON servers.id = reviews.server
GROUP BY servers.id
ORDER BY servers.score DESC

mysql count brings back at least 1 row

I've recently moved jobs and gone from a working with T-SQL to MySql. So far so good until today.
i'm running the following sql script:
SELECT PB.idproductbundle AS ID,
PB.Name AS Name,
PB.Discount AS Discount,
PB.DiscountIsPercent AS DiscountIsPercent,
COUNT(PB_P.idproductbundle) AS ProductCount
FROM `mydb`.productbundles AS PB
LEFT JOIN `mydb`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle
simple command to bring back all product bundles with a count of how many products in that bundle.
Strange thing is, there is currently no data in tables: productbundles or ProductBundle_Product.
but it insits on bringing back 1 row. all the columns are their default value:
ID Name Discount DiscountIsPercent ProductCount
NULL, NULL, NULL, NULL, '0'
In T-Sql this would have no rows.
Because you have a COUNT clause in the select, which will bring back a zero if there are no rows that satisfy the query. So you're guaranteed at least one row - the result of the COUNT telling you there are zero rows.
Turns out i was missing a group by :/
SELECT PB.idproductbundle AS ID,
PB.Name AS Name,
PB.Discount AS Discount,
PB.DiscountIsPercent AS DiscountIsPercent,
COUNT(PB_P.idproductbundle) AS ProductCount
FROM `ukiss-order-management`.productbundles AS PB
LEFT JOIN `ukiss-order-management`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle
GROUP BY PB.idproductbundle