Query about Items in one Table - mysql

I looking for a query which based on a table were I have all Items from my shops. This table contains the following columns:
location: Shop Name
product: Item Number
attributinstance_id: not in use so it always is NULL
Units:count of Items
I like to write a query where I can see which Items are in the other Shops and how much of them and the result should be in the same row.
For example:
Shop 1 Shop 2 Shop 3
item 1 1 2 5
I get it running with the items which are in all tree shops available or was available but when i look up items which were only available in one of the tree shops they will not showing. Has there anybody an idea?
My idea so far:
SELECT Distinct
products.name AS Artikel,
eins.units AS Europa_Center,
zwei.Units AS O_Strasse,
drei.units AS K_Strasse
FROM stockcurrent eins
LEFT JOIN stockcurrent zwei
ON (eins.product = zwei.product
AND eins.location ='0'
AND zwei.location ='a59cb899-27f4-460c-b5df-89a89eaaef75')
LEFT JOIN stockcurrent drei
ON (zwei.product = drei.product
AND zwei.location ='a59cb899-27f4-460c-b5df-89a89eaaef75'
AND drei.location ='3b53adf5-eaee-4a13-b22b-39d50b14e497')
JOIN products
ON products.id = eins.product
JOIN locations
ON locations.id = eins.location
The Whole think is for the POS System Unicentas 4.3. The Table look like this:
Location Product atributsinstance_id Units
shop_1 item_1 NULL 5
shop_2 item_1 NULL 1
shop_3 item_1 NULL 0
shop_2 item_2 Null 3
shop_1 item_3 Null 2
shop_2 item_3 Null 4
shop_3 item_3 Null 1

You need conditional aggregation:
SELECT product_id,
SUM(CASE WHEN location = 'SHOP1' THEN units ELSE 0 END) as shop_1,
SUM(CASE WHEN location = 'SHOP2' THEN units ELSE 0 END) as shop_2,
SUM(CASE WHEN location = 'SHOP3' THEN units ELSE 0 END) as shop_3
FROM stock
GROUP BY product_id

Related

SQL query to return items with count sale_id = 4

I have 3 tables:
user: user_id | user_name
Sale: sale_id(pk) | user_id(fk) | sale_amount
item: item_id(pk) | sale_id(fk)
The sale_id is generated everytime a user purchases from the item table.
I wish to generate an output that will written me user_id, sale_id and count(item_id) i.e, total items purchased by a user in separate transaction where count(sale_id) in item table =4.
I tried using this but it does not work:
select s.user_id,s.id, count(i.item_id)
from sale s join item i
on s.id = i.sale_id
group by s.use_id, i.sale_id
having count(i.sale_id) = 4
;
If a user_id 1 as 2 different transactions where in first transaction(sale_id = 3) he buys 2 items and in second transaction (sale_id = 13) he purchases 4 items which means in the item table each four items purchased will have same sale_id. So, I want to see the output where users have purchased 4 items in one transaction.
output:
s.user_id | i.sale_id | count(i.item_id)
1 13 4
you have aggregation with respect to s.user_id,s.id however you are grouping by s.user_id, i.sale_id. This typically causes error because your query is malformed.
furthermore if you put i.sale_id into group by and try to use having with count(i.sale_id) = 4 this never returns results. because count(i.sale_id) will always be either 0 or 1 when you group by with sale_id

complex sql query counting and grouping

in table LOG i have the following entries
userid item role
1 Apple Buyer
2 Banana Seller
3 Apple Buyer
1 Banana Seller
3 Orange Buyer
etc
i'm trying to create the following two tables with SQL. table 1:
item countUser1Buy countUser1Sell
Apple
Banana
Orange
AND TAble 2 (here i mean the mean of totals. as in, the mean of the total apples bought
item alluserBuyTotalMean allUserBuyTotalMedian allUserBuyRange allUserBuyStandardDev
Apple
Banana
Orange
i think the first should be a variation on
`SELECT `item`, `role`, count(`item`) as count FROM `LOG` GROUP BY `item`
which is close, giving me
item role count
Apple Buyer 1
Banana Seller 1
but i cant figure out how to make it as i'm trying to get. the second i'm really stumped. thanks
You need conditional aggregation for this:
SELECT `item`,
COUNT(CASE WHEN role='Seller' THEN 1 END) AS countUserSell,
COUNT(CASE WHEN role='Buyer' THEN 1 END) AS countUserBuy
FROM `LOG`
GROUP BY `item`
First query:
SELECT item
, SUM(role = 'Buyer') AS countUser1Buy
, SUM(role = 'Seller') AS countUser1Sell
FROM LOG
WHERE userid = 1
GROUP BY item;
Second query:
SELECT item
, AVG(total) AS alluserBuyTotalMean
, CONCAT(MIN(total), ' - ', MAX(total)) AS allUserBuyRange
, STDDEV_POP(total) AS allUserBuyStandardDev
FROM (
SELECT item
, COUNT(*) AS total
FROM LOG
WHERE role = 'Buyer'
GROUP BY item, userid
) t
GROUP BY item;
SELECT
`item`
,SUM(CASE WHEN `userid`= 1 AND `role` = 'Buyer' THEN 1 ELSE 0 END) as countUser1Buy
,SUM(CASE WHEN `userid` = 1 AND `role` = 'Seller' THEN 1 ELSE 0 END) as countUser1Sell
FROM `LOG`
GROUP BY Item
You will need to make a new CASE statement for each userid, but this should work. Results in the following:
item countUser1Buy countUser1Sell
Apple 1 0
Banana 0 1
Orange 0 0

JasperReports: crosstab to display null columns

I have three tables, inventory_location, item, item_stock
inventory_location :
inv_loc_id (PK)
inv_loc_desc
item :
item_id (PK)
code_no
item_desc
inv_cat_id
item_stock :
item_stock_id (PK)
item_id
inv_loc_id
quantity
I want to display a report like this :
Code Description Loc1 Loc2 Total
1 Desc1 5 3 8
inventory_location has data : (Loc1,Loc2,Loc3,Loc4,Loc5) & can be added with another record.
My problem is, if a location is not yet in item_stock - meaning no item stored in it yet, it will display like the following :
Code Description Loc1 Loc2 null null Total
1 Desc1 5 3 0 0 8
What I need is, to display all the locations even if they are not yet in item_stock.
Here's my query, hopefully you could correct me.
SELECT
il.`inv_loc_id` AS inv_loc_id,
il.`inv_loc_desc` AS inv_loc_desc,
i.`item_id` AS item_id,
i.`nrc_no` AS nrc_no,
i.`code_no` AS code_no,
i.`item_desc` AS item_desc,
i.`unit_id` AS unit_id,
iss.`item_stock_id` AS item_stock_id,
iss.`inv_loc_id` AS inv_loc_id,
iss.`quantity` AS quantity,
i.`inv_cat_id` AS inv_cat_id
FROM `item` i
LEFT JOIN `item_stock` iss
ON iss.`item_id` = i.`item_id`
LEFT JOIN `inventory_location` AS il
ON il.`inv_loc_id` = iss.`inv_loc_id`
WHERE i.inv_cat_id = 1
GROUP BY iss.inv_loc_id,
i.item_id
ORDER BY iss.item_stock_id DESC
FROM inventory_location il
LEFT JOIN item_stock iss ON iss.inv_loc_id = il.inv_loc_id
LEFT JOIN item i ON iss.item_id = i.item_id
Change your FROM clause so that you start with inventory_location.

Joining Tables and Pivoting Data with MySQL

Objective 1:
Your sales data is stored in the Purchases table.
Your sales staff wants to see the sales data in a pivoted form, broken down by quarter.
If your Purchases table doesn't have sales data, create some. Be sure the data spans four quarters.
Next, write a query to pivot the data as follows:
Album Q1 Q2 Q3 Q4
OK Computer 2 5 3 7
Sea Change 8 6 2 1
Do not create a separate table or a view. Do not alter any tables.
Save your query as dba1lesson10project1.sql and hand in the project.
This is What I need to do. But, the table it wants me to work with looks like this. And it states in the assignment I cannot alter it at all.
CustomerID DateOfPurchase SongID
1 2007-03-31 3
3 2007-06-30 4
4 2007-09-30 4
5 2007-12-31 5
I know I need to join three tables together so I can group by the title. Which are my Songs, Albums, and Purchases tables.
SELECT Albums.Title FROM Albums
LEFT JOIN Songs
INNER JOIN Purchases
ON Songs.SongID = Purchases.SongID
ON Albums.Title = Purchases.SongID,
SELECT Title,
SUM(CASE WHEN QUARTER(DateOfPurchase) = 1 THEN 1 ELSE 0 END) AS 'Q1',
SUM(CASE WHEN QUARTER(DateOfPurchase) = 2 THEN 1 ELSE 0 END) AS 'Q2',
SUM(CASE WHEN QUARTER(DateOfPurchase) = 3 THEN 1 ELSE 0 END) AS 'Q3',
SUM(CASE WHEN QUARTER(DateOfPurchase) = 4 THEN 1 ELSE 0 END) AS 'Q4'
From Purchases
GROUP BY Title;
I'm kind of at a loss here when it comes to Joining three separate tables then pivoting the data
I've tried the code above in multiple other variants which has failed me past the table joining portion.
Any help would be much appreciated.
My suggestion before attempting to PIVOT the data would be to first, write the query to return the columns that you need, this will involve joining your tables. You didn't provide your table definitions so I am making a few guesses on the structure. If your tables are structured similar to the following:
CREATE TABLE Purchases
(`CustomerID` int, `DateOfPurchase` datetime, `SongID` int)
;
CREATE TABLE Albums
(`AlbumId` int, `Title` varchar(11))
;
CREATE TABLE Songs
(`SongID` int, `AlbumID` int)
;
Then you would SELECT from the tables using a JOIN similar to this code:
select a.title,
p.DateOfPurchase
from albums a
inner join songs s
on a.albumid = s.albumId
inner join purchases p
on s.songid = p.songid
This query will return to you the album Title as well as the DateOfPurchase which you need to pivot the data. Once you have the JOINS worked out, then you can replace the p.DateOfPurchase with your aggregate function and CASE expression and add the GROUP BY clause to get the final result:
select a.title,
SUM(CASE WHEN Quarter(p.DateOfPurchase) = 1 THEN 1 ElSE 0 END) AS Q1,
SUM(CASE WHEN Quarter(p.DateOfPurchase) = 2 THEN 1 ELSE 0 END) AS Q2,
SUM(CASE WHEN Quarter(p.DateOfPurchase) = 3 THEN 1 ELSE 0 END) AS Q3,
SUM(CASE WHEN Quarter(p.DateOfPurchase) = 4 THEN 1 ELSE 0 END) AS Q4
from albums a
inner join songs s
on a.albumid = s.albumId
inner join purchases p
on s.songid = p.songid
group by a.title;
See SQL Fiddle with Demo. This gives a result:
| TITLE | Q1 | Q2 | Q3 | Q4 |
| OK Computer | 1 | 0 | 0 | 0 |
| Sea Change | 0 | 1 | 1 | 0 |

Join tables and get the same column to more columns based on the value

Lets say I join two tables and get a result like
id vendor vendor_id quantity
1 Sony 1 25
1 Apple 2 12
1 HTC 3 5
And I want the result to be like
id Quantity_Sony Quantity_Apple Quantity_HTC
1 25 12 5
How can I do that, I use Left joins to join the tables. I use mySql
SELECT ID,
MAX(CASE WHEN vendor = 'Sony' THEN Quantity END) Quantity_Sony,
MAX(CASE WHEN vendor = 'Apple' THEN Quantity END) Quantity_Apple,
MAX(CASE WHEN vendor = 'HTC' THEN Quantity END) Quantity_ATC
FROM
(
-- add your existing query here
) x
GROUP BY ID