Hi guys so I have the following code that should look at a event cost, figure out from a transaction table if its been fully paid and from that display the unpaid balance.
Select CASESTUDY_CLIENT.CLEINT_FNAME as First_Name,
casestudy_client.client_sname as Surname,
casestudy_client.client_Phonenumber as Phone_number,
casestudy_event.event_totalcost- casestudy_transaction.transaction_value as Unpaid_balance
from casestudy_client
inner join casestudy_event
on casestudy_client.client_id = casestudy_event.event_clientid
inner join casestudy_transaction
on casestudy_event.event_id = casestudy_transaction.transaction_eventid
where casestudy_event.EVENT_EVENTSTAGE ='complete' and
casestudy_event.event_totalcost > casestudy_transaction.transaction_value;
This works perfectly however if we have a multi part transaction then it fails to pick up the matching event id and assumes its another event.
In my test data I have an event that costs £500, one transaction that has a value of 400 and one transaction with a value of 99. The current output shows the two records one with an unpaid balance of 100 and the other with 401. What I would like to show is just 99
If this is possible in relatively simple way please let me know as I can't figure out how to make it work
You need to add together transactions.
Select CASESTUDY_CLIENT.CLEINT_FNAME as First_Name,
casestudy_client.client_sname as Surname,
casestudy_client.client_Phonenumber as Phone_number,
casestudy_event.event_totalcost- sum(casestudy_transaction.transaction_value) as Unpaid_balance
from casestudy_client
inner join casestudy_event
on casestudy_client.client_id = casestudy_event.event_clientid
inner join casestudy_transaction
on casestudy_event.event_id = casestudy_transaction.transaction_eventid
where casestudy_event.EVENT_EVENTSTAGE ='complete' and
casestudy_event.event_totalcost > casestudy_transaction.transaction_value
GROUP BY CASESTUDY_CLIENT.CLEINT_FNAME, casestudy_client.client_sname, casestudy_client.client_Phonenumber, casestudy_event.event_totalcost
Also, CLEINT_FNAME is probably a typo.
It appears you want to sum the transaction values before subtracting them from the cost.
Select CASESTUDY_CLIENT.CLEINT_FNAME as First_Name,
casestudy_client.client_sname as Surname,
casestudy_client.client_Phonenumber as Phone_number,
casestudy_event.event_totalcost- casestudy_transaction.transaction_value as Unpaid_balance
from casestudy_client
inner join casestudy_event
on casestudy_client.client_id = casestudy_event.event_clientid
inner join (
select casestudy_transaction.transaction_eventid, sum(transaction_value) as transaction_value
from casestudy_transaction
group by casestudy_transaction.transaction_eventid
) as casestudy_transaction
on casestudy_event.event_id = casestudy_transaction.transaction_eventid
where casestudy_event.EVENT_EVENTSTAGE ='complete'
and casestudy_event.event_totalcost> casestudy_transaction.transaction_value ;
I would probably left join the transactions subquery though so you can include the events that haven't been paid for yet, but that just depends on the goal of the query.
Related
Tables
ARMASTER = Customer Information
ORDERHEAD = Sales order information (Ship to, bill to)
Hey everyone! I'm still quite new to SQL and I do belive I'm picking it up fairly quick. I have been racking my brain on this for a few hours now and have asked around the office and nobody seems to have a solution.
I'm trying to identify how many times a customer account has been used as a SHIP TO location & a BILL TO location.
SELECT ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD AS "MASTER",
COUNT (ORDERHEAD.BILLTO) AS "COUNT_BILL",
COUNT (ORDERHEAD.SHIPTO) AS "COUNT_SHIP"
FROM ARMASTER
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.BILLTO
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.SHIPTO
GROUP BY ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD
And I'm not even remotley getting what I should be getting. However, when I remove one of my joins the count is exactly what it should be for either 1 or the other of them.
Any guidance would be muchly appreciated! Thank you!
I think it can also work
SELECT CUSTOMER,
DIVISION,
STATUS,
CUHEAD AS "MASTER",
(SELECT COUNT(1) FROM ORDERHEAD WHERE SHIPTO = ARMASTER.CUSTOMER ) AS "COUNT_BILL",
(SELECT COUNT(1) FROM ORDERHEAD WHERE BILLTO = ARMASTER.CUSTOMER ) AS "COUNT_SHIP"
FROM ARMASTER
EDIT - i'm reposting this question in an attempt to explain what i mean better
I'm using SQL 2008 R2 and I work for a retail department store and we need a report to show all the sales orders made in each department, and sections of those departments.
What i want is to group up all the sales order lines by department and section, but remove only the sections that have a total sales value of less than £50. I still want to see order lines that are over £50, though.
Here is an example of what i currently have:
Data before filtering
I want to remove the Accessories section and all lines contained within it, as it has a total section value of less than £50. So i would want it looking like this after filtering:
Data after filtering
Here is my code:
SELECT department.department_name
,section.section_name
,sales_order_detail.sales_order_number
,sales_order_detail.sales_order_line
,LineValue
FROM
sales_order_detail INNER JOIN stock_item ON sales_order_detail.stock_item_code = stock_item.stock_item_code
INNER JOIN style ON stock_item.style_code = style.style_code
INNER JOIN department ON style.dept_code = department.department_code
INNER JOIN section ON style.section_code = section.section_code AND style.dept_code = section.department_code AND department.department_code = section.department_code
Can you please explain all the ways this can be done. I've tried using GROUP BY and HAVING but that then filters out all my sales order lines. I've tried using a Group Filter in the visual studio report design surface which removes the lines but then aggregates calculated at the Department group scope don't take into account the lines removed at the section level.
I appreciate any help i can get on this.
Jacob
As you are using 2008R2, you can use the magic that are Windowed Functions to calculate the total of the group that the row belongs to (the partition part of the over clause below) and then wrap your query into a filtering select statement. Not having your data this is obviously not tested, but it should work:
select department_name
,section_name
,sales_order_number
,sales_order_line
,LineValue
,GroupTotal
from(
select d.department_name
,se.section_name
,sod.sales_order_number
,sod.sales_order_line
,sod.qty_ordered * sod.selling_price AS LineValue
,sum(sod.qty_ordered * sod.selling_price) over (partition by d.department_name
,se.section_name
) as GroupTotal
from sales_order_detail sod
inner join stock_item si
on sod.stock_item_code = si.stock_item_code
inner join style s
on stock_item.style_code = s.style_code
inner join department d
on s.dept_code = d.department_code
inner join section se
on s.section_code = se.section_code
and s.dept_code = se.department_code
and d.department_code = se.department_code
) a
where GroupTotal > 50
Trying to return only the 1st supplier code and have all other fields unaffected.
`Select
Container.Part_Key,
Part.Part_No,
Part.Name,
Part.Revision,
Container.Quantity,
Container.Container_Status,
OP.Operation_No,
Opp.Operation_Code,
Part.Part_Status,
Supplier.Supplier_Code
From Part_v_Container as Container
Join Part_v_Part as Part
ON Part.Part_Key = Container.Part_Key
Join Part_v_Part_Operation as Op
On Op.Part_Operation_Key = Container.Part_Operation_Key
Join Part_v_Operation as OPP
On OPP.Operation_Key = OP.Operation_Key
Join part_v_approved_supplier as Approved
On Approved.part_key = container.part_key
Join common_v_Supplier as Supplier
On Supplier.supplier_no = Approved.Supplier_No
Where Container.Active = '1'
group by container.part_key`
There will be duplicate part numbers, revisions, etc. Not worried about that. I just want the part no to list only one approved supplier to the far right, even though for any given part, there are multiple approved suppliers listed in the database.
Furthermore, the order the database lists the approved suppliers does not matter.
Thanks!
Add a sub query in your select list
( select top 1 supplier.supplier_code
From supplier
Where Supplier.supplier_no = Approved.Supplier_No order by Supplier.supplier_no) as supplier code
This can be the last field in the select list
You could add An appropriate order by.
This would work in SQL Server
I have a database with customers, items and orders. This is an exam project, so is maybe not the best for real life. I have this problem. I want a list of what all our customers have bought and a total price of that, but I can't get anything to work.
select Kunder.navn,Ordre.*, Indeholder.antal, Varer.*
from Kunder
join Ordre on Kunder.kunde_nr=Ordre.kunde_nr
join Indeholder on Ordre.ordre_nr=Indeholder.ordre_nr
join Varer on Varer.vare_nr=Indeholder.vare_nr
where er_industri=0
order by bestillingsdato
I have tried a lot like:
sum (Indeholder.antal * Varer.privat_pris) as total
group by Ordre.ordre_nr
But I just can't get anything that I've tried, to give me what I'm looking for.
I hope that you want to help me, and that you understand what I mean (I'm not a native english speaker).
I have uploaded an image of our mapping, so you can see what our database look like:
And for clarity, just a translation:
Kunder = customers
Varer = items
Ordre = orders
Indeholder = includes
Just add more columns on your group by clause.
Apart from the aggregate functions [SUM(), COUNT(), AGV()] the same list of columns must be present at the group by clause
and at select clause:
So, generally:
select <col1, col2, col3...>, SUM(...)
from ...
group by <col1, col2, col3...>
If you want a per customer AND order groupping:
select Kunder.navn, Ordre.ordre_nr, sum (Indeholder.antal * Varer.privat_pris) as total
from Kunder
join Ordre on Kunder.kunde_nr=Ordre.kunde_nr
join Indeholder on Ordre.ordre_nr=Indeholder.ordre_nr
join Varer on Varer.vare_nr=Indeholder.vare_nr
where er_industri=0
group by Kunder.navn, Ordre.ordre_nr
You could also do a per customer groupping:
select Kunder.navn, sum (Indeholder.antal * Varer.privat_pris) as total
from Kunder
join Ordre on Kunder.kunde_nr=Ordre.kunde_nr
join Indeholder on Ordre.ordre_nr=Indeholder.ordre_nr
join Varer on Varer.vare_nr=Indeholder.vare_nr
where er_industri=0
group by Kunder.navn
I have 2 queries which display 2 different values but the results from one query are unexpected.
Query1:
SELECT SUM(T.amount_reward_user) AS total_grouping
FROM fem_user_cards UC
LEFT JOIN fem_transactions T USING(card_number)
LEFT JOIN fem_company_login FCL
ON T.fem_company_login_id=FCL.fem_company_login_id
WHERE UC.fem_user_login_id=193
AND FCL.grouping<>0 AND T.from_group=1 AND T.authorised=1
GROUP BY UC.fem_user_login_id
The above query gives me the total reward amount for the user 193. Here the condition is the reward amount which i am getting out of this query must be from the group which is evident from T.from_group=1. So this seems to be working correct.
Query2:
SELECT SUM(T.amount_reward_user) AS total_grouping
FROM fem_transactions T
LEFT JOIN fem_company_login FCL
ON T.fem_company_login_id=FCL.fem_company_login_id
WHERE T.fem_user_login_id=193
AND FCL.grouping<>0 AND T.from_group=1 AND T.authorised=1
In this query even though T.from_group=1 it is adding up the reward amount from T.from_group=0 as well. Does anyone know what the problem is?
For example:
The user 193 purchased something in a company A(in the group so T.from_group=1) for $5 and got the reward of $1 and bought another product for $5 from company B(out of group so T.from_group=0) and get a reward of $1 again.
My expected output is: 1
I am getting:
Query1: 1
Query2: 2
I could simply use the query 1 but i have issues with other things when i use that so i need to understand what is going on really!!
My aim is to get the sum of reward amounts from the database. The sum should be of different grouped companies. That is if we have 5 different grouped companies and user is using his moneycard in those 5 companies, I should get 5 different totals. If he used his card in any company which is not in the group the total should not include transaction from this un-grouped company. I have C++ code to feed the database as transactions happens. My fem_transactions table has company_id, amount_reward_user, fem_user_login_id, from_group (this decides if the user has transacted out of the group or from the group), authorized etc..
I want to filter the amount spent by the user 193 in each group separately.
I understood the JOINTS concepts now but i need to know why t.from_group on the second query is ignored ?
The problem is that you have criteria in the Where clause against columns on the right side of the Left Join. In the both queries, this is And FCL.grouping <> 0. This effectively changes the Left Join to an Inner Join since you are in requiring that an FCL.grouping value exist and not be zero (or Null). What you want, I think, is to move that criteria into the On clause:
First query:
Select Sum(T.amount_reward_user) AS total_grouping
From fem_user_cards UC
Left Join fem_transactions T Using(card_number)
Left Join fem_company_login FCL
On FCL.fem_company_login_id = T.fem_company_login_id
And FCL.grouping<>0
Where UC.fem_user_login_id=193
And T.from_group=1
And T.authorised=1
Group By UC.fem_user_login_id
Second query
Select Sum(T.amount_reward_user) AS total_grouping
From fem_transactions T
Left Join fem_company_login FCL
On FCL.fem_company_login_id = T.fem_company_login_id
And FCL.grouping<>0
Where UC.fem_user_login_id=193
And T.from_group=1
And T.authorised=1
What is not clear in all of this is what you are trying to achieve. I.e., why even have the Left Joins in either query if you are not filtering on them. If you do specifically want to filter for rows where there is a fem_company_login value with a grouping value <> 0, then use your original query and change the Left Join to an Inner Join. If you want to filter for people that either have a grouping value <> 0 or no value at all then do something like:
Select Sum(T.amount_reward_user) AS total_grouping
From fem_transactions T
Left Join fem_company_login FCL
On FCL.fem_company_login_id = T.fem_company_login_id
Where UC.fem_user_login_id=193
And T.from_group=1
And T.authorised=1
And ( FCL.PrimaryKeyColumn Is Null Or FCL.grouping <> 0 )