I have an SQL table named “DATA” as follows: I’m attempting to return the following 3 columns: plan, planCount, totalCostPerPlan I’ve been working a query to return this and so far it looks like this: It sort of works, however, I get the data only partially grouped as follows: (The tab…
Tag: sql
SQL Server CASE expression that does not result in waterfall
If I have a case expression is there a way it does not work as a waterfall? For example if I have the following dataset: Is there a way I can show the following results To show Right now it shows as Answer You can use a left join instead:
SQL query returns 0 for COUNT(*) if no rows
I just got started with SQL and joined this forum recently, thus I’m still lacking the knowledge. Please do be considerate if I’ve made any mistakes. I intend to get the data from the past n days. For each day, the query will count the total rows and return the result. However, there are no record…
How can I alter data type and datas in it, numbers separated with comma
I have table , that has field named AMOUNT , amount takes number, table has 1.4m records, i need to upgrade them all . I would like the change NUMBER to varchar and make look like amount datas comma separated , eg: 76543-> 76,543. How can I able to do it? Answer i need to upgrade them all Don’t; if
GROUP BY with Key:Value filters for each group
Given a table with following schema: RecordId ProcessId Data 80830F00-379C-11EA-96B7-372060281AFC 1 4.2 80830F00-379C-11EA-96B7-372060281AFC 2 445588662211448 80830F00-379C-11EA-96B7-372060281AFC 7 1 791F6D30-379C-11EA-96B7-372060281AFC 1 3.3 791F6D30-379C-11EA-96B7-372060281AFC 2 999999999999999 791F6D30-379…
Top 3 services used for each day – SQL [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year. Improve this question The query below gives me top 3 services for 6th June 2021. I want to repeat this for each day …
PostgreSQL UPSERT (INSERT … ON CONFLICT UPDATE) fails
I have a row in my postgresql database that I want to update. My code generates this SQL statement to insert else update the one field that’s changed: What I don’t understand is that I can select that one row and it is present with a non-NULL num_syncs. But the UPSERT is failing because it doesn&#…
How to combine multiple records in one in SQL
I have a SQL table that looks something like this: OP ID First name Last name Phone number I 123 John Smith 888-555 U 123 777-555 I have to combine this rows through select query into something like this: ID First name Last name Phone number 123 John Smith 777-555 I have trouble writing query because my only …
How to union current table and history table order by recent edit
I am currently working on preparing audit table and the output structure I am planning to have is, latest record from current table and followed by associated(id) records from history table for all the records. So that user can see the latest changes and its history. At present I am using UNION of two tables …
How do I return a row with 0 if the group by has 0 records?
I have this alert_levels table: Then I have this alerts table The alert_level_id in the alerts table is a foreign key of id from the alert_levels table. What I want is to count the number of occurences of each alert_type grouped by the alert_level_id whithin a chosen time period. And if there is no occurency …