Reducing rows based on a grouped MAX() - sql-server

Scratching my head for a day on this.
Need to reduce a list of films and genres to a list of films and genres based on the grouped max() views associated with the film.
So transforming 'a' to 'b' here with T-SQL
(Purple rows are the valid ones that will make it output table: Image )
+---------+--------------+-------+
| Title | Genre | Views |
+---------+--------------+-------+
| Mad Max | Mockumentary | 1 |
| Mad Max | Sci-fi | 169 |
| Mad Max | Documentary | 32 |
| Titanic | Drama | 6 |
| E.T. | Sci-fi | 34 |
| E.T. | Sci-fi | 2 |
| E.T. | Horror | 1 |
| Taken | Triller | 60 |
| Taken | Crime Drama | 2 |
| Taken | Triller | 40 |
| Taken | Crime Drama | 15 |
+---------+--------------+-------+
Expected outcome
+---------+---------+-------+
| Title | Genre | Views |
+---------+---------+-------+
| Mad Max | Sci-fi | 169 |
| Titanic | Drama | 6 |
| E.T. | Sci-fi | 36 |
| Taken | Triller | 100 |
+---------+---------+-------+

Try this one...
SELECT title, genre, views
FROM (SELECT title,
genre,
Sum(views) AS views,
ROW_NUMBER() OVER (PARTITION BY title ORDER BY Sum(views) DESC) AS ranks
FROM tablename
GROUP BY title, genre) tmp
WHERE ranks < 2
Output
+---------+---------+-------+
| title | genre | views |
+---------+---------+-------+
| E.T. | Sci-fi | 36 |
| Mad Max | Sci-fi | 169 |
| Taken | Triller | 100 |
| Titanic | Drama | 6 |
+---------+---------+-------+
Online Demo: http://www.sqlfiddle.com/#!18/e34fe/1/0

Related

Pivoting a table with multiple columns in SQL

My goal here is to take a list of two corresponding store numbers and provide an output similar to:
Ultimate goal: produce a list of closest stores by travel time and distance based on source data of 2 rows per zip9 where each row is the travel time in distance, and in time, to a store in question.
The result is that each zip code has 2 stores to choose from, and the requirement is being able to return one row with both options.
+-----------+---------------+---------------------+-------------------+-------------------------+
| zip | Shortest_time | Shortest_time_store | Shortest_distance | Shortest_distance_store |
+-----------+---------------+---------------------+-------------------+-------------------------+
| 70011134 | 38.7035 | 75 | 21.3124 | 115 |
| 70011186 | 38.4841 | 75 | 21.4144 | 115 |
| 70011207 | 39.1567 | 75 | 21.1826 | 115 |
| 100013232 | 22.976 | 145 | 9.5031 | 115 |
| 112075140 | 21.888 | 145 | 7.3705 | 115 |
+-----------+---------------+---------------------+-------------------+-------------------------+
Original dataset
+---------------+--------------------------+-----------------------+------------------+
| CORRECTED_ZIP | SourceOrganizationNumber | Travel Time (Minutes) | Distance (Miles) |
+---------------+--------------------------+-----------------------+------------------+
| 70011134 | 75 | 38.7035 | 26.8628 |
| 70011134 | 115 | 39.3969 | 21.3124 |
| 70011186 | 75 | 38.4841 | 26.7609 |
| 70011186 | 115 | 39.6389 | 21.4144 |
| 70011207 | 75 | 39.1567 | 31.2771 |
| 70011207 | 115 | 39.188 | 21.1826 |
| 100013232 | 115 | 28.6561 | 9.50311 |
| 100013232 | 145 | 22.976 | 10.0307 |
| 112075140 | 115 | 36.1803 | 7.37053 |
| 112075140 | 145 | 21.888 | 9.50123 |
+---------------+--------------------------+-----------------------+------------------+
Dataset after I've modified it with this query:
SELECT TOP 1000 [corrected_zip]
, TRY_CONVERT( DECIMAL(18, 4), ROUND([Travel Time (Minutes)], 4)) AS [Unit of Measurement]
, [SourceOrganizationNumber]
, 'Time' AS [Type]
FROM [db].[dbo].[my_table_A] [tt]
WHERE [tt].[CORRECTED_ZIP] IN('070011134', '070011186', '070011207', '112075140', '100013232')
AND [Travel Time (Minutes)] IN
(
SELECT MIN([Travel Time (Minutes)])
FROM [db].[dbo].[my_table_A]
WHERE [CORRECTED_ZIP] = [tt].[CORRECTED_ZIP]
GROUP BY [CORRECTED_ZIP]
)
UNION ALL
SELECT TOP 1000 [corrected_zip]
, TRY_CONVERT( DECIMAL(18, 4), ROUND([Distance (Miles)], 4))
, [SourceOrganizationNumber]
, 'Distance'
FROM [db].[dbo].[my_table_A] [tt]
WHERE [tt].[CORRECTED_ZIP] IN('070011134', '070011186', '070011207', '112075140', '100013232')
AND [Distance (Miles)] IN
(
SELECT MIN([Distance (Miles)])
FROM [db].[dbo].[my_table_A]
WHERE [CORRECTED_ZIP] = [tt].[CORRECTED_ZIP]
GROUP BY [CORRECTED_ZIP]
)
ORDER BY [CORRECTED_ZIP];
+---------------+---------------------+--------------------------+----------+
| corrected_zip | Unit of Measurement | SourceOrganizationNumber | Type |
+---------------+---------------------+--------------------------+----------+
| 70011134 | 38.7035 | 75 | Time |
| 70011134 | 21.3124 | 115 | Distance |
| 70011186 | 21.4144 | 115 | Distance |
| 70011186 | 38.4841 | 75 | Time |
| 70011207 | 39.1567 | 75 | Time |
| 70011207 | 21.1826 | 115 | Distance |
| 100013232 | 9.5031 | 115 | Distance |
| 100013232 | 22.976 | 145 | Time |
| 112075140 | 21.888 | 145 | Time |
| 112075140 | 7.3705 | 115 | Distance |
+---------------+---------------------+--------------------------+----------+
Data after I attempted to pivot it
+---------------+--------------------------+----------+---------+
| corrected_zip | SourceOrganizationNumber | Distance | Time |
+---------------+--------------------------+----------+---------+
| 070011134 | 115 | 21.3124 | NULL |
| 070011134 | 75 | NULL | 38.7035 |
| 070011186 | 115 | 21.4144 | NULL |
| 070011186 | 75 | NULL | 38.4841 |
| 070011207 | 115 | 21.1826 | NULL |
| 070011207 | 75 | NULL | 39.1567 |
| 100013232 | 115 | 9.5031 | NULL |
| 100013232 | 145 | NULL | 22.9760 |
| 112075140 | 115 | 7.3705 | NULL |
| 112075140 | 145 | NULL | 21.8880 |
+---------------+--------------------------+----------+---------+
It seems like my issue is picking the correct store ID as opposed to grouping by store ID?
You can use row_number() twice in a subquery(once to rank by time, another by distance), and then do conditional aggregation in the outer query:
select
corrected_zip,
min(travel_time) shortest_time,
min(case when rnt = 1 then source_organization_number end) shortest_time_store,
min(distance) shortest_distance,
min(case when rnd = 1 then source_organization_number end) shortest_distance_store
from (
select
t.*,
row_number() over(partition by corrected_zip order by travel_time) rnt,
row_number() over(partition by corrected_zip order by distance) rnd
from mytable t
) t
group by corrected_zip

Find the newest entry of a crosstable per record?

I have three tables:
My products with their IDs and their features.
is a table with treatments of my products with a treatment-ID, a method, and a date. The treatments are done in batches of many products so there is a crosstable
with the products IDs and the treatment IDs and a bool value for the success of the treatment.
Each product can undergo many different treatments so there is a many-to-many relation. I now want to add to the product table (1.) for every product a value that shows the method of its most recent successful treatment if there is any.
I made a query that groups the crosstable's entries by product-ID but I don't know how to show the method and date of it's last treatment.
table 1:
| productID | size | weight | height | ... |
|-----------|:----:|-------:|--------|-----|
| 1 | 13 | 16 | 9 | ... |
| 2 | 12 | 17 | 12 | ... |
| 3 | 11 | 15 | 15 | ... |
| ... | ... | ... | ... | ... |
table 2:
| treatmentID | method | date |
|-------------|:--------:|-----------:|
| 1 | dye blue | 01.02.2016 |
| 2 | dye red | 01.02.2017 |
| 3 | dye blue | 01.02.2018 |
| ... | ... | ... |
table 3:
| productID | treatmentID | success |
|-----------|:-----------:|--------:|
| 1 | 1 | yes |
| 1 | 2 | yes |
| 1 | 3 | no |
| ... | ... | ... |
I need table 1 to be like:
table 1:
| productID | size | weight | height | latest succesful method |
|-----------|:----:|-------:|--------|-------------------------|
| 1 | 13 | 16 | 9 | dye red |
| 2 | 12 | 17 | 12 | ... |
| 3 | 11 | 15 | 15 | ... |
| ... | ... | ... | ... | ... |
My query:
SELECT table3.productID, table2.method
FROM table2 INNER JOIN table3 ON table2.treatmentID = table3.treatmentID
GROUP BY table3.productID, table2.method
HAVING (((table3.productID)=Max([table2].[date])))
ORDER BY table3.productID DESC;
but this does NOT show only one (the most recent) entry but all of them.
Simplest solution here would be to write either a subquery within your sql, or create a new query to act as a subquery(it will look like a table) to help indicate(or elminate) the records you want to see.
Using similar but potentially slightly different source data as you only gave one example.
Table1
| ProductID | Size | Weight | Height |
|-----------|------|--------|--------|
| 1 | 13 | 16 | 9 |
| 2 | 12 | 17 | 12 |
| 3 | 11 | 15 | 15 |
Table2
| TreatmentID | Method | Date |
|-------------|------------|----------|
| 1 | dye blue | 1/2/2016 |
| 2 | dye red | 1/2/2017 |
| 3 | dye blue | 1/2/2018 |
| 4 | dye yellow | 1/4/2017 |
| 5 | dye brown | 1/5/2018 |
Table3
| ProductID | TreatmentID | Success |
|-----------|-------------|---------|
| 1 | 1 | yes |
| 1 | 2 | yes |
| 1 | 3 | no |
| 2 | 4 | no |
| 2 | 5 | yes |
First order of business is to get the max(dates) and productIds of successful treatments.
We'll do this by aggregating the date along with the productIDs and "success".
SELECT Table3.productid, Max(Table2.Date) AS MaxOfdate, Table3.success
FROM Table2 INNER JOIN Table3 ON Table2.treatmentid = Table3.treatmentid
GROUP BY Table3.productid, Table3.success;
This should give us something along the lines of:
| ProductID | MaxofDate | Success |
|-----------|-----------|---------|
| 1 | 1/2/2018 | No |
| 1 | 1/2/2017 | Yes |
| 2 | 1/4/2017 | No |
| 2 | 1/8/2017 | Yes |
We'll save this query as a "regular" query. I named mine "max", you should probably use something more descriptive. You'll see "max" in this next query.
Next we'll join tables1-3 together but in addition we will also use this "max" subquery to link tables 1 and 2 by the productID and MaxOfDate to TreatmentDate where success = "yes" to find the details of the most recent SUCCESSFUL treatment.
SELECT table1.productid, table1.size, table1.weight, table1.height, Table2.method
FROM ((table1 INNER JOIN [max] ON table1.productid = max.productid)
INNER JOIN Table2 ON max.MaxOfdate = Table2.date) INNER JOIN Table3 ON
(Table2.treatmentid = Table3.treatmentid) AND (table1.productid = Table3.productid)
WHERE (((max.success)="yes"));
The design will look something like this:
Design
(ps. you can add queries to your design query editor by clicking on the "Queries" tab when you are adding tables to your query design. They act just like tables, just be careful as very detailed queries tend to bog down Access)
Running this query should give us our final results.
| ProductID | Size | Weight | Height | Method |
|-----------|------|--------|--------|-----------|
| 1 | 13 | 16 | 9 | dye red |
| 2 | 12 | 17 | 12 | dye brown |

get all rows where column value is same in cassandra cql

This is my table.
cqlsh:sachhya> select * FROM emp;
emp_id | age | emp_name | exp | mobile
--------+-----+--------------+-----+------------
5 | 29 | RAHUL SHARMA | 9 | 2312343123
1 | 24 | SACHHYA | 15 | 9090987876
2 | 14 | SACHHYA | 15 | 9090987876
4 | 22 | ANKUR | 32 | 3213456321
90 | 30 | sumeet | 2 | 91234212
3 | 14 | SACHHYA | 3 | 9090987876
PRIMARY KEY (Partition key) IS emp_id.
I want to display all rows where emp_name is 'SACHHYA'. What command should i use?
Below is the cql query that i am using.
select * FROM emp WHERE emp_name='SACHHYA';
But i am getting an error:
InvalidRequest: Error from server: code=2200 [Invalid query]
message="Predicates on non-primary-key columns (emp_name) are not yet
supported for non secondary index queries"
I have found one solution for my question, We can crate index on 'emp_name' column after that we can use 'emp_name' filter.
EX:
CREATE INDEX NameIndx ON emp (emp_name);
SELECT * from sachhya.emp WHERE emp_name = 'SACHHYA';
My output:
emp_id | age | desegnation | emp_name | exp | mobile
--------+-----+------------------+----------+-----+------------
711 | 22 | Trainee Engineer | SACHHYA | 1 | 9232189345
2 | 24 | Engineer | SACHHYA | 3 | 9033864540
My Table:
emp_id | age | desegnation | emp_name | exp | mobile
--------+-----+------------------+----------+------+------------
5 | 29 | Technical Lead | RAHUL | 9 | 2312343123
10 | 45 | Deleviry Manager | ANDREW | 22 | 9214569345
711 | 22 | Trainee Engineer | SACHHYA | 1 | 9232189345
2 | 24 | Engineer | SACHHYA | 3 | 9033864540
4 | 26 | Engineer | ANKUR | 3 | 3213456321
22 | 20 | Intern | SAM | null | 8858699345
7 | 22 | Trainee Engineer | JACOB | 1 | 9232189345
17 | 28 | Senior Engineer | JACK | 4 | 8890341799
90 | 30 | Senior Engineer | HERCULES | 6 | 9353405163
3 | 32 | Technical Lead | ROSS | 8 | 7876561355

Unpivoting multiple, repeated columns with multiple (denormalized) values

I have a table with multiple columns like this...
+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+
| Name | Email | Address | Order1 | Shipping1 | Date1 | Order2 | Shipping2 | Date2 | Order3 | Shipping3 | Date3 |
+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+
| John | john#abcd.com | 123 | Rock | 123 | 02/11/2017 | Computer | 123 | 07/11/2017 | Pen | 123 | 12/11/2017 |
| Jane | jane#abcd.com | 234 | Scissor | 234 | 03/11/2017 | Laptop | 234 | 08/11/2017 | Pencil | 234 | 13/11/2017 |
| Julie | julie#abcd.com | 345 | Paper | 345 | 04/11/2017 | Mouse | 345 | 09/11/2017 | Clips | 345 | 14/11/2017 |
| Jaden | jaden#abcd.com | 456 | Spock | 456 | 05/11/2017 | Screen | 456 | 10/11/2017 | Pins | 456 | 15/11/2017 |
| Jabba | jabba#abcd.com | 678 | Lizard | 678 | 06/11/2017 | Pen Drive | 678 | 11/11/2017 | Notepad | 678 | 16/11/2017 |
+-------+----------------+---------+---------+-----------+------------+-----------+-----------+------------+---------+-----------+------------+
And I want to unpivot the columns into rows like this in T-SQL...
+-------+----------------+---------+-----------+----------+------------+
| Name | Email | Address | Order | Shipping | Date |
+-------+----------------+---------+-----------+----------+------------+
| John | john#abcd.com | 123 | Rock | 123 | 02/11/2017 |
| John | john#abcd.com | 123 | Computer | 123 | 07/11/2017 |
| John | john#abcd.com | 123 | Pen | 123 | 12/11/2017 |
| Jane | jane#abcd.com | 234 | Scissor | 234 | 03/11/2017 |
| Jane | jane#abcd.com | 234 | Laptop | 234 | 08/11/2017 |
| Jane | jane#abcd.com | 234 | Pencil | 234 | 13/11/2017 |
| Julie | julie#abcd.com | 345 | Paper | 345 | 04/11/2017 |
| Julie | julie#abcd.com | 345 | Mouse | 345 | 09/11/2017 |
| Julie | julie#abcd.com | 345 | Clips | 345 | 14/11/2017 |
| Jaden | jaden#abcd.com | 456 | Spock | 456 | 05/11/2017 |
| Jaden | jaden#abcd.com | 456 | Screen | 456 | 10/11/2017 |
| Jaden | jaden#abcd.com | 456 | Pins | 456 | 15/11/2017 |
| Jabba | jabba#abcd.com | 678 | Lizard | 678 | 06/11/2017 |
| Jabba | jabba#abcd.com | 678 | Pen Drive | 678 | 11/11/2017 |
| Jabba | jabba#abcd.com | 678 | Notepad | 678 | 16/11/2017 |
+-------+----------------+---------+-----------+----------+------------+
I googled and checked other posts related to this but unable to get three values. :(
Appreciate the help!
You won't need to necessarily use UNPIVOT here at all.
You'll be able to UNION the three denormalized Orders back into a flattened output, like so:
SELECT name, Email, Address, Order1 AS Order, Shipping1 as Shipping, Date1 AS Date
FROM Table1
UNION ALL
SELECT name, Email, Address, Order2, Shipping2, Date2
FROM Table1
UNION ALL
SELECT name, Email, Address, Order3, Shipping3, Date3
FROM Table1
ORDER BY Name, Date;
SqlFiddle here
(The column names are set by the first select in the UNION, and the ORDER is applied to the final UNIONed data)

Magento database: Invoice items database table?

Does anyone know where the Invoice data is stored in Magento database?
For example, I've found that the order data is stored in sales_order, sales_flat_order, sales_flat_order_item.
I've also found out that the main invoice data is stored in sales_order_entity, sales_order_entity_decimal and sales_order_entity_int. Through that I can change the subtotal and totals of the invoice in the system.
But! I don't know where to find the items data? For orders, that data is in sales_flat_order_item, but my sales_flat_invoice_item table is empty?!
http://img809.imageshack.us/img809/1921/invoicey.jpg
I will tell you what I know for 1.4.0.1 which is the version i currently develop for, it may or may not be the same for whatever version you are using.
Also, why are you in the database anyways? Magento has made models for you to use so that you don't have to work in the database. Regardless I will describe how I find whatever attribute I'm looking for ...
For starters I'm assuming that your already logged into the database via a mysql client, run
SELECT `entity_type_id`,`entity_type_code`,`entity_table` FROM `eav_entity_type`
which will get you something like ...
+----------------+----------------------+----------------------------------+
| entity_type_id | entity_type_code | entity_table |
+----------------+----------------------+----------------------------------+
| 1 | customer | customer/entity |
| 2 | customer_address | customer/address_entity |
| 3 | catalog_category | catalog/category |
| 4 | catalog_product | catalog/product |
| 5 | quote | sales/quote |
| 6 | quote_item | sales/quote_item |
| 7 | quote_address | sales/quote_address |
| 8 | quote_address_item | sales/quote_entity |
| 9 | quote_address_rate | sales/quote_entity |
| 10 | quote_payment | sales/quote_entity |
| 11 | order | sales/order |
| 12 | order_address | sales/order_entity |
| 13 | order_item | sales/order_entity |
| 14 | order_payment | sales/order_entity |
| 15 | order_status_history | sales/order_entity |
| 16 | invoice | sales/order_entity |
| 17 | invoice_item | sales/order_entity |
| 18 | invoice_comment | sales/order_entity |
| 19 | shipment | sales/order_entity |
| 20 | shipment_item | sales/order_entity |
| 21 | shipment_comment | sales/order_entity |
| 22 | shipment_track | sales/order_entity |
| 23 | creditmemo | sales/order_entity |
| 24 | creditmemo_item | sales/order_entity |
| 25 | creditmemo_comment | sales/order_entity |
+----------------+----------------------+----------------------------------+
We want to know more about the "invoice_item" entity so lets see what attributes it has ... run
SELECT `attribute_id`,`entity_type_id`,`attribute_code`,`backend_type` FROM `eav_attribute` WHERE `entity_type_id`=17;
and you'll get something like ...
+--------------+----------------+----------------------------------+--------------+
| attribute_id | entity_type_id | attribute_code | backend_type |
+--------------+----------------+----------------------------------+--------------+
| 349 | 17 | additional_data | text |
| 340 | 17 | base_cost | decimal |
| 346 | 17 | base_discount_amount | decimal |
| 345 | 17 | base_price | decimal |
| 679 | 17 | base_price_incl_tax | decimal |
| 348 | 17 | base_row_total | decimal |
| 681 | 17 | base_row_total_incl_tax | decimal |
| 347 | 17 | base_tax_amount | decimal |
| 567 | 17 | base_weee_tax_applied_amount | decimal |
| 568 | 17 | base_weee_tax_applied_row_amount | decimal |
| 579 | 17 | base_weee_tax_disposition | decimal |
| 580 | 17 | base_weee_tax_row_disposition | decimal |
| 337 | 17 | description | text |
| 342 | 17 | discount_amount | decimal |
| 336 | 17 | name | varchar |
| 334 | 17 | order_item_id | int |
| 333 | 17 | parent_id | static |
| 341 | 17 | price | decimal |
| 678 | 17 | price_incl_tax | decimal |
| 335 | 17 | product_id | int |
| 339 | 17 | qty | decimal |
| 344 | 17 | row_total | decimal |
| 680 | 17 | row_total_incl_tax | decimal |
| 338 | 17 | sku | varchar |
| 343 | 17 | tax_amount | decimal |
| 571 | 17 | weee_tax_applied | text |
| 569 | 17 | weee_tax_applied_amount | decimal |
| 570 | 17 | weee_tax_applied_row_amount | decimal |
| 577 | 17 | weee_tax_disposition | decimal |
| 578 | 17 | weee_tax_row_disposition | decimal |
+--------------+----------------+----------------------------------+--------------+
the last column (backend_type) combined with the table for the entity (entity_table) is where the attribute for that entity will be so attribute "additional_data" should be in sales_order_entity_text with an attribute_id of 349.
Armed with this information now we just need to find an invoice, I'll use an example from a test install of mine. Lets look for the "base_price" of an invoice item.
First lets find all the items that are associated to the invoice (in my case invoice entity_id of 1303954)
SELECT * FROM `sales_order_entity` WHERE `entity_type_id`=17 AND `parent_id`=1303954;
which gives 2 items
+-----------+----------------+------------------+--------------+-----------+----------+---------------------+---------------------+-----------+
| entity_id | entity_type_id | attribute_set_id | increment_id | parent_id | store_id | created_at | updated_at | is_active |
+-----------+----------------+------------------+--------------+-----------+----------+---------------------+---------------------+-----------+
| 1303955 | 17 | 0 | | 1303954 | NULL | 2011-06-01 14:10:48 | 2011-06-01 14:10:48 | 1 |
| 1303956 | 17 | 0 | | 1303954 | NULL | 2011-06-01 14:10:48 | 2011-06-01 14:10:48 | 1 |
+-----------+----------------+------------------+--------------+-----------+----------+---------------------+---------------------+-----------+
Lets choose the first one and find the 'base_price'
SELECT * FROM `sales_order_entity_decimal` WHERE `attribute_id`=345 AND `entity_id`=1303955;
Which gives us ....
+----------+----------------+--------------+-----------+---------+
| value_id | entity_type_id | attribute_id | entity_id | value |
+----------+----------------+--------------+-----------+---------+
| 7361390 | 17 | 345 | 1303955 | 31.2500 |
+----------+----------------+--------------+-----------+---------+
Which of course its just a simple update to change it.
Again if you can do it via a Magento model I would highly suggest you do it that way, but if manual is the only way to go then well I hope this helped :)

Resources