Please help me with this.
I have the data as this:
ID Name TotalCost
---- ---------------- ----------
1 Wash, Dry & Fold 175.00
2 Hand Wash and Fold 275.00
3 Pressing Only 25.00
4 Hand Wash and Fold 205.00
5 Dry Clean 395.00
6 Pressing Only 100.00
I would like to display my table like this:
Is this possible using PIVOT without Agrregate?
ID Wash, Dry & Fold Hand Wash and Fold Pressing Only Dry Clean
---- ---------------- ----------------- -------------- -----------
1 175.00
2 275.00
3 25.00
4 205.00
5 395.00
6 100.00
Thank you.
select *
from
(
select ID, Name, TotalCost
from Table
) src
pivot
(
sum(TotalCost)
for Name in ('Wash, Dry & Fold','Hand Wash and Fold','Pressing Only','Dry Clean' )
) piv;
Related
So first of all, I have 2 table. The first one is the categories
Id Name
------------
1 Interior
2 Eksterior
3 Display
then the secon table is history which the data is the task I've finished
Id category_name category_id date (month) User Id
---------------------------------------------------------------
001 Interior 1 3 1084
002 Eksterior 2 3 1084
003 Interior 1 4 1089
004 Eksterior 2 4 1085
005 Display 3 4 1085
and what I want is to get categories by month, user id and know which one already done and not done from history, like this
example the data in March with user id 1084 :
Id Name Status
---------------------------
1 Interior done
2 Eksterior done
3 Display not done
or like this :
Id Name Status
--------------------------
1 Interior 1
2 Eksterior 1
3 Display 0
if the category in history table exist, the status will be 1 for done and 0 for not done.
this is my query before :
SELECT c.id, c.category, c.id=h.category_id status FROM categories c, history h WHERE MONTH(h.created_at)
I keep retrieving the wrong result for my query. Please help me..
Seems like:
SELECT *
FROM
categories c
LEFT JOIN history h on h.category_id = c.id AND h."date (month)" = 3
..will get you towards what you want: there will be NULL in the row from history table, for category Display; you can use this "is or is not null" to create your done/not done column
I'm new to mySQL and working on a query which essentially takes one initial table and summarizes its contents in two separate tables. The second "summary" table is dropping tuples where a value is 0; I need to fix that. Here's the specifics:
I have a large Inventory table, which I assemble from other tables:
SID CID SECTION RATING
==========================
100 1001 1 A
101 1001 1 A
102 1002 1 B
103 1002 2 F
104 1003 1 A
...etc...
("CID" and "Section" are the primary keys here, I don't know if whether the PK is spread across two attributes is significant here.) I have a successful query ("numTable1") which counts the number of SIDs for each CID and Section. For example, with the above Inventory table, "sumTable1" would be:
CID SECTION numSID
=====================
1001 1 2
1002 1 1
1002 2 1
1003 1 1
...etc...
Here's the SQL:
create temporary table numTable1(
SELECT ot1.cid, ot2.section, count(ot1.sid) 'numSID'
FROM otherTable1 ot1, otherTable2 ot2
WHERE ot1.cid=ot2.cid and ot1.section=ot2.section
GROUP BY ot1.cid, ot2.section
);
"NumTable" works great. Here's the problem: I also need a query which counts the number of 'A' Ratings per CID/Section:
create temporary table numA(
select t9.cid, t9.section, count(ot1.rating) 'As'
from otherTable1 ot1, t9
where grade = 'A' and t9.cid=ot1.cid and t9.section=ot1.section
group by t9.cid, t9.section
);
Here's the output of "numA":
CID SECTION As
=====================
1001 1 2
1003 1 1
...etc...
But here's the problem; some CID/Sections have no A Ratings, and they are being washed out. What I need is this:
CID SECTION As
=====================
1001 1 2
1002 1 0
1002 2 0
1003 1 1
...etc...
You'll note that the above table can be seamlessly merged with "numTable1", which is the ultimate aim here:
CID SECTION numSID As
==========================
1001 1 2 2
1002 1 1 0
1002 2 1 0
1003 1 1 1
But as long as "numA" drops CID/Sections with 0 As, I'm dead in the water. I assume my "group by" statement in "numA" is washing out the zeroes... but I'm not sure about that. Any suggestions?
#NigelRen provided the solution - a left join. Here's the code:
-- Left Join on numTable1 with numA... makes for total students // numAs
-- Resultant table is totA: "Total A Ratings"
create temporary table totA(
select numTable1.cid, numTable19.section, numTable1.numStudents, if(numA.As is null, 0, numA.As) 'A Ratings'
from numTable1
left join numA on numTable1.cid=numA.cid and numTable1.section=numA.section
);
I'm building a e-Commerce platform (PHP + MySQL) and I want to add a attribute (feature) to products, the ability to specify (enable/disable) the selling status for specific city.
Here are simplified tables:
cities
id name
==========
1 Roma
2 Berlin
3 Paris
4 London
products
id name cities
==================
1 TV 1,2,4
2 Phone 1,3,4
3 Book 1,2,3,4
4 Guitar 3
In this simple example is easy to query (using FIND_IN_SET or LIKE) to check the availability of product for specific city.
This is OK for 4 city in this example or even 100 cities but will be practical for a large number of cities and for very large number of products?
For better "performance" or better database design should I add another table to table to JOIN in query (productid, cityid, status) ?
availability
id productid cityid status
=============================
1 1 1 1
2 1 2 1
3 1 4 1
4 2 1 1
5 2 3 1
6 2 4 1
7 3 1 1
8 3 2 1
9 3 3 1
10 3 4 1
11 4 3 1
For better "performance" or better database design should I add
another table
YES definitely you should create another table to hold that information likewise you posted rather storing in , separated list which is against Normalization concept. Also, there is no way you can gain better performance when you try to JOIN and find out the details pf products available in which cities.
At any point in time if you want to get back a comma separated list like 1,2,4 of values then you can do a GROUP BY productid and use GROUP_CONCAT(cityid) to get the same.
I've got a huge table, containing three "selection"-columns and many "data"-columns.
ID Thing1 Thing2 Thing3 avgData1 avgData2 highestEtc
---- -------- -------- -------- ---------- ---------- ------------
1 1 2 2 321 654 999
2 2 1 1 123 456 11
3 2 1 1 987 789 77
4 2 1 1 765 567 11
In my queries, I'm now selecting all entries with "Thing1" = x, "Thing2" = y, "Thing3" = z (Those three columns are selection-criteria.)
The purpose of getting those lines is to perform an action on each of the following data-columns: If it starts with "avg", I want to calculate an average of the specific column on all selected entries. On another prefix I want to count which number appears the most.
Is there a way of letting the MySQL Database do all this for me? I need a SQL-Statement that calculates the averages of the columns automatically, and performs other actions too.
For example, let's say I'd select the criteria Thing1=2, Thing2=1 and Thing3=1. Is there a way of writing the statement so that it returns only ONE entry, with the calculated things?
Result
----------------- ----------------- ----
(123+987+765)/3 (456+789+567)/3 11
I heard that this should be possible, and that it is a bad method of NOT letting the database perform those actions directly. Unfortunately, I have no idea how to do it.
Try this:-
SELECT ID, AVG(avgData1) AS RESULT1, AVG(avgData2) AS RESULT2, highestEtc
FROM YOUR_TAB
WHERE Thing1 = 2
AND Thing2 = 1
AND Thing3 = 1
GROUP BY ID
HAVING COUNT(highestEtc) > 1;
Hope this helps you.
I am trying to use the ConcatRelated function to provide a summary report of the prior day's absences, tardies, and vacations. I have tried several variations and can't seem to get it to work in an Access Query. My table looks as below:
ID A_date Area ATV_Shift Associate_Name Absent Tardy Vacation Reason
-- --------- ----------- --------- -------------- ------ ----- -------- --------------
1 1/11/2015 Asm Kenmore First Keon Wilson 1 Sick
2 1/11/2015 Asm Kenmore First Frank Burns 1 Doctor
3 1/11/2015 Asm Kenmore Second Paul Mattocks 1 FLMA
4 1/11/2015 Decoration First Jane Doe 1 Car Broke Down
5 1/11/2015 Asm Maytag Second John Doe 1
I need to make a query that displays the previous days data (Date()-1). The reasons need to be separated by spaces. I can get everything else to sum in a query but I am unable to get the reasons to concat. I have tried following the examples but just cant get it to function. I was only able to get it to work using a simple SQL query but that returned lines for each reason not in one cell.
I changed your A_date values to 1/13/2015 and stored those sample data in a table named YourTable. Using that table, this is the output in Access 2010 from the query below.
A_date SumOfAbsent SumOfTardy SumOfVacation Reasons
--------- ----------- ---------- ------------- -------------------------------
1/13/2015 5 Car Broke Down Doctor FLMA Sick
SELECT
y.A_date,
Sum(y.Absent) AS SumOfAbsent,
Sum(y.Tardy) AS SumOfTardy,
Sum(y.Vacation) AS SumOfVacation,
ConcatRelated(
'Reason',
'YourTable',
'A_date=Date()-1',
'Reason',
' '
) AS Reasons
FROM YourTable AS y
WHERE y.A_date = Date()-1
GROUP BY y.A_date;