Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Query Help - Some simple (hopefully) grouping

$
0
0
I have been tasked with creating a SQL query which does a couple of things and, being a relative new user to SQL, was hoping the community here might be able to help- I don't necessarily need direct answers, but rather help with what I should be looking for to get the right answers! I have three main tables:Table 1 = AreaTable 2 = Product_CategoryTable 3 = Product_AttributesI'd like to do a count of the number of product_categories, and a sum of the entries in Product_Attributes grouped first by Area, then Product_Category, e.g.Area1, ProductCat1, Product_attribute1Area1, ProductCat2, Product_attribute1Area1, ProductCat2, Product_attribute2Area1, ProductCat2, Product_attribute3Area2, ProductCat1, Product_attribute1Area2, ProductCat1, Product_attribute2Area3, ProductCat1, Product_attribute8Area3, ProductCat2, Product_attribute7Area3, ProductCat3, Product_attribute3Should give me something like:Area | productCat | NoOfProductCat | NoOfAttribsArea1 | ProductCat1 | 1 | 1Area1 | ProductCat2 | 1 | 3Area2 | ProductCat1 | 1 | 2Area3 | ProductCat1 | 1 | 1Area3 | ProductCat2 | 1 | 1Area3 | ProductCat3 | 1 | 1My second challenge is, I'd like to show the (decimal) ratio of the noofattribs to noofproductcat fields.My third, and final, challenge (let me know if I should split this into separate questions) - some of my attribs are ages - and I'd like to filter group them (again) so any which are under 30 days old for example are marked as grouped as this month, and a second grouping based on any which are over 30 days (for ease of reference, I'll use the tables above, and call the dates as being either expdate1 for under 30 days or expdate2 for being over 30 days):Area1, ProductCat1, Product_attribute1, expdate1Area1, ProductCat2, Product_attribute1, expdate2Area1, ProductCat2, Product_attribute2, expdate1Area1, ProductCat2, Product_attribute3, expdate1Area2, ProductCat1, Product_attribute1, expdate2Area2, ProductCat1, Product_attribute2, expdate1Area3, ProductCat1, Product_attribute8, expdate2Area3, ProductCat2, Product_attribute7, expdate1Area3, ProductCat3, Product_attribute3, expdate1should give:Area | productCat | NoOfProductCat | NoOfAttribs | expdate1 | expdate2Area1 | ProductCat1 | 1 | 1 | 1 | 0Area1 | ProductCat2 | 1 | 3 | 2 | 1Area2 | ProductCat1 | 1 | 2 | 1 | 1Area3 | ProductCat1 | 1 | 1 | 0 | 1Area3 | ProductCat2 | 1 | 1 | 1 | 0Area3 | ProductCat3 | 1 | 1 | 1 | 0Does that make sense?Thanks for any help!

Viewing all articles
Browse latest Browse all 3145

Trending Articles