SQL Query To See if Sums Dont Add Up - sql

I have the two tables below is in a database
Is there a SQL query that can get all the invoicesamounts from the invoice detail table and see if the sum of the detailamount based on the invoicenum dont add up to the total invoiceamount to the corresponding invoice number in the invoice table?
Invoice Table
invoicenum | invoicedate | invoiceamount
-----------+-------------+--------------
00551198 | 1/1/2014 | $150.5
00551199 | 1/2/2014 | $10
invoiceDetail Table
invoicenum | trackingno | detailamount
-----------+--------------------+-------------
00551198 | 1Z2F12346861507266 | $50
00551198 | 1Z2F12346861507267 | $80
00551198 | 1Z2F12346861507268 | $20.5
00551199 | 1Z2F12346861503423 | $10.5

An aggregation and join should do the trick:
select i.*, id.sumdetail
from invoices i left join
(select id.invoicenum, sum(detailamount) as sumdetail
from invoicedetail id
group by id.invoicenum
) id
on i.invoicenum = id.invoicenum
where id.sumdetail <> i.invoicenum or id.sumdetail is null;

Here's your query
select *, case when t1.s=i.invoiceamount then 'equal' else 'not equal' end from invoice i
left join
(select sum(detailamount) as s, invoicenum from invoicedetail group by invoicenum) as t1
on t1.invoicenum = i.invoicenum

Related

change and merge multiple values on column if there is duplicate value in other columns

Sorry, I'm new to SQL..I'm using Sybase Central
I need help, I'm currently stuck on data table from select statement (stored procedure) like this :
select case when product like 'A%' then 'Product A' when product like 'B%'
then 'Product B' else 'Product C' end as
'ProductGroup',product,invoice,customer from data_sales
group by product,invoice,customer
Results :
+--------------+---------+---------+---------+
| ProductGroup | Product | Invoice | Customer|
+--------------+---------+---------+---------+
| Product A | A1 | INV001 | MR.A |
| Product A | A1 | INV002 | MR.B |
| Product B | B1 | INV002 | MR.B |
| Product B | B1 | INV003 | MR.C |
+--------------+---------+---------+---------+
I want to merge and change the value into Product C in ProductGroup column, if there is duplicate values on Invoices or Customers columns
Results should be like this :
+--------------+--------+---------+
| ProductGroup | Invoice| Customer|
+--------------+--------+---------+
| Product A | INV001 | MR.A |
| Product C | INV002 | MR.B |
| Product B | INV003 | MR.C |
+--------------+--------+---------+
I've been using case when and group by method, but it's still showing duplicate results
Any help would be really appreciated
Thank you
The below query does check the invoices or customers for duplicates done via group by. Grouping invoices will group the repeated invoices and will give the aggregate count as greater than 1 if found repeated else 0 similarly in or condition for customers. Is this what you want ?
Update Table
Set Product="Product C" where
Invoice In
(Select Invoice
from table group by
Invoice having
count(*)>1) or
Customers In (Select Customers
from table group by
Customers having
count(*)>1)
You can check the duplicates by having count(*)>1 clause with group by
update tab t
set ProductGroup = 'Product C'
where exists (
select 1
from tab tt
where exists ( select Invoice
from tab
where Invoice = tt.Invoice
group by Invoice
having count(*)>1 )
or exists ( select Customer
from tab
where Customer = tt.Customer
group by Customer
having count(*)>1 ) )
If you just want a query which returns the requested information (so no update):
select distinct CASE WHEN (select count()
from Tab t2
where t2.Customer = t1.Customer or t2.Invoice = t1.Invoice) > 1
THEN 'Product C'
ELSE ProductGroup
END "ProductGroup", Invoice, Customer
from Tab t1

How to join result from two select query

i have two table table1 and table2 ItemId foreing key to Table 2
Table 1
|itemsId | Item | Quantity|
----------------------------
1 | item | 20
Table 2
|ItemId | Status | Quantity
---------------------------
1 |Item Out| 5
I subtract quantity in table1 - table2 and assign the value in New Column Called Remain now the problem is i want join the result column with table 1 and table2
ItemId | Item | Quantity | Remaining |
---------------------------------------
1 | item | 20 | 15
I have try This
SELECT *
FROM (select i.ItemsId,i.Date,s.[Item Name],i.Quantity,i.[Recieved By],i.[From],i.[Reference No]
from Store s inner join
ItemsMovement i
on s.ItemsId = i.ItemsId
group by i.ItemsId
) T1 INNER JOIN
(select SUM(a.Quantity - b.Quantity) AS Remaining
FROM Store a inner join
ItemsMovement b
on a.ItemsId = b.ItemsId
where b.Status ='Items In'
group by a.ItemsId
) T2
I think you can do what you want more easily with conditional aggregation:
select i.ItemsId, i.Date, s.[Item Name], i.Quantity, i.[Recieved By],
i.[From],i.[Reference No],
sum(case when i.Status = 'Items In' then s.Quantity - i.Quantity end) as remaining
from Store s inner join
ItemsMovement i
on s.ItemsId = i.ItemsId
group by i.ItemsId;

SQL Display details and count fields through checks

I have created an Oracle database which contains information regarding renting flats/apartments. I am trying to create a SQL script which will, first, count how many students have not paid their first invoice (so there should be an "is not null" check on this field) and, second, display their details.
Here is an example of the data the table is called INVOICE:
+-----------+-------+---------------+---------------+----------------+--------+---------------+-------------+
| INVOICEID | PRICE | PAYMENTMETHOD | FIRSTREMINDER | SECONDREMINDER | RENTID | PAYMENTSTATUS | DATESENT |
+-----------+-------+---------------+---------------+----------------+--------+---------------+-------------+
| 1 | 415 | Visa | 10/FEB/2016 | - | 1 | Paid | 15/MAR/2016 |
| 2 | 600 | Cash | 15/FEB/2016 | - | 2 | Unpaid | 12/MAR/2016 |
| 3 | 750 | Visa | 10/FEB/2016 | 15/MAR/2016 | 1 | Paid | 15/MAR/2016 |
+-----------+-------+---------------+---------------+----------------+--------+---------------+-------------+
Using this data, the SQL statement should return the details for number 2 in the table and count them.
I'd be grateful if anyone could help with this because I have no clue where to start. SQL is not my main programming language.
It seems this should suffice...
To get the count:
select count(*) from invoice where paymentstatus = 'Unpaid'
To get the details for those rows:
select * from invoice where paymentstatus = 'Unpaid'
You mentioned something about an "is not null" check "on this field" - what field are you referring to? Isn't the paymentstatus column sufficient?
The query below enumerates the invoice #'s per RENTID and allows you to select 1st invoices that were unpaid:
select * from (
select *,
row_number() over (partition by rentid order by invoiceid) invoice_number
from mytable
) t where invoice_number = 1 and paymentstatus = 'Unpaid'
If you just want the count, then replace select * with select count(*)
select count(*) from (
select *,
row_number() over (partition by rentid order by invoiceid) invoice_number
from mytable
) t where invoice_number = 1 and paymentstatus = 'Unpaid'
Another way that checks whether secondreminder is null to determine whether an invoice is a 1st invoice
select count(*)
from mytable
where secondreminder is null
and paymentstatus = 'Unpaid'

SQL Group By Issue with same item ID

I am trying to track the total number of sales a rep has along with the amount of time he was clocked into work.
I have the following two tables:
table1:
employeeID | item | price | timeID
----------------------------------------
1 | 1 | 12.92 | 123
1 | 2 | 10.00 | 123
1 | 2 | 10.00 | 456
table2:
ID | minutes_in_shift
--------------------------
123 | 45
456 | 15
I would join these two queries with the following SQL:
SELECT
t1.employeeID, t1.item, t1.price, t1.shiftID, t2.minutes_in_shift
FROM table1 t1
JOIN table 2 t2 ON (t2.ID = t1.timeID)
Which would return the following table:
employeeID | item | price | timeID | minutes_in_shift
---------------------------------------------------
1 | 1 | 12.92 | 123 | 45
1 | 2 | 10.00 | 123 | 45
1 | 2 | 10.00 | 456 | 15
I would like for the consolidate results, however, to have this outcome:
employeeID | itemsSold | priceTotals | totaltimeworked
-----------------------------------------------------------------
1 | 3 | 32.92 | 60
I could use COUNT and SUM for the items and price but I cannot figure out how to properly show the total time worked in the manner it appears above.
Note: I am only having trouble with calculating the time worked. In shift 123 - employee 1 was working 45 minutes, regardless of how many items he sold.
Any suggestions?
If you wish to use the sample data as they are you will need to extract the shifts and sum the minutes, like this:
with a as (
select employeeID, count(*) itemsSold, sum(price) priceTotals
from Sampletable1
group by employeeID),
b as (
select employeeID, shiftID, max(minutes_in_shift) minutes_in_shift
from Sampletable1
group by employeeID, shiftID),
c as (
select employeeID, sum(minutes_in_shift) totaltimeworked
from b
group by employeeID)
select a.employeeID, a.itemsSold, a.priceTotals, c.totaltimeworked
from a inner join c on a.employeeID = c.employeeID
However, with your existing tables the select statement will be much easier:
with a as (
select employeeID, timeID, count(*) itemsSold, sum(price) priceTotals
from table1
group by employeeID, timeID)
select a.employeeID, sum(a.itemsSold), sum(a.priceTotals), sum(table2.minutes_in_shift) totaltimeworked
from a inner join table2 on a.timeID = table2.ID
group by a.employeeID
I think this query should do what you want:
SELECT t1.employeeID,
count(t1.item) AS itemsSold,
sum(t1.price) AS priceTotals,
sum(DISTINCT t2.minutes_in_shift) AS totaltimeworked
FROM table1 t1
JOIN table2 t2 ON (t2.ID = t1.timeID)
GROUP BY t1.employeeID;
Check on SQL Fiddle

SQL join from the same table

How do I get grand total for orders made by credit card only from such a table :
order_id | meta_name | meta_value
___________________________________
1 | type | credit
1 | total | 1030.00
...
2 | type | check
2 | total | 930.00
..
3 | type | credit
3 | total | 330.00
what is the best way to describe such select operation if you are to search the Internet for a solution to this problem.
suppose I am MySQL.
You can do this with either join or group by. Here is the join method:
select ttype.order_id, cast(ttotal.meta_value as money)
from table ttype join
table ttotal
on ttype.order_id = ttotal.order_id and
ttype.meta_name = 'type' and
ttype.meta_value = 'credit' and
ttotal.meta_name = 'total';
If yo could have more than one total for an order, then you would still want to aggregate:
select ttype.order_id, sum(cast(ttotal.meta_value as money))
from table ttype join
table ttotal
on ttype.order_id = ttotal.order_id and
ttype.meta_name = 'type' and
ttype.meta_value = 'credit' and
ttotal.meta_name = 'total'
group by ttype.order_id