Posts

Showing posts with the label sql

How to count unique rows in Oracle

Image
Clash Royale CLAN TAG #URR8PPP How to count unique rows in Oracle I have an oracle database table with a lot of columns. I'd like to count the number of fully unique rows. The only thing I could find is: SELECT COUNT(DISTINCT col_name) FROM table; This however would require me listing all the columns and I haven't been able to come up with syntax that will do that for me. I'm guessing the reason for that is that this query would be very low performance? Is there a recommended way of doing this? Possible duplicate of SQL - how to count unique combination of columns – Organic Advocate Feb 15 at 22:40 5 Answers 5 How about SELECT COUNT(*) FROM (SELECT DISTINCT * FROM Table) Please format code to aid readability. This ...

Retrieve data from second sql table inside a modal using php

Image
Clash Royale CLAN TAG #URR8PPP Retrieve data from second sql table inside a modal using php So the question would be, how can I retrieve data for Locatie (drop-down list) inside the modal if the data is in a second table named locationstbl with the column named address in sql. There might be some way to use JOIN or something like that just to display the data I need in that dropdown list ? Thank you. Here's the code. <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0"> <thead> <tr> <th>#</th> <th>Locatie</th> <th>Serie Aparat</th> <th>Tip Joc</th> <th>Cabinet</th> <th>Data Expirare</th> <th>Actiuni</th> </tr> </thead> <tbody> <?php $sql = ...

SQL merge minimum time difference

Image
Clash Royale CLAN TAG #URR8PPP SQL merge minimum time difference I'm having trouble with a SQL statement that is a bit over my skill level. Running this in a DB2 datawarehouse. I need to join two columns (CODE1 and CODE2) from TABLE2 into TABLE1 based on some IDs and the minimum time difference between a date in TABLE1 (STARTDATE) and a date in TABLE2 (TIME_SENT). The statement below shows what I'm trying to do, but having issues with ordering of group by and having clause. group by having SELECT * FROM TABLE1 LEFT JOIN (SELECT B.ID1, B.ID2, D.CODE1, D.CODE2 FROM TABLE1 B, TABLE2 D WHERE D.STATUS = '7' GROUP BY B.ID1, B.ID2 HAVING ABS(B.STARTDATE - D.TIME_SENT) = MIN(ABS(B.STARTDATE - D.TIME_SENT)) TABLE2 ON TABLE1.ID1 = TABLE2.ID1 AND TABLE1.ID2 = TABLE2.ID2; Appreciate any help with this. STRUCTURE TABLE1: --------------------------------------------------------- | ID1 (VARCHAR) | ID2 (VARCHAR) | STARTDATE (TIMESTAMP) | --...

Sql Table data formation

Image
Clash Royale CLAN TAG #URR8PPP Sql Table data formation I have a below table ID Level StoreId 2604553 3 A 2604553 7 C 2604553 7 D 2604553 7 E 2678540 3 A 2678540 7 A 2678540 3 B 2678540 7 B 2678540 3 C I need out put like Below ID A B C D E 2604553 3 0 7 7 7 2678540 3 3 3 0 0 2678540 3 7 3 0 0 2678540 7 3 3 0 0 2678540 7 7 3 0 0 Please help me how to achieve this. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

What are good ways to prevent SQL injection? [duplicate]

Image
Clash Royale CLAN TAG #URR8PPP What are good ways to prevent SQL injection? [duplicate] This question already has an answer here: I have to program an application management system for my OJT company. The front end will be done in C# and the back end in SQL. Now I have never done a project of this scope before; in school we had only basic lessons about SQL. Somehow our teacher completely failed to discuss SQL injections, something which I have only now come in contact with by reading about it on the net. So anyway my question is: how do you prevent SQL injections in C#? I vaguely think that it can be done by properly masking the text fields of the application so that it only accepts input in a specified format. For example: an e-mail textbox should be of the format "example@examplecompany.tld". Would this approach be sufficient? Or does .NET have pre-defined methods that handle stuff like this? Can I apply a filter to a textbox so it only accepts email-address format or a nam...

Stored Procedure issue which states Procedure: [dbo].[GetInfo] contains an unresolved reference to an object

Image
Clash Royale CLAN TAG #URR8PPP Stored Procedure issue which states Procedure: [dbo].[GetInfo] contains an unresolved reference to an object Guys I really need your help. So, the problem is in Visual Studio I am trying to create stored procedue with inner join of two tables: КатегорииЗатрат (in English means Category) and Расходы (in English means Expenses) in order to make sure that I could show name of Category instead of its id. However, I encountered with error as Procedure: [dbo].[GetInfo] contains an unresolved reference to an object. Either the object does not exist or the reference is ambiguous because it could refer to any of the following objects: [dbo].[КатегорииЗатрат].[c]::[КатегорииЗатратId] or [dbo].[Расходы].[c]::[КатегорииЗатратId] . The code that I am using: Procedure: [dbo].[GetInfo] contains an unresolved reference to an object. Either the object does not exist or the reference is ambiguous because it could refer to any of the following objects: [dbo].[КатегорииЗатра...

INSERT SQL works only with hard coded values

Image
Clash Royale CLAN TAG #URR8PPP INSERT SQL works only with hard coded values Cannot insert a variable using INSERT in MS ACCESS. the insert works great when I insert a hard-coded value such as dbs.Execute "INSERT INTO Flights " _ & "(FlightID) VALUES " _ & "('2');" But fails with a variable Private Sub MainSaveBtn_Click() Dim dbs As Database Set dbs = CurrentDb Dim id As Integer id = 20 Debug.Print id dbs.Execute "INSERT INTO Flights " _ & "(FlightID) VALUES " _ & "('id');" dbs.Close End Sub *Flights table has only one Integer colum named FlightID What am i missing here? You are trying to insert the text 'id' into an integer column. – pritaeas 18 mins ago ...

SQL Query for find the decade with the largest number of records

Image
Clash Royale CLAN TAG #URR8PPP SQL Query for find the decade with the largest number of records I have a database including the following tables: ACTOR (id, fname, lname, gender) MOVIE (id, name, year, rank) DIRECTOR (id, fname, lname) CAST (a_id, m_id, role) MOVIE_DIRECTOR (d_id, m_id) Now I want to retrieve the data of the following question. Ques: "A decade is a sequence of 10 consecutive years. For example, 1965, 1966, ..., 1974 is a decade, and so is 1967, 1968, ..., 1976. Find the decade with the largest number of films" Homework? What have you tried? – JimmyB 2 hours ago Hint: Every movie can be seen to be the beginning of a decade. Then, the decade are all years between this movie's year and the movie's year+10. – JimmyB 2 hours ago ...

Mysql Query to count common number in a column

Image
Clash Royale CLAN TAG #URR8PPP Mysql Query to count common number in a column I have a main table as first image and I need to produce output as second table. Can any one please help me to do mysql query. Main Table Output 1 Answer 1 You can do : select anumber, sum(tsp = 'aplace') as aplace, sum(tsp = 'bplace') as bplace, sum(tsp = 'cplace') as cplace from table t group by anumber; By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Can I use a PL/SQL trigger to check category and concat to a incremental number based on category?

Image
Clash Royale CLAN TAG #URR8PPP Can I use a PL/SQL trigger to check category and concat to a incremental number based on category? I got a productID - P0001KTC and P0001DR. productID If product category is kitchen, I will assign a productID - PROD001KTC , else if the category is dining room, then the productID should be PROD001DR . productID - PROD001KTC productID PROD001DR Is it possible to write a sequence inside a trigger to check the product category and assign an id as mentioned above? if there is another living room category product inserted then the id will be PROD001LR. give at least one example on table. – Sanjay Radadiya Jul 31 '16 at 5:25 Which version of Oracle are you on? – trincot Jul 31 '16 at 5:42 ...

Serialize field in SQL condition and compare greater than

Image
Clash Royale CLAN TAG #URR8PPP Serialize field in SQL condition and compare greater than I have two dates in serialized field in my DB . That filed looks like a:2:{i:0;s:8:"20180801";i:1;s:8:"20180808";} a:2:{i:0;s:8:"20180801";i:1;s:8:"20180808";} My important variable looks like $data = $_GET['DATA']; $data = $_GET['DATA']; Now I want get all post where $data is between dates in serialize field. For example: If $data = 20180805 my query should find post with that field $data = 20180805 a:2:{i:0;s:8:"20180801";i:1;s:8:"20180808";} a:2:{i:0;s:8:"20180801";i:1;s:8:"20180808";} My query looks like: $test2 = $wpdb->get_results(" SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_value >= '$data' AND meta_value <= '$data' ...

SQL Grouping of Salaries

Image
Clash Royale CLAN TAG #URR8PPP SQL Grouping of Salaries I have a question to ask and I am supposed to create a query which shows: MIN(lastname) MAX(firstname) SUM(salary) AVG(salary) --------------------------------------------------------------- DAVIES TRINA 17500 3500 This was the query/queries I created: SQL> SELECT MIN(LASTNAME), MAX(FIRSTNAME), SUM(SALARY), AVG(SALARY) 2 FROM EMPLOYEES 3 GROUP BY JOB_ID; SQL> SELECT MIN(LASTNAME), MAX(FIRSTNAME), SUM(SALARY), AVG(SALARY) 2 FROM EMPLOYEES 3 GROUP BY JOB_ID, MANAGER_ID; But I get multiple rows shown to me and in the part where DAVIES TRINA was shown the SUM and AVG salary is different and I am unsure how they were grouped. MIN(LASTNAME) MAX(FIRSTNAME) SUM(SALARY) AVG(SALARY) ---------- ---------- ----------- ----------- ERNST DIANA 10200 5100 HIGGINS SHELLEY 12000 12000 GIETZ WILLIAM 8300 ...

Converting MYSQL to WordPress $WPDB

Image
Clash Royale CLAN TAG #URR8PPP Converting MYSQL to WordPress $WPDB I’m upgrading an archaic Wordpress site from 3.7. It has some SQL query requests that don’t function after 3.9+ where you now have to connect to the DB via $WPDB. I've taken an initial stab at it but am hung up on the mysql_fetch_array part and also unclear if what I've already done is even correct. Seems like the mysql_fetch_array needs to use get_results. I'm fully aware that this is a less than ideal way to approach this and fully intend to modernize everything but in keeping a long story short it would be very helpful to be able to sort this out within this old jumbled paradigm if at all possible. Attached is the original code and then my attempt (also added a v2 attempt) . Thanks for taking a look. SQL: $sql = "SELECT wp_acf_values.value FROM wp_acf_values, wp_acf_fields WHERE wp_acf_fields.post_id = '620' AND wp_acf_fields.id = wp_acf_values.field_id ORDER BY wp_acf_fields.order_no"; ...

Find the length of the longest row in a column in oracle

Image
Clash Royale CLAN TAG #URR8PPP Find the length of the longest row in a column in oracle Does anybody know if there is a way to find what the length of the longest row in a column in Oracle? Basically I need to get the length of the longest row and then use that length plus 1 with SUBSTR to make the output of the column one character longer than the longest string. SUBSTR Thanks EDIT: Thanks for the advice. However, the MAX(LENGTH(column_name)) AS MAXLENGTH approach gives me the number I want but when I try to use it with SUBSTR(column_name,1, MAXLENGTH) I get an invalid identifier error. MAX(LENGTH(column_name)) AS MAXLENGTH SUBSTR(column_name,1, MAXLENGTH) SO I made a function to return the numberI wanted then used: SUBSTR(column_name,1,maxlengthfunc) This gave me the following output: SUBSTR(NAME,1,MAXLENGTHFUNC) Rather than: SUBSTR(NAME, 1, 19) And it didn't shrink the output column size like I needed. Also RTRIM(name)||' ' didn't do anything for me in SQL develop...