I have 3 tables with data and I need to select sum of sales (SalesAmount) per country(CountryRegionName) in 2014 fiscal year (FiscalYear).
The first table is Geography.
There are data with CountryRegionName and also SalesTerritoryKey (number which means some districts of CountryRegionName).
The second table, anong others, has information about OrderDateKey, SalesTerritoryKey and SalesAmount. The OrderDateKey field include records in the following format: "20130828".
The third table, among others, has fields DateKey (in the same format as OrderDateKey from second table and FiscalYear (format like: "2012").
I dont't understand how I can select needed data using all 3 tables because second table has information about year (OrderDateKey) in different format which third table supports in field FiscalYear.
Currently I stopped which following statement:
`SELECT CountryRegionName, SUM(SalesAmount) FROM FirstTable GROUP BY SalesTerritoryKey`
I know that we need to join all tables and make some subqueries but don't understand how to compare dates and countries which are in different formats.
Thanks for your help!
SELECT t1.CountryRegionName, SUM(t2.SalesAmount)
FROM ThirdTable t3
LEFT JOIN SecondTable t2
ON t3.DateKey = t2.OrderDateKey
LEFT JOIN FirstTable t1
ON t1.SalesTerritoryKey = t2.SalesTerritoryKey
WHERE t3.FiscalYear = "2014"
GROUP BY t1.CountryRegionName
Related
I have a problem with my query. I've managed to display data from 2 tables. Next I want to add up SALE_AMT from the 3rd table, the problem is that order by cannot, the program output adds up all data not by order by
This my query:
SELECT
customer.CUST_NO, customer.CUST_NM,
store.STR_CD, store.STR_NM, SUM(SALE_AMT)
FROM
customer, store, cust_sale_pnt
GROUP BY
CUST_NO
table customer
table cust_sale_pnt
table store
My result
my result
Can you guys help me to sum data based on grouping cust_no?
The problem is, that you don't specify a condition that's used to combine the records of the different tables. In effect, you're creating a 'cross join'.
That is, any record of table customer is combined with any record of table store and the result is combined with any record of table cust_sale_pnt.
In effect, you get each of your 3 customers combined with each of your 6 sales. Therefore the sum over sale_amt is identical for each customer.
You want an INNER JOIN to combine the records:
SELECT c.CUST_NO, c.CUST_NM, SUM(csp.SALE_AMT)
FROM customer c
INNER JOIN cust_sale_pnt csp ON c.CUST_NO = csp.CUST_NO
INNER JOIN store ON csp.STR_CD = store.STR_CD
GROUP BY c.CUST_NO, c.CUST_NM;
I am running a MySQL Server on Ubuntu, patched up to date...
In MySQL, I have 2 tables in a database. I am trying to get a stock query change working and it kind of is, but it's not :(
What I have is a table (table A) that holds the last time I have checked stock levels, and another table (table B) that holds current stock levels. Each table has identical column names and types.
What I want to do is report on the changes from table B. The reason is that there are about 1/2 million items in this table - and I cannot just update each item using the table as a source as I am limited to 100 changes at a time. So, ideally, I want to get the changes - store them in a temporary table, and use that table to update our system with just those changes...
The following below brings back the changes but shows both Table A and Table B.
I have tried using a Left Join to only report back on Table B but I'm not a mysql (or any SQL) guy, and googling all this... Can anyone help please. TIA. Stuart
SELECT StockItemName,StockLevel
FROM (
SELECT StockItemName,StockLevel FROM stock
UNION ALL
SELECT StockItemName,StockLevel FROM stock_copy
) tbl
GROUP BY StockItemName,StockLevel
HAVING count(*) = 1
ORDER BY StockItemName;
The query below spit out records that have different stock level in both table.
SELECT s.StockItemName, s.StockLevel, sc.StockLevel
FROM stock s
LEFT JOIN stock_copy sc ON sc.Id = s.Id AND sc.StockLevel <> s.StockLevel
ORDER BY s.StockItemName
ok - I solved it - as there wasn't a unique ID on each table that could be matched, and rather than make one, I used 3 colums to create the unique ID and left joined on that.
SELECT sc.StockItem, sc.StockItemName, sc.Warehouse, sc.stocklevel
FROM stock s
LEFT JOIN stock_copy sc ON (sc.StockItem = s.StockItem AND sc.StockItemName = s.StockItemName AND sc.Warehouse = s.Warehouse AND sc.StockLevel <> s.StockLevel)
having sc.StockLevel is not Null;
Using SQL in MySQLWorkbench I'd like to group records by 'UserID',
select only the records that match where 'Weeks' are in(5,6,7,8) and sum the values for each user in 'Score'.
The records are actually stored in two tables that I am joining, a lineup table and lineup history table:
INNER JOIN vi_lineup on vi_lineup.lineup_master_id = vi_lineup_master.lineup_master_id
Obviously, I am a newbie. I did search S.O. first but did not find a matching question. I'll keep looking and hope someone answers here. Thanks for any help.
A comment asked about the Weeks. The game is based on a weekly model and we number them sequentially. Here's a working query joining the tables and selecting records based on the weeks:
select * FROM vi_lineup LU
INNER JOIN vi_lineup_master LM
WHERE (LU.week > 4 and LU.week < 9)
AND LU.lineup_master_id = LM.lineup_master_id
Limit 0,148000;
What I would like to do now is Group the records by LM.UserID and sum the values in LU.Score
Thanks!
You have not told us where userid or score comes from. But assuming you can clarify that yourself, the "basic" structure of the query will be like this:
SELECT
??.UserID, SUM(??.score) AS sum_score
FROM vi_lineup lu
INNER JOIN vi_lineup_master lm ON lu.lineup_master_id = lm.lineup_master_id
WHERE lu.week between 5 and 8
GROUP BY
??.UserID
You will need to replace each ?? with the correct table alias "lu" or "lm"
I only have one table to count, I am not using any join. Is this possible?
Select engagement_type as name, COUNT(engagement_type) as y
From events
group By engagement_type
order By engagement_type
But only result is 1 row with count per engagement_type. I want to show all count of accounts without any engagement_type. Like these:
Will appreciate your answers! Thanks!
If there is a lookup-table, say EngagementTypes, where all possible values of engagement types are stored, then you can query this table to get the full list of all types and do a LEFT JOIN to events table in order to get the corresponding count:
Select t1.engagement_type as name, COUNT(t2.engagement_type) as y
From EngagementTypes AS t1
left join events as t2 on t1.engagement_type = t2.engagement_type
group By t1.engagement_type
order By t1.engagement_type
I have two tables... cloads and jpurch I need to pull transactionamount and transaction date column from both tables using one query. The transactions I need to pull must be between a date range of 1-1-2016 and 3-1-2016. Obviously the query I posted below wont work since it doesn't even address the jpurch in the where statement. The transaction dates do not need to match. I need all data for those columns.
select c.transactiondate, c.transactionamount, d.transactiondate, d.transactionamount
from cloads c, jpurch d
where c.transactiondate between '2016-05-01' and '2016-06-06'
#sanky send a good article on joins and is a good place to start. #SujeetSinha also brings up a point about how you will relate one table to the other.
I suspect the answer is you want only records between the tables that match (INNER JOIN) and that you want to match on transactiondate from one table to the other. In that case you could write a query like below. Because both tables have to match on an inner join your where statement for cloads would also filter jpurch.
select
c.transactiondate
,c.transactionamount
,d.transactiondate
,d.transactionamount
from
cloads c
INNER JOIN jpurch d
ON c.transactiondate = d.transactiondate
where
c.transactiondate between '2016-05-01' and '2016-06-06'
Thanks for More Info seems we as a community are still awaiting some help on what you want the data to look like in order to best guide you in your query. If you can take 3-4 rows from each of your tables show us the beginning data and a mock up of how you want it to look from those rows we will better be able to assist you.
If no relation and into 1 table are up meaning you want to APPEND one table to the other? If so use a UNION or UNION ALL depending on if you want all of the data returned or only DISTINCT values of it. ALL means give you everything with UNION alone means don't repeat rows that are already in the table on top of the union statement.
select
c.transactiondate
,c.transactionamount
from
cloads c
where
c.transactiondate between '2016-05-01' and '2016-06-06'
UNION ALL
select
d.transactiondate
,d.transactionamount
from
jpurch d
where
d.transactiondate between '2016-05-01' and '2016-06-06'