Need Help building a SQL query - mysql

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

Related

MariaDB Select SUM wit Distinct in columns

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?

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

sum in mysql than group by

I am making a small lottery game for fun and to improve myself.
On the database, I have
table(id, package, value, price, purchase_code,round)
See an example. There is two package, package1 and package2.
package1 has a value of 3 and package2 has a value of 4. This means, that if I buy the package1, i got 3 ticket which is playing, giving me bigger chance to win in the current round, so it inserts 3 record into a table, containing the informations. So in this case, I have the following records in my table:
id pacakage_id value price purchase_code round
1 1 3 10 w3hjkrw 1
2 1 3 10 w3hjkrw 1
3 1 3 10 w3hjkrw 1
I would like to see overall how money the users spent , and for this, I used sum(price).
Ok, but as you can see, the three record was one purchase, so sum(price) would give me the result 30. I tried to group by purchase_code, but it is not doing what I want.
Here is the code:
$income_query = mysql_query("SELECT SUM(price) FROM lottery WHERE round = '$current_round' GROUP BY code") or die(mysql_error());
while($result = mysql_fetch_array($income_query)) {
$round_money = $result['SUM(price)']." $";
Think you will need to do a sub query to get the package id price. Possibly using distinct, although I would just use a normal aggregate function (MAX will do the job here).
Something like this:-
SELECT code, SUM(package_id_price)
FROM(
SELECT code, package_id, MAX(price) AS package_id_price
FROM lottery
WHERE round = '$current_round'
GROUP BY code, package_id
) Sub1
GROUP BY code

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;

Ranking Values in .asp using Access

fairly new to .asp and the boards, so please bear with me. I am trying to utilize an access DB which stores 24 unique records (baseball teams) with values for TeamName, TeamUID (unique ID), TotalHR, TotalRS, TotalWins and TotalLosses. I would like to use this table to display where a team ranks in each category. For example each team has their own page which is based on their TeamUID. I want to show where they rank (1st through 24th) in each category but cannot figure out how to do that in the page. Any suggestions?
You can assign a rank with MS Access, for example:
SELECT table1.ANumber,
(SELECT count(*)
FROM table1 t
WHERE t.anumber<=table1.anumber) AS rank
FROM table1
WHERE (((table1.ANumber) Is Not Null))
ORDER BY table1.ANumber;
Run against an ADO connection, you can use GetString or GetRows to assign this to a string or array.
The above example will give a duplicate number where there is a duplicate value for ANumber. For example:
ANumber Rank
-1 1
1 7
1 7
1 7
1 7
1 7
1 7
2 11
2 11