Building a sql query from two tables - sql

I created a query which selects sum of columns from a table grouped by a field in Crystal reports, now I want the result to be filtered by a date range from another table which I am unable to do. Please Help...
Here is the query
SELECT
BDETAIL.HSN,
SUM(BDETAIL.TAXABLE),
SUM(BDETAIL.SGST_V),
SUM(BDETAIL.CGST_V),
SUM(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM
BDETAIL
JOIN
BILL ON BDETAIL.BILL_ID = BILL.BILL_ID
WHERE
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY
BDETAIL.HSN, BDETAIL.SGST_P

I finally figured it out ..here is the working query
SELECT BDETAIL.HSN,
sum(BDETAIL.TAXABLE),
Sum(BDETAIL.SGST_V),
Sum(BDETAIL.CGST_V),
Sum(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM BDETAIL INNER JOIN BILL ON
( BILL.BILL_ID = BDETAIL.BILL_ID AND
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY BDETAIL.HSN, BDETAIL.SGST_P;
thank you all for showing interest in my issue.

Related

Use group by with sum in query

These 3 tables that you see in the image are related
Course table and coaching table and sales table
I want to make a report from this table on how much each coach has sold by each course period.
The query I created is as follows, but unfortunately it has a problem and I do not know where the problem is.
Please help me fix the problem
Thank you
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM
dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.pid
For this use, SUM() is an Aggregate Function, so you need to refer all the
fields that you want to get in your result set.
Example:
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid,
dbo.tblPost.postTitle, dbo.tblArticleAuthor.authorName
But this query does not solve the need for your report.
If you just need to get "how much each coach has sold by each course" , you can try the query bellow.
SELECT
dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle,
SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle
If you need, send more details regarding the desired result.
Here you can find more information about SQL SERVER Aggregate Functions:
https://learn.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql?view=sql-server-ver15
And here a quick example regarding SQL Aliases to build queries with a simple
and effective way:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_alias_table
Per your description of the task, the problem is that you only GROUPed BY dbo.tblCustomersOrders.pid, which is the period's id I guess, but you also need to GROUP BY the coach, which is dbo.tblArticleAuthor.authorName, I guess again. Plus in the SELECT field list you can not use more columns only that are aggregated + GROUPed.

How to show all employees absent dates only in MS Access 2010 query?

I want to have a query which shows me records of only those employees that were absent on various dates.
I have two queries in MS Access 2010. One is qAllDatesAllEmp which has all monthly dates for all employees. Second is qDailyWorkEmp which shows date-wise the working of employees. Now, say Emp1 had only one missing date of 5-Jan-2019; emp2 had 15-Jan-2019 missing and so on, the result should show me only these missing dates with employee names. What i have tried so far is that I have joined the date field from qAllDatesAllEmp to the date field in qDailyWorkEmp with join arrow pointing from qAllDatesAllEmp to qDailyWorkEmp. I also added the date field from qDailyWorkEmp to the query grid below but removed the check mark. When I run this query, it gives blank result. I would really welcome the experts' help on how to get the desired output below.
My Sample Data:
Query1: qAllDatesAllEmp contains {WorkDate; empID; empName; CityBased}
Query2: qDailyWorkEmp contains {DailyDate; empID; empName; CityBased}
The SQL for this is as follows:
SELECT qDailyWorkEmp.empID, qDailyWorkEmp.empName, qDailyWorkEmp.CityBased
FROM qAllDatesAllEmp LEFT JOIN
qDailyWorkEmp
ON qAllDatesAllEmp.WorkDate = qDailyWorkEmp.DailyDate
WHERE (((qDailyWorkEmp.DailyDate) Is Null));
My desired output:
empID; EmpName; MissingDate
001; ABC; 5-Jan-2019
007; LMN; 15-Jan-2019
...and so on
From what I can gather from your question, it seems like you are on the right track with your left join query, however, since your qAllDatesAllEmp query contains the master data, you should output the data from this query for records for which the corresponding values are null in your qDailyWorkEmp query.
You also appear to have a typo in your query on this line:
qAllDatesAllEmp.WhatDate = qDailyWorkEmp.DailyDate
As you have stated in your question that the query qAllDatesAllEmp contains the fields:
Query1: qAllDatesAllEmp contains {WorkDate; empID; empName; CityBased}
WhatDate is not one of these fields.
You will also need to join the empID field in both queries, so that you are comparing the date field on a per-employee basis, as opposed to comparing each date with the dates for all employees - hence your join criteria should be:
qAllDatesAllEmp LEFT JOIN qDailyWorkEmp ON
qAllDatesAllEmp.WorkDate = qDailyWorkEmp.DailyDate AND
qAllDatesAllEmp.empID = qDailyWorkEmp.empID
With the information provided, I might suggest the following:
SELECT
qAllDatesAllEmp.empID,
qAllDatesAllEmp.empName,
qAllDatesAllEmp.CityBased
qAllDatesAllEmp.WorkDate as MissingDate
FROM
qAllDatesAllEmp LEFT JOIN qDailyWorkEmp ON
qAllDatesAllEmp.WorkDate = qDailyWorkEmp.DailyDate AND
qAllDatesAllEmp.empID = qDailyWorkEmp.empID
WHERE
qDailyWorkEmp.DailyDate IS NULL

Select information from different table SQL

SELECT Boeking.reisnummer, (aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
FROM Boeking
WHERE Boeking.reisnummer = Reis.Reisnummer
AND Reis.Reisnummer = Reis.Bestemmingscode;
Table 1 (Boeking) has aantal_volwassen and aantal_kinderen, and Reisnummer.
Table 2 (Reis) has Reisnummer and Bestemmingscode.
I have to show the total of aantal_volwassenen and aantal_kinderen. But i also have to show Reis.bestemmingscode which comes from a different table. Currently i have to enter a parameter, how can i solve this?
You need to specify all the tables in the FROM part of your query. The tables should then be joined (JOIN) to get the data you need.
SELECT Boeking.reisnummer
,(aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
,Reis.Bestemmingscode
FROM Boeking INNER JOIN Reis
ON Boeking.reisnummer = Reis.Reisnummer

Error 1214 when using GROUP BY in my view

I've got a problem with an view on my database. All other views are working correctly, but when I'm trying to add a GROUP BY statement behind my query,
then I'll get the next Error:
1214 - The used table type doesn't support FULLTEXT indexes
I've set all my tables to ISAM, and i noticed the whole problem is in the GROUP BY statement.
My query looks like this:
CREATE VIEW `id_winkels`
AS
SELECT
`w`.`w_id` AS `w_id`,
`w`.`k_id` AS `k_id`,
`w`.`w_naam` AS `w_naam`,
`w`.`w_logo` AS `w_logo`,
`w`.`w_homepage` AS `w_homepage`,
`w`.`w_straat` AS `w_straat`,
`w`.`w_huisnummer` AS `w_huisnummer`,
`w`.`w_postcode` AS `w_postcode`,
`w`.`w_woonplaats` AS `w_woonplaats`,
`w`.`w_land` AS `w_land`,
`w`.`w_actief` AS `w_actief`,
`w`.`w_datum` AS `w_datum`,
COUNT(`p`.`p_id`) AS `totaal`
FROM `Winkels` `w`
LEFT JOIN `Producten` `p`
ON `w`.`w_id` = `p`.`w_id`
GROUP BY `w`.`w_naam`
Is there another option to count the amount of products by my shop without useing a group by?
When I'm running the query without the group by statement, it will return my total amount of product in one row.
As you only select fields from Winkels, you should get the product count in a subquery rather than joining the tables:
CREATE VIEW id_winkels AS
SELECT
w.w_id AS w_id,
w.k_id AS k_id,
w.w_naam AS w_naam,
w.w_logo AS w_logo,
w.w_homepage AS w_homepage,
w.w_straat AS w_straat,
w.w_huisnummer AS w_huisnummer,
w.w_postcode AS w_postcode,
w.w_woonplaats AS w_woonplaats,
w.w_land AS w_land,
w.w_actief AS w_actief,
w.w_datum AS w_datum,
(
select count(*)
from Producten p
where p.w_id = w.w_id
) AS totaal
FROM Winkels w;

SUM(a*b) not working

I have a PHP page running in postgres. I have 3 tables - workorders, wo_parts and part2vendor. I am trying to multiply 2 table column row datas together, ie wo_parts has a field called qty and part2vendor has a field called cost. These 2 are joined by wo_parts.pn and part2vendor.pn. I have created a query like this:
$scoreCostQuery = "SELECT SUM(part2vendor.cost*wo_parts.qty) as total_score
FROM part2vendor
INNER JOIN wo_parts
ON (wo_parts.pn=part2vendor.pn)
WHERE workorder=$workorder";
But if I add the costs of the parts multiplied by the qauntities supplied, it adds to a different number than what the script is doing. Help....I am new to this but if someone can show me in SQL I can modify it for postgres. Thanks
Without seeing example data, there's no way for us to know why you're query totals are coming out differently that when you do the math by hand. It could be a bad join, so you are getting more/less records than you expected. It's also possible that your calculations are off. Pick an example with the smallest number of associated records & compare.
My suggestion is to add a GROUP BY to the query:
SELECT SUM(p.cost * wp.qty) as total_score
FROM part2vendor p
JOIN wo_parts wp ON wp.pn = p.pn
WHERE workorder = $workorder
GROUP BY workorder
FYI: MySQL was designed to allow flexibility in the GROUP BY, while no other db I've used does - it's a source of numerous questions on SO "why does this work in MySQL when it doesn't work on db x...".
To Check that your Quantities are correct:
SELECT wp.qty,
p.cost
FROM WO_PARTS wp
JOIN PART2VENDOR p ON p.pn = wp.pn
WHERE p.workorder = $workorder
Check that the numbers are correct for a given order.
You could try a sub-query instead.
(Note, I don't have a Postgres installation to test this on so consider this more like pseudo code than a working example... It does work in MySQL tho)
SELECT
SUM(p.`score`) AS 'total_score'
FROM part2vendor AS p2v
INNER JOIN (
SELECT pn, cost * qty AS `score`
FROM wo_parts
) AS p
ON p.pn = p2v.pn
WHERE p2n.workorder=$workorder"
In the question, you say the cost column is in part2vendor, but in the query you reference wo_parts.cost. If the wo_parts table has its own cost column, that's the source of the problem.