Hello I am currently stuck on a problem. Any help will be appreciated. The data I am looking to classify are weights ranging from 0-80. If the line item is between 0-19 tag it as light 19-39 medium, etc... I am struggling for a starting point for how to get this to work. This query is pulling multiple fields from the data source but this is the only one that is being modified. After this the data will be re-classified based on light, medium, etc.
Thanks for the help. Let me know if anything is needed
You can make a table called WeightClasses
Then you can join to this
Select t.*, wc.weightclassname
From Things as t
inner join WeightClasses as wc
on t.Weight >= wc.LowerBound and t.Weight < wc.UpperBound
This will produce results like this
I think this is a lot easier to read and auditable than IIF statements. You can change your classifications in a table without changing code and you can make sense of your classes very quickly and easily by looking at the table. You can also add weight classes just by adding new records.
USE IIF like this:
IIF(SteelData.[Unit Weight]<19.0001,'Light',IIF(SteelData.[Unit Weight] <
39.00001,'Medium',IIF([etc.])))
Related
I’m working on a website where there are many distinct lines of garments. A line is defined as garments of the same design, style, manufacturer etc. Within a line, garments come in different sizes and colours.
To compose a chart of all the colours available in one particular line the following query works fine (I’m working in Coldfusion but PHP would use the same query).
SELECT
skuColourwayID,cHex,cLongName,skuID, cwColID1
from garment_sku
join garment_colourways
on cwID=skuColourwayID
join garment_colour_name
on cID=cwColID1
where skuLineID= <cfqueryPARAM value = "#url.lnID#" CFSQLType = "CF_SQL_INTEGER">
group by skuColourwayID
In order to accumulate all the info I need, I have to access three tables using joins. (I don’t have any choice about how the data is presented to me). The line is identified by lnID which in the above case starts as a url variable. Starting with the sku table (garment_sku) I access the colourways table (garment_colourways) and get the colourway colour id (cwColID1). By applying this to the colour name table (garment_colour_name) I can get the actual name of the colour, and it’s hex value.
This all works perfectly, EXCEPT a few garments come in bi-colours (ie different colour sleeves to body or collar etc). There is a second column in the garment_sku table, cwColID2 which denotes the second colour.
One way around this would be to perform two separate queries, where cwColID1 is replaced by cwColID2 in the second query. I could then combine the queries programatically to achive the bi-colour where required. However, this seems rather inelegant and I’m sure MySQL has a way of dealing with this in one query?
I hate to say it but there is also provison in the tables for a third colourway cwColID3 though I’ve never come across any three colour garments and would be very happy to solve this for just the two colours.
Thanks for any help you can give.
You just need to JOIN to the garment_colour_name table a second time to get the name of the second colour. Note that since not all garments will have 2 colours, you need to use a LEFT JOIN rather than an INNER JOIN:
SELECT
skuColourwayID,
gc1.cHex AS c1Hex,
gc1.cLongName AS c1Name,
gc2.cHex AS c2Hex,
gc2.cLongName AS c2Name,
skuID,
cwColID1,
cwColID2
from garment_sku
join garment_colourways
on cwID=skuColourwayID
join garment_colour_name gc1
on gc1.cID=cwColID1
left join garment_colour_name gc2
on gc2.cID=cwColID2
where skuLineID= <cfqueryPARAM value = "#url.lnID#" CFSQLType = "CF_SQL_INTEGER">
group by skuColourwayID
I'm an intern in a property rental company. I'm in charge of developping the CRM under Symfony.
So, ones of my entities are properties (houses) and their availabilities. See the table structure below.
The problem I'm facing for now, is that the availabilities had been defined for each day (e.g. 28/01, 29/01, 30/01) instead of being defined for a range of day (e.g. 28/01 -> 30/01). So, the table is really heavy (~710 000 rows). Furthermore, before we changed the way of editing an availability, it created a new row for a same date instead of editing it. So, there are a lot of duplications in this table.
What I want, is to lighten the DB by keeping only the rows which have the max value in date_modif_availabilities for the same date_availabilities and id_properties.
For example, if I have these rows (availabilities_duplications):
I only want to keep the row with the latest modif like this (availabilities_keep_max_value) :
The thing is, I don't know enough the SQL language. I'm able to write few basics scripts but not complex subqueries. Even with code samples that I found.
Thank you in advance for your help.
You could select the elements for which no element with greater modified date exists.
Something like this:
SELECT avl1.*
FROM availabilities avl1
WHERE NOT EXISTS (SELECT *
FROM availabilities avl2
WHERE avl1.date_availabilities = avl2.date_availabilities
AND avl1.id_properties = avl2.id_properties
AND avl2.date_modif_availabilities > avl1.date_modif_availabilities);
This of course has the pre-condition that the combination of the three columns date_availabilities, id_properties and date_modif_availabilities is unique.
Furthermore, it seems that all columns (except the PK) may be NULL. Looks kinda odd to me.
You can use subquery :
select t.*
from table t
where id_availabilities = (select t1.id_availabilities
from table t1
where t1.id_properties = t.id_properties and
t1.date_availabilities = t.date_availabilities
order by t1.date_modif_availabilities desc
limit 1
);
However, if you have concern about the performance, then you want index on (id_properties, date_availabilities and id_availabilities).
I'm giving my best efforts to write a query to get the desired output format shown the second table here. Is there a better way to achieve this, table 1 has the raw data and I want to find the sum of monthly usage of unique devices for a given user. Any help is really appreciated.
table format
Apologize for not being clear in first place. tagged different image to illustrate better. If you look at this data in new image attached. After I filter by username - I get that data output. My need is to get the sum of usage by month by device.
Ex: rows highlighted in the image, where iPhone-6sPlus is used multiple times each month across months. I'm looking for a query that gives output as
iPhone-6SPlus is used xx_hrs in Jan, yy_hrs in feb so on. Similarly for other device models. Hope this helps. Thanks.
Better image
create table #product (model varchar(50),users varchar(5), monthofuse Varchar(3),yearofuse int,usage int)
Insert into #product values('X','a', 'JAN',2010,34), ('X','a', 'Feb',2010,20),('X','a', 'Mar',2010,10),('Y','a', 'Jan',2010,30),
('Y','b', 'Jan',2010,30),('Y','b', 'Feb',2010,30),('X','a', 'JAN',2011,50)
select * from #product
Select * FROM
(Select users,monthofuse,usage,model from #product) q
Pivot
(
sum(usage) for q.monthofuse in([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]))As pvttable
i have the following part of query:
SUM(COALESCE(user_a, user_b)) AS income_adsense
Now, i have a html table in my web app where i present the data from this query.
problem is i want to mark data in one color if answer is from col user_a and different color if answer is from user_b col.
is there a way to achieve that in my query itself? (some sort of flag maybe?)
.
right now the only solution i have is to return all col's and work with the data on the client side but i am wondering if there's a cleaner/best practice solution.
guess it's worth mentioning i don't want to change the table structure.
Well, to make sense of the data I would do something like:
CASE
WHEN SUM(user_a) > SUM(user_b)
THEN 'User_A'
ELSE 'User_B'
END [Most of the data comes from]
You could also have two separate SUM() columns to make sense of this and compare the sum of values in your application.
SUM(user_a) [User_A Score Weight]
, SUM(user_b) [User_B Score Weight]
I have a really simple recordset by the name of qryUserFiles:
string: [UserID], [ftype]; Long: [fsize]
(records are file-information details of each file in each users' userfolder on the file-server)
I have created a report which lists the sum of [fsize] for each [UserID] by grouping on [UserID] and putting =Sum([fsize]) in a textbox in the header of the group. So far so good. So that shows me the total size of that user's user-share on the fileserver. (Helpful with respect to both convincing the users to clean up their stuff, and convincing the executives that we need to buy more storage!)
Now the headbanging part.
I want to add, in that same header, various textboxes containing the sum of the [fsize] for various values of [ftype]. So, for example, I'd want the sum of [fsize] for files where ftype="jpg" for each UserID. (And then another sum of [fsize] for files where ftype="mov" for each UserID, and so on for various 'problematic' file-types!)
I tried putting a
Dsum("fsize","qryUserFiles","ftype='jpg'")
in the group header, but, as expected, it is looking at the entirety of the 'qryUserFiles' recordset, and giving me the domain-total of size for jpg, not the this-user-total of size for jpg.
I considered trying to add something to the 'where' clause of the dsum function that would include 'this' user as criteria, but how would I refer to 'this' userID, with respect to the grouping-pointer?
Or is there a way easier way to do what I want without going nuts!? Would this be easier in another query? I feel like there's a simple, obvious answer just out of reach!
(I'm hoping to avoid brute-force VBA code to step through the table record by record, and calculate the stats with Dsum and add them to a new table. But if that's how to do this....)
I think - judging from what I read - you just need to let the report do its stuff. You can group on user and ftype - including the totals in the details and the totals in the Group Footer
You definitely should not use a DSum formula