Need ideas - simple SQL
Essentially, here's my script--Get the storeid, prodid, sales, and summation of sales per product ID accross all stores
SELECT storeid, prodid, sales,
SUM(sales) OVER (PARTITION BY prodid ORDER BY sales DESC)
FROM salestbl;
storeid prodid sales Group Sum(sales)
----------- ------ ----------- ----------------
1001 A 100000.00 170000.00
1002 A 40000.00 170000.00
1003 A 30000.00 170000.00
1003 B 65000.00 65000.00
1001 C 60000.00 115000.00
1002 C 35000.00 115000.00
1003 C 20000.00 115000.00
1003 D 50000.00 110000.00
1001 D 35000.00 110000.00
1002 D 25000.00 110000.00
1001 F 150000.00 150000.00
With the code above, what I wanted to do is to get the storeid field, prodid (product which hasa sale minimum in stores) and the total sales of product by prodidNow, I came up with this to do.
select tblA.storeid, tblA.prodid, tblA.sales, tblA.sales_sum_per_prod
from
(SELECT storeid, prodid, sales,
SUM(sales) OVER (PARTITION BY prodid) as sales_sum_per_prod
FROM salestbl) as tblA,
(SELECT prodid, min(sales) as min_sales
FROM salestbl
group by prodid) as tblB
where
tblA.prodid=tblB.prodid
and tblA.sales=tblB.min_sales;
storeid prodid sales sales_sum_per_prod
----------- ------ ----------- ------------------
1003 A 30000.00 170000.00
1003 B 65000.00 65000.00
1001 F 150000.00 150000.00
1002 D 25000.00 110000.00
1003 C 20000.00 115000.00
My question would be, if you have the same scenario:How would you write the SQL?
Is there an approach much more in terms of simplicity or performance?
I want to try your solution and ideas so that I can test here and finally compare the plan of the explain command.
I'm looking forward to an another or even better approach to solve my problem.
Thanks in advance ^_^
Hello
Here's one way:
WITH got_analytics AS
(
SELECT storeid, prodid, sales
, SUM (sales) OVER ( PARTITION BY prodid
) AS group_sum_sales
, ROW_NUMBER () OVER ( PARTITION BY prodid
ORDER BY sales
) AS r_num
FROM salestbl
)
SELECT storeid, prodid, sales, group_sum_sales
FROM got_analytics
WHERE r_num = 1
;
If you would care to post CREATE TABLE and INSERT instructions for the sample data, and then I could test it.
Published by: Frank Kulash, 28 February 2012 06:49
Changed "SELECT SELECT" "Select".
Tags: Database
Similar Questions
-
Is there a way to get a 'default' DataTemplate based on a simple sql query?
Hi all
I wonder if there is a way to produce a default value of the DataTemplate (or at least the dataStructure of the model portion).
I like to call a trigger beforeReport to fill in the data for the report as such, I have to use dataTemplate instead of a simple SQL query, "select * from table_name". I have more than 50 columns on the report so I was hoping that I could not manually built a dataTemplate (or at less the part of dataStructure) for each report just be able to call a trigger.
Thanks in advance for your ideas.
Kind regards
YahyaYou need not define the cases you need the xml structure to be automatically decided by BEEP.
This datatemplate allows you to get the following structure
.. .. .. .. .. .. .. .. -
How to make this simple sql run faster?
Here is a very simple sql, but it takes too long to finish as an hour or more, and the problem is in the table. It's a huge table with 17660025 lines.
In addition, upd_dt is indexed.
Execution plan seems to be no problem: (is 9i, and I do not think that it would not perform better if the UNIQUE HASH is used instead of SORT UNIQUE)select distinct (dstn_type) from ims_ets_prty_msge where ims_ets_prty_msge.upd_dt > sysdate - 30;
Here is some info on the table----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 6 | 78 | 1409 | | 1 | SORT UNIQUE | | 6 | 78 | 1409 | | 2 | TABLE ACCESS BY INDEX ROWID| IMS_ETS_PRTY_MSGE | 856K| 10M| 10 | |* 3 | INDEX RANGE SCAN | IMS_ETS_PRTY_MSGE_INDX5 | 154K| | 2 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("IMS_ETS_PRTY_MSGE"."UPD_DT">SYSDATE@!-30) Note: cpu costing is off
The thing is that we must make it run faster.NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE CHAIN_CNT AVG_ROW_LEN 17129540 455259 622 502 455278 188
And all I can think is to create a bitmap on dstn_type (low cardinality) index and partition the table using upd_dt;
So my question would be is anyway that we can get a better response time without having to make that kind of change?
Or is this kind of change really help?
Any thoughts would be appreciated!
Thank youHave you thought about putting an index on (upd_dt, dstn_type)?
With the query you posted, and this index, it should be possible to use a full scan of the index, no visit to the table at all.
A bitmap index would not be a good idea, if it is an OLTP application / table.
-
the ed-meister has provided an excellent simple mode for the installation of version 3.0 of firefox and I did it correctly, but then when I looked at the edits, he said that I need to upgrade to firefox 5.0 and I need the simple mode for linux and I don't know what to type in the terminal/console
Thanks for your help if necessary
Check with the EeeUser forum to see what they have to say.
http://Forum.EeeUser.com/I don't see Installing a browser 2011 in a 2007 operating system. I replaced Xandros with Easy Peasy 1.6 some time ago, 3.0 and 3.5 were too darn slow on Xandros even with the GTK update. A 900MHx processor will never be a speed demon, but you I have to give him a chance with a better than Xandros OS.
Personally, I stay with 3.6.x until the end of the support. I don't see the need to upgrade to the latest version of Firefox now, especially on Linux.
http://www.geteasypeasy.com/ -
I need a simple solution to the cursor jumping around.
I need ' a simple fixto the cursor jumping around. I tried a system restore, but it does not work. I'm not a programmer, so it must be simple, please help. I'm about to throw this Windows OS Dell computer out the window. Laughing out loud
Hello
1 does happen on all applications?
2. you are using a desktop computer or a laptop?Method 1.
If you use a laptop try disable the touchpad and check.Method 2.
Step A.
Check to see if the problem exists in Safe Mode, if the computer works as expected in mode without failure, then we can solve the problem in the clean boot state.See the article below for the procedure safe mode in Windows XP
A description of the options to start in Windows XP Mode
http://support.Microsoft.com/kb/315222Step B.
You need to perform a clean boot to find the program that is causing and then disable or remove.How to configure Windows XP to start in a "clean boot" State
http://support.Microsoft.com/kb/310353/en-usNote: When you are finished troubleshooting, follow the steps as explained in the article to reset the computer to start as usual.
I hope this helps.
-
I use Windows Vista and that you need to download SQL Server 2005 Express Service Pack 4. I get an error code 64 C help, please.
Hello
I suggest you to try the steps below and check if it helps.
Method 1: Try to reset components to update Windows from the following link, which would help you to install the updates of windows. http://support.Microsoft.com/kb/971058
Method 2: If the problem persists, then try to set up your computer in a clean boot state, and check to see if the same problem happens.
By setting your boot system minimum state helps determine if third-party applications or startup items are causing the problem.
How to troubleshoot a problem by performing a clean boot in Windows Vista or Windows 7:
http://support.Microsoft.com/kb/929135Note: After the boot minimum troubleshooting step, follow step 7 in the link provided to return the computer to a Normal startup mode.
Method 3: Also try to install the updates manually from the link below.
http://www.Microsoft.com/download/en/details.aspx?displaylang=en&ID=184
You can also consult the following link for more information:
Troubleshoot problems with installing updates.
http://Windows.Microsoft.com/en-us/Windows-Vista/troubleshoot-problems-with-installing-updates
Hope this information is useful.
-
need the simple way to set the computer back to factory settings, and already saved safe data
need the simple way to set the computer back to factory settings, and already saved safe data
Vista recovery media obtain and/or use the Partition Recovery Vista on your computer to the factory settings .
There is no Vista free download legal available.
Contact your computer manufacturer and ask them to send a recovery disk/s Vista set.
Normally, they do this for a cost of $ small.
In addition, ask them if you have a recovery Partition on your computer/laptop to restore it to factory settings.
See if a manual provided with the computer or go to the manufacturer's website, email or you can call for information on how to make a recovery.
Normally, you have to press F10 or F11 at startup to start the recovery process...
Another way I've seen on some models is press F8 and go to a list of startup options, and launch a recovery of standards of plant with it, by selecting the repair option.
Also ask them if it is possible to do the recovery disk/s for the recovery Partition in case of a system Crash or hard drive failure.
They will tell you how to do this.
Every computer manufacturer has their own way of making recovery disk/s.
Or borrow a good Microsoft Vista DVD (not Dell, HP, etc).
A good Vista DVD contains all versions of Vista.
The product key determines which version of Vista is installed.There are 2 disks of Vista: one for 32-bit operating system, and one for 64-bit operating system.
If install a cleaning is required with a good DVD of Vista (not HP, Dell recovery disks):
Go to your Bios/Setup, or the Boot Menu at startup and change the Boot order to make the DVD/CD drive 1st in the boot order, then reboot with the disk in the drive.
At the startup/power on you should see at the bottom of the screen either F2 or DELETE, go to Setup/Bios or F12 for the Boot Menu
http://support.Microsoft.com/default.aspx/KB/918884
MS advice on the conduct of clean install.
http://www.theeldergeekvista.com/vista_clean_installation.htm
A tutorial on the use of a clean install
http://www.winsupersite.com/showcase/winvista_install_03.asp
Super Guide Windows Vista Installation
After installation > go to the website of the manufacturer of your computer/notebook > drivers and downloads Section > key in your model number > get latest Vista drivers for it > download/install them.
Save all data, because it will be lost during a clean installation.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://support.Microsoft.com/default.aspx/KB/326246
'How to replace Microsoft software or hardware, order service packs and upgrades, and replace product manuals'
See you soon.
Mick Murphy - Microsoft partner
-
I need ideas for my application
I need ideas for my MultiApp app on the app world, we have feet to meters Converter, temperature converter, timer, stopwatch, alarm clock, and we need more ideas...
Thank you, but I also have a twin brother, so I guess he can help me
-
I'm trying to download a file from the active section of the creative cloud, but it is not letting me. It is saying I need a simple or full membership, which I do. I have the plan composition of the photographer. I made sure I'm connected, but it is still trying to put me on a plan, which I have. Help please!
Hashim, see the FAQ section on: CC help | Creative market Cloud
-
Output is not as expected (getting wrapped) for a simple sql script
Hello
I try to run a simple sql script with a few queries including a semi colon output (;) when text file separated that later - I can open in excel.
My SQL file contains in part the code - below
SELECT
"PORTFOLIO" | « ; » ||
"CONTRACT # | « ; » ||
"CONS_INV #" | « ; » ||
"CUSTOMER #" | « ; » ||
"CLIENT_NAME | « ; » ||
'MESSAGE ' | « ; » ||
"REQUEST_ID" | « ; » ||
"BILL_TO_COUNTRY" | « ; » ||
"BILL_TO_ADDRESS1" | « ; » ||
"BILL_TO_ADDRESS2" | « ; » ||
"BILL_TO_ADDRESS3" | « ; » ||
"BILL_TO_ADDRESS4" | « ; » ||
"BILL_TO_CITY" | « ; » ||
"BILL_TO_POSTAL_CODE" | « ; » ||
"BILL_TO_STATE" | « ; » ||
"BILL_TO_PROVINCE" | « ; » ||
"BILL_TO_COUNTY" | « ; » ||
"SHIP_TO_COUNTRY | « ; » ||
"SHIP_TO_ADDRESS1" | « ; » ||
"SHIP_TO_ADDRESS2" | « ; » ||
"SHIP_TO_ADDRESS3" | « ; » ||
"SHIP_TO_ADDRESS4" | « ; » ||
"SHIP_TO_CITY" | « ; » ||
"SHIP_TO_POSTAL_CODE" | « ; » ||
"SHIP_TO_STATE | « ; » ||
"SHIP_TO_PROVINCE" | « ; » ||
"SHIP_TO_COUNTY" | « ; » ||
"INVOICE_DATE | « ; » ||
"INVOICE_CREATION_DATE" | « ; » ||
"PARTIALLY_DROPPED" | « ; » ||
"AMOUNT".
OF the double
/
SELECT "DOUBLE
/
SELECT "error Code" | « ; » ||' Error message "OF the DOUBLE
/
SELECT lookup_code. « ; » || Description
OF fnd_lookup_values
WHERE lookup_type = 'CUST_ERROR. '
/
The issue I'm facing is that the output of the competitive program file comes out bit so enter each line only 132 characters in a line and therefore gets pushed in the next row in excel.
Output-
PORTFOLIO; CONTRACT #; CONS_INV #; CUSTOMER #; CLIENT_NAME; MESSAGE; REQUEST_ID; BILL_TO_COUNTRY; BILL_TO_ADDRESS1; BILL_TO_ADDRESS2; BILL_TO_A
DDRESS3; BILL_TO_ADDRESS4; BILL_TO_CITY; BILL_TO_POSTAL_CODE; BILL_TO_STATE; BILL_TO_PROVINCE; BILL_TO_COUNTY; SHIP_TO_COUNTRY; SHIP_TO_ADDR
ESS1; SHIP_TO_ADDRESS2; SHIP_TO_ADDRESS3; SHIP_TO_ADDRESS4; SHIP_TO_CITY; SHIP_TO_POSTAL_CODE; SHIP_TO_STATE; SHIP_TO_PROVINCE; SHIP_TO_COUN
TY; INVOICE_DATE; INVOICE_CREATION_DATE; PARTIALLY_DROPPED; AMOUNT
Please advice on all of the commands that I can use in the script sql in order to expand the output page or any other workaround solutions!
Please answer as soon as possible if you can help me!
Thank you!SQL * MORE?
SQL> set lin 1000 SQL> spool your_file_out.csv SQL> your_query SQL> spool off
Kind regards
Malakshinov Sayan -
Hi all
We need a simple query that needed to avoid the custom code in our engine of journalist.
Existing report:
Overall use of Volume
Tables involved:
ent_host_disk_usage: (Mo used aggregates and mapped to the aggregate volume are in the same table)
netapp_volumes_to_aggregates_current: mapping between the granulate and volume
devices of
This should show current and historical information based on the report quick selection .the existing report uses the
the current data to historical query also.
the solution that need us:
We need the model query to use the ent_host_disk_usage as the base table, so when the current option is selected it using the ent_host_disk_usage_current table
and when summaries are selected it using the table ent_host_disk_usage for the volume and the amount.
Who ever gives the best query will be greatly thanked
Refer to the table below for the example:
disk_usage table
------------------------
mapping tablefilesystemid usedmb spType ----------------------------------------------------------------- aggr 10000 aggregate /vol 100 FS c: 1000 FS aggr2 5000 aggregate d: 100 FS f: 200 FS e: 300 FS
------------------------
Report:aggregate name filesystemid -------------------------------------------------------------------------- aggr1 /vol aggr1 c: aggr2 d: aggr2 f: aggr2 e:
--------------------
Published by: user11101052 on April 25, 2009 10:01name used vol used --------------------------------------------------------------------------- aggr1 10000 /vol 100 aggr1 10000 c: 1000 aggr2 5000 d: 100 aggr2 5000 e: 300 aggr2 5000 f: 200
Hello
Welcome to the forum!
This site compress space. If you want to post something that is formatted as a table, type the 6 characters
{code}
(small letters only, inside curly braces) before and after the formatted text, to maintain spacing.It looks like you want a row of output for each line of the mapping. Both used columns (it would be less confusing to give them different names: I'll call them a_used and f_used) comes from the disk_usage table: this seems to call for a join. The problem is that, to get a_used to connect using aggregate_name and is f_used, that you need to include using filesystemid. It would be easy if they are in two different tables. You can imagine that they are in two different tables, by joining two copies of the same table, like this:
SELECT m.aggregate_name , a.usedmb AS a_used , m.filesystemid , f.usedmb AS f_used FROM mapping m JOIN disk_usage a ON m.aggregate_name = a.filesystemid JOIN disk_usage f ON m.filesystemid = f.filesystemid;
-
Simple sql statement I need help
Hello all, I need the syntax for the following sql query: "Using that a single update to write the query to update a salary of 10% for wages below 100 k and 15% for salaries above 100 k". TIAUPDATE table1 SET salary = salary BOX WHEN < 100000="" then="" salary="" *="" 1.1="" when="" salary=""> = 100000 THEN salary * 1.15 END;
-
Under Update taking 2 h complete 3000 sets.
UPDATE ARR_TRANSACTIONS ARR
SET = NUMBER99
(SELECT ARR_1.NUMBER99
OF ARR_TRANSACTIONS ARR_1, ARR_HEADER BH
WHERE ARR_1.ARR_ID = BH. ARR_ID
AND BH. FLAG = "Y".
AND ARR_1.LINE_ID = ARR. ORIG_INV_LINE_ID
AND ARR_1.NUMBER9 IS NOT NULL
AND ROWNUM = 1)
WHERE ARR BATCH_ID = 26219
AND ARR. NUMBER99 IS NULL
I added more some condition where to reduce cost and its working fine. But I need to rise more than performance on SQL.
Please help with SQL.
I do not see the number of 3000 lines in the plan - so I guess that the optimizer does not compute with the correct numbers. Could generate the plan with dbms_xplan.display_cursor? This could tell us something about filter predicates and access.
In the given situation, I would check:
- is the access to the index in step 1 (filtered by the relevant conditions: this could be BATCH_ID = 26219) return actually 248 entries (or something similar)?
- Access table in step 2 returning 204 lines (or something similar) after applying the filters given? If the number of lines is much bigger then a Nested Loops is perhaps not a good idea.
To make this much simpler check, you could create a plan with statistics of content (RowSource) given that the plan containing the real and the number of estimated rows.
If the estimated cardinalities are not plausible the next question would be if the statistics are strong and up to date.
-
Apex 4.2: really simple SQL date of issue
Hello
Anyone can get this SQL query with the correct date for Apex syntax?
Select * from table
where datecolumn > ' 2010-03-31'
I tried with #2010-03-31 #; I tried the dd-mm-yyyy; I tried the backslashes; I tried to use a date() function; and I tried cast() and I'm stumped.
As far as I remember, I put the global date format in dd-mm-yyyy.
The column that I need to analyze will finally be a timestamp with time zone column (for a plugin works).
However at this point, I can't do a simple working either date column, so it's just a problem of syntax newbie.
Help appreciated!
Thank you
EmmaHi Emma,
because you use a literal string, you must specify the format;select * from table where datecolumn >to_date( '2010-03-31', 'YYYY-MM-DD')
Kofi
-
So I am new to SQL and ORACLE. I need to know how to do the commands below in a single statement. I did it in two here, but I have no idea how do it in one. Any ideas?
ALTER TABLE books
ADD Catcode VARCHAR2 (3);
ALTER TABLE books
ADD CONSTRAINT books_catcode_fk FOREIGN KEY (catcode)
REFERENCES (catcode) category;
Any help appreciated ^^SQL> ed Wrote file afiedt.buf 1 alter table books 2 add( catcode varchar2(3), 3 constraint books_catcode_fk 4 foreign key( catcode ) 5* references category( catcode ) ) SQL> / Table altered.
Justin
Maybe you are looking for
-
HP 50 g graphic calculator coordinated
How to use the graphing calculator HP 50g to points such as (4.2) and (-6,0) by themselves?How to use the calculator HP 50 g to derive a formula for details 2 or more?
-
Internet, home network ONLY
My laptop connects to the router and leaves the internet very well. It will not connect, however, on other computers on our home network. I ran the wizard several times and it seems to work very well, but he never finds the other computers on our n
-
I need an admin password to connect to the system bios for my laptop, I tried everything I could when it breaks down 3 times it says disable System. 93257993 can help you please.
-
Get a stop code - 64320981, please help!
-
A compatible video card, I bought
I have a dell inspiron 660 windows 7 version its a intel i5 2320 8 GB ram and 1 TB of HDD IK this map requires 400w power supply but I did some research and it shud work power wise, but I was wondering if everything else seems good "the card is a" Sa