MariaDB Select SUM wit Distinct in columns - mysql

I have one table "TABLE1" ... in that table I have 2 columns InvoiceNumber and Total
when invoice is created that creates line like
InvoiceNumber Total
1 5.75
1 5.75
1 5.75
2 3.25
2 3.25
3 9.99
3 9.99
3 9.99
And of course thers is lot of other column contains the line details... but the Total column is the total of the invoice...
I need to get the SUM of the column total but with DISTINC select on the invoicenumber column...
I wish my explanation is now better !!! :)
Tank you very much I appreciate it !
Old question ....
I Have this kind of query... but I dont know exactly how to write it to make it work... Someone can help me ? Tank you very much and sorry if my englis is not very good ...
SELECT
SUM(1Z1.TotalStx) AS TotSTX,
1Z1.Dept,
1Z1.TrType,
ItemDept.DeptNumber,
ItemDept.VenteEncaisse
FROM
(SELECT DISTINCT 1Z1.InvoiceNumber)
JOIN ItemDept ON ItemDept.DeptNumber = 1Z1.Dept
WHERE Dept <> '0'
AND DateHre >= '2017-11-01'
AND DateHre <= '2017-11-30 23:59:59'
AND ItemDept.VenteEncaisse = '1'
ORDER BY 1Z1.Id;

If you want the DISTINCT values of InvoiceNumber, then which Dept, TrType, etc are you hoping to get?
Let's see SHOW CREATE TABLE 1Z1. If it shows PRIMARY KEY(InvoiceNumber), then there is no ambiguity and DISTINCT is unnecessary. If that is not the PK, then you need to figure out how to deal with the ambiguity before moving forward.
What table is DateHre in?

Related

Count the number of registration by date

I am stuck with a select I have to do, I have a data base where a new claim file is registered in the table called “claims”, in this table every file is registered as follows :
Sorry, i have attached above a print screen with how the tables look, i don't know why are as bellow when i post it.
ClaimFileNumber || Vehicle number || ……. || OpeningDate
1 abc 20170302
2 bcd 20170302
3 efg 20170301
4 hij 20170301
I need a select which can help me to find out how many claim files are open on each day from when this year started until now, ordered by top 5 days for each month like for example, on the month of May we have: 20170506 - 300 claims, 20170511 – 295 claims, 20170509 – 200 claims etc.
Or it is ok a select which can give me the number of claims opened per day and order them desc.
The problem is that the date stored in table OpeningDate it is stored as numeric and not as date, this is the tricky part at least for me.
I cannot use a select like “select count (OpeningDate) from claim where openingdate = 20170302” for each day because there are more than 200 days from when the year have started.
Thank you in advance for your help.
This should do it:
SELECT OpeningDate, COUNT(OpeningDate)
FROM claim
WHERE LEFT(OpeningDate, 4) = '2017'
GROUP BY OpeningDate
ORDER BY OpeningDate ASC, COUNT(OpeningDate) DESC
You need group by:
select OpeningDate,count(1) from your_table group by OpeningDate
For top 5, you need order by and limit
select OpeningDate,count(1)
from your_table
group by OpeningDateorder
order by 2 desc
limit 5

Retrieve rows that have a first entry in 2014 in MySQL

I want to retrieve all rows from a table that have their first entry on or after 01/01/2014 but no later than 31/12/2014
Example of the table:
OID FK_OID Treatment Trt_DATE
1 100 19304 2011-05-24
2 100 19304 2011-08-01
3 100 19306 2014-03-05
4 200 19305 2012-02-02
5 300 19308 2014-01-20
6 400 19308 2014-06-06
For example. I would like to pull all entries that have STARTED treatment in 2014. So above i would to extract FK_OID's 300 and 400 because their first entry is in 2014, but i would like to omit FK_OID 100 because they have 2 entries prior to 2014.
How do i go about this? I can extract all entries within a date range etc but that brings back all entries for that date and doesn't omit anyone who has an entry prior to the start of the date range. It just returns their first entry in 2014.
For the ones who need to see that i have tried something. See below.
I am not an experienced coder and this is the best i can get because i don't have the knowledge.
SELECT
mod,
(select NHSNum from person p
WHERE
p.oid = t.fk_oid) as 'NHS'
FROM
timeline t
Where trt_date BETWEEN '2014-01-01' AND '2014-12-31'
ORDER BY trt_date ASC
This returns every treatment for 2014 regardless of whether it is the first ever one for that person. I want to omit anyone from this list who has had treatment before 01/01/2014 as well as only return the first treatment per person. For example, this code returns all treatments for all people in 2014. I only want their first one and only if it is their first one ever.
Thanks.
create table aThing
( oid int auto_increment primary key,
fk_oid int not null,
treatment int not null,
trt_date date not null
);
insert aThing (fk_oid,treatment,trt_date) values
(100, 19304, '2011-05-24'),
(100, 19304, '2011-08-01'),
(100, 19306, '2014-03-05'),
(200, 19305, '2012-02-02'),
(300, 19308, '2014-01-20'),
(400, 19308, '2014-06-06');
select fk_oid,dt
from
( select fk_oid,min(trt_date) as dt
from aThing
group by fk_oid
) xDerived
where year(dt)=2014;
+--------+------------+
| fk_oid | dt |
+--------+------------+
| 300 | 2014-01-20 |
| 400 | 2014-06-06 |
+--------+------------+
The inner part, the nested one, become a derived table, and is given a name xDerived. This means that even though it is just a result set, by making it a derived table, it can be referred to by name. So it is not a physical table, but a derived one, or virtual one.
So that derived table is a very simple group by with an aggregate function. It says, for every fk_oid, bring back one row and only 1 row, with its minimum value for trt_date.
So if you have 10 million rows in that table called aThing, but only 17 distinct values for fk_oid, it will return only 17 rows. Each row being the minimum of trt_date for its fk_oid.
So now that that is achieved, the outer wrapper says just show me those two columns (but with a year check). There is a complicated to explain reason why I had to do that, so I will try to do it here.
But I might need a little time to explain it well, so bear with me.
This will be a shortcut way to say it. I had to get the min into an alias, and I only had access to that alias if resolved in a derived table, to cleanse it so to speak, and then access it with an outer wrapper.
An alias of aggregate column, like as dt, is not available (as a pseudo like column name which is what an alias is) ... it is not available in a where clause. But by wrapping it in a derived table name, I cleanse it so to speak, and then I can access it in a where clause.
So I can't access it directly in its own query in the where clause, but when I wrap it in an envelope (a derived table), I can access it on the outside.
I will try better to explain it later, maybe, but I would have to show alternative attempts to gain access to results, and the syntax errors that would result.
There's probably a more elegant solution, but this seems to satisfy the requirement...
SELECT x.*
FROM my_table x
JOIN
( SELECT fk_oid
, MIN(trt_date) min_date
FROM my_table
GROUP
BY fk_oid
HAVING min_date > '2014-01-01'
) a
ON a.fk_oid = x.fk_oid
LEFT
JOIN my_table b
ON b.fk_oid = a.fk_oid
AND b.trt_date > '2014-12-31'
WHERE b.oid IS NULL;
Having a few years a experience with this, i decided to revisit it. The solution i now use regularly is:
SELECT t1.column1, t1.column2
FROM MyTable AS t1
LEFT OUTER JOIN MyTable AS t2
ON t1.fkoid = t2.fkoid
AND (t1.date > t2.date
OR (t1.date = t2.date AND t1.oid > t2.oId))
WHERE t2.fkoid IS NULL and t1.date >= '2014-01-01'

Need Help building a SQL query

I was wondering if anyone could help me with this, I have had a go at a number of things like using union queries but I think I am looking in the wrong place.
I am trying to run a SQL query on a singular table to provide to users with only datareader permissions to discover total sales.
The table itself contains sales and refunds which have a type of 1 or 3 in the database.
Table example structure:
Reference TransactionType StockCode Value
Sale01 1 Bat1 10
Sale01 1 Bat1 10
Sale09 1 Ball1 3
Sale15 1 Shin1 50
Sale16 1 Bat1 10
Refund06 3 Bat1 10
What I need to get is a total value for stock whereby the refund total is taken away from the total sum so that it appears as such:
StockCode TotalSales
Bat1 20
Ball1 3
Shin1 50
This means that I would have to have a calcuation under one column that says:
Select (if type is 1 then sum(value) - (if type is 3 then sum(value)
In honestly I simply can't do it! I have tried using temporary tables and such but it has completely stumped me.
Here you go
SELECT StockCode,
SUM( (CASE WHEN TransactionType = 3 THEN -1 ELSE 1 END) * Value)
FROM yourtable
GROUP BY StockCode

Retrieve unique data from MYSQL database

I have a table in my database which contains 5 rows. I am trying to write an sql statement that will retrieve all rows which only have 1 agency assigned to them.
case_id agency_ID
1 4
2 4
3 3
4 2
4 4
To clarify I would like to select the required rows (and any further rows) but only if the case_id is unique. Any rows with duplicates would be ommited.
I have tried to use DISTINCT(case_id), COUNT(*) to count all rows but it doesn't work and it's slowly sapping away my soul. It is probably an easy fix, but for the life of me I just can't see it.
Hope this is enough information to go on. Any help would be greatly appreciated.
SELECT * FROM your_table GROUP BY case_id HAVING COUNT(agency_ID) = 1
You can try
SELECT case_id,agency_ID,COUNT(case_id) as c
FROM yourTable
GROUP BY case_id
HAVING (c=1)

Triple count and groupby using MySql on a simple table... is it possible in one query?

I'm having a question that can be explained using a simple fictive table.
Table "Drinks" has just three fields:
Id (1..N) - Primary key
Date ('2012-09-19'...) - Each date can occur very often
Hot (1 for yes, and 0 for false).
I would like to produce a list like this:
Date Total Hot Cold
2012-09-19 14 6 8
2012-09-10 21 18 3
Etc.
The field "Cold" is as you might expect calculated as (Total - Hot).
What I've got so far is:
SELECT Date, count(*) AS Total FROM Drinks GROUP BY Date;
This gives me the desired table, but of course without the columns "Hot" and "Cold".
Is there a way to modify my query so I can produce this table in one go? I can of course built the table in phases using PHP code, but that is probably not the elegant way nor the fastest.
I'm happy to watch and learn... :)
You can add CASE statements in your SELECT clause.
SELECT Date,
count(*) AS Total,
SUM(CASE WHEN Hot = 1 THEN 1 ELSE 0 END) totlHOT,
SUM(CASE WHEN Hot = 0 THEN 1 ELSE 0 END) totalCold
FROM Drinks
GROUP BY Date;
SELECT Date,
count(*) AS Total,
SUM(Hot = 1) Hot,
SUM(Hot = 0) Cold
FROM Drinks
GROUP BY Date;